001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.servlet;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
018    import com.liferay.portal.kernel.portlet.LiferayPortletSession;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.util.PortalUtil;
022    
023    import java.util.Collections;
024    import java.util.Iterator;
025    import java.util.LinkedHashMap;
026    import java.util.List;
027    import java.util.Map;
028    import java.util.Set;
029    
030    import javax.portlet.PortletRequest;
031    
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.http.HttpSession;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Shuyang Zhou
038     * @author Raymond Aug??
039     */
040    public class SessionMessages {
041    
042            public static final String KEY_SUFFIX_CLOSE_REDIRECT = ".closeRedirect";
043    
044            public static final String KEY_SUFFIX_CLOSE_REFRESH_PORTLET =
045                    ".closeRefreshPortlet";
046    
047            public static final String KEY_SUFFIX_DELETE_SUCCESS_DATA =
048                    ".deleteSuccessData";
049    
050            public static final String KEY_SUFFIX_FORCE_SEND_REDIRECT =
051                    ".forceSendRedirect";
052    
053            public static final String KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE =
054                    ".hideDefaultErrorMessage";
055    
056            public static final String KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE =
057                    ".hideDefaultSuccessMessage";
058    
059            public static final String KEY_SUFFIX_PORTLET_NOT_AJAXABLE =
060                    ".portletNotAjaxable";
061    
062            public static final String KEY_SUFFIX_REFRESH_PORTLET = ".refreshPortlet";
063    
064            public static final String KEY_SUFFIX_REFRESH_PORTLET_DATA =
065                    ".refreshPortletData";
066    
067            public static final String KEY_SUFFIX_UPDATED_CONFIGURATION =
068                    ".updatedConfiguration";
069    
070            public static final String KEY_SUFFIX_UPDATED_PREFERENCES =
071                    ".updatedPreferences";
072    
073            public static void add(HttpServletRequest request, Class<?> clazz) {
074                    add(request.getSession(false), clazz.getName());
075            }
076    
077            public static void add(
078                    HttpServletRequest request, Class<?> clazz, Object value) {
079    
080                    add(request.getSession(false), clazz.getName(), value);
081            }
082    
083            public static void add(HttpServletRequest request, String key) {
084                    add(request.getSession(false), key);
085            }
086    
087            public static void add(
088                    HttpServletRequest request, String key, Object value) {
089    
090                    add(request.getSession(false), key, value);
091            }
092    
093            public static void add(HttpSession session, Class<?> clazz) {
094                    add(session, clazz.getName());
095            }
096    
097            public static void add(HttpSession session, Class<?> clazz, Object value) {
098                    add(session, clazz.getName(), value);
099            }
100    
101            public static void add(HttpSession session, String key) {
102                    Map<String, Object> map = _getMap(session, null, true);
103    
104                    if (map == null) {
105                            return;
106                    }
107    
108                    map.put(key, key);
109            }
110    
111            public static void add(HttpSession session, String key, Object value) {
112                    Map<String, Object> map = _getMap(session, null, true);
113    
114                    if (map == null) {
115                            return;
116                    }
117    
118                    map.put(key, value);
119            }
120    
121            public static void add(PortletRequest portletRequest, Class<?> clazz) {
122                    add(portletRequest, clazz.getName());
123            }
124    
125            public static void add(
126                    PortletRequest portletRequest, Class<?> clazz, Object value) {
127    
128                    add(portletRequest, clazz.getName(), value);
129            }
130    
131            public static void add(PortletRequest portletRequest, String key) {
132                    Map<String, Object> map = _getMap(portletRequest, true);
133    
134                    if (map == null) {
135                            return;
136                    }
137    
138                    map.put(key, key);
139            }
140    
141            public static void add(
142                    PortletRequest portletRequest, String key, Object value) {
143    
144                    Map<String, Object> map = _getMap(portletRequest, true);
145    
146                    if (map == null) {
147                            return;
148                    }
149    
150                    map.put(key, value);
151            }
152    
153            public static void clear(HttpServletRequest request) {
154                    clear(request.getSession(false));
155            }
156    
157            public static void clear(HttpSession session) {
158                    Map<String, Object> map = _getMap(session, null, false);
159    
160                    if (map != null) {
161                            map.clear();
162                    }
163            }
164    
165            public static void clear(PortletRequest portletRequest) {
166                    Map<String, Object> map = _getMap(portletRequest, false);
167    
168                    if (map != null) {
169                            map.clear();
170                    }
171            }
172    
173            public static boolean contains(HttpServletRequest request, Class<?> clazz) {
174                    return contains(request.getSession(false), clazz.getName());
175            }
176    
177            public static boolean contains(HttpServletRequest request, String key) {
178                    return contains(request.getSession(false), key);
179            }
180    
181            public static boolean contains(HttpSession session, Class<?> clazz) {
182                    return contains(session, clazz.getName());
183            }
184    
185            public static boolean contains(HttpSession session, String key) {
186                    Map<String, Object> map = _getMap(session, null, false);
187    
188                    if (map == null) {
189                            return false;
190                    }
191    
192                    return map.containsKey(key);
193            }
194    
195            public static boolean contains(
196                    PortletRequest portletRequest, Class<?> clazz) {
197    
198                    return contains(portletRequest, clazz.getName());
199            }
200    
201            public static boolean contains(PortletRequest portletRequest, String key) {
202                    Map<String, Object> map = _getMap(portletRequest, false);
203    
204                    if (map == null) {
205                            return false;
206                    }
207    
208                    return map.containsKey(key);
209            }
210    
211            public static Object get(HttpServletRequest request, Class<?> clazz) {
212                    return get(request.getSession(false), clazz.getName());
213            }
214    
215            public static Object get(HttpServletRequest request, String key) {
216                    return get(request.getSession(false), key);
217            }
218    
219            public static Object get(HttpSession session, Class<?> clazz) {
220                    return get(session, clazz.getName());
221            }
222    
223            public static Object get(HttpSession session, String key) {
224                    Map<String, Object> map = _getMap(session, null, false);
225    
226                    if (map == null) {
227                            return null;
228                    }
229    
230                    return map.get(key);
231            }
232    
233            public static Object get(PortletRequest portletRequest, Class<?> clazz) {
234                    return get(portletRequest, clazz.getName());
235            }
236    
237            public static Object get(PortletRequest portletRequest, String key) {
238                    Map<String, Object> map = _getMap(portletRequest, false);
239    
240                    if (map == null) {
241                            return null;
242                    }
243    
244                    return map.get(key);
245            }
246    
247            public static boolean isEmpty(HttpServletRequest request) {
248                    return isEmpty(request.getSession(false));
249            }
250    
251            public static boolean isEmpty(HttpSession session) {
252                    Map<String, Object> map = _getMap(session, null, false);
253    
254                    if (map == null) {
255                            return true;
256                    }
257    
258                    return map.isEmpty();
259            }
260    
261            public static boolean isEmpty(PortletRequest portletRequest) {
262                    Map<String, Object> map = _getMap(portletRequest, false);
263    
264                    if (map == null) {
265                            return true;
266                    }
267    
268                    return map.isEmpty();
269            }
270    
271            public static Iterator<String> iterator(HttpServletRequest request) {
272                    return iterator(request.getSession(false));
273            }
274    
275            public static Iterator<String> iterator(HttpSession session) {
276                    Map<String, Object> map = _getMap(session, null, false);
277    
278                    if (map == null) {
279                            List<String> list = Collections.<String>emptyList();
280    
281                            return list.iterator();
282                    }
283    
284                    Set<String> set = Collections.unmodifiableSet(map.keySet());
285    
286                    return set.iterator();
287            }
288    
289            public static Iterator<String> iterator(PortletRequest portletRequest) {
290                    Map<String, Object> map = _getMap(portletRequest, false);
291    
292                    if (map == null) {
293                            List<String> list = Collections.<String>emptyList();
294    
295                            return list.iterator();
296                    }
297    
298                    Set<String> set = Collections.unmodifiableSet(map.keySet());
299    
300                    return set.iterator();
301            }
302    
303            public static Set<String> keySet(HttpServletRequest request) {
304                    return keySet(request.getSession(false));
305            }
306    
307            public static Set<String> keySet(HttpSession session) {
308                    Map<String, Object> map = _getMap(session, null, false);
309    
310                    if (map == null) {
311                            return Collections.emptySet();
312                    }
313    
314                    return Collections.unmodifiableSet(map.keySet());
315            }
316    
317            public static Set<String> keySet(PortletRequest portletRequest) {
318                    Map<String, Object> map = _getMap(portletRequest, false);
319    
320                    if (map == null) {
321                            return Collections.emptySet();
322                    }
323    
324                    return Collections.unmodifiableSet(map.keySet());
325            }
326    
327            public static void print(HttpServletRequest request) {
328                    print(request.getSession(false));
329            }
330    
331            public static void print(HttpSession session) {
332                    Iterator<String> itr = iterator(session);
333    
334                    while (itr.hasNext()) {
335                            System.out.println(itr.next());
336                    }
337            }
338    
339            public static void print(PortletRequest portletRequest) {
340                    Iterator<String> itr = iterator(portletRequest);
341    
342                    while (itr.hasNext()) {
343                            System.out.println(itr.next());
344                    }
345            }
346    
347            public static int size(HttpServletRequest request) {
348                    return size(request.getSession(false));
349            }
350    
351            public static int size(HttpSession session) {
352                    Map<String, Object> map = _getMap(session, null, false);
353    
354                    if (map == null) {
355                            return 0;
356                    }
357    
358                    return map.size();
359            }
360    
361            public static int size(PortletRequest portletRequest) {
362                    Map<String, Object> map = _getMap(portletRequest, false);
363    
364                    if (map == null) {
365                            return 0;
366                    }
367    
368                    return map.size();
369            }
370    
371            protected static Map<String, Object> _getMap(
372                    HttpSession session, String portletKey, boolean createIfAbsent) {
373    
374                    if (session == null) {
375                            return null;
376                    }
377    
378                    if (portletKey == null) {
379                            portletKey = StringPool.BLANK;
380                    }
381    
382                    Map<String, Object> map = null;
383    
384                    try {
385                            map = (Map<String, Object>)session.getAttribute(
386                                    portletKey + _CLASS_NAME);
387    
388                            if ((map == null) && createIfAbsent) {
389                                    map = new SessionMessagesMap();
390    
391                                    session.setAttribute(portletKey + _CLASS_NAME, map);
392                            }
393                    }
394                    catch (IllegalStateException ise) {
395    
396                            // Session is already invalidated, just return a null map
397    
398                    }
399    
400                    return map;
401            }
402    
403            protected static Map<String, Object> _getMap(
404                    PortletRequest portletRequest, boolean createIfAbsent) {
405    
406                    return _getMap(
407                            _getPortalSession(portletRequest), _getPortletKey(portletRequest),
408                            createIfAbsent);
409            }
410    
411            protected static HttpSession _getPortalSession(
412                    PortletRequest portletRequest) {
413    
414                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
415                            portletRequest);
416    
417                    request = PortalUtil.getOriginalServletRequest(request);
418    
419                    return request.getSession();
420            }
421    
422            protected static String _getPortletKey(PortletRequest portletRequest) {
423                    StringBundler sb = new StringBundler(5);
424    
425                    LiferayPortletRequest liferayPortletRequest =
426                            PortalUtil.getLiferayPortletRequest(portletRequest);
427    
428                    sb.append(LiferayPortletSession.PORTLET_SCOPE_NAMESPACE);
429                    sb.append(liferayPortletRequest.getPortletName());
430                    sb.append(LiferayPortletSession.LAYOUT_SEPARATOR);
431                    sb.append(liferayPortletRequest.getPlid());
432                    sb.append(StringPool.QUESTION);
433    
434                    return sb.toString();
435            }
436    
437            private static final String _CLASS_NAME = SessionMessages.class.getName();
438    
439            private static class SessionMessagesMap
440                    extends LinkedHashMap<String, Object> {
441    
442                    @Override
443                    public boolean containsKey(Object key) {
444                            key = _transformKey(key);
445    
446                            return super.containsKey(key);
447                    }
448    
449                    @Override
450                    public Object get(Object key) {
451                            key = _transformKey(key);
452    
453                            return super.get(key);
454                    }
455    
456                    @Override
457                    public Object put(String key, Object value) {
458                            key = _transformKey(key);
459    
460                            return super.put(key, value);
461                    }
462    
463                    private String _transformKey(Object key) {
464                            String keyString = String.valueOf(key);
465    
466                            if (keyString != null) {
467                                    if (keyString.equals("request_processed")) {
468                                            keyString = "requestProcessed";
469                                    }
470                            }
471    
472                            return keyString;
473                    }
474    
475            }
476    
477    }