1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.servlet;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.servlet.ServletVersionDetector;
20  import com.liferay.portal.util.PropsValues;
21  
22  import java.util.Enumeration;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpSession;
28  
29  /**
30   * <a href="SharedSessionUtil.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   * @author Brian Myunghun Kim
34   * @author Shuyang Zhou
35   */
36  public class SharedSessionUtil {
37  
38      public static Map<String, Object> getSharedSessionAttributes(
39          HttpServletRequest request) {
40  
41          HttpSession session = request.getSession();
42  
43          if (ServletVersionDetector.is2_5()) {
44              Map<String, Object> attributes = new HashMap<String, Object>();
45  
46              Enumeration<String> enu = session.getAttributeNames();
47  
48              while (enu.hasMoreElements()) {
49                  String name = enu.nextElement();
50  
51                  Object value = session.getAttribute(name);
52  
53                  if (value == null) {
54                      continue;
55                  }
56  
57                  for (String sharedName :
58                          PropsValues.SHARED_SESSION_ATTRIBUTES) {
59  
60                      if (!name.startsWith(sharedName)) {
61                          continue;
62                      }
63  
64                      if (_log.isDebugEnabled()) {
65                          _log.debug("Sharing " + name);
66                      }
67  
68                      attributes.put(name, value);
69  
70                      break;
71                  }
72              }
73  
74              return attributes;
75          }
76          else {
77              SharedSessionAttributeCache sharedSessionAttributeCache =
78                  SharedSessionAttributeCache.getInstance(session);
79  
80              Map<String, Object> values =
81                  sharedSessionAttributeCache.getValues();
82  
83              if (_log.isDebugEnabled()) {
84                  _log.debug("Shared session attributes " + values);
85              }
86  
87              return values;
88          }
89      }
90  
91      private static Log _log = LogFactoryUtil.getLog(SharedSessionUtil.class);
92  
93  }