001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
030     * @author Brian Wing Shun Chan
031     * @author Shuyang Zhou
032     */
033    public class SessionErrors {
034    
035            public static void add(HttpServletRequest request, Class<?> clazz) {
036                    add(request.getSession(), clazz.getName());
037            }
038    
039            public static void add(
040                    HttpServletRequest request, Class<?> clazz, Object value) {
041    
042                    add(request.getSession(), clazz.getName(), value);
043            }
044    
045            public static void add(HttpServletRequest request, String key) {
046                    add(request.getSession(), key);
047            }
048    
049            public static void add(
050                    HttpServletRequest request, String key, Object value) {
051    
052                    add(request.getSession(), key, value);
053            }
054    
055            public static void add(HttpSession session, Class<?> clazz) {
056                    add(session, clazz.getName());
057            }
058    
059            public static void add(HttpSession session, Class<?> clazz, Object value) {
060                    add(session, clazz.getName(), value);
061            }
062    
063            public static void add(HttpSession session, String key) {
064                    Map<String, Object> map = _getMap(session, true);
065    
066                    map.put(key, key);
067            }
068    
069            public static void add(HttpSession session, String key, Object value) {
070                    Map<String, Object> map = _getMap(session, true);
071    
072                    map.put(key, value);
073            }
074    
075            public static void add(PortletRequest portletRequest, Class<?> clazz) {
076                    add(portletRequest.getPortletSession(), clazz.getName());
077            }
078    
079            public static void add(
080                    PortletRequest portletRequest, Class<?> clazz, Object value) {
081    
082                    add(portletRequest.getPortletSession(), clazz.getName(), value);
083            }
084    
085            public static void add(PortletRequest portletRequest, String key) {
086                    add(portletRequest.getPortletSession(), key);
087            }
088    
089            public static void add(
090                    PortletRequest portletRequest, String key, Object value) {
091    
092                    add(portletRequest.getPortletSession(), key, value);
093            }
094    
095            public static void add(PortletSession portletSession, Class<?> clazz) {
096                    add(portletSession, clazz.getName());
097            }
098    
099            public static void add(
100                    PortletSession portletSession, Class<?> clazz, Object value) {
101    
102                    add(portletSession, clazz.getName(), value);
103            }
104    
105            public static void add(PortletSession portletSession, String key) {
106                    Map<String, Object> map = _getMap(portletSession, true);
107    
108                    map.put(key, key);
109            }
110    
111            public static void add(
112                    PortletSession portletSession, String key, Object value) {
113    
114                    Map<String, Object> map = _getMap(portletSession, true);
115    
116                    map.put(key, value);
117            }
118    
119            public static void clear(HttpServletRequest request) {
120                    clear(request.getSession());
121            }
122    
123            public static void clear(HttpSession session) {
124                    Map<String, Object> map = _getMap(session, false);
125    
126                    if (map != null) {
127                            map.clear();
128                    }
129            }
130    
131            public static void clear(PortletRequest portletRequest) {
132                    clear(portletRequest.getPortletSession());
133            }
134    
135            public static void clear(PortletSession portletSession) {
136                    Map<String, Object> map = _getMap(portletSession, false);
137    
138                    if (map != null) {
139                            map.clear();
140                    }
141            }
142    
143            public static boolean contains(HttpServletRequest request, Class<?> clazz) {
144                    return contains(request.getSession(), clazz.getName());
145            }
146    
147            public static boolean contains(HttpServletRequest request, String key) {
148                    return contains(request.getSession(), key);
149            }
150    
151            public static boolean contains(HttpSession session, Class<?> clazz) {
152                    return contains(session, clazz.getName());
153            }
154    
155            public static boolean contains(HttpSession session, String key) {
156                    Map<String, Object> map = _getMap(session, false);
157    
158                    if (map == null) {
159                            return false;
160                    }
161                    else {
162                            return map.containsKey(key);
163                    }
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                    else {
189                            return map.containsKey(key);
190                    }
191            }
192    
193            public static Object get(HttpServletRequest request, Class<?> clazz) {
194                    return get(request.getSession(), clazz.getName());
195            }
196    
197            public static Object get(HttpServletRequest request, String key) {
198                    return get(request.getSession(), key);
199            }
200    
201            public static Object get(HttpSession session, Class<?> clazz) {
202                    return get(session, clazz.getName());
203            }
204    
205            public static Object get(HttpSession session, String key) {
206                    Map<String, Object> map = _getMap(session, false);
207    
208                    if (map == null) {
209                            return null;
210                    }
211                    else {
212                            return map.get(key);
213                    }
214            }
215    
216            public static Object get(PortletRequest portletRequest, Class<?> clazz) {
217                    return get(portletRequest.getPortletSession(), clazz.getName());
218            }
219    
220            public static Object get(PortletRequest portletRequest, String key) {
221                    return get(portletRequest.getPortletSession(), key);
222            }
223    
224            public static Object get(PortletSession portletSession, Class<?> clazz) {
225                    return get(portletSession, clazz.getName());
226            }
227    
228            public static Object get(PortletSession portletSession, String key) {
229                    Map<String, Object> map = _getMap(portletSession, false);
230    
231                    if (map == null) {
232                            return null;
233                    }
234                    else {
235                            return map.get(key);
236                    }
237            }
238    
239            public static boolean isEmpty(HttpServletRequest request) {
240                    return isEmpty(request.getSession());
241            }
242    
243            public static boolean isEmpty(HttpSession session) {
244                    Map<String, Object> map = _getMap(session, false);
245    
246                    if (map == null) {
247                            return true;
248                    }
249                    else {
250                            return map.isEmpty();
251                    }
252            }
253    
254            public static boolean isEmpty(PortletRequest portletRequest) {
255                    return isEmpty(portletRequest.getPortletSession());
256            }
257    
258            public static boolean isEmpty(PortletSession portletSession) {
259                    Map<String, Object> map = _getMap(portletSession, false);
260    
261                    if (map == null) {
262                            return true;
263                    }
264                    else {
265                            return map.isEmpty();
266                    }
267            }
268    
269            public static Iterator<String> iterator(HttpServletRequest request) {
270                    return iterator(request.getSession());
271            }
272    
273            public static Iterator<String> iterator(HttpSession session) {
274                    Map<String, Object> map = _getMap(session, false);
275    
276                    if (map == null) {
277                            return Collections.<String>emptyList().iterator();
278                    }
279                    else {
280                            return Collections.unmodifiableSet(map.keySet()).iterator();
281                    }
282            }
283    
284            public static Iterator<String> iterator(PortletRequest portletRequest) {
285                    return iterator(portletRequest.getPortletSession());
286            }
287    
288            public static Iterator<String> iterator(PortletSession portletSession) {
289                    Map<String, Object> map = _getMap(portletSession, false);
290    
291                    if (map == null) {
292                            return Collections.<String>emptyList().iterator();
293                    }
294                    else {
295                            return Collections.unmodifiableSet(map.keySet()).iterator();
296                    }
297            }
298    
299            public static Set<String> keySet(HttpServletRequest request) {
300                    return keySet(request.getSession());
301            }
302    
303            public static Set<String> keySet(HttpSession session) {
304                    Map<String, Object> map = _getMap(session, false);
305    
306                    if (map == null) {
307                            return Collections.emptySet();
308                    }
309                    else {
310                            return Collections.unmodifiableSet(map.keySet());
311                    }
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                    else {
325                            return Collections.unmodifiableSet(map.keySet());
326                    }
327            }
328    
329            public static void print(HttpServletRequest request) {
330                    print(request.getSession());
331            }
332    
333            public static void print(HttpSession session) {
334                    Iterator<String> itr = iterator(session);
335    
336                    while (itr.hasNext()) {
337                            System.out.println(itr.next());
338                    }
339            }
340    
341            public static void print(PortletRequest portletRequest) {
342                    print(portletRequest.getPortletSession());
343            }
344    
345            public static void print(PortletSession portletSession) {
346                    Iterator<String> itr = iterator(portletSession);
347    
348                    while (itr.hasNext()) {
349                            System.out.println(itr.next());
350                    }
351            }
352    
353            public static int size(HttpServletRequest request) {
354                    return size(request.getSession());
355            }
356    
357            public static int size(HttpSession session) {
358                    Map<String, Object> map = _getMap(session, false);
359    
360                    if (map == null) {
361                            return 0;
362                    }
363                    else {
364                            return map.size();
365                    }
366            }
367    
368            public static int size(PortletRequest portletRequest) {
369                    return size(portletRequest.getPortletSession());
370            }
371    
372            public static int size(PortletSession portletSession) {
373                    Map<String, Object> map = _getMap(portletSession, false);
374    
375                    if (map == null) {
376                            return 0;
377                    }
378                    else {
379                            return map.size();
380                    }
381            }
382    
383            private static Map<String, Object> _getMap(
384                    HttpSession session, boolean createIfAbsent) {
385    
386                    Map<String, Object> map = null;
387    
388                    try {
389                            map = (Map<String, Object>)session.getAttribute(_CLASS_NAME);
390    
391                            if ((map == null) && createIfAbsent) {
392                                    map = new LinkedHashMap<String, Object>();
393    
394                                    session.setAttribute(_CLASS_NAME, map);
395                            }
396                    }
397                    catch (IllegalStateException ise) {
398    
399                            // Session is already invalidated, just return a null map
400    
401                    }
402    
403                    return map;
404            }
405    
406            private static Map<String, Object> _getMap(
407                    PortletSession portletSession, boolean createIfAbsent) {
408    
409                    Map<String, Object> map = null;
410    
411                    try {
412                            map = (Map<String, Object>)portletSession.getAttribute(_CLASS_NAME);
413    
414                            if ((map == null) && createIfAbsent) {
415                                    map = new LinkedHashMap<String, Object>();
416    
417                                    portletSession.setAttribute(_CLASS_NAME, map);
418                            }
419                    }
420                    catch (IllegalStateException ise) {
421    
422                            // Session is already invalidated, just return a null map
423    
424                    }
425    
426                    return map;
427            }
428    
429            private static final String _CLASS_NAME = SessionErrors.class.getName();
430    
431    }