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