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    
022    import javax.portlet.PortletRequest;
023    import javax.portlet.PortletSession;
024    
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpSession;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Shuyang Zhou
031     */
032    public class SessionMessages {
033    
034            public static final String KEY_SUFFIX_CLOSE_REDIRECT = ".closeRedirect";
035    
036            public static final String KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE =
037                    ".hideDefaultErrorMessage";
038    
039            public static final String KEY_SUFFIX_PORTLET_NOT_AJAXABLE =
040                    ".portletNotAjaxable";
041    
042            public static final String KEY_SUFFIX_REFRESH_PORTLET = ".refreshPortlet";
043    
044            public static final String KEY_SUFFIX_REFRESH_PORTLET_DATA =
045                    ".refreshPortletData";
046    
047            public static final String KEY_SUFFIX_UPDATED_CONFIGURATION =
048                    ".updatedConfiguration";
049    
050            public static final String KEY_SUFFIX_UPDATED_PREFERENCES =
051                    ".updatedPreferences";
052    
053            public static void add(HttpServletRequest request, String key) {
054                    add(request.getSession(), key);
055            }
056    
057            public static void add(
058                    HttpServletRequest request, String key, Object value) {
059    
060                    add(request.getSession(), key, value);
061            }
062    
063            public static void add(HttpSession session, String key) {
064                    Map<String, Object> messages = _getMessages(session, true);
065    
066                    messages.put(key, key);
067            }
068    
069            public static void add(HttpSession session, String key, Object value) {
070                    Map<String, Object> messages = _getMessages(session, true);
071    
072                    messages.put(key, value);
073            }
074    
075            public static void add(PortletRequest portletRequest, String key) {
076                    add(portletRequest.getPortletSession(), key);
077            }
078    
079            public static void add(
080                    PortletRequest portletRequest, String key, Object value) {
081    
082                    add(portletRequest.getPortletSession(), key, value);
083            }
084    
085            public static void add(PortletSession portletSession, String key) {
086                    Map<String, Object> messages = _getMessages(portletSession, true);
087    
088                    messages.put(key, key);
089            }
090    
091            public static void add(
092                    PortletSession portletSession, String key, Object value) {
093    
094                    Map<String, Object> messages = _getMessages(portletSession, true);
095    
096                    messages.put(key, value);
097            }
098    
099            public static void clear(HttpServletRequest request) {
100                    clear(request.getSession());
101            }
102    
103            public static void clear(HttpSession session) {
104                    Map<String, Object> messages = _getMessages(session, false);
105    
106                    if (messages != null) {
107                            messages.clear();
108                    }
109            }
110    
111            public static void clear(PortletRequest portletRequest) {
112                    clear(portletRequest.getPortletSession());
113            }
114    
115            public static void clear(PortletSession portletSession) {
116                    Map<String, Object> messages = _getMessages(portletSession, false);
117    
118                    if (messages != null) {
119                            messages.clear();
120                    }
121            }
122    
123            public static boolean contains(HttpServletRequest request, String key) {
124                    return contains(request.getSession(), key);
125            }
126    
127            public static boolean contains(HttpSession session, String key) {
128                    Map<String, Object> messages = _getMessages(session, false);
129    
130                    if (messages == null) {
131                            return false;
132                    }
133                    else {
134                            return messages.containsKey(key);
135                    }
136            }
137    
138            public static boolean contains(PortletRequest portletRequest, String key) {
139                    return contains(portletRequest.getPortletSession(), key);
140            }
141    
142            public static boolean contains(PortletSession portletSession, String key) {
143                    Map<String, Object> messages = _getMessages(portletSession, false);
144    
145                    if (messages == null) {
146                            return false;
147                    }
148                    else {
149                            return messages.containsKey(key);
150                    }
151            }
152    
153            public static Object get(HttpServletRequest request, String key) {
154                    return get(request.getSession(), key);
155            }
156    
157            public static Object get(HttpSession session, String key) {
158                    Map<String, Object> messages = _getMessages(session, false);
159    
160                    if (messages == null) {
161                            return null;
162                    }
163                    else {
164                            return messages.get(key);
165                    }
166            }
167    
168            public static Object get(PortletRequest portletRequest, String key) {
169                    return get(portletRequest.getPortletSession(), key);
170            }
171    
172            public static Object get(PortletSession portletSession, String key) {
173                    Map<String, Object> messages = _getMessages(portletSession, false);
174    
175                    if (messages == null) {
176                            return null;
177                    }
178                    else {
179                            return messages.get(key);
180                    }
181            }
182    
183            public static boolean isEmpty(HttpServletRequest request) {
184                    return isEmpty(request.getSession());
185            }
186    
187            public static boolean isEmpty(HttpSession session) {
188                    Map<String, Object> messages = _getMessages(session, false);
189    
190                    if (messages == null) {
191                            return true;
192                    }
193                    else {
194                            return messages.isEmpty();
195                    }
196            }
197    
198            public static boolean isEmpty(PortletRequest portletRequest) {
199                    return isEmpty(portletRequest.getPortletSession());
200            }
201    
202            public static boolean isEmpty(PortletSession portletSession) {
203                    Map<String, Object> messages = _getMessages(portletSession, false);
204    
205                    if (messages == null) {
206                            return true;
207                    }
208                    else {
209                            return messages.isEmpty();
210                    }
211            }
212    
213            public static Iterator<String> iterator(HttpServletRequest request) {
214                    return iterator(request.getSession());
215            }
216    
217            public static Iterator<String> iterator(HttpSession session) {
218                    Map<String, Object> messages = _getMessages(session, false);
219    
220                    if (messages == null) {
221                            return Collections.<String>emptyList().iterator();
222                    }
223                    else {
224                            return Collections.unmodifiableSet(messages.keySet()).iterator();
225                    }
226            }
227    
228            public static Iterator<String> iterator(PortletRequest portletRequest) {
229                    return iterator(portletRequest.getPortletSession());
230            }
231    
232            public static Iterator<String> iterator(PortletSession portletSession) {
233                    Map<String, Object> messages = _getMessages(portletSession, false);
234    
235                    if (messages == null) {
236                            return Collections.<String>emptyList().iterator();
237                    }
238                    else {
239                            return Collections.unmodifiableSet(messages.keySet()).iterator();
240                    }
241            }
242    
243            public static void print(HttpServletRequest request) {
244                    print(request.getSession());
245            }
246    
247            public static void print(HttpSession session) {
248                    Iterator<String> itr = iterator(session);
249    
250                    while (itr.hasNext()) {
251                            System.out.println(itr.next());
252                    }
253            }
254    
255            public static void print(PortletRequest portletRequest) {
256                    print(portletRequest.getPortletSession());
257            }
258    
259            public static void print(PortletSession portletSession) {
260                    Iterator<String> itr = iterator(portletSession);
261    
262                    while (itr.hasNext()) {
263                            System.out.println(itr.next());
264                    }
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> messages = _getMessages(session, false);
273    
274                    if (messages == null) {
275                            return 0;
276                    }
277                    else {
278                            return messages.size();
279                    }
280            }
281    
282            public static int size(PortletRequest portletRequest) {
283                    return size(portletRequest.getPortletSession());
284            }
285    
286            public static int size(PortletSession portletSession) {
287                    Map<String, Object> messages = _getMessages(portletSession, false);
288    
289                    if (messages == null) {
290                            return 0;
291                    }
292                    else {
293                            return messages.size();
294                    }
295            }
296    
297            private static Map<String, Object> _getMessages(
298                    HttpSession session, boolean createIfAbsent) {
299    
300                    Map<String, Object> messages = null;
301    
302                    try {
303                            messages = (Map<String, Object>)session.getAttribute(
304                                    SessionMessages.class.getName());
305    
306                            if ((messages == null) && createIfAbsent) {
307                                    messages = new LinkedHashMap<String, Object>();
308    
309                                    session.setAttribute(SessionMessages.class.getName(), messages);
310                            }
311                    }
312                    catch (IllegalStateException ise) {
313    
314                            // Session is already invalidated, just return a null map
315    
316                    }
317    
318                    return messages;
319            }
320    
321            private static Map<String, Object> _getMessages(
322                    PortletSession portletSession, boolean createIfAbsent) {
323    
324                    Map<String, Object> messages = null;
325    
326                    try {
327                            messages = (Map<String, Object>)portletSession.getAttribute(
328                                    SessionMessages.class.getName());
329    
330                            if ((messages == null) && createIfAbsent) {
331                                    messages = new LinkedHashMap<String, Object>();
332    
333                                    portletSession.setAttribute(
334                                            SessionMessages.class.getName(), messages);
335                            }
336                    }
337                    catch (IllegalStateException ise) {
338    
339                            // Session is already invalidated, just return a null map
340    
341                    }
342    
343                    return messages;
344            }
345    
346    }