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 com.liferay.portal.kernel.util.WebKeys;
018    import com.liferay.portal.util.PortalUtil;
019    
020    import java.util.Collections;
021    import java.util.Iterator;
022    import java.util.LinkedHashMap;
023    import java.util.List;
024    import java.util.Map;
025    import java.util.Set;
026    
027    import javax.portlet.PortletRequest;
028    
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpSession;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Shuyang Zhou
035     * @author Sergio Gonz??lez
036     */
037    public class PortalMessages {
038    
039            public static final String KEY_ANIMATION = "animation";
040    
041            public static final String KEY_CSS_CLASS = "cssClass";
042    
043            public static final String KEY_JSP_PATH = "jspPath";
044    
045            public static final String KEY_MESSAGE = "message";
046    
047            public static final String KEY_PORTLET_ID = "portletId";
048    
049            public static final String KEY_TIMEOUT = "timeout";
050    
051            public static void add(HttpServletRequest request, Class<?> clazz) {
052                    add(request.getSession(), clazz.getName());
053            }
054    
055            public static void add(
056                    HttpServletRequest request, Class<?> clazz, Object value) {
057    
058                    add(request.getSession(), clazz.getName(), value);
059            }
060    
061            public static void add(HttpServletRequest request, String key) {
062                    add(request.getSession(), key);
063            }
064    
065            public static void add(
066                    HttpServletRequest request, String key, Object value) {
067    
068                    add(request.getSession(), key, value);
069            }
070    
071            public static void add(HttpSession session, Class<?> clazz) {
072                    add(session, clazz.getName());
073            }
074    
075            public static void add(HttpSession session, Class<?> clazz, Object value) {
076                    add(session, clazz.getName(), value);
077            }
078    
079            public static void add(HttpSession session, String key) {
080                    Map<String, Object> map = _getMap(session, true);
081    
082                    map.put(key, key);
083            }
084    
085            public static void add(HttpSession session, String key, Object value) {
086                    Map<String, Object> map = _getMap(session, true);
087    
088                    map.put(key, value);
089            }
090    
091            public static void add(PortletRequest portletRequest, Class<?> clazz) {
092                    add(PortalUtil.getHttpServletRequest(portletRequest), clazz.getName());
093            }
094    
095            public static void add(
096                    PortletRequest portletRequest, Class<?> clazz, Object value) {
097    
098                    add(
099                            PortalUtil.getHttpServletRequest(portletRequest), clazz.getName(),
100                            value);
101            }
102    
103            public static void add(PortletRequest portletRequest, String key) {
104                    add(PortalUtil.getHttpServletRequest(portletRequest), key);
105            }
106    
107            public static void add(
108                    PortletRequest portletRequest, String key, Object value) {
109    
110                    add(PortalUtil.getHttpServletRequest(portletRequest), key, value);
111            }
112    
113            public static void clear(HttpServletRequest request) {
114                    clear(request.getSession());
115            }
116    
117            public static void clear(HttpSession session) {
118                    Map<String, Object> map = _getMap(session, false);
119    
120                    if (map != null) {
121                            map.clear();
122                    }
123            }
124    
125            public static void clear(PortletRequest portletRequest) {
126                    clear(PortalUtil.getHttpServletRequest(portletRequest));
127            }
128    
129            public static boolean contains(HttpServletRequest request, Class<?> clazz) {
130                    return contains(request.getSession(), clazz.getName());
131            }
132    
133            public static boolean contains(HttpServletRequest request, String key) {
134                    return contains(request.getSession(), key);
135            }
136    
137            public static boolean contains(HttpSession session, Class<?> clazz) {
138                    return contains(session, clazz.getName());
139            }
140    
141            public static boolean contains(HttpSession session, String key) {
142                    Map<String, Object> map = _getMap(session, false);
143    
144                    if (map == null) {
145                            return false;
146                    }
147    
148                    return map.containsKey(key);
149            }
150    
151            public static boolean contains(
152                    PortletRequest portletRequest, Class<?> clazz) {
153    
154                    return contains(
155                            PortalUtil.getHttpServletRequest(portletRequest), clazz.getName());
156            }
157    
158            public static boolean contains(PortletRequest portletRequest, String key) {
159                    return contains(PortalUtil.getHttpServletRequest(portletRequest), key);
160            }
161    
162            public static Object get(HttpServletRequest request, Class<?> clazz) {
163                    return get(request.getSession(), clazz.getName());
164            }
165    
166            public static Object get(HttpServletRequest request, String key) {
167                    return get(request.getSession(), key);
168            }
169    
170            public static Object get(HttpSession session, Class<?> clazz) {
171                    return get(session, clazz.getName());
172            }
173    
174            public static Object get(HttpSession session, String key) {
175                    Map<String, Object> map = _getMap(session, false);
176    
177                    if (map == null) {
178                            return null;
179                    }
180    
181                    return map.get(key);
182            }
183    
184            public static Object get(PortletRequest portletRequest, Class<?> clazz) {
185                    return get(
186                            PortalUtil.getHttpServletRequest(portletRequest), clazz.getName());
187            }
188    
189            public static Object get(PortletRequest portletRequest, String key) {
190                    return get(PortalUtil.getHttpServletRequest(portletRequest), key);
191            }
192    
193            public static boolean isEmpty(HttpServletRequest request) {
194                    return isEmpty(request.getSession());
195            }
196    
197            public static boolean isEmpty(HttpSession session) {
198                    Map<String, Object> map = _getMap(session, false);
199    
200                    if (map == null) {
201                            return true;
202                    }
203    
204                    return map.isEmpty();
205            }
206    
207            public static boolean isEmpty(PortletRequest portletRequest) {
208                    return isEmpty(PortalUtil.getHttpServletRequest(portletRequest));
209            }
210    
211            public static Iterator<String> iterator(HttpServletRequest request) {
212                    return iterator(request.getSession());
213            }
214    
215            public static Iterator<String> iterator(HttpSession session) {
216                    Map<String, Object> map = _getMap(session, false);
217    
218                    if (map == null) {
219                            List<String> list = Collections.<String>emptyList();
220    
221                            return list.iterator();
222                    }
223    
224                    Set<String> set = Collections.unmodifiableSet(map.keySet());
225    
226                    return set.iterator();
227            }
228    
229            public static Iterator<String> iterator(PortletRequest portletRequest) {
230                    return iterator(PortalUtil.getHttpServletRequest(portletRequest));
231            }
232    
233            public static Set<String> keySet(HttpServletRequest request) {
234                    return keySet(request.getSession());
235            }
236    
237            public static Set<String> keySet(HttpSession session) {
238                    Map<String, Object> map = _getMap(session, false);
239    
240                    if (map == null) {
241                            return Collections.emptySet();
242                    }
243    
244                    return Collections.unmodifiableSet(map.keySet());
245            }
246    
247            public static Set<String> keySet(PortletRequest portletRequest) {
248                    return keySet(PortalUtil.getHttpServletRequest(portletRequest));
249            }
250    
251            public static void print(HttpServletRequest request) {
252                    print(request.getSession());
253            }
254    
255            public static void print(HttpSession session) {
256                    Iterator<String> itr = iterator(session);
257    
258                    while (itr.hasNext()) {
259                            System.out.println(itr.next());
260                    }
261            }
262    
263            public static void print(PortletRequest portletRequest) {
264                    print(PortalUtil.getHttpServletRequest(portletRequest));
265            }
266    
267            public static int size(HttpServletRequest request) {
268                    return size(request.getSession());
269            }
270    
271            public static int size(HttpSession session) {
272                    Map<String, Object> map = _getMap(session, false);
273    
274                    if (map == null) {
275                            return 0;
276                    }
277    
278                    return map.size();
279            }
280    
281            public static int size(PortletRequest portletRequest) {
282                    return size(PortalUtil.getHttpServletRequest(portletRequest));
283            }
284    
285            private static Map<String, Object> _getMap(
286                    HttpSession session, boolean createIfAbsent) {
287    
288                    Map<String, Object> map = null;
289    
290                    try {
291                            map = (Map<String, Object>)session.getAttribute(
292                                    WebKeys.PORTAL_MESSAGES);
293    
294                            if ((map == null) && createIfAbsent) {
295                                    map = new LinkedHashMap<String, Object>();
296    
297                                    session.setAttribute(WebKeys.PORTAL_MESSAGES, map);
298                            }
299                    }
300                    catch (IllegalStateException ise) {
301    
302                            // Session is already invalidated, just return a null map
303    
304                    }
305    
306                    return map;
307            }
308    
309    }