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.List;
021 import java.util.Map;
022 import java.util.Set;
023
024 import javax.portlet.PortletRequest;
025 import javax.portlet.PortletSession;
026
027 import javax.servlet.http.HttpServletRequest;
028 import javax.servlet.http.HttpSession;
029
030
034 public class SessionErrors {
035
036 public static void add(HttpServletRequest request, Class<?> clazz) {
037 add(request.getSession(), clazz.getName());
038 }
039
040 public static void add(
041 HttpServletRequest request, Class<?> clazz, Object value) {
042
043 add(request.getSession(), clazz.getName(), value);
044 }
045
046 public static void add(HttpServletRequest request, String key) {
047 add(request.getSession(), key);
048 }
049
050 public static void add(
051 HttpServletRequest request, String key, Object value) {
052
053 add(request.getSession(), key, value);
054 }
055
056 public static void add(HttpSession session, Class<?> clazz) {
057 add(session, clazz.getName());
058 }
059
060 public static void add(HttpSession session, Class<?> clazz, Object value) {
061 add(session, clazz.getName(), value);
062 }
063
064 public static void add(HttpSession session, String key) {
065 Map<String, Object> map = _getMap(session, true);
066
067 map.put(key, key);
068 }
069
070 public static void add(HttpSession session, String key, Object value) {
071 Map<String, Object> map = _getMap(session, true);
072
073 map.put(key, value);
074 }
075
076 public static void add(PortletRequest portletRequest, Class<?> clazz) {
077 add(portletRequest.getPortletSession(), clazz.getName());
078 }
079
080 public static void add(
081 PortletRequest portletRequest, Class<?> clazz, Object value) {
082
083 add(portletRequest.getPortletSession(), clazz.getName(), value);
084 }
085
086 public static void add(PortletRequest portletRequest, String key) {
087 add(portletRequest.getPortletSession(), key);
088 }
089
090 public static void add(
091 PortletRequest portletRequest, String key, Object value) {
092
093 add(portletRequest.getPortletSession(), key, value);
094 }
095
096 public static void add(PortletSession portletSession, Class<?> clazz) {
097 add(portletSession, clazz.getName());
098 }
099
100 public static void add(
101 PortletSession portletSession, Class<?> clazz, Object value) {
102
103 add(portletSession, clazz.getName(), value);
104 }
105
106 public static void add(PortletSession portletSession, String key) {
107 Map<String, Object> map = _getMap(portletSession, true);
108
109 map.put(key, key);
110 }
111
112 public static void add(
113 PortletSession portletSession, String key, Object value) {
114
115 Map<String, Object> map = _getMap(portletSession, true);
116
117 map.put(key, value);
118 }
119
120 public static void clear(HttpServletRequest request) {
121 clear(request.getSession());
122 }
123
124 public static void clear(HttpSession session) {
125 Map<String, Object> map = _getMap(session, false);
126
127 if (map != null) {
128 map.clear();
129 }
130 }
131
132 public static void clear(PortletRequest portletRequest) {
133 clear(portletRequest.getPortletSession());
134 }
135
136 public static void clear(PortletSession portletSession) {
137 Map<String, Object> map = _getMap(portletSession, false);
138
139 if (map != null) {
140 map.clear();
141 }
142 }
143
144 public static boolean contains(HttpServletRequest request, Class<?> clazz) {
145 return contains(request.getSession(), clazz.getName());
146 }
147
148 public static boolean contains(HttpServletRequest request, String key) {
149 return contains(request.getSession(), key);
150 }
151
152 public static boolean contains(HttpSession session, Class<?> clazz) {
153 return contains(session, clazz.getName());
154 }
155
156 public static boolean contains(HttpSession session, String key) {
157 Map<String, Object> map = _getMap(session, false);
158
159 if (map == null) {
160 return false;
161 }
162
163 return map.containsKey(key);
164 }
165
166 public static boolean contains(
167 PortletRequest portletRequest, Class<?> clazz) {
168
169 return contains(portletRequest.getPortletSession(), clazz.getName());
170 }
171
172 public static boolean contains(PortletRequest portletRequest, String key) {
173 return contains(portletRequest.getPortletSession(), key);
174 }
175
176 public static boolean contains(
177 PortletSession portletSession, Class<?> clazz) {
178
179 return contains(portletSession, clazz.getName());
180 }
181
182 public static boolean contains(PortletSession portletSession, String key) {
183 Map<String, Object> map = _getMap(portletSession, false);
184
185 if (map == null) {
186 return false;
187 }
188
189 return map.containsKey(key);
190 }
191
192 public static Object get(HttpServletRequest request, Class<?> clazz) {
193 return get(request.getSession(), clazz.getName());
194 }
195
196 public static Object get(HttpServletRequest request, String key) {
197 return get(request.getSession(), key);
198 }
199
200 public static Object get(HttpSession session, Class<?> clazz) {
201 return get(session, clazz.getName());
202 }
203
204 public static Object get(HttpSession session, String key) {
205 Map<String, Object> map = _getMap(session, false);
206
207 if (map == null) {
208 return null;
209 }
210
211 return map.get(key);
212 }
213
214 public static Object get(PortletRequest portletRequest, Class<?> clazz) {
215 return get(portletRequest.getPortletSession(), clazz.getName());
216 }
217
218 public static Object get(PortletRequest portletRequest, String key) {
219 return get(portletRequest.getPortletSession(), key);
220 }
221
222 public static Object get(PortletSession portletSession, Class<?> clazz) {
223 return get(portletSession, clazz.getName());
224 }
225
226 public static Object get(PortletSession portletSession, String key) {
227 Map<String, Object> map = _getMap(portletSession, false);
228
229 if (map == null) {
230 return null;
231 }
232
233 return map.get(key);
234 }
235
236 public static boolean isEmpty(HttpServletRequest request) {
237 return isEmpty(request.getSession());
238 }
239
240 public static boolean isEmpty(HttpSession session) {
241 Map<String, Object> map = _getMap(session, false);
242
243 if (map == null) {
244 return true;
245 }
246
247 return map.isEmpty();
248 }
249
250 public static boolean isEmpty(PortletRequest portletRequest) {
251 return isEmpty(portletRequest.getPortletSession());
252 }
253
254 public static boolean isEmpty(PortletSession portletSession) {
255 Map<String, Object> map = _getMap(portletSession, false);
256
257 if (map == null) {
258 return true;
259 }
260
261 return map.isEmpty();
262 }
263
264 public static Iterator<String> iterator(HttpServletRequest request) {
265 return iterator(request.getSession());
266 }
267
268 public static Iterator<String> iterator(HttpSession session) {
269 Map<String, Object> map = _getMap(session, false);
270
271 if (map == null) {
272 List<String> list = Collections.<String>emptyList();
273
274 return list.iterator();
275 }
276
277 Set<String> set = Collections.unmodifiableSet(map.keySet());
278
279 return set.iterator();
280 }
281
282 public static Iterator<String> iterator(PortletRequest portletRequest) {
283 return iterator(portletRequest.getPortletSession());
284 }
285
286 public static Iterator<String> iterator(PortletSession portletSession) {
287 Map<String, Object> map = _getMap(portletSession, false);
288
289 if (map == null) {
290 List<String> list = Collections.<String>emptyList();
291
292 return list.iterator();
293 }
294
295 Set<String> set = Collections.unmodifiableSet(map.keySet());
296
297 return set.iterator();
298 }
299
300 public static Set<String> keySet(HttpServletRequest request) {
301 return keySet(request.getSession());
302 }
303
304 public static Set<String> keySet(HttpSession session) {
305 Map<String, Object> map = _getMap(session, false);
306
307 if (map == null) {
308 return Collections.emptySet();
309 }
310
311 return Collections.unmodifiableSet(map.keySet());
312 }
313
314 public static Set<String> keySet(PortletRequest portletRequest) {
315 return keySet(portletRequest.getPortletSession());
316 }
317
318 public static Set<String> keySet(PortletSession portletSession) {
319 Map<String, Object> map = _getMap(portletSession, false);
320
321 if (map == null) {
322 return Collections.emptySet();
323 }
324
325 return Collections.unmodifiableSet(map.keySet());
326 }
327
328 public static void print(HttpServletRequest request) {
329 print(request.getSession());
330 }
331
332 public static void print(HttpSession session) {
333 Iterator<String> itr = iterator(session);
334
335 while (itr.hasNext()) {
336 System.out.println(itr.next());
337 }
338 }
339
340 public static void print(PortletRequest portletRequest) {
341 print(portletRequest.getPortletSession());
342 }
343
344 public static void print(PortletSession portletSession) {
345 Iterator<String> itr = iterator(portletSession);
346
347 while (itr.hasNext()) {
348 System.out.println(itr.next());
349 }
350 }
351
352 public static int size(HttpServletRequest request) {
353 return size(request.getSession());
354 }
355
356 public static int size(HttpSession session) {
357 Map<String, Object> map = _getMap(session, false);
358
359 if (map == null) {
360 return 0;
361 }
362
363 return map.size();
364 }
365
366 public static int size(PortletRequest portletRequest) {
367 return size(portletRequest.getPortletSession());
368 }
369
370 public static int size(PortletSession portletSession) {
371 Map<String, Object> map = _getMap(portletSession, false);
372
373 if (map == null) {
374 return 0;
375 }
376
377 return map.size();
378 }
379
380 private static Map<String, Object> _getMap(
381 HttpSession session, boolean createIfAbsent) {
382
383 Map<String, Object> map = null;
384
385 try {
386 map = (Map<String, Object>)session.getAttribute(_CLASS_NAME);
387
388 if ((map == null) && createIfAbsent) {
389 map = new LinkedHashMap<String, Object>();
390
391 session.setAttribute(_CLASS_NAME, map);
392 }
393 }
394 catch (IllegalStateException ise) {
395
396
397
398 }
399
400 return map;
401 }
402
403 private static Map<String, Object> _getMap(
404 PortletSession portletSession, boolean createIfAbsent) {
405
406 Map<String, Object> map = null;
407
408 try {
409 map = (Map<String, Object>)portletSession.getAttribute(_CLASS_NAME);
410
411 if ((map == null) && createIfAbsent) {
412 map = new LinkedHashMap<String, Object>();
413
414 portletSession.setAttribute(_CLASS_NAME, map);
415 }
416 }
417 catch (IllegalStateException ise) {
418
419
420
421 }
422
423 return map;
424 }
425
426 private static final String _CLASS_NAME = SessionErrors.class.getName();
427
428 }