001    /**
002     * Copyright (c) 2000-2010 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.util.servlet;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.util.PwdGenerator;
020    import com.liferay.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    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class SessionParameters {
036    
037            public static final boolean USE_SESSION_PARAMETERS = GetterUtil.getBoolean(
038                    SystemProperties.get(SessionParameters.class.getName()), true);
039    
040            public static final String KEY = SessionParameters.class.getName();
041    
042            // Servlet Request
043    
044            public static String get(HttpServletRequest request, String parameter) {
045                    return get(request.getSession(), parameter);
046            }
047    
048            public static String get(HttpSession session, String parameter) {
049                    if (!USE_SESSION_PARAMETERS) {
050                            return parameter;
051                    }
052    
053                    Map<String, String> parameters = _getParameters(session);
054    
055                    String newParameter = parameters.get(parameter);
056    
057                    if (newParameter == null) {
058                            newParameter =
059                                    PwdGenerator.getPassword() + StringPool.UNDERLINE + parameter;
060    
061                            parameters.put(parameter, newParameter);
062                    }
063    
064                    return newParameter;
065            }
066    
067            private static Map<String, String> _getParameters(HttpSession session) {
068                    Map<String, String> parameters = null;
069    
070                    try {
071                            parameters = (Map<String, String>)session.getAttribute(KEY);
072    
073                            if (parameters == null) {
074                                    parameters = new HashMap<String, String>();
075    
076                                    session.setAttribute(KEY, parameters);
077                            }
078                    }
079                    catch (IllegalStateException ise) {
080                            parameters = new HashMap<String, String>();
081                    }
082    
083                    return parameters;
084            }
085    
086            // Portlet Request
087    
088            public static String get(PortletRequest portletRequest, String parameter) {
089                    return get(portletRequest.getPortletSession(), parameter);
090            }
091    
092            public static String get(PortletSession portletSession, String parameter) {
093                    if (!USE_SESSION_PARAMETERS) {
094                            return parameter;
095                    }
096    
097                    Map<String, String> parameters = _getParameters(portletSession);
098    
099                    String newParameter = parameters.get(parameter);
100    
101                    if (newParameter == null) {
102                            newParameter =
103                                    PwdGenerator.getPassword() + StringPool.UNDERLINE + parameter;
104    
105                            parameters.put(parameter, newParameter);
106                    }
107    
108                    return newParameter;
109            }
110    
111            private static Map<String, String> _getParameters(
112                    PortletSession portletSession) {
113    
114                    Map<String, String> parameters = null;
115    
116                    try {
117                            parameters = (Map<String, String>)portletSession.getAttribute(KEY);
118    
119                            if (parameters == null) {
120                                    parameters = new LinkedHashMap<String, String>();
121    
122                                    portletSession.setAttribute(KEY, parameters);
123                            }
124                    }
125                    catch (IllegalStateException ise) {
126                            parameters = new LinkedHashMap<String, String>();
127                    }
128    
129                    return parameters;
130            }
131    
132    }