1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.servlet;
24  
25  import java.util.Collections;
26  import java.util.Iterator;
27  import java.util.LinkedHashMap;
28  import java.util.Map;
29  
30  import javax.portlet.PortletRequest;
31  import javax.portlet.PortletSession;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpSession;
35  
36  /**
37   * <a href="SessionMessages.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class SessionMessages {
43  
44      public static final String KEY = SessionMessages.class.getName();
45  
46      // Servlet Request
47  
48      public static void add(HttpServletRequest request, String key) {
49          add(request.getSession(), key);
50      }
51  
52      public static void add(HttpSession session, String key) {
53          Map<String, Object> messages = _getMessages(session);
54  
55          messages.put(key, key);
56      }
57  
58      public static void add(
59          HttpServletRequest request, String key, Object value) {
60  
61          add(request.getSession(), key, value);
62      }
63  
64      public static void add(HttpSession session, String key, Object value) {
65          Map<String, Object> messages = _getMessages(session);
66  
67          messages.put(key, value);
68      }
69  
70      public static void clear(HttpServletRequest request) {
71          clear(request.getSession());
72      }
73  
74      public static void clear(HttpSession session) {
75          Map<String, Object> messages = _getMessages(session);
76  
77          messages.clear();
78      }
79  
80      public static boolean contains(HttpServletRequest request, String key) {
81          return contains(request.getSession(), key);
82      }
83  
84      public static boolean contains(HttpSession session, String key) {
85          Map<String, Object> messages = _getMessages(session);
86  
87          return messages.containsKey(key);
88      }
89  
90      public static Object get(HttpServletRequest request, String key) {
91          return get(request.getSession(), key);
92      }
93  
94      public static Object get(HttpSession session, String key) {
95          Map<String, Object> messages = _getMessages(session);
96  
97          return messages.get(key);
98      }
99  
100     public static boolean isEmpty(HttpServletRequest request) {
101         return isEmpty(request.getSession());
102     }
103 
104     public static boolean isEmpty(HttpSession session) {
105         Map<String, Object> messages = _getMessages(session);
106 
107         return messages.isEmpty();
108     }
109 
110     public static Iterator<String> iterator(HttpServletRequest request) {
111         return iterator(request.getSession());
112     }
113 
114     public static Iterator<String> iterator(HttpSession session) {
115         Map<String, Object> messages = _getMessages(session);
116 
117         return Collections.unmodifiableSet(messages.keySet()).iterator();
118     }
119 
120     public static void print(HttpServletRequest request) {
121         print(request.getSession());
122     }
123 
124     public static void print(HttpSession session) {
125         Iterator<String> itr = iterator(session);
126 
127         while (itr.hasNext()) {
128             System.out.println(itr.next());
129         }
130     }
131 
132     public static int size(HttpServletRequest request) {
133         return size(request.getSession());
134     }
135 
136     public static int size(HttpSession session) {
137         Map<String, Object> messages = _getMessages(session);
138 
139         return messages.size();
140     }
141 
142     private static Map<String, Object> _getMessages(HttpSession session) {
143         Map<String, Object> messages = null;
144 
145         try {
146             messages = (Map<String, Object>)session.getAttribute(KEY);
147 
148             if (messages == null) {
149                 messages = new LinkedHashMap<String, Object>();
150 
151                 session.setAttribute(KEY, messages);
152             }
153         }
154         catch (IllegalStateException ise) {
155             messages = new LinkedHashMap<String, Object>();
156         }
157 
158         return messages;
159     }
160 
161     // Portlet Request
162 
163     public static void add(PortletRequest portletRequest, String key) {
164         add(portletRequest.getPortletSession(), key);
165     }
166 
167     public static void add(PortletSession portletSession, String key) {
168         Map<String, Object> messages = _getMessages(portletSession);
169 
170         messages.put(key, key);
171     }
172 
173     public static void add(
174         PortletRequest portletRequest, String key, Object value) {
175 
176         add(portletRequest.getPortletSession(), key, value);
177     }
178 
179     public static void add(
180         PortletSession portletSession, String key, Object value) {
181 
182         Map<String, Object> messages = _getMessages(portletSession);
183 
184         messages.put(key, value);
185     }
186 
187     public static void clear(PortletRequest portletRequest) {
188         clear(portletRequest.getPortletSession());
189     }
190 
191     public static void clear(PortletSession portletSession) {
192         Map<String, Object> messages = _getMessages(portletSession);
193 
194         messages.clear();
195     }
196 
197     public static boolean contains(PortletRequest portletRequest, String key) {
198         return contains(portletRequest.getPortletSession(), key);
199     }
200 
201     public static boolean contains(PortletSession portletSession, String key) {
202         Map<String, Object> messages = _getMessages(portletSession);
203 
204         return messages.containsKey(key);
205     }
206 
207     public static Object get(PortletRequest portletRequest, String key) {
208         return get(portletRequest.getPortletSession(), key);
209     }
210 
211     public static Object get(PortletSession portletSession, String key) {
212         Map<String, Object> messages = _getMessages(portletSession);
213 
214         return messages.get(key);
215     }
216 
217     public static boolean isEmpty(PortletRequest portletRequest) {
218         return isEmpty(portletRequest.getPortletSession());
219     }
220 
221     public static boolean isEmpty(PortletSession portletSession) {
222         Map<String, Object> messages = _getMessages(portletSession);
223 
224         return messages.isEmpty();
225     }
226 
227     public static Iterator<String> iterator(PortletRequest portletRequest) {
228         return iterator(portletRequest.getPortletSession());
229     }
230 
231     public static Iterator<String> iterator(PortletSession portletSession) {
232         Map<String, Object> messages = _getMessages(portletSession);
233 
234         return Collections.unmodifiableSet(messages.keySet()).iterator();
235     }
236 
237     public static void print(PortletRequest portletRequest) {
238         print(portletRequest.getPortletSession());
239     }
240 
241     public static void print(PortletSession portletSession) {
242         Iterator<String> itr = iterator(portletSession);
243 
244         while (itr.hasNext()) {
245             System.out.println(itr.next());
246         }
247     }
248 
249     public static int size(PortletRequest portletRequest) {
250         return size(portletRequest.getPortletSession());
251     }
252 
253     public static int size(PortletSession portletSession) {
254         Map<String, Object> messages = _getMessages(portletSession);
255 
256         return messages.size();
257     }
258 
259     private static Map<String, Object> _getMessages(
260         PortletSession portletSession) {
261 
262         Map<String, Object> messages = null;
263 
264         try {
265             messages = (Map<String, Object>)portletSession.getAttribute(KEY);
266 
267             if (messages == null) {
268                 messages = new LinkedHashMap<String, Object>();
269 
270                 portletSession.setAttribute(KEY, messages);
271             }
272         }
273         catch (IllegalStateException ise) {
274             messages = new LinkedHashMap<String, Object>();
275         }
276 
277         return messages;
278     }
279 
280 }