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