1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.servlet;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.util.PwdGenerator;
28  import com.liferay.util.SystemProperties;
29  
30  import java.util.HashMap;
31  import java.util.LinkedHashMap;
32  import java.util.Map;
33  
34  import javax.portlet.PortletRequest;
35  import javax.portlet.PortletSession;
36  
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpSession;
39  
40  /**
41   * <a href="SessionParameters.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class SessionParameters {
47  
48      public static final boolean USE_SESSION_PARAMETERS = GetterUtil.getBoolean(
49          SystemProperties.get(SessionParameters.class.getName()), true);
50  
51      public static final String KEY = SessionParameters.class.getName();
52  
53      // Servlet Request
54  
55      public static String get(HttpServletRequest request, String parameter) {
56          return get(request.getSession(), parameter);
57      }
58  
59      public static String get(HttpSession session, String parameter) {
60          if (!USE_SESSION_PARAMETERS) {
61              return parameter;
62          }
63  
64          Map<String, String> parameters = _getParameters(session);
65  
66          String newParameter = parameters.get(parameter);
67  
68          if (newParameter == null) {
69              newParameter =
70                  PwdGenerator.getPassword() + StringPool.UNDERLINE + parameter;
71  
72              parameters.put(parameter, newParameter);
73          }
74  
75          return newParameter;
76      }
77  
78      private static Map<String, String> _getParameters(HttpSession session) {
79          Map<String, String> parameters = null;
80  
81          try {
82              parameters = (Map<String, String>)session.getAttribute(KEY);
83  
84              if (parameters == null) {
85                  parameters = new HashMap<String, String>();
86  
87                  session.setAttribute(KEY, parameters);
88              }
89          }
90          catch (IllegalStateException ise) {
91              parameters = new HashMap<String, String>();
92          }
93  
94          return parameters;
95      }
96  
97      // Portlet Request
98  
99      public static String get(PortletRequest portletRequest, String parameter) {
100         return get(portletRequest.getPortletSession(), parameter);
101     }
102 
103     public static String get(PortletSession portletSession, String parameter) {
104         if (!USE_SESSION_PARAMETERS) {
105             return parameter;
106         }
107 
108         Map<String, String> parameters = _getParameters(portletSession);
109 
110         String newParameter = parameters.get(parameter);
111 
112         if (newParameter == null) {
113             newParameter =
114                 PwdGenerator.getPassword() + StringPool.UNDERLINE + parameter;
115 
116             parameters.put(parameter, newParameter);
117         }
118 
119         return newParameter;
120     }
121 
122     private static Map<String, String> _getParameters(
123         PortletSession portletSession) {
124 
125         Map<String, String> parameters = null;
126 
127         try {
128             parameters = (Map<String, String>)portletSession.getAttribute(KEY);
129 
130             if (parameters == null) {
131                 parameters = new LinkedHashMap<String, String>();
132 
133                 portletSession.setAttribute(KEY, parameters);
134             }
135         }
136         catch (IllegalStateException ise) {
137             parameters = new LinkedHashMap<String, String>();
138         }
139 
140         return parameters;
141     }
142 
143 }