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