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