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