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