001
014
015 package com.liferay.portal.kernel.servlet;
016
017 import java.util.Collections;
018 import java.util.Iterator;
019 import java.util.LinkedHashMap;
020 import java.util.Map;
021 import java.util.Set;
022
023 import javax.portlet.PortletRequest;
024 import javax.portlet.PortletSession;
025
026 import javax.servlet.http.HttpServletRequest;
027 import javax.servlet.http.HttpSession;
028
029
033 public class SessionErrors {
034
035 public static final String KEY = SessionErrors.class.getName();
036
037 public static void add(HttpServletRequest request, String key) {
038 add(request.getSession(), key);
039 }
040
041 public static void add(
042 HttpServletRequest request, String key, Object value) {
043
044 add(request.getSession(), key, value);
045 }
046
047 public static void add(HttpSession session, String key) {
048 Map<String, Object> errors = _getErrors(session, true);
049
050 errors.put(key, key);
051 }
052
053 public static void add(HttpSession session, String key, Object value) {
054 Map<String, Object> errors = _getErrors(session, true);
055
056 errors.put(key, value);
057 }
058
059 public static void add(PortletRequest portletRequest, String key) {
060 add(portletRequest.getPortletSession(), key);
061 }
062
063 public static void add(
064 PortletRequest portletRequest, String key, Object value) {
065
066 add(portletRequest.getPortletSession(), key, value);
067 }
068
069 public static void add(PortletSession portletSession, String key) {
070 Map<String, Object> errors = _getErrors(portletSession, true);
071
072 errors.put(key, key);
073 }
074
075 public static void add(
076 PortletSession portletSession, String key, Object value) {
077
078 Map<String, Object> errors = _getErrors(portletSession, true);
079
080 errors.put(key, value);
081 }
082
083 public static void clear(HttpServletRequest request) {
084 clear(request.getSession());
085 }
086
087 public static void clear(HttpSession session) {
088 Map<String, Object> errors = _getErrors(session, false);
089
090 if (errors != null) {
091 errors.clear();
092 }
093 }
094
095 public static void clear(PortletRequest portletRequest) {
096 clear(portletRequest.getPortletSession());
097 }
098
099 public static void clear(PortletSession portletSession) {
100 Map<String, Object> errors = _getErrors(portletSession, false);
101
102 if (errors != null) {
103 errors.clear();
104 }
105 }
106
107 public static boolean contains(HttpServletRequest request, String key) {
108 return contains(request.getSession(), key);
109 }
110
111 public static boolean contains(HttpSession session, String key) {
112 Map<String, Object> errors = _getErrors(session, false);
113
114 if (errors == null) {
115 return false;
116 }
117 else {
118 return errors.containsKey(key);
119 }
120 }
121
122 public static boolean contains(PortletRequest portletRequest, String key) {
123 return contains(portletRequest.getPortletSession(), key);
124 }
125
126 public static boolean contains(PortletSession portletSession, String key) {
127 Map<String, Object> errors = _getErrors(portletSession, false);
128
129 if (errors == null) {
130 return false;
131 }
132 else {
133 return errors.containsKey(key);
134 }
135 }
136
137 public static Object get(HttpServletRequest request, String key) {
138 return get(request.getSession(), key);
139 }
140
141 public static Object get(HttpSession session, String key) {
142 Map<String, Object> errors = _getErrors(session, false);
143
144 if (errors == null) {
145 return null;
146 }
147 else {
148 return errors.get(key);
149 }
150 }
151
152 public static Object get(PortletRequest portletRequest, String key) {
153 return get(portletRequest.getPortletSession(), key);
154 }
155
156 public static Object get(PortletSession portletSession, String key) {
157 Map<String, Object> errors = _getErrors(portletSession, false);
158
159 if (errors == null) {
160 return null;
161 }
162 else {
163 return errors.get(key);
164 }
165 }
166
167 public static boolean isEmpty(HttpServletRequest request) {
168 return isEmpty(request.getSession());
169 }
170
171 public static boolean isEmpty(HttpSession session) {
172 Map<String, Object> errors = _getErrors(session, false);
173
174 if (errors == null) {
175 return true;
176 }
177 else {
178 return errors.isEmpty();
179 }
180 }
181
182 public static boolean isEmpty(PortletRequest portletRequest) {
183 return isEmpty(portletRequest.getPortletSession());
184 }
185
186 public static boolean isEmpty(PortletSession portletSession) {
187 Map<String, Object> errors = _getErrors(portletSession, false);
188
189 if (errors == null) {
190 return true;
191 }
192 else {
193 return errors.isEmpty();
194 }
195 }
196
197 public static Iterator<String> iterator(HttpServletRequest request) {
198 return iterator(request.getSession());
199 }
200
201 public static Iterator<String> iterator(HttpSession session) {
202 Map<String, Object> errors = _getErrors(session, false);
203
204 if (errors == null) {
205 return Collections.<String>emptyList().iterator();
206 }
207 else {
208 return Collections.unmodifiableSet(errors.keySet()).iterator();
209 }
210 }
211
212 public static Iterator<String> iterator(PortletRequest portletRequest) {
213 return iterator(portletRequest.getPortletSession());
214 }
215
216 public static Iterator<String> iterator(PortletSession portletSession) {
217 Map<String, Object> errors = _getErrors(portletSession, false);
218
219 if (errors == null) {
220 return Collections.<String>emptyList().iterator();
221 }
222 else {
223 return Collections.unmodifiableSet(errors.keySet()).iterator();
224 }
225 }
226
227 public static Set<String> keySet(HttpServletRequest request) {
228 return keySet(request.getSession());
229 }
230
231 public static Set<String> keySet(HttpSession session) {
232 Map<String, Object> errors = _getErrors(session, false);
233
234 if (errors == null) {
235 return Collections.emptySet();
236 }
237 else {
238 return Collections.unmodifiableSet(errors.keySet());
239 }
240 }
241
242 public static Set<String> keySet(PortletRequest portletRequest) {
243 return keySet(portletRequest.getPortletSession());
244 }
245
246 public static Set<String> keySet(PortletSession portletSession) {
247 Map<String, Object> errors = _getErrors(portletSession, false);
248
249 if (errors == null) {
250 return Collections.emptySet();
251 }
252 else {
253 return Collections.unmodifiableSet(errors.keySet());
254 }
255 }
256
257 public static void print(HttpServletRequest request) {
258 print(request.getSession());
259 }
260
261 public static void print(HttpSession session) {
262 Iterator<String> itr = iterator(session);
263
264 while (itr.hasNext()) {
265 System.out.println(itr.next());
266 }
267 }
268
269 public static void print(PortletRequest portletRequest) {
270 print(portletRequest.getPortletSession());
271 }
272
273 public static void print(PortletSession portletSession) {
274 Iterator<String> itr = iterator(portletSession);
275
276 while (itr.hasNext()) {
277 System.out.println(itr.next());
278 }
279 }
280
281 public static int size(HttpServletRequest request) {
282 return size(request.getSession());
283 }
284
285 public static int size(HttpSession session) {
286 Map<String, Object> errors = _getErrors(session, false);
287
288 if (errors == null) {
289 return 0;
290 }
291 else {
292 return errors.size();
293 }
294 }
295
296 public static int size(PortletRequest portletRequest) {
297 return size(portletRequest.getPortletSession());
298 }
299
300 public static int size(PortletSession portletSession) {
301 Map<String, Object> errors = _getErrors(portletSession, false);
302
303 if (errors == null) {
304 return 0;
305 }
306 else {
307 return errors.size();
308 }
309 }
310
311 private static Map<String, Object> _getErrors(
312 HttpSession session, boolean createIfAbsent) {
313
314 Map<String, Object> errors = null;
315
316 try {
317 errors = (Map<String, Object>)session.getAttribute(KEY);
318
319 if ((errors == null) && createIfAbsent) {
320 errors = new LinkedHashMap<String, Object>();
321
322 session.setAttribute(KEY, errors);
323 }
324 }
325 catch (IllegalStateException ise) {
326
327
328
329 }
330
331 return errors;
332 }
333
334 private static Map<String, Object> _getErrors(
335 PortletSession portletSession, boolean createIfAbsent) {
336
337 Map<String, Object> errors = null;
338
339 try {
340 errors = (Map<String, Object>)portletSession.getAttribute(KEY);
341
342 if ((errors == null) && createIfAbsent) {
343 errors = new LinkedHashMap<String, Object>();
344
345 portletSession.setAttribute(KEY, errors);
346 }
347 }
348 catch (IllegalStateException ise) {
349
350
351
352 }
353
354 return errors;
355 }
356
357 }