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