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.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                            HttpSession portletSession = super.getSession(create);
079    
080                            if (portletSession != null) {
081                                    return getSharedSessionWrapper(_portalSession, portletSession);
082                            }
083    
084                            return null;
085                    }
086            }
087    
088            public HttpSession getSharedSession() {
089                    return _portalSession;
090            }
091    
092            protected void checkPortalSession() {
093                    try {
094                            _portalSession.isNew();
095                    }
096                    catch (IllegalStateException ise) {
097                            _portalSession = super.getSession(true);
098                    }
099            }
100    
101            protected HttpSession getSharedSessionWrapper(
102                    HttpSession portalSession, HttpSession portletSession) {
103    
104                    if (CompoundSessionIdSplitterUtil.hasSessionDelimiter() &&
105                            !(portalSession instanceof CompoundSessionIdHttpSession)) {
106    
107                            portalSession = new CompoundSessionIdHttpSession(portalSession);
108                    }
109    
110                    if (CompoundSessionIdSplitterUtil.hasSessionDelimiter() &&
111                            !(portletSession instanceof CompoundSessionIdHttpSession)) {
112    
113                            portletSession = new CompoundSessionIdHttpSession(portletSession);
114                    }
115    
116                    if (ServerDetector.isJetty()) {
117                            return new JettySharedSessionWrapper(portalSession, portletSession);
118                    }
119                    else {
120                            return new SharedSessionWrapper(portalSession, portletSession);
121                    }
122            }
123    
124            protected boolean isPortletConfigurationPortlet() {
125                    String namespace = PortalUtil.getPortletNamespace(
126                            PortletKeys.PORTLET_CONFIGURATION);
127    
128                    String portletResource = ParamUtil.getString(
129                            this, namespace + "portletResource");
130    
131                    if (Validator.isNull(portletResource)) {
132                            return false;
133                    }
134    
135                    ThemeDisplay themeDisplay = (ThemeDisplay)this.getAttribute(
136                            WebKeys.THEME_DISPLAY);
137    
138                    Portlet portlet = null;
139    
140                    try {
141                            portlet = PortletLocalServiceUtil.getPortletById(
142                                    themeDisplay.getCompanyId(), portletResource);
143                    }
144                    catch (Exception e) {
145                    }
146    
147                    if (portlet == null) {
148                            return false;
149                    }
150    
151                    PortletApp portletApp = portlet.getPortletApp();
152    
153                    if (portletApp.isWARFile()) {
154                            return true;
155                    }
156    
157                    return false;
158            }
159    
160            private HttpSession _portalSession;
161            private boolean _shared;
162    
163    }