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