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