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