001
014
015 package com.liferay.portal.util;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.util.GetterUtil;
020 import com.liferay.portal.kernel.util.PropsKeys;
021 import com.liferay.portal.kernel.util.PropsUtil;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portlet.PortalPreferences;
024 import com.liferay.portlet.PortletPreferencesFactoryUtil;
025
026 import java.util.ConcurrentModificationException;
027
028 import javax.servlet.http.HttpServletRequest;
029 import javax.servlet.http.HttpSession;
030
031
035 public class SessionClicks {
036
037 public static String get(
038 HttpServletRequest request, String key, String defaultValue) {
039
040 return get(request, _DEFAULT_NAMESPACE, key, defaultValue);
041 }
042
043 public static String get(
044 HttpServletRequest request, String namespace, String key,
045 String defaultValue) {
046
047 try {
048 PortalPreferences portalPreferences =
049 PortletPreferencesFactoryUtil.getPortalPreferences(request);
050
051 return portalPreferences.getValue(namespace, key, defaultValue);
052 }
053 catch (Exception e) {
054 _log.error(e, e);
055
056 return null;
057 }
058 }
059
060 public static String get(
061 HttpSession session, String key, String defaultValue) {
062
063 return get(session, _DEFAULT_NAMESPACE, key, defaultValue);
064 }
065
066 public static String get(
067 HttpSession session, String namespace, String key,
068 String defaultValue) {
069
070 String sessionKey = namespace.concat(StringPool.COLON).concat(key);
071
072 return GetterUtil.getString(
073 session.getAttribute(sessionKey), defaultValue);
074 }
075
076 public static void put(
077 HttpServletRequest request, String key, String value) {
078
079 put(request, _DEFAULT_NAMESPACE, key, value);
080 }
081
082 public static void put(
083 HttpServletRequest request, String namespace, String key,
084 String value) {
085
086 if ((key.length() > _SESSION_CLICKS_MAX_SIZE_TERMS) ||
087 (value.length() > _SESSION_CLICKS_MAX_SIZE_TERMS)) {
088
089 if (_log.isWarnEnabled()) {
090 _log.warn(
091 "Session clicks has attempted to exceed the maximum " +
092 "size allowed for keys or values with {key=" + key +
093 ", value=" + value + "}");
094 }
095
096 return;
097 }
098
099 while (true) {
100 try {
101 PortalPreferences portalPreferences =
102 PortletPreferencesFactoryUtil.getPortalPreferences(request);
103
104 int size = portalPreferences.size();
105
106 if (size <= _SESSION_CLICKS_MAX_ALLOWED_VALUES) {
107 portalPreferences.setValue(namespace, key, value);
108 }
109 else {
110 if (_log.isWarnEnabled()) {
111 _log.warn(
112 "Session clicks has attempted to exceed the " +
113 "maximum number of allowed values with {key=" +
114 key + ", value=" + value + "}");
115 }
116 }
117
118 break;
119 }
120 catch (ConcurrentModificationException cme) {
121 continue;
122 }
123 catch (Exception e) {
124 _log.error(e, e);
125
126 break;
127 }
128 }
129 }
130
131 public static void put(HttpSession session, String key, String value) {
132 put(session, _DEFAULT_NAMESPACE, key, value);
133 }
134
135 public static void put(
136 HttpSession session, String namespace, String key, String value) {
137
138 String sessionKey = namespace.concat(StringPool.COLON).concat(key);
139
140 session.setAttribute(sessionKey, value);
141 }
142
143 private static final String _DEFAULT_NAMESPACE =
144 SessionClicks.class.getName();
145
146 private static final int _SESSION_CLICKS_MAX_ALLOWED_VALUES =
147 GetterUtil.getInteger(
148 PropsUtil.get(PropsKeys.SESSION_CLICKS_MAX_ALLOWED_VALUES));
149
150 private static final int _SESSION_CLICKS_MAX_SIZE_TERMS =
151 GetterUtil.getInteger(
152 PropsUtil.get(PropsKeys.SESSION_CLICKS_MAX_SIZE_TERMS));
153
154 private static final Log _log = LogFactoryUtil.getLog(SessionClicks.class);
155
156 }