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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.portal.util.PropsValues;
021    import com.liferay.util.servlet.NullSession;
022    
023    import java.util.Collections;
024    import java.util.Enumeration;
025    import java.util.HashMap;
026    import java.util.List;
027    import java.util.Map;
028    
029    import javax.servlet.ServletContext;
030    import javax.servlet.http.HttpSession;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class SharedSessionWrapper implements HttpSession {
036    
037            public SharedSessionWrapper(
038                    HttpSession portalSession, HttpSession portletSession) {
039    
040                    if (portalSession == null) {
041                            _portalSession = new NullSession();
042    
043                            if (_log.isWarnEnabled()) {
044                                    _log.warn("Wrapped portal session is null");
045                            }
046                    }
047    
048                    _portalSession = portalSession;
049                    _portletSession = portletSession;
050            }
051    
052            @Override
053            public Object getAttribute(String name) {
054                    HttpSession session = getSessionDelegate(name);
055    
056                    return session.getAttribute(name);
057            }
058    
059            @Override
060            public Enumeration<String> getAttributeNames() {
061                    HttpSession session = getSessionDelegate();
062    
063                    Enumeration<String> namesEnu = session.getAttributeNames();
064    
065                    if (session == _portletSession) {
066                            List<String> namesList = Collections.list(namesEnu);
067    
068                            Enumeration<String> portalSessionNamesEnu =
069                                    _portalSession.getAttributeNames();
070    
071                            while (portalSessionNamesEnu.hasMoreElements()) {
072                                    String name = portalSessionNamesEnu.nextElement();
073    
074                                    if (containsSharedAttribute(name)) {
075                                            namesList.add(name);
076                                    }
077                            }
078    
079                            namesEnu = Collections.enumeration(namesList);
080                    }
081    
082                    return namesEnu;
083            }
084    
085            @Override
086            public long getCreationTime() {
087                    HttpSession session = getSessionDelegate();
088    
089                    return session.getCreationTime();
090            }
091    
092            @Override
093            public String getId() {
094                    HttpSession session = getSessionDelegate();
095    
096                    return session.getId();
097            }
098    
099            @Override
100            public long getLastAccessedTime() {
101                    HttpSession session = getSessionDelegate();
102    
103                    return session.getLastAccessedTime();
104            }
105    
106            @Override
107            public int getMaxInactiveInterval() {
108                    HttpSession session = getSessionDelegate();
109    
110                    return session.getMaxInactiveInterval();
111            }
112    
113            @Override
114            public ServletContext getServletContext() {
115                    HttpSession session = getSessionDelegate();
116    
117                    return session.getServletContext();
118            }
119    
120            /**
121             * @deprecated As of 6.1.0
122             */
123            @Override
124            public javax.servlet.http.HttpSessionContext getSessionContext() {
125                    HttpSession session = getSessionDelegate();
126    
127                    return session.getSessionContext();
128            }
129    
130            @Override
131            public Object getValue(String name) {
132                    return getAttribute(name);
133            }
134    
135            @Override
136            public String[] getValueNames() {
137                    List<String> names = ListUtil.fromEnumeration(getAttributeNames());
138    
139                    return names.toArray(new String[names.size()]);
140            }
141    
142            @Override
143            public void invalidate() {
144                    HttpSession session = getSessionDelegate();
145    
146                    session.invalidate();
147            }
148    
149            @Override
150            public boolean isNew() {
151                    HttpSession session = getSessionDelegate();
152    
153                    return session.isNew();
154            }
155    
156            @Override
157            public void putValue(String name, Object value) {
158                    setAttribute(name, value);
159            }
160    
161            @Override
162            public void removeAttribute(String name) {
163                    HttpSession session = getSessionDelegate(name);
164    
165                    session.removeAttribute(name);
166            }
167    
168            @Override
169            public void removeValue(String name) {
170                    removeAttribute(name);
171            }
172    
173            @Override
174            public void setAttribute(String name, Object value) {
175                    HttpSession session = getSessionDelegate(name);
176    
177                    session.setAttribute(name, value);
178            }
179    
180            @Override
181            public void setMaxInactiveInterval(int maxInactiveInterval) {
182                    HttpSession session = getSessionDelegate();
183    
184                    session.setMaxInactiveInterval(maxInactiveInterval);
185            }
186    
187            protected boolean containsSharedAttribute(String name) {
188                    for (String sharedName : PropsValues.SESSION_SHARED_ATTRIBUTES) {
189                            if (name.startsWith(sharedName)) {
190                                    return true;
191                            }
192                    }
193    
194                    return false;
195            }
196    
197            protected HttpSession getSessionDelegate() {
198                    if (_portletSession != null) {
199                            return _portletSession;
200                    }
201                    else {
202                            return _portalSession;
203                    }
204            }
205    
206            protected HttpSession getSessionDelegate(String name) {
207                    if (_portletSession == null) {
208                            return _portalSession;
209                    }
210    
211                    if (_sharedSessionAttributesExcludes.containsKey(name)) {
212                            return _portletSession;
213                    }
214                    else if (containsSharedAttribute(name)) {
215                            return _portalSession;
216                    }
217                    else {
218                            return _portletSession;
219                    }
220            }
221    
222            private static Log _log = LogFactoryUtil.getLog(SharedSessionWrapper.class);
223    
224            private static Map<String, String> _sharedSessionAttributesExcludes;
225    
226            static {
227                    _sharedSessionAttributesExcludes = new HashMap<String, String>();
228    
229                    for (String name : PropsValues.SESSION_SHARED_ATTRIBUTES_EXCLUDES) {
230                            _sharedSessionAttributesExcludes.put(name, name);
231                    }
232            }
233    
234            private HttpSession _portalSession;
235            private HttpSession _portletSession;
236    
237    }