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_DELETE_SUCCESS_DATA =
039                    ".deleteSuccessData";
040    
041            public static final String KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE =
042                    ".hideDefaultErrorMessage";
043    
044            public static final String KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE =
045                    ".hideDefaultSuccessMessage";
046    
047            public static final String KEY_SUFFIX_PORTLET_NOT_AJAXABLE =
048                    ".portletNotAjaxable";
049    
050            public static final String KEY_SUFFIX_REFRESH_PORTLET = ".refreshPortlet";
051    
052            public static final String KEY_SUFFIX_REFRESH_PORTLET_DATA =
053                    ".refreshPortletData";
054    
055            public static final String KEY_SUFFIX_UPDATED_CONFIGURATION =
056                    ".updatedConfiguration";
057    
058            public static final String KEY_SUFFIX_UPDATED_PREFERENCES =
059                    ".updatedPreferences";
060    
061            public static void add(HttpServletRequest request, Class<?> clazz) {
062                    add(request.getSession(), clazz.getName());
063            }
064    
065            public static void add(
066                    HttpServletRequest request, Class<?> clazz, Object value) {
067    
068                    add(request.getSession(), clazz.getName(), value);
069            }
070    
071            public static void add(HttpServletRequest request, String key) {
072                    add(request.getSession(), key);
073            }
074    
075            public static void add(
076                    HttpServletRequest request, String key, Object value) {
077    
078                    add(request.getSession(), key, value);
079            }
080    
081            public static void add(HttpSession session, Class<?> clazz) {
082                    add(session, clazz.getName());
083            }
084    
085            public static void add(HttpSession session, Class<?> clazz, Object value) {
086                    add(session, clazz.getName(), value);
087            }
088    
089            public static void add(HttpSession session, String key) {
090                    Map<String, Object> map = _getMap(session, true);
091    
092                    map.put(key, key);
093            }
094    
095            public static void add(HttpSession session, String key, Object value) {
096                    Map<String, Object> map = _getMap(session, true);
097    
098                    map.put(key, value);
099            }
100    
101            public static void add(PortletRequest portletRequest, Class<?> clazz) {
102                    add(portletRequest.getPortletSession(), clazz.getName());
103            }
104    
105            public static void add(
106                    PortletRequest portletRequest, Class<?> clazz, Object value) {
107    
108                    add(portletRequest.getPortletSession(), clazz.getName(), value);
109            }
110    
111            public static void add(PortletRequest portletRequest, String key) {
112                    add(portletRequest.getPortletSession(), key);
113            }
114    
115            public static void add(
116                    PortletRequest portletRequest, String key, Object value) {
117    
118                    add(portletRequest.getPortletSession(), key, value);
119            }
120    
121            public static void add(PortletSession portletSession, Class<?> clazz) {
122                    add(portletSession, clazz.getName());
123            }
124    
125            public static void add(
126                    PortletSession portletSession, Class<?> clazz, Object value) {
127    
128                    add(portletSession, clazz.getName(), value);
129            }
130    
131            public static void add(PortletSession portletSession, String key) {
132                    Map<String, Object> map = _getMap(portletSession, true);
133    
134                    map.put(key, key);
135            }
136    
137            public static void add(
138                    PortletSession portletSession, String key, Object value) {
139    
140                    Map<String, Object> map = _getMap(portletSession, true);
141    
142                    map.put(key, value);
143            }
144    
145            public static void clear(HttpServletRequest request) {
146                    clear(request.getSession());
147            }
148    
149            public static void clear(HttpSession session) {
150                    Map<String, Object> map = _getMap(session, false);
151    
152                    if (map != null) {
153                            map.clear();
154                    }
155            }
156    
157            public static void clear(PortletRequest portletRequest) {
158                    clear(portletRequest.getPortletSession());
159            }
160    
161            public static void clear(PortletSession portletSession) {
162                    Map<String, Object> map = _getMap(portletSession, false);
163    
164                    if (map != null) {
165                            map.clear();
166                    }
167            }
168    
169            public static boolean contains(HttpServletRequest request, Class<?> clazz) {
170                    return contains(request.getSession(), clazz.getName());
171            }
172    
173            public static boolean contains(HttpServletRequest request, String key) {
174                    return contains(request.getSession(), key);
175            }
176    
177            public static boolean contains(HttpSession session, Class<?> clazz) {
178                    return contains(session, clazz.getName());
179            }
180    
181            public static boolean contains(HttpSession session, String key) {
182                    Map<String, Object> map = _getMap(session, false);
183    
184                    if (map == null) {
185                            return false;
186                    }
187    
188                    return map.containsKey(key);
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    
214                    return map.containsKey(key);
215            }
216    
217            public static Object get(HttpServletRequest request, Class<?> clazz) {
218                    return get(request.getSession(), clazz.getName());
219            }
220    
221            public static Object get(HttpServletRequest request, String key) {
222                    return get(request.getSession(), key);
223            }
224    
225            public static Object get(HttpSession session, Class<?> clazz) {
226                    return get(session, clazz.getName());
227            }
228    
229            public static Object get(HttpSession session, String key) {
230                    Map<String, Object> map = _getMap(session, false);
231    
232                    if (map == null) {
233                            return null;
234                    }
235    
236                    return map.get(key);
237            }
238    
239            public static Object get(PortletRequest portletRequest, Class<?> clazz) {
240                    return get(portletRequest.getPortletSession(), clazz.getName());
241            }
242    
243            public static Object get(PortletRequest portletRequest, String key) {
244                    return get(portletRequest.getPortletSession(), key);
245            }
246    
247            public static Object get(PortletSession portletSession, Class<?> clazz) {
248                    return get(portletSession, clazz.getName());
249            }
250    
251            public static Object get(PortletSession portletSession, String key) {
252                    Map<String, Object> map = _getMap(portletSession, false);
253    
254                    if (map == null) {
255                            return null;
256                    }
257    
258                    return map.get(key);
259            }
260    
261            public static boolean isEmpty(HttpServletRequest request) {
262                    return isEmpty(request.getSession());
263            }
264    
265            public static boolean isEmpty(HttpSession session) {
266                    Map<String, Object> map = _getMap(session, false);
267    
268                    if (map == null) {
269                            return true;
270                    }
271    
272                    return map.isEmpty();
273            }
274    
275            public static boolean isEmpty(PortletRequest portletRequest) {
276                    return isEmpty(portletRequest.getPortletSession());
277            }
278    
279            public static boolean isEmpty(PortletSession portletSession) {
280                    Map<String, Object> map = _getMap(portletSession, false);
281    
282                    if (map == null) {
283                            return true;
284                    }
285    
286                    return map.isEmpty();
287            }
288    
289            public static Iterator<String> iterator(HttpServletRequest request) {
290                    return iterator(request.getSession());
291            }
292    
293            public static Iterator<String> iterator(HttpSession session) {
294                    Map<String, Object> map = _getMap(session, false);
295    
296                    if (map == null) {
297                            List<String> list = Collections.<String>emptyList();
298    
299                            return list.iterator();
300                    }
301    
302                    Set<String> set = Collections.unmodifiableSet(map.keySet());
303    
304                    return set.iterator();
305            }
306    
307            public static Iterator<String> iterator(PortletRequest portletRequest) {
308                    return iterator(portletRequest.getPortletSession());
309            }
310    
311            public static Iterator<String> iterator(PortletSession portletSession) {
312                    Map<String, Object> map = _getMap(portletSession, false);
313    
314                    if (map == null) {
315                            List<String> list = Collections.<String>emptyList();
316    
317                            return list.iterator();
318                    }
319    
320                    Set<String> set = Collections.unmodifiableSet(map.keySet());
321    
322                    return set.iterator();
323            }
324    
325            public static Set<String> keySet(HttpServletRequest request) {
326                    return keySet(request.getSession());
327            }
328    
329            public static Set<String> keySet(HttpSession session) {
330                    Map<String, Object> map = _getMap(session, false);
331    
332                    if (map == null) {
333                            return Collections.emptySet();
334                    }
335    
336                    return Collections.unmodifiableSet(map.keySet());
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    
350                    return Collections.unmodifiableSet(map.keySet());
351            }
352    
353            public static void print(HttpServletRequest request) {
354                    print(request.getSession());
355            }
356    
357            public static void print(HttpSession session) {
358                    Iterator<String> itr = iterator(session);
359    
360                    while (itr.hasNext()) {
361                            System.out.println(itr.next());
362                    }
363            }
364    
365            public static void print(PortletRequest portletRequest) {
366                    print(portletRequest.getPortletSession());
367            }
368    
369            public static void print(PortletSession portletSession) {
370                    Iterator<String> itr = iterator(portletSession);
371    
372                    while (itr.hasNext()) {
373                            System.out.println(itr.next());
374                    }
375            }
376    
377            public static int size(HttpServletRequest request) {
378                    return size(request.getSession());
379            }
380    
381            public static int size(HttpSession session) {
382                    Map<String, Object> map = _getMap(session, false);
383    
384                    if (map == null) {
385                            return 0;
386                    }
387    
388                    return map.size();
389            }
390    
391            public static int size(PortletRequest portletRequest) {
392                    return size(portletRequest.getPortletSession());
393            }
394    
395            public static int size(PortletSession portletSession) {
396                    Map<String, Object> map = _getMap(portletSession, false);
397    
398                    if (map == null) {
399                            return 0;
400                    }
401    
402                    return map.size();
403            }
404    
405            private static Map<String, Object> _getMap(
406                    HttpSession session, boolean createIfAbsent) {
407    
408                    Map<String, Object> map = null;
409    
410                    try {
411                            map = (Map<String, Object>)session.getAttribute(_CLASS_NAME);
412    
413                            if ((map == null) && createIfAbsent) {
414                                    map = new SessionMessagesMap();
415    
416                                    session.setAttribute(_CLASS_NAME, map);
417                            }
418                    }
419                    catch (IllegalStateException ise) {
420    
421                            // Session is already invalidated, just return a null map
422    
423                    }
424    
425                    return map;
426            }
427    
428            private static Map<String, Object> _getMap(
429                    PortletSession portletSession, boolean createIfAbsent) {
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 SessionMessagesMap();
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 = SessionMessages.class.getName();
452    
453            private static class SessionMessagesMap
454                    extends LinkedHashMap<String, Object> {
455    
456                    @Override
457                    public Object get(Object key) {
458                            key = _transformKey(key);
459    
460                            return super.get(key);
461                    }
462    
463                    @Override
464                    public boolean containsKey(Object key) {
465                            key = _transformKey(key);
466    
467                            return super.containsKey(key);
468                    }
469    
470                    @Override
471                    public Object put(String key, Object value) {
472                            key = _transformKey(key);
473    
474                            return super.put(key, value);
475                    }
476    
477                    private String _transformKey(Object key) {
478                            String keyString = String.valueOf(key);
479    
480                            if (keyString != null) {
481                                    if (keyString.equals("request_processed")) {
482                                            keyString = "requestProcessed";
483                                    }
484                            }
485    
486                            return keyString;
487                    }
488    
489            }
490    
491    }