001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.portlet.PortalPreferences;
020    import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;
021    
022    import java.util.ConcurrentModificationException;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpSession;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     * @author Raymond Aug??
030     */
031    public class SessionClicks {
032    
033            public static String get(
034                    HttpServletRequest request, String key, String defaultValue) {
035    
036                    return get(request, _DEFAULT_NAMESPACE, key, defaultValue);
037            }
038    
039            public static String get(
040                    HttpServletRequest request, String namespace, String key,
041                    String defaultValue) {
042    
043                    try {
044                            PortalPreferences portalPreferences =
045                                    PortletPreferencesFactoryUtil.getPortalPreferences(request);
046    
047                            return portalPreferences.getValue(namespace, key, defaultValue);
048                    }
049                    catch (Exception e) {
050                            _log.error(e, e);
051    
052                            return null;
053                    }
054            }
055    
056            public static String get(
057                    HttpSession session, String key, String defaultValue) {
058    
059                    return get(session, _DEFAULT_NAMESPACE, key, defaultValue);
060            }
061    
062            public static String get(
063                    HttpSession session, String namespace, String key,
064                    String defaultValue) {
065    
066                    String sessionKey = namespace.concat(StringPool.COLON).concat(key);
067    
068                    return GetterUtil.getString(
069                            session.getAttribute(sessionKey), defaultValue);
070            }
071    
072            public static void put(
073                    HttpServletRequest request, String key, String value) {
074    
075                    put(request, _DEFAULT_NAMESPACE, key, value);
076            }
077    
078            public static void put(
079                    HttpServletRequest request, String namespace, String key,
080                    String value) {
081    
082                    if ((key.length() > _SESSION_CLICKS_MAX_SIZE_TERMS) ||
083                            (value.length() > _SESSION_CLICKS_MAX_SIZE_TERMS)) {
084    
085                            if (_log.isWarnEnabled()) {
086                                    _log.warn(
087                                            "Session clicks has attempted to exceed the maximum " +
088                                                    "size allowed for keys or values with {key=" + key +
089                                                            ", value=" + value + "}");
090                            }
091    
092                            return;
093                    }
094    
095                    while (true) {
096                            try {
097                                    PortalPreferences portalPreferences =
098                                            PortletPreferencesFactoryUtil.getPortalPreferences(request);
099    
100                                    int size = portalPreferences.size();
101    
102                                    if (size <= _SESSION_CLICKS_MAX_ALLOWED_VALUES) {
103                                            portalPreferences.setValue(namespace, key, value);
104                                    }
105                                    else {
106                                            if (_log.isWarnEnabled()) {
107                                                    _log.warn(
108                                                            "Session clicks has attempted to exceed the " +
109                                                                    "maximum number of allowed values with {key=" +
110                                                                            key + ", value=" + value + "}");
111                                            }
112                                    }
113    
114                                    break;
115                            }
116                            catch (ConcurrentModificationException cme) {
117                                    continue;
118                            }
119                            catch (Exception e) {
120                                    _log.error(e, e);
121    
122                                    break;
123                            }
124                    }
125            }
126    
127            public static void put(HttpSession session, String key, String value) {
128                    put(session, _DEFAULT_NAMESPACE, key, value);
129            }
130    
131            public static void put(
132                    HttpSession session, String namespace, String key, String value) {
133    
134                    String sessionKey = namespace.concat(StringPool.COLON).concat(key);
135    
136                    session.setAttribute(sessionKey, value);
137            }
138    
139            private static final String _DEFAULT_NAMESPACE =
140                    SessionClicks.class.getName();
141    
142            private static final int _SESSION_CLICKS_MAX_ALLOWED_VALUES =
143                    GetterUtil.getInteger(
144                            PropsUtil.get(PropsKeys.SESSION_CLICKS_MAX_ALLOWED_VALUES));
145    
146            private static final int _SESSION_CLICKS_MAX_SIZE_TERMS =
147                    GetterUtil.getInteger(
148                            PropsUtil.get(PropsKeys.SESSION_CLICKS_MAX_SIZE_TERMS));
149    
150            private static final Log _log = LogFactoryUtil.getLog(SessionClicks.class);
151    
152    }