1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class SessionMessages {
42  
43      public static final String KEY = SessionMessages.class.getName();
44  
45      // Servlet Request
46  
47      public static void add(HttpServletRequest request, String key) {
48          add(request.getSession(), key);
49      }
50  
51      public static void add(HttpSession session, String key) {
52          Map<String, Object> messages = _getMessages(session);
53  
54          messages.put(key, key);
55      }
56  
57      public static void add(
58          HttpServletRequest request, String key, Object value) {
59  
60          add(request.getSession(), key, value);
61      }
62  
63      public static void add(HttpSession session, String key, Object value) {
64          Map<String, Object> messages = _getMessages(session);
65  
66          messages.put(key, value);
67      }
68  
69      public static void clear(HttpServletRequest request) {
70          clear(request.getSession());
71      }
72  
73      public static void clear(HttpSession session) {
74          Map<String, Object> messages = _getMessages(session);
75  
76          messages.clear();
77      }
78  
79      public static boolean contains(HttpServletRequest request, String key) {
80          return contains(request.getSession(), key);
81      }
82  
83      public static boolean contains(HttpSession session, String key) {
84          Map<String, Object> messages = _getMessages(session);
85  
86          return messages.containsKey(key);
87      }
88  
89      public static Object get(HttpServletRequest request, String key) {
90          return get(request.getSession(), key);
91      }
92  
93      public static Object get(HttpSession session, String key) {
94          Map<String, Object> messages = _getMessages(session);
95  
96          return messages.get(key);
97      }
98  
99      public static boolean isEmpty(HttpServletRequest request) {
100         return isEmpty(request.getSession());
101     }
102 
103     public static boolean isEmpty(HttpSession session) {
104         Map<String, Object> messages = _getMessages(session);
105 
106         return messages.isEmpty();
107     }
108 
109     public static Iterator<String> iterator(HttpServletRequest request) {
110         return iterator(request.getSession());
111     }
112 
113     public static Iterator<String> iterator(HttpSession session) {
114         Map<String, Object> messages = _getMessages(session);
115 
116         return Collections.unmodifiableSet(messages.keySet()).iterator();
117     }
118 
119     public static void print(HttpServletRequest request) {
120         print(request.getSession());
121     }
122 
123     public static void print(HttpSession session) {
124         Iterator<String> itr = iterator(session);
125 
126         while (itr.hasNext()) {
127             System.out.println(itr.next());
128         }
129     }
130 
131     public static int size(HttpServletRequest request) {
132         return size(request.getSession());
133     }
134 
135     public static int size(HttpSession session) {
136         Map<String, Object> messages = _getMessages(session);
137 
138         return messages.size();
139     }
140 
141     private static Map<String, Object> _getMessages(HttpSession session) {
142         Map<String, Object> messages = null;
143 
144         try {
145             messages = (Map<String, Object>)session.getAttribute(KEY);
146 
147             if (messages == null) {
148                 messages = new LinkedHashMap<String, Object>();
149 
150                 session.setAttribute(KEY, messages);
151             }
152         }
153         catch (IllegalStateException ise) {
154             messages = new LinkedHashMap<String, Object>();
155         }
156 
157         return messages;
158     }
159 
160     // Portlet Request
161 
162     public static void add(PortletRequest portletRequest, String key) {
163         add(portletRequest.getPortletSession(), key);
164     }
165 
166     public static void add(PortletSession portletSession, String key) {
167         Map<String, Object> messages = _getMessages(portletSession);
168 
169         messages.put(key, key);
170     }
171 
172     public static void add(
173         PortletRequest portletRequest, String key, Object value) {
174 
175         add(portletRequest.getPortletSession(), key, value);
176     }
177 
178     public static void add(
179         PortletSession portletSession, String key, Object value) {
180 
181         Map<String, Object> messages = _getMessages(portletSession);
182 
183         messages.put(key, value);
184     }
185 
186     public static void clear(PortletRequest portletRequest) {
187         clear(portletRequest.getPortletSession());
188     }
189 
190     public static void clear(PortletSession portletSession) {
191         Map<String, Object> messages = _getMessages(portletSession);
192 
193         messages.clear();
194     }
195 
196     public static boolean contains(PortletRequest portletRequest, String key) {
197         return contains(portletRequest.getPortletSession(), key);
198     }
199 
200     public static boolean contains(PortletSession portletSession, String key) {
201         Map<String, Object> messages = _getMessages(portletSession);
202 
203         return messages.containsKey(key);
204     }
205 
206     public static Object get(PortletRequest portletRequest, String key) {
207         return get(portletRequest.getPortletSession(), key);
208     }
209 
210     public static Object get(PortletSession portletSession, String key) {
211         Map<String, Object> messages = _getMessages(portletSession);
212 
213         return messages.get(key);
214     }
215 
216     public static boolean isEmpty(PortletRequest portletRequest) {
217         return isEmpty(portletRequest.getPortletSession());
218     }
219 
220     public static boolean isEmpty(PortletSession portletSession) {
221         Map<String, Object> messages = _getMessages(portletSession);
222 
223         return messages.isEmpty();
224     }
225 
226     public static Iterator<String> iterator(PortletRequest portletRequest) {
227         return iterator(portletRequest.getPortletSession());
228     }
229 
230     public static Iterator<String> iterator(PortletSession portletSession) {
231         Map<String, Object> messages = _getMessages(portletSession);
232 
233         return Collections.unmodifiableSet(messages.keySet()).iterator();
234     }
235 
236     public static void print(PortletRequest portletRequest) {
237         print(portletRequest.getPortletSession());
238     }
239 
240     public static void print(PortletSession portletSession) {
241         Iterator<String> itr = iterator(portletSession);
242 
243         while (itr.hasNext()) {
244             System.out.println(itr.next());
245         }
246     }
247 
248     public static int size(PortletRequest portletRequest) {
249         return size(portletRequest.getPortletSession());
250     }
251 
252     public static int size(PortletSession portletSession) {
253         Map<String, Object> messages = _getMessages(portletSession);
254 
255         return messages.size();
256     }
257 
258     private static Map<String, Object> _getMessages(
259         PortletSession portletSession) {
260 
261         Map<String, Object> messages = null;
262 
263         try {
264             messages = (Map<String, Object>)portletSession.getAttribute(KEY);
265 
266             if (messages == null) {
267                 messages = new LinkedHashMap<String, Object>();
268 
269                 portletSession.setAttribute(KEY, messages);
270             }
271         }
272         catch (IllegalStateException ise) {
273             messages = new LinkedHashMap<String, Object>();
274         }
275 
276         return messages;
277     }
278 
279 }