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