001    /**
002     * Copyright (c) 2000-2012 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.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portlet.PortalPreferences;
020    import com.liferay.portlet.PortletPreferencesFactoryUtil;
021    
022    import javax.servlet.http.HttpServletRequest;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     * @author Raymond Augé
027     */
028    public class SessionClicks {
029    
030            public static String get(
031                    HttpServletRequest request, String key, String defaultValue) {
032    
033                    return get(request, _DEFAULT_NAMESPACE, key, defaultValue);
034            }
035    
036            public static String get(
037                    HttpServletRequest request, String namespace, String key,
038                    String defaultValue) {
039    
040                    try {
041                            PortalPreferences preferences =
042                                    PortletPreferencesFactoryUtil.getPortalPreferences(request);
043    
044                            return preferences.getValue(namespace, key, defaultValue);
045                    }
046                    catch (Exception e) {
047                            _log.error(e, e);
048    
049                            return null;
050                    }
051            }
052    
053            public static void put(
054                    HttpServletRequest request, String key, String value) {
055    
056                    put(request, _DEFAULT_NAMESPACE, key, value);
057            }
058    
059            public static void put(
060                    HttpServletRequest request, String namespace, String key,
061                    String value) {
062    
063                    try {
064                            PortalPreferences preferences =
065                                    PortletPreferencesFactoryUtil.getPortalPreferences(request);
066    
067                            preferences.setValue(namespace, key, value);
068                    }
069                    catch (Exception e) {
070                            _log.error(e, e);
071                    }
072            }
073    
074            private static final String _DEFAULT_NAMESPACE =
075                    SessionClicks.class.getName();
076    
077            private static Log _log = LogFactoryUtil.getLog(SessionClicks.class);
078    
079    }