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.util.servlet;
16  
17  import com.liferay.portal.kernel.util.GetterUtil;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.util.PwdGenerator;
20  import com.liferay.util.SystemProperties;
21  
22  import java.util.HashMap;
23  import java.util.LinkedHashMap;
24  import java.util.Map;
25  
26  import javax.portlet.PortletRequest;
27  import javax.portlet.PortletSession;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpSession;
31  
32  /**
33   * <a href="SessionParameters.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class SessionParameters {
38  
39      public static final boolean USE_SESSION_PARAMETERS = GetterUtil.getBoolean(
40          SystemProperties.get(SessionParameters.class.getName()), true);
41  
42      public static final String KEY = SessionParameters.class.getName();
43  
44      // Servlet Request
45  
46      public static String get(HttpServletRequest request, String parameter) {
47          return get(request.getSession(), parameter);
48      }
49  
50      public static String get(HttpSession session, String parameter) {
51          if (!USE_SESSION_PARAMETERS) {
52              return parameter;
53          }
54  
55          Map<String, String> parameters = _getParameters(session);
56  
57          String newParameter = parameters.get(parameter);
58  
59          if (newParameter == null) {
60              newParameter =
61                  PwdGenerator.getPassword() + StringPool.UNDERLINE + parameter;
62  
63              parameters.put(parameter, newParameter);
64          }
65  
66          return newParameter;
67      }
68  
69      private static Map<String, String> _getParameters(HttpSession session) {
70          Map<String, String> parameters = null;
71  
72          try {
73              parameters = (Map<String, String>)session.getAttribute(KEY);
74  
75              if (parameters == null) {
76                  parameters = new HashMap<String, String>();
77  
78                  session.setAttribute(KEY, parameters);
79              }
80          }
81          catch (IllegalStateException ise) {
82              parameters = new HashMap<String, String>();
83          }
84  
85          return parameters;
86      }
87  
88      // Portlet Request
89  
90      public static String get(PortletRequest portletRequest, String parameter) {
91          return get(portletRequest.getPortletSession(), parameter);
92      }
93  
94      public static String get(PortletSession portletSession, String parameter) {
95          if (!USE_SESSION_PARAMETERS) {
96              return parameter;
97          }
98  
99          Map<String, String> parameters = _getParameters(portletSession);
100 
101         String newParameter = parameters.get(parameter);
102 
103         if (newParameter == null) {
104             newParameter =
105                 PwdGenerator.getPassword() + StringPool.UNDERLINE + parameter;
106 
107             parameters.put(parameter, newParameter);
108         }
109 
110         return newParameter;
111     }
112 
113     private static Map<String, String> _getParameters(
114         PortletSession portletSession) {
115 
116         Map<String, String> parameters = null;
117 
118         try {
119             parameters = (Map<String, String>)portletSession.getAttribute(KEY);
120 
121             if (parameters == null) {
122                 parameters = new LinkedHashMap<String, String>();
123 
124                 portletSession.setAttribute(KEY, parameters);
125             }
126         }
127         catch (IllegalStateException ise) {
128             parameters = new LinkedHashMap<String, String>();
129         }
130 
131         return parameters;
132     }
133 
134 }