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