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 SessionMessages {
034
035 public static final String KEY_SUFFIX_CLOSE_REDIRECT = ".closeRedirect";
036
037 public static final String KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE =
038 ".hideDefaultErrorMessage";
039
040 public static final String KEY_SUFFIX_PORTLET_NOT_AJAXABLE =
041 ".portletNotAjaxable";
042
043 public static final String KEY_SUFFIX_REFRESH_PORTLET = ".refreshPortlet";
044
045 public static final String KEY_SUFFIX_REFRESH_PORTLET_DATA =
046 ".refreshPortletData";
047
048 public static final String KEY_SUFFIX_UPDATED_CONFIGURATION =
049 ".updatedConfiguration";
050
051 public static final String KEY_SUFFIX_UPDATED_PREFERENCES =
052 ".updatedPreferences";
053
054 public static void add(HttpServletRequest request, Class<?> clazz) {
055 add(request.getSession(), clazz.getName());
056 }
057
058 public static void add(
059 HttpServletRequest request, Class<?> clazz, Object value) {
060
061 add(request.getSession(), clazz.getName(), value);
062 }
063
064 public static void add(HttpServletRequest request, String key) {
065 add(request.getSession(), key);
066 }
067
068 public static void add(
069 HttpServletRequest request, String key, Object value) {
070
071 add(request.getSession(), key, value);
072 }
073
074 public static void add(HttpSession session, Class<?> clazz) {
075 add(session, clazz.getName());
076 }
077
078 public static void add(HttpSession session, Class<?> clazz, Object value) {
079 add(session, clazz.getName(), value);
080 }
081
082 public static void add(HttpSession session, String key) {
083 Map<String, Object> map = _getMap(session, true);
084
085 map.put(key, key);
086 }
087
088 public static void add(HttpSession session, String key, Object value) {
089 Map<String, Object> map = _getMap(session, true);
090
091 map.put(key, value);
092 }
093
094 public static void add(PortletRequest portletRequest, Class<?> clazz) {
095 add(portletRequest.getPortletSession(), clazz.getName());
096 }
097
098 public static void add(
099 PortletRequest portletRequest, Class<?> clazz, Object value) {
100
101 add(portletRequest.getPortletSession(), clazz.getName(), value);
102 }
103
104 public static void add(PortletRequest portletRequest, String key) {
105 add(portletRequest.getPortletSession(), key);
106 }
107
108 public static void add(
109 PortletRequest portletRequest, String key, Object value) {
110
111 add(portletRequest.getPortletSession(), key, value);
112 }
113
114 public static void add(PortletSession portletSession, Class<?> clazz) {
115 add(portletSession, clazz.getName());
116 }
117
118 public static void add(
119 PortletSession portletSession, Class<?> clazz, Object value) {
120
121 add(portletSession, clazz.getName(), value);
122 }
123
124 public static void add(PortletSession portletSession, String key) {
125 Map<String, Object> map = _getMap(portletSession, true);
126
127 map.put(key, key);
128 }
129
130 public static void add(
131 PortletSession portletSession, String key, Object value) {
132
133 Map<String, Object> map = _getMap(portletSession, true);
134
135 map.put(key, value);
136 }
137
138 public static void clear(HttpServletRequest request) {
139 clear(request.getSession());
140 }
141
142 public static void clear(HttpSession session) {
143 Map<String, Object> map = _getMap(session, false);
144
145 if (map != null) {
146 map.clear();
147 }
148 }
149
150 public static void clear(PortletRequest portletRequest) {
151 clear(portletRequest.getPortletSession());
152 }
153
154 public static void clear(PortletSession portletSession) {
155 Map<String, Object> map = _getMap(portletSession, false);
156
157 if (map != null) {
158 map.clear();
159 }
160 }
161
162 public static boolean contains(HttpServletRequest request, Class<?> clazz) {
163 return contains(request.getSession(), clazz.getName());
164 }
165
166 public static boolean contains(HttpServletRequest request, String key) {
167 return contains(request.getSession(), key);
168 }
169
170 public static boolean contains(HttpSession session, Class<?> clazz) {
171 return contains(session, clazz.getName());
172 }
173
174 public static boolean contains(HttpSession session, String key) {
175 Map<String, Object> map = _getMap(session, false);
176
177 if (map == null) {
178 return false;
179 }
180 else {
181 return map.containsKey(key);
182 }
183 }
184
185 public static boolean contains(
186 PortletRequest portletRequest, Class<?> clazz) {
187
188 return contains(portletRequest.getPortletSession(), clazz.getName());
189 }
190
191 public static boolean contains(PortletRequest portletRequest, String key) {
192 return contains(portletRequest.getPortletSession(), key);
193 }
194
195 public static boolean contains(
196 PortletSession portletSession, Class<?> clazz) {
197
198 return contains(portletSession, clazz.getName());
199 }
200
201 public static boolean contains(PortletSession portletSession, String key) {
202 Map<String, Object> map = _getMap(portletSession, false);
203
204 if (map == null) {
205 return false;
206 }
207 else {
208 return map.containsKey(key);
209 }
210 }
211
212 public static Object get(HttpServletRequest request, Class<?> clazz) {
213 return get(request.getSession(), clazz.getName());
214 }
215
216 public static Object get(HttpServletRequest request, String key) {
217 return get(request.getSession(), key);
218 }
219
220 public static Object get(HttpSession session, Class<?> clazz) {
221 return get(session, clazz.getName());
222 }
223
224 public static Object get(HttpSession session, String key) {
225 Map<String, Object> map = _getMap(session, false);
226
227 if (map == null) {
228 return null;
229 }
230 else {
231 return map.get(key);
232 }
233 }
234
235 public static Object get(PortletRequest portletRequest, Class<?> clazz) {
236 return get(portletRequest.getPortletSession(), clazz.getName());
237 }
238
239 public static Object get(PortletRequest portletRequest, String key) {
240 return get(portletRequest.getPortletSession(), key);
241 }
242
243 public static Object get(PortletSession portletSession, Class<?> clazz) {
244 return get(portletSession, clazz.getName());
245 }
246
247 public static Object get(PortletSession portletSession, String key) {
248 Map<String, Object> map = _getMap(portletSession, false);
249
250 if (map == null) {
251 return null;
252 }
253 else {
254 return map.get(key);
255 }
256 }
257
258 public static boolean isEmpty(HttpServletRequest request) {
259 return isEmpty(request.getSession());
260 }
261
262 public static boolean isEmpty(HttpSession session) {
263 Map<String, Object> map = _getMap(session, false);
264
265 if (map == null) {
266 return true;
267 }
268 else {
269 return map.isEmpty();
270 }
271 }
272
273 public static boolean isEmpty(PortletRequest portletRequest) {
274 return isEmpty(portletRequest.getPortletSession());
275 }
276
277 public static boolean isEmpty(PortletSession portletSession) {
278 Map<String, Object> map = _getMap(portletSession, false);
279
280 if (map == null) {
281 return true;
282 }
283 else {
284 return map.isEmpty();
285 }
286 }
287
288 public static Iterator<String> iterator(HttpServletRequest request) {
289 return iterator(request.getSession());
290 }
291
292 public static Iterator<String> iterator(HttpSession session) {
293 Map<String, Object> map = _getMap(session, false);
294
295 if (map == null) {
296 return Collections.<String>emptyList().iterator();
297 }
298 else {
299 return Collections.unmodifiableSet(map.keySet()).iterator();
300 }
301 }
302
303 public static Iterator<String> iterator(PortletRequest portletRequest) {
304 return iterator(portletRequest.getPortletSession());
305 }
306
307 public static Iterator<String> iterator(PortletSession portletSession) {
308 Map<String, Object> map = _getMap(portletSession, false);
309
310 if (map == null) {
311 return Collections.<String>emptyList().iterator();
312 }
313 else {
314 return Collections.unmodifiableSet(map.keySet()).iterator();
315 }
316 }
317
318 public static Set<String> keySet(HttpServletRequest request) {
319 return keySet(request.getSession());
320 }
321
322 public static Set<String> keySet(HttpSession session) {
323 Map<String, Object> map = _getMap(session, false);
324
325 if (map == null) {
326 return Collections.emptySet();
327 }
328 else {
329 return Collections.unmodifiableSet(map.keySet());
330 }
331 }
332
333 public static Set<String> keySet(PortletRequest portletRequest) {
334 return keySet(portletRequest.getPortletSession());
335 }
336
337 public static Set<String> keySet(PortletSession portletSession) {
338 Map<String, Object> map = _getMap(portletSession, false);
339
340 if (map == null) {
341 return Collections.emptySet();
342 }
343 else {
344 return Collections.unmodifiableSet(map.keySet());
345 }
346 }
347
348 public static void print(HttpServletRequest request) {
349 print(request.getSession());
350 }
351
352 public static void print(HttpSession session) {
353 Iterator<String> itr = iterator(session);
354
355 while (itr.hasNext()) {
356 System.out.println(itr.next());
357 }
358 }
359
360 public static void print(PortletRequest portletRequest) {
361 print(portletRequest.getPortletSession());
362 }
363
364 public static void print(PortletSession portletSession) {
365 Iterator<String> itr = iterator(portletSession);
366
367 while (itr.hasNext()) {
368 System.out.println(itr.next());
369 }
370 }
371
372 public static int size(HttpServletRequest request) {
373 return size(request.getSession());
374 }
375
376 public static int size(HttpSession session) {
377 Map<String, Object> map = _getMap(session, false);
378
379 if (map == null) {
380 return 0;
381 }
382 else {
383 return map.size();
384 }
385 }
386
387 public static int size(PortletRequest portletRequest) {
388 return size(portletRequest.getPortletSession());
389 }
390
391 public static int size(PortletSession portletSession) {
392 Map<String, Object> map = _getMap(portletSession, false);
393
394 if (map == null) {
395 return 0;
396 }
397 else {
398 return map.size();
399 }
400 }
401
402 private static Map<String, Object> _getMap(
403 HttpSession session, boolean createIfAbsent) {
404
405 Map<String, Object> map = null;
406
407 try {
408 map = (Map<String, Object>)session.getAttribute(_CLASS_NAME);
409
410 if ((map == null) && createIfAbsent) {
411 map = new LinkedHashMap<String, Object>();
412
413 session.setAttribute(_CLASS_NAME, map);
414 }
415 }
416 catch (IllegalStateException ise) {
417
418
419
420 }
421
422 return map;
423 }
424
425 private static Map<String, Object> _getMap(
426 PortletSession portletSession, boolean createIfAbsent) {
427
428 Map<String, Object> map = null;
429
430 try {
431 map = (Map<String, Object>)portletSession.getAttribute(_CLASS_NAME);
432
433 if ((map == null) && createIfAbsent) {
434 map = new LinkedHashMap<String, Object>();
435
436 portletSession.setAttribute(_CLASS_NAME, map);
437 }
438 }
439 catch (IllegalStateException ise) {
440
441
442
443 }
444
445 return map;
446 }
447
448 private static final String _CLASS_NAME = SessionMessages.class.getName();
449
450 }