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  import java.util.Set;
30  
31  import javax.portlet.PortletRequest;
32  import javax.portlet.PortletSession;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpSession;
36  
37  /**
38   * <a href="SessionErrors.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class SessionErrors {
43  
44      public static final String KEY = SessionErrors.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> errors = _getErrors(session);
54  
55          errors.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> errors = _getErrors(session);
66  
67          errors.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> errors = _getErrors(session);
76  
77          errors.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> errors = _getErrors(session);
86  
87          return errors.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> errors = _getErrors(session);
96  
97          return errors.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> errors = _getErrors(session);
106 
107         return errors.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> errors = _getErrors(session);
116 
117         return Collections.unmodifiableSet(errors.keySet()).iterator();
118     }
119 
120     public static Set<String> keySet(HttpServletRequest request) {
121         return keySet(request.getSession());
122     }
123 
124     public static Set<String> keySet(HttpSession session) {
125         Map<String, Object> errors = _getErrors(session);
126 
127         return Collections.unmodifiableSet(errors.keySet());
128     }
129 
130     public static void print(HttpServletRequest request) {
131         print(request.getSession());
132     }
133 
134     public static void print(HttpSession session) {
135         Iterator<String> itr = iterator(session);
136 
137         while (itr.hasNext()) {
138             System.out.println(itr.next());
139         }
140     }
141 
142     public static int size(HttpServletRequest request) {
143         return size(request.getSession());
144     }
145 
146     public static int size(HttpSession session) {
147         Map<String, Object> errors = _getErrors(session);
148 
149         return errors.size();
150     }
151 
152     private static Map<String, Object> _getErrors(HttpSession session) {
153         Map<String, Object> errors = null;
154 
155         try {
156             errors = (Map<String, Object>)session.getAttribute(KEY);
157 
158             if (errors == null) {
159                 errors = new LinkedHashMap<String, Object>();
160 
161                 session.setAttribute(KEY, errors);
162             }
163         }
164         catch (IllegalStateException ise) {
165             errors = new LinkedHashMap<String, Object>();
166         }
167 
168         return errors;
169     }
170 
171     // Portlet Request
172 
173     public static void add(PortletRequest portletRequest, String key) {
174         add(portletRequest.getPortletSession(), key);
175     }
176 
177     public static void add(PortletSession portletSession, String key) {
178         Map<String, Object> errors = _getErrors(portletSession);
179 
180         errors.put(key, key);
181     }
182 
183     public static void add(
184         PortletRequest portletRequest, String key, Object value) {
185 
186         add(portletRequest.getPortletSession(), key, value);
187     }
188 
189     public static void add(
190         PortletSession portletSession, String key, Object value) {
191 
192         Map<String, Object> errors = _getErrors(portletSession);
193 
194         errors.put(key, value);
195     }
196 
197     public static void clear(PortletRequest portletRequest) {
198         clear(portletRequest.getPortletSession());
199     }
200 
201     public static void clear(PortletSession portletSession) {
202         Map<String, Object> errors = _getErrors(portletSession);
203 
204         errors.clear();
205     }
206 
207     public static boolean contains(PortletRequest portletRequest, String key) {
208         return contains(portletRequest.getPortletSession(), key);
209     }
210 
211     public static boolean contains(PortletSession portletSession, String key) {
212         Map<String, Object> errors = _getErrors(portletSession);
213 
214         return errors.containsKey(key);
215     }
216 
217     public static Object get(PortletRequest portletRequest, String key) {
218         return get(portletRequest.getPortletSession(), key);
219     }
220 
221     public static Object get(PortletSession portletSession, String key) {
222         Map<String, Object> errors = _getErrors(portletSession);
223 
224         return errors.get(key);
225     }
226 
227     public static boolean isEmpty(PortletRequest portletRequest) {
228         return isEmpty(portletRequest.getPortletSession());
229     }
230 
231     public static boolean isEmpty(PortletSession portletSession) {
232         Map<String, Object> errors = _getErrors(portletSession);
233 
234         return errors.isEmpty();
235     }
236 
237     public static Iterator<String> iterator(PortletRequest portletRequest) {
238         return iterator(portletRequest.getPortletSession());
239     }
240 
241     public static Iterator<String> iterator(PortletSession portletSession) {
242         Map<String, Object> errors = _getErrors(portletSession);
243 
244         return Collections.unmodifiableSet(errors.keySet()).iterator();
245     }
246 
247     public static Set<String> keySet(PortletRequest portletRequest) {
248         return keySet(portletRequest.getPortletSession());
249     }
250 
251     public static Set<String> keySet(PortletSession portletSession) {
252         Map<String, Object> errors = _getErrors(portletSession);
253 
254         return Collections.unmodifiableSet(errors.keySet());
255     }
256 
257     public static void print(PortletRequest portletRequest) {
258         print(portletRequest.getPortletSession());
259     }
260 
261     public static void print(PortletSession portletSession) {
262         Iterator<String> itr = iterator(portletSession);
263 
264         while (itr.hasNext()) {
265             System.out.println(itr.next());
266         }
267     }
268 
269     public static int size(PortletRequest portletRequest) {
270         return size(portletRequest.getPortletSession());
271     }
272 
273     public static int size(PortletSession portletSession) {
274         Map<String, Object> errors = _getErrors(portletSession);
275 
276         return errors.size();
277     }
278 
279     private static Map<String, Object> _getErrors(
280         PortletSession portletSession) {
281 
282         Map<String, Object> errors = null;
283 
284         try {
285             errors = (Map<String, Object>)portletSession.getAttribute(KEY);
286 
287             if (errors == null) {
288                 errors = new LinkedHashMap<String, Object>();
289 
290                 portletSession.setAttribute(KEY, errors);
291             }
292         }
293         catch (IllegalStateException ise) {
294             errors = new LinkedHashMap<String, Object>();
295         }
296 
297         return errors;
298     }
299 
300 }