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.util.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 req, String key) {
49          add(req.getSession(), key);
50      }
51  
52      public static void add(HttpSession ses, String key) {
53          Map messages = _getMessages(ses);
54  
55          messages.put(key, key);
56      }
57  
58      public static void add(HttpServletRequest req, String key, Object value) {
59          add(req.getSession(), key, value);
60      }
61  
62      public static void add(HttpSession ses, String key, Object value) {
63          Map messages = _getMessages(ses);
64  
65          messages.put(key, value);
66      }
67  
68      public static void clear(HttpServletRequest req) {
69          clear(req.getSession());
70      }
71  
72      public static void clear(HttpSession ses) {
73          Map messages = _getMessages(ses);
74  
75          messages.clear();
76      }
77  
78      public static boolean contains(HttpServletRequest req, String key) {
79          return contains(req.getSession(), key);
80      }
81  
82      public static boolean contains(HttpSession ses, String key) {
83          Map messages = _getMessages(ses);
84  
85          return messages.containsKey(key);
86      }
87  
88      public static Object get(HttpServletRequest req, String key) {
89          return get(req.getSession(), key);
90      }
91  
92      public static Object get(HttpSession ses, String key) {
93          Map messages = _getMessages(ses);
94  
95          return messages.get(key);
96      }
97  
98      public static boolean isEmpty(HttpServletRequest req) {
99          return isEmpty(req.getSession());
100     }
101 
102     public static boolean isEmpty(HttpSession ses) {
103         Map messages = _getMessages(ses);
104 
105         return messages.isEmpty();
106     }
107 
108     public static Iterator iterator(HttpServletRequest req) {
109         return iterator(req.getSession());
110     }
111 
112     public static Iterator iterator(HttpSession ses) {
113         Map messages = _getMessages(ses);
114 
115         return Collections.unmodifiableSet(messages.keySet()).iterator();
116     }
117 
118     public static void print(HttpServletRequest req) {
119         print(req.getSession());
120     }
121 
122     public static void print(HttpSession ses) {
123         Iterator itr = iterator(ses);
124 
125         while (itr.hasNext()) {
126             System.out.println(itr.next());
127         }
128     }
129 
130     public static int size(HttpServletRequest req) {
131         return size(req.getSession());
132     }
133 
134     public static int size(HttpSession ses) {
135         Map messages = _getMessages(ses);
136 
137         return messages.size();
138     }
139 
140     private static Map _getMessages(HttpSession ses) {
141         Map messages = null;
142 
143         try {
144             messages = (Map)ses.getAttribute(KEY);
145 
146             if (messages == null) {
147                 messages = new LinkedHashMap();
148 
149                 ses.setAttribute(KEY, messages);
150             }
151         }
152         catch (IllegalStateException ise) {
153             messages = new LinkedHashMap();
154         }
155 
156         return messages;
157     }
158 
159     // Portlet Request
160 
161     public static void add(PortletRequest req, String key) {
162         add(req.getPortletSession(), key);
163     }
164 
165     public static void add(PortletSession ses, String key) {
166         Map messages = _getMessages(ses);
167 
168         messages.put(key, key);
169     }
170 
171     public static void add(PortletRequest req, String key, Object value) {
172         add(req.getPortletSession(), key, value);
173     }
174 
175     public static void add(PortletSession ses, String key, Object value) {
176         Map messages = _getMessages(ses);
177 
178         messages.put(key, value);
179     }
180 
181     public static void clear(PortletRequest req) {
182         clear(req.getPortletSession());
183     }
184 
185     public static void clear(PortletSession ses) {
186         Map messages = _getMessages(ses);
187 
188         messages.clear();
189     }
190 
191     public static boolean contains(PortletRequest req, String key) {
192         return contains(req.getPortletSession(), key);
193     }
194 
195     public static boolean contains(PortletSession ses, String key) {
196         Map messages = _getMessages(ses);
197 
198         return messages.containsKey(key);
199     }
200 
201     public static Object get(PortletRequest req, String key) {
202         return get(req.getPortletSession(), key);
203     }
204 
205     public static Object get(PortletSession ses, String key) {
206         Map messages = _getMessages(ses);
207 
208         return messages.get(key);
209     }
210 
211     public static boolean isEmpty(PortletRequest req) {
212         return isEmpty(req.getPortletSession());
213     }
214 
215     public static boolean isEmpty(PortletSession ses) {
216         Map messages = _getMessages(ses);
217 
218         return messages.isEmpty();
219     }
220 
221     public static Iterator iterator(PortletRequest req) {
222         return iterator(req.getPortletSession());
223     }
224 
225     public static Iterator iterator(PortletSession ses) {
226         Map messages = _getMessages(ses);
227 
228         return Collections.unmodifiableSet(messages.keySet()).iterator();
229     }
230 
231     public static void print(PortletRequest req) {
232         print(req.getPortletSession());
233     }
234 
235     public static void print(PortletSession ses) {
236         Iterator itr = iterator(ses);
237 
238         while (itr.hasNext()) {
239             System.out.println(itr.next());
240         }
241     }
242 
243     public static int size(PortletRequest req) {
244         return size(req.getPortletSession());
245     }
246 
247     public static int size(PortletSession ses) {
248         Map messages = _getMessages(ses);
249 
250         return messages.size();
251     }
252 
253     private static Map _getMessages(PortletSession ses) {
254         Map messages = null;
255 
256         try {
257             messages = (Map)ses.getAttribute(KEY);
258 
259             if (messages == null) {
260                 messages = new LinkedHashMap();
261 
262                 ses.setAttribute(KEY, messages);
263             }
264         }
265         catch (IllegalStateException ise) {
266             messages = new LinkedHashMap();
267         }
268 
269         return messages;
270     }
271 
272 }