001
014
015 package com.liferay.util.servlet;
016
017 import com.liferay.portal.kernel.util.GetterUtil;
018 import com.liferay.portal.kernel.util.StringPool;
019 import com.liferay.portal.kernel.util.StringUtil;
020 import com.liferay.portal.kernel.util.SystemProperties;
021
022 import java.util.HashMap;
023 import java.util.LinkedHashMap;
024 import java.util.Map;
025
026 import javax.portlet.PortletRequest;
027 import javax.portlet.PortletSession;
028
029 import javax.servlet.http.HttpServletRequest;
030 import javax.servlet.http.HttpSession;
031
032
035 public class SessionParameters {
036
037 public static final String KEY = SessionParameters.class.getName();
038
039 public static final boolean USE_SESSION_PARAMETERS = GetterUtil.getBoolean(
040 SystemProperties.get(SessionParameters.class.getName()), true);
041
042 public static String get(HttpServletRequest request, String parameter) {
043 return get(request.getSession(), parameter);
044 }
045
046 public static String get(HttpSession session, String parameter) {
047 if (!USE_SESSION_PARAMETERS) {
048 return parameter;
049 }
050
051 Map<String, String> parameters = _getParameters(session);
052
053 String newParameter = parameters.get(parameter);
054
055 if (newParameter == null) {
056 newParameter =
057 StringUtil.randomString() + StringPool.UNDERLINE +
058 parameter;
059
060 parameters.put(parameter, newParameter);
061 }
062
063 return newParameter;
064 }
065
066 public static String get(PortletRequest portletRequest, String parameter) {
067 return get(portletRequest.getPortletSession(), parameter);
068 }
069
070 public static String get(PortletSession portletSession, String parameter) {
071 if (!USE_SESSION_PARAMETERS) {
072 return parameter;
073 }
074
075 Map<String, String> parameters = _getParameters(portletSession);
076
077 String newParameter = parameters.get(parameter);
078
079 if (newParameter == null) {
080 newParameter =
081 StringUtil.randomString() + StringPool.UNDERLINE +
082 parameter;
083
084 parameters.put(parameter, newParameter);
085 }
086
087 return newParameter;
088 }
089
090 private static Map<String, String> _getParameters(HttpSession session) {
091 Map<String, String> parameters = null;
092
093 try {
094 parameters = (Map<String, String>)session.getAttribute(KEY);
095
096 if (parameters == null) {
097 parameters = new HashMap<String, String>();
098
099 session.setAttribute(KEY, parameters);
100 }
101 }
102 catch (IllegalStateException ise) {
103 parameters = new HashMap<String, String>();
104 }
105
106 return parameters;
107 }
108
109 private static Map<String, String> _getParameters(
110 PortletSession portletSession) {
111
112 Map<String, String> parameters = null;
113
114 try {
115 parameters = (Map<String, String>)portletSession.getAttribute(KEY);
116
117 if (parameters == null) {
118 parameters = new LinkedHashMap<String, String>();
119
120 portletSession.setAttribute(KEY, parameters);
121 }
122 }
123 catch (IllegalStateException ise) {
124 parameters = new LinkedHashMap<String, String>();
125 }
126
127 return parameters;
128 }
129
130 }