1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.kernel.util.ArrayUtil;
21  import com.liferay.portal.util.PropsValues;
22  
23  import java.util.Enumeration;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpSession;
29  
30  /**
31   * <a href="SharedSessionUtil.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   * @author Brian Myunghun Kim
35   * @author Shuyang Zhou
36   */
37  public class SharedSessionUtil {
38  
39      public static Map<String, Object> getSharedSessionAttributes(
40          HttpServletRequest request) {
41  
42          HttpSession session = request.getSession();
43  
44          if (ServletVersionDetector.is2_5()) {
45              Map<String, Object> attributes = new HashMap<String, Object>();
46  
47              Enumeration<String> enu = session.getAttributeNames();
48  
49              while (enu.hasMoreElements()) {
50                  String name = enu.nextElement();
51  
52                  Object value = session.getAttribute(name);
53  
54                  if (value == null) {
55                      continue;
56                  }
57  
58                  if (ArrayUtil.contains(
59                          PropsValues.SHARED_SESSION_ATTRIBUTES_EXCLUDES, name)) {
60  
61                      continue;
62                  }
63  
64                  for (String sharedName :
65                          PropsValues.SHARED_SESSION_ATTRIBUTES) {
66  
67                      if (!name.startsWith(sharedName)) {
68                          continue;
69                      }
70  
71                      if (_log.isDebugEnabled()) {
72                          _log.debug("Sharing " + name);
73                      }
74  
75                      attributes.put(name, value);
76  
77                      break;
78                  }
79              }
80  
81              return attributes;
82          }
83          else {
84              SharedSessionAttributeCache sharedSessionAttributeCache =
85                  SharedSessionAttributeCache.getInstance(session);
86  
87              Map<String, Object> values =
88                  sharedSessionAttributeCache.getValues();
89  
90              if (_log.isDebugEnabled()) {
91                  _log.debug("Shared session attributes " + values);
92              }
93  
94              return values;
95          }
96      }
97  
98      private static Log _log = LogFactoryUtil.getLog(SharedSessionUtil.class);
99  
100 }