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