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  public class SessionParameters {
46  
47      public static final boolean USE_SESSION_PARAMETERS = GetterUtil.getBoolean(
48          SystemProperties.get(SessionParameters.class.getName()), true);
49  
50      public static final String KEY = SessionParameters.class.getName();
51  
52      // Servlet Request
53  
54      public static String get(HttpServletRequest request, String parameter) {
55          return get(request.getSession(), parameter);
56      }
57  
58      public static String get(HttpSession session, String parameter) {
59          if (!USE_SESSION_PARAMETERS) {
60              return parameter;
61          }
62  
63          Map<String, String> parameters = _getParameters(session);
64  
65          String newParameter = parameters.get(parameter);
66  
67          if (newParameter == null) {
68              newParameter =
69                  PwdGenerator.getPassword() + StringPool.UNDERLINE + parameter;
70  
71              parameters.put(parameter, newParameter);
72          }
73  
74          return newParameter;
75      }
76  
77      private static Map<String, String> _getParameters(HttpSession session) {
78          Map<String, String> parameters = null;
79  
80          try {
81              parameters = (Map<String, String>)session.getAttribute(KEY);
82  
83              if (parameters == null) {
84                  parameters = new HashMap<String, String>();
85  
86                  session.setAttribute(KEY, parameters);
87              }
88          }
89          catch (IllegalStateException ise) {
90              parameters = new HashMap<String, String>();
91          }
92  
93          return parameters;
94      }
95  
96      // Portlet Request
97  
98      public static String get(PortletRequest portletRequest, String parameter) {
99          return get(portletRequest.getPortletSession(), parameter);
100     }
101 
102     public static String get(PortletSession portletSession, String parameter) {
103         if (!USE_SESSION_PARAMETERS) {
104             return parameter;
105         }
106 
107         Map<String, String> parameters = _getParameters(portletSession);
108 
109         String newParameter = parameters.get(parameter);
110 
111         if (newParameter == null) {
112             newParameter =
113                 PwdGenerator.getPassword() + StringPool.UNDERLINE + parameter;
114 
115             parameters.put(parameter, newParameter);
116         }
117 
118         return newParameter;
119     }
120 
121     private static Map<String, String> _getParameters(
122         PortletSession portletSession) {
123 
124         Map<String, String> parameters = null;
125 
126         try {
127             parameters = (Map<String, String>)portletSession.getAttribute(KEY);
128 
129             if (parameters == null) {
130                 parameters = new LinkedHashMap<String, String>();
131 
132                 portletSession.setAttribute(KEY, parameters);
133             }
134         }
135         catch (IllegalStateException ise) {
136             parameters = new LinkedHashMap<String, String>();
137         }
138 
139         return parameters;
140     }
141 
142 }