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