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