001    /**
002     * Copyright (c) 2000-2010 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.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    }