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