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.servlet;
016    
017    import com.liferay.portal.kernel.servlet.filters.compoundsessionid.CompoundSessionIdHttpSession;
018    import com.liferay.portal.kernel.servlet.filters.compoundsessionid.CompoundSessionIdSplitterUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.ServerDetector;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.util.WebKeys;
023    import com.liferay.portal.model.Portlet;
024    import com.liferay.portal.model.PortletApp;
025    import com.liferay.portal.service.PortletLocalServiceUtil;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.PortletKeys;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletRequestWrapper;
032    import javax.servlet.http.HttpSession;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     * @author Brian Myunghun Kim
037     */
038    public class SharedSessionServletRequest extends HttpServletRequestWrapper {
039    
040            public SharedSessionServletRequest(
041                    HttpServletRequest request, boolean shared) {
042    
043                    super(request);
044    
045                    _portalSession = request.getSession();
046    
047                    if (CompoundSessionIdSplitterUtil.hasSessionDelimiter() &&
048                            !(_portalSession instanceof CompoundSessionIdHttpSession)) {
049    
050                            _portalSession = new CompoundSessionIdHttpSession(_portalSession);
051                    }
052    
053                    _shared = shared;
054            }
055    
056            @Override
057            public HttpSession getSession() {
058                    checkPortalSession();
059    
060                    if (_shared || isPortletConfigurationPortlet()) {
061                            return _portalSession;
062                    }
063                    else {
064                            return getSharedSessionWrapper(_portalSession, super.getSession());
065                    }
066            }
067    
068            @Override
069            public HttpSession getSession(boolean create) {
070                    if (create) {
071                            checkPortalSession();
072                    }
073    
074                    if (_shared || isPortletConfigurationPortlet()) {
075                            return _portalSession;
076                    }
077                    else {
078                            return getSharedSessionWrapper(
079                                    _portalSession, super.getSession(create));
080                    }
081            }
082    
083            public HttpSession getSharedSession() {
084                    return _portalSession;
085            }
086    
087            protected void checkPortalSession() {
088                    try {
089                            _portalSession.isNew();
090                    }
091                    catch (IllegalStateException e) {
092                            _portalSession = super.getSession(true);
093                    }
094            }
095    
096            protected HttpSession getSharedSessionWrapper(
097                    HttpSession portalSession, HttpSession portletSession) {
098    
099                    if (CompoundSessionIdSplitterUtil.hasSessionDelimiter() &&
100                            !(portalSession instanceof CompoundSessionIdHttpSession)) {
101    
102                            portalSession = new CompoundSessionIdHttpSession(portalSession);
103                    }
104    
105                    if (CompoundSessionIdSplitterUtil.hasSessionDelimiter() &&
106                            !(portletSession instanceof CompoundSessionIdHttpSession)) {
107    
108                            portletSession = new CompoundSessionIdHttpSession(portletSession);
109                    }
110    
111                    if (ServerDetector.isJetty()) {
112                            return new JettySharedSessionWrapper(portalSession, portletSession);
113                    }
114                    else {
115                            return new SharedSessionWrapper(portalSession, portletSession);
116                    }
117            }
118    
119            protected boolean isPortletConfigurationPortlet() {
120                    String namespace = PortalUtil.getPortletNamespace(
121                            PortletKeys.PORTLET_CONFIGURATION);
122    
123                    String portletResource = ParamUtil.getString(
124                            this, namespace + "portletResource");
125    
126                    if (Validator.isNull(portletResource)) {
127                            return false;
128                    }
129    
130                    ThemeDisplay themeDisplay = (ThemeDisplay)this.getAttribute(
131                            WebKeys.THEME_DISPLAY);
132    
133                    Portlet portlet = null;
134    
135                    try {
136                            portlet = PortletLocalServiceUtil.getPortletById(
137                                    themeDisplay.getCompanyId(), portletResource);
138                    }
139                    catch (Exception e) {
140                    }
141    
142                    if (portlet == null) {
143                            return false;
144                    }
145    
146                    PortletApp portletApp = portlet.getPortletApp();
147    
148                    if (portletApp.isWARFile()) {
149                            return true;
150                    }
151    
152                    return false;
153            }
154    
155            private HttpSession _portalSession;
156            private boolean _shared;
157    
158    }