001
014
015 package com.liferay.portal.kernel.servlet;
016
017 import java.util.Collections;
018 import java.util.Iterator;
019 import java.util.LinkedHashMap;
020 import java.util.Map;
021 import java.util.Set;
022
023 import javax.portlet.PortletRequest;
024 import javax.portlet.PortletSession;
025
026 import javax.servlet.http.HttpServletRequest;
027 import javax.servlet.http.HttpSession;
028
029
033 public class SessionErrors {
034
035 public static void add(HttpServletRequest request, String key) {
036 add(request.getSession(), key);
037 }
038
039 public static void add(
040 HttpServletRequest request, String key, Object value) {
041
042 add(request.getSession(), key, value);
043 }
044
045 public static void add(HttpSession session, String key) {
046 Map<String, Object> errors = _getErrors(session, true);
047
048 errors.put(key, key);
049 }
050
051 public static void add(HttpSession session, String key, Object value) {
052 Map<String, Object> errors = _getErrors(session, true);
053
054 errors.put(key, value);
055 }
056
057 public static void add(PortletRequest portletRequest, String key) {
058 add(portletRequest.getPortletSession(), key);
059 }
060
061 public static void add(
062 PortletRequest portletRequest, String key, Object value) {
063
064 add(portletRequest.getPortletSession(), key, value);
065 }
066
067 public static void add(PortletSession portletSession, String key) {
068 Map<String, Object> errors = _getErrors(portletSession, true);
069
070 errors.put(key, key);
071 }
072
073 public static void add(
074 PortletSession portletSession, String key, Object value) {
075
076 Map<String, Object> errors = _getErrors(portletSession, true);
077
078 errors.put(key, value);
079 }
080
081 public static void clear(HttpServletRequest request) {
082 clear(request.getSession());
083 }
084
085 public static void clear(HttpSession session) {
086 Map<String, Object> errors = _getErrors(session, false);
087
088 if (errors != null) {
089 errors.clear();
090 }
091 }
092
093 public static void clear(PortletRequest portletRequest) {
094 clear(portletRequest.getPortletSession());
095 }
096
097 public static void clear(PortletSession portletSession) {
098 Map<String, Object> errors = _getErrors(portletSession, false);
099
100 if (errors != null) {
101 errors.clear();
102 }
103 }
104
105 public static boolean contains(HttpServletRequest request, String key) {
106 return contains(request.getSession(), key);
107 }
108
109 public static boolean contains(HttpSession session, String key) {
110 Map<String, Object> errors = _getErrors(session, false);
111
112 if (errors == null) {
113 return false;
114 }
115 else {
116 return errors.containsKey(key);
117 }
118 }
119
120 public static boolean contains(PortletRequest portletRequest, String key) {
121 return contains(portletRequest.getPortletSession(), key);
122 }
123
124 public static boolean contains(PortletSession portletSession, String key) {
125 Map<String, Object> errors = _getErrors(portletSession, false);
126
127 if (errors == null) {
128 return false;
129 }
130 else {
131 return errors.containsKey(key);
132 }
133 }
134
135 public static Object get(HttpServletRequest request, String key) {
136 return get(request.getSession(), key);
137 }
138
139 public static Object get(HttpSession session, String key) {
140 Map<String, Object> errors = _getErrors(session, false);
141
142 if (errors == null) {
143 return null;
144 }
145 else {
146 return errors.get(key);
147 }
148 }
149
150 public static Object get(PortletRequest portletRequest, String key) {
151 return get(portletRequest.getPortletSession(), key);
152 }
153
154 public static Object get(PortletSession portletSession, String key) {
155 Map<String, Object> errors = _getErrors(portletSession, false);
156
157 if (errors == null) {
158 return null;
159 }
160 else {
161 return errors.get(key);
162 }
163 }
164
165 public static boolean isEmpty(HttpServletRequest request) {
166 return isEmpty(request.getSession());
167 }
168
169 public static boolean isEmpty(HttpSession session) {
170 Map<String, Object> errors = _getErrors(session, false);
171
172 if (errors == null) {
173 return true;
174 }
175 else {
176 return errors.isEmpty();
177 }
178 }
179
180 public static boolean isEmpty(PortletRequest portletRequest) {
181 return isEmpty(portletRequest.getPortletSession());
182 }
183
184 public static boolean isEmpty(PortletSession portletSession) {
185 Map<String, Object> errors = _getErrors(portletSession, false);
186
187 if (errors == null) {
188 return true;
189 }
190 else {
191 return errors.isEmpty();
192 }
193 }
194
195 public static Iterator<String> iterator(HttpServletRequest request) {
196 return iterator(request.getSession());
197 }
198
199 public static Iterator<String> iterator(HttpSession session) {
200 Map<String, Object> errors = _getErrors(session, false);
201
202 if (errors == null) {
203 return Collections.<String>emptyList().iterator();
204 }
205 else {
206 return Collections.unmodifiableSet(errors.keySet()).iterator();
207 }
208 }
209
210 public static Iterator<String> iterator(PortletRequest portletRequest) {
211 return iterator(portletRequest.getPortletSession());
212 }
213
214 public static Iterator<String> iterator(PortletSession portletSession) {
215 Map<String, Object> errors = _getErrors(portletSession, false);
216
217 if (errors == null) {
218 return Collections.<String>emptyList().iterator();
219 }
220 else {
221 return Collections.unmodifiableSet(errors.keySet()).iterator();
222 }
223 }
224
225 public static Set<String> keySet(HttpServletRequest request) {
226 return keySet(request.getSession());
227 }
228
229 public static Set<String> keySet(HttpSession session) {
230 Map<String, Object> errors = _getErrors(session, false);
231
232 if (errors == null) {
233 return Collections.emptySet();
234 }
235 else {
236 return Collections.unmodifiableSet(errors.keySet());
237 }
238 }
239
240 public static Set<String> keySet(PortletRequest portletRequest) {
241 return keySet(portletRequest.getPortletSession());
242 }
243
244 public static Set<String> keySet(PortletSession portletSession) {
245 Map<String, Object> errors = _getErrors(portletSession, false);
246
247 if (errors == null) {
248 return Collections.emptySet();
249 }
250 else {
251 return Collections.unmodifiableSet(errors.keySet());
252 }
253 }
254
255 public static void print(HttpServletRequest request) {
256 print(request.getSession());
257 }
258
259 public static void print(HttpSession session) {
260 Iterator<String> itr = iterator(session);
261
262 while (itr.hasNext()) {
263 System.out.println(itr.next());
264 }
265 }
266
267 public static void print(PortletRequest portletRequest) {
268 print(portletRequest.getPortletSession());
269 }
270
271 public static void print(PortletSession portletSession) {
272 Iterator<String> itr = iterator(portletSession);
273
274 while (itr.hasNext()) {
275 System.out.println(itr.next());
276 }
277 }
278
279 public static int size(HttpServletRequest request) {
280 return size(request.getSession());
281 }
282
283 public static int size(HttpSession session) {
284 Map<String, Object> errors = _getErrors(session, false);
285
286 if (errors == null) {
287 return 0;
288 }
289 else {
290 return errors.size();
291 }
292 }
293
294 public static int size(PortletRequest portletRequest) {
295 return size(portletRequest.getPortletSession());
296 }
297
298 public static int size(PortletSession portletSession) {
299 Map<String, Object> errors = _getErrors(portletSession, false);
300
301 if (errors == null) {
302 return 0;
303 }
304 else {
305 return errors.size();
306 }
307 }
308
309 private static Map<String, Object> _getErrors(
310 HttpSession session, boolean createIfAbsent) {
311
312 Map<String, Object> errors = null;
313
314 try {
315 errors = (Map<String, Object>)session.getAttribute(
316 SessionErrors.class.getName());
317
318 if ((errors == null) && createIfAbsent) {
319 errors = new LinkedHashMap<String, Object>();
320
321 session.setAttribute(SessionErrors.class.getName(), errors);
322 }
323 }
324 catch (IllegalStateException ise) {
325
326
327
328 }
329
330 return errors;
331 }
332
333 private static Map<String, Object> _getErrors(
334 PortletSession portletSession, boolean createIfAbsent) {
335
336 Map<String, Object> errors = null;
337
338 try {
339 errors = (Map<String, Object>)portletSession.getAttribute(
340 SessionErrors.class.getName());
341
342 if ((errors == null) && createIfAbsent) {
343 errors = new LinkedHashMap<String, Object>();
344
345 portletSession.setAttribute(
346 SessionErrors.class.getName(), errors);
347 }
348 }
349 catch (IllegalStateException ise) {
350
351
352
353 }
354
355 return errors;
356 }
357
358 }