001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.util.servlet;
016    
017    import com.liferay.portal.kernel.util.ServerDetector;
018    
019    import java.util.Map;
020    
021    import javax.servlet.http.HttpServletRequest;
022    import javax.servlet.http.HttpServletRequestWrapper;
023    import javax.servlet.http.HttpSession;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Brian Myunghun Kim
028     */
029    public class SharedSessionServletRequest extends HttpServletRequestWrapper {
030    
031            public SharedSessionServletRequest(
032                    HttpServletRequest request, Map<String, Object> sharedSessionAttributes,
033                    boolean shared) {
034    
035                    super(request);
036    
037                    _sharedSessionAttributes = sharedSessionAttributes;
038    
039                    _session = getSharedSessionWrapper(request.getSession());
040                    _shared = shared;
041            }
042    
043            public HttpSession getSession() {
044                    if (_shared) {
045                            return _session;
046                    }
047                    else {
048                            return getSharedSessionWrapper(super.getSession());
049                    }
050            }
051    
052            public HttpSession getSession(boolean create) {
053                    if (_shared) {
054                            return _session;
055                    }
056                    else {
057                            return getSharedSessionWrapper(super.getSession(create));
058                    }
059            }
060    
061            public HttpSession getSharedSession() {
062                    return _session;
063            }
064    
065            protected HttpSession getSharedSessionWrapper(HttpSession session) {
066                    if (ServerDetector.isJetty()) {
067                            return new JettySharedSessionWrapper(
068                                    session, _sharedSessionAttributes);
069                    }
070                    else {
071                            return new SharedSessionWrapper(session, _sharedSessionAttributes);
072                    }
073            }
074    
075            private HttpSession _session;
076            private Map<String, Object> _sharedSessionAttributes;
077            private boolean _shared;
078    
079    }