001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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     */
031    public class SessionMessages {
032    
033            public static final String KEY = SessionMessages.class.getName();
034    
035            // Servlet Request
036    
037            public static void add(HttpServletRequest request, String key) {
038                    add(request.getSession(), key);
039            }
040    
041            public static void add(HttpSession session, String key) {
042                    Map<String, Object> messages = _getMessages(session);
043    
044                    messages.put(key, key);
045            }
046    
047            public static void add(
048                    HttpServletRequest request, String key, Object value) {
049    
050                    add(request.getSession(), key, value);
051            }
052    
053            public static void add(HttpSession session, String key, Object value) {
054                    Map<String, Object> messages = _getMessages(session);
055    
056                    messages.put(key, value);
057            }
058    
059            public static void clear(HttpServletRequest request) {
060                    clear(request.getSession());
061            }
062    
063            public static void clear(HttpSession session) {
064                    Map<String, Object> messages = _getMessages(session);
065    
066                    messages.clear();
067            }
068    
069            public static boolean contains(HttpServletRequest request, String key) {
070                    return contains(request.getSession(), key);
071            }
072    
073            public static boolean contains(HttpSession session, String key) {
074                    Map<String, Object> messages = _getMessages(session);
075    
076                    return messages.containsKey(key);
077            }
078    
079            public static Object get(HttpServletRequest request, String key) {
080                    return get(request.getSession(), key);
081            }
082    
083            public static Object get(HttpSession session, String key) {
084                    Map<String, Object> messages = _getMessages(session);
085    
086                    return messages.get(key);
087            }
088    
089            public static boolean isEmpty(HttpServletRequest request) {
090                    return isEmpty(request.getSession());
091            }
092    
093            public static boolean isEmpty(HttpSession session) {
094                    Map<String, Object> messages = _getMessages(session);
095    
096                    return messages.isEmpty();
097            }
098    
099            public static Iterator<String> iterator(HttpServletRequest request) {
100                    return iterator(request.getSession());
101            }
102    
103            public static Iterator<String> iterator(HttpSession session) {
104                    Map<String, Object> messages = _getMessages(session);
105    
106                    return Collections.unmodifiableSet(messages.keySet()).iterator();
107            }
108    
109            public static void print(HttpServletRequest request) {
110                    print(request.getSession());
111            }
112    
113            public static void print(HttpSession session) {
114                    Iterator<String> itr = iterator(session);
115    
116                    while (itr.hasNext()) {
117                            System.out.println(itr.next());
118                    }
119            }
120    
121            public static int size(HttpServletRequest request) {
122                    return size(request.getSession());
123            }
124    
125            public static int size(HttpSession session) {
126                    Map<String, Object> messages = _getMessages(session);
127    
128                    return messages.size();
129            }
130    
131            private static Map<String, Object> _getMessages(HttpSession session) {
132                    Map<String, Object> messages = null;
133    
134                    try {
135                            messages = (Map<String, Object>)session.getAttribute(KEY);
136    
137                            if (messages == null) {
138                                    messages = new LinkedHashMap<String, Object>();
139    
140                                    session.setAttribute(KEY, messages);
141                            }
142                    }
143                    catch (IllegalStateException ise) {
144                            messages = new LinkedHashMap<String, Object>();
145                    }
146    
147                    return messages;
148            }
149    
150            // Portlet Request
151    
152            public static void add(PortletRequest portletRequest, String key) {
153                    add(portletRequest.getPortletSession(), key);
154            }
155    
156            public static void add(PortletSession portletSession, String key) {
157                    Map<String, Object> messages = _getMessages(portletSession);
158    
159                    messages.put(key, key);
160            }
161    
162            public static void add(
163                    PortletRequest portletRequest, String key, Object value) {
164    
165                    add(portletRequest.getPortletSession(), key, value);
166            }
167    
168            public static void add(
169                    PortletSession portletSession, String key, Object value) {
170    
171                    Map<String, Object> messages = _getMessages(portletSession);
172    
173                    messages.put(key, value);
174            }
175    
176            public static void clear(PortletRequest portletRequest) {
177                    clear(portletRequest.getPortletSession());
178            }
179    
180            public static void clear(PortletSession portletSession) {
181                    Map<String, Object> messages = _getMessages(portletSession);
182    
183                    messages.clear();
184            }
185    
186            public static boolean contains(PortletRequest portletRequest, String key) {
187                    return contains(portletRequest.getPortletSession(), key);
188            }
189    
190            public static boolean contains(PortletSession portletSession, String key) {
191                    Map<String, Object> messages = _getMessages(portletSession);
192    
193                    return messages.containsKey(key);
194            }
195    
196            public static Object get(PortletRequest portletRequest, String key) {
197                    return get(portletRequest.getPortletSession(), key);
198            }
199    
200            public static Object get(PortletSession portletSession, String key) {
201                    Map<String, Object> messages = _getMessages(portletSession);
202    
203                    return messages.get(key);
204            }
205    
206            public static boolean isEmpty(PortletRequest portletRequest) {
207                    return isEmpty(portletRequest.getPortletSession());
208            }
209    
210            public static boolean isEmpty(PortletSession portletSession) {
211                    Map<String, Object> messages = _getMessages(portletSession);
212    
213                    return messages.isEmpty();
214            }
215    
216            public static Iterator<String> iterator(PortletRequest portletRequest) {
217                    return iterator(portletRequest.getPortletSession());
218            }
219    
220            public static Iterator<String> iterator(PortletSession portletSession) {
221                    Map<String, Object> messages = _getMessages(portletSession);
222    
223                    return Collections.unmodifiableSet(messages.keySet()).iterator();
224            }
225    
226            public static void print(PortletRequest portletRequest) {
227                    print(portletRequest.getPortletSession());
228            }
229    
230            public static void print(PortletSession portletSession) {
231                    Iterator<String> itr = iterator(portletSession);
232    
233                    while (itr.hasNext()) {
234                            System.out.println(itr.next());
235                    }
236            }
237    
238            public static int size(PortletRequest portletRequest) {
239                    return size(portletRequest.getPortletSession());
240            }
241    
242            public static int size(PortletSession portletSession) {
243                    Map<String, Object> messages = _getMessages(portletSession);
244    
245                    return messages.size();
246            }
247    
248            private static Map<String, Object> _getMessages(
249                    PortletSession portletSession) {
250    
251                    Map<String, Object> messages = null;
252    
253                    try {
254                            messages = (Map<String, Object>)portletSession.getAttribute(KEY);
255    
256                            if (messages == null) {
257                                    messages = new LinkedHashMap<String, Object>();
258    
259                                    portletSession.setAttribute(KEY, messages);
260                            }
261                    }
262                    catch (IllegalStateException ise) {
263                            messages = new LinkedHashMap<String, Object>();
264                    }
265    
266                    return messages;
267            }
268    
269    }