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