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.util.ServerDetector;
018    import com.liferay.portal.resiliency.spi.agent.SPIAgentRequest;
019    
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpServletRequestWrapper;
022    import javax.servlet.http.HttpSession;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     * @author Brian Myunghun Kim
027     */
028    public class SharedSessionServletRequest extends HttpServletRequestWrapper {
029    
030            public SharedSessionServletRequest(
031                    HttpServletRequest request, boolean shared) {
032    
033                    super(request);
034    
035                    _portalSession = request.getSession();
036                    _shared = shared;
037            }
038    
039            @Override
040            public HttpSession getSession() {
041                    return getSession(true);
042            }
043    
044            @Override
045            public HttpSession getSession(boolean create) {
046                    if (create) {
047                            checkPortalSession();
048                    }
049    
050                    if (_shared) {
051                            return _portalSession;
052                    }
053    
054                    HttpSession portletSession = super.getSession(create);
055    
056                    if ((portletSession != null) && (portletSession != _portalSession)) {
057                            SPIAgentRequest.populatePortletSessionAttributes(
058                                    this, portletSession);
059    
060                            return getSharedSessionWrapper(_portalSession, portletSession);
061                    }
062    
063                    return portletSession;
064            }
065    
066            public HttpSession getSharedSession() {
067                    return _portalSession;
068            }
069    
070            protected void checkPortalSession() {
071                    try {
072                            _portalSession.isNew();
073                    }
074                    catch (IllegalStateException ise) {
075                            _portalSession = super.getSession(true);
076                    }
077            }
078    
079            protected HttpSession getSharedSessionWrapper(
080                    HttpSession portalSession, HttpSession portletSession) {
081    
082                    if (ServerDetector.isJetty()) {
083                            return new JettySharedSessionWrapper(portalSession, portletSession);
084                    }
085                    else {
086                            return new SharedSessionWrapper(portalSession, portletSession);
087                    }
088            }
089    
090            private HttpSession _portalSession;
091            private boolean _shared;
092    
093    }