001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.servlet;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.util.CollectionFactory;
025: import com.liferay.util.PwdGenerator;
026: import com.liferay.util.SystemProperties;
027:
028: import java.util.LinkedHashMap;
029: import java.util.Map;
030:
031: import javax.portlet.PortletRequest;
032: import javax.portlet.PortletSession;
033:
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpSession;
036:
037: /**
038: * <a href="SessionParameters.java.html"><b><i>View Source</i></b></a>
039: *
040: * @author Brian Wing Shun Chan
041: *
042: */
043: public class SessionParameters {
044:
045: public static final boolean USE_SESSION_PARAMETERS = GetterUtil
046: .getBoolean(SystemProperties.get(SessionParameters.class
047: .getName()), true);
048:
049: public static final String KEY = SessionParameters.class.getName();
050:
051: // Servlet Request
052:
053: public static String get(HttpServletRequest req, String parameter) {
054: return get(req.getSession(), parameter);
055: }
056:
057: public static String get(HttpSession ses, String parameter) {
058: if (!USE_SESSION_PARAMETERS) {
059: return parameter;
060: }
061:
062: Map parameters = _getParameters(ses);
063:
064: String newParameter = (String) parameters.get(parameter);
065:
066: if (newParameter == null) {
067: newParameter = PwdGenerator.getPassword(PwdGenerator.KEY3,
068: 10)
069: + "_" + parameter;
070:
071: parameters.put(parameter, newParameter);
072: }
073:
074: return newParameter;
075: }
076:
077: private static Map _getParameters(HttpSession ses) {
078: Map parameters = null;
079:
080: try {
081: parameters = (Map) ses.getAttribute(KEY);
082:
083: if (parameters == null) {
084: parameters = CollectionFactory.getHashMap();
085:
086: ses.setAttribute(KEY, parameters);
087: }
088: } catch (IllegalStateException ise) {
089: parameters = CollectionFactory.getHashMap();
090: }
091:
092: return parameters;
093: }
094:
095: // Portlet Request
096:
097: public static String get(PortletRequest req, String parameter) {
098: return get(req.getPortletSession(), parameter);
099: }
100:
101: public static String get(PortletSession ses, String parameter) {
102: if (!USE_SESSION_PARAMETERS) {
103: return parameter;
104: }
105:
106: Map parameters = _getParameters(ses);
107:
108: String newParameter = (String) parameters.get(parameter);
109:
110: if (newParameter == null) {
111: newParameter = PwdGenerator.getPassword() + "_" + parameter;
112:
113: parameters.put(parameter, newParameter);
114: }
115:
116: return newParameter;
117: }
118:
119: private static Map _getParameters(PortletSession ses) {
120: Map parameters = null;
121:
122: try {
123: parameters = (Map) ses.getAttribute(KEY);
124:
125: if (parameters == null) {
126: parameters = new LinkedHashMap();
127:
128: ses.setAttribute(KEY, parameters);
129: }
130: } catch (IllegalStateException ise) {
131: parameters = new LinkedHashMap();
132: }
133:
134: return parameters;
135: }
136:
137: }
|