001    /**
002     * Copyright (c) 2000-2012 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            public Object getAttribute(String name) {
053                    HttpSession session = getSessionDelegate(name);
054    
055                    return session.getAttribute(name);
056            }
057    
058            public Enumeration<String> getAttributeNames() {
059                    HttpSession session = getSessionDelegate();
060    
061                    Enumeration<String> namesEnu = session.getAttributeNames();
062    
063                    if (session == _portletSession) {
064                            List<String> namesList = Collections.list(namesEnu);
065    
066                            Enumeration<String> portalSessionNamesEnu =
067                                    _portalSession.getAttributeNames();
068    
069                            while (portalSessionNamesEnu.hasMoreElements()) {
070                                    String name = portalSessionNamesEnu.nextElement();
071    
072                                    if (containsSharedAttribute(name)) {
073                                            namesList.add(name);
074                                    }
075                            }
076    
077                            namesEnu = Collections.enumeration(namesList);
078                    }
079    
080                    return namesEnu;
081            }
082    
083            public long getCreationTime() {
084                    HttpSession session = getSessionDelegate();
085    
086                    return session.getCreationTime();
087            }
088    
089            public String getId() {
090                    HttpSession session = getSessionDelegate();
091    
092                    return session.getId();
093            }
094    
095            public long getLastAccessedTime() {
096                    HttpSession session = getSessionDelegate();
097    
098                    return session.getLastAccessedTime();
099            }
100    
101            public int getMaxInactiveInterval() {
102                    HttpSession session = getSessionDelegate();
103    
104                    return session.getMaxInactiveInterval();
105            }
106    
107            public ServletContext getServletContext() {
108                    HttpSession session = getSessionDelegate();
109    
110                    return session.getServletContext();
111            }
112    
113            /**
114             * @deprecated
115             */
116            public javax.servlet.http.HttpSessionContext getSessionContext() {
117                    HttpSession session = getSessionDelegate();
118    
119                    return session.getSessionContext();
120            }
121    
122            public Object getValue(String name) {
123                    return getAttribute(name);
124            }
125    
126            public String[] getValueNames() {
127                    List<String> names = ListUtil.fromEnumeration(getAttributeNames());
128    
129                    return names.toArray(new String[names.size()]);
130            }
131    
132            public void invalidate() {
133                    HttpSession session = getSessionDelegate();
134    
135                    session.invalidate();
136            }
137    
138            public boolean isNew() {
139                    HttpSession session = getSessionDelegate();
140    
141                    return session.isNew();
142            }
143    
144            public void putValue(String name, Object value) {
145                    setAttribute(name, value);
146            }
147    
148            public void removeAttribute(String name) {
149                    HttpSession session = getSessionDelegate(name);
150    
151                    session.removeAttribute(name);
152            }
153    
154            public void removeValue(String name) {
155                    removeAttribute(name);
156            }
157    
158            public void setAttribute(String name, Object value) {
159                    HttpSession session = getSessionDelegate(name);
160    
161                    session.setAttribute(name, value);
162            }
163    
164            public void setMaxInactiveInterval(int maxInactiveInterval) {
165                    HttpSession session = getSessionDelegate();
166    
167                    session.setMaxInactiveInterval(maxInactiveInterval);
168            }
169    
170            protected boolean containsSharedAttribute(String name) {
171                    for (String sharedName : PropsValues.SESSION_SHARED_ATTRIBUTES) {
172                            if (name.startsWith(sharedName)) {
173                                    return true;
174                            }
175                    }
176    
177                    return false;
178            }
179    
180            protected HttpSession getSessionDelegate() {
181                    if (_portletSession != null) {
182                            return _portletSession;
183                    }
184                    else {
185                            return _portalSession;
186                    }
187            }
188    
189            protected HttpSession getSessionDelegate(String name) {
190                    if (_portletSession == null) {
191                            return _portalSession;
192                    }
193    
194                    if (_sharedSessionAttributesExcludes.containsKey(name)) {
195                            return _portletSession;
196                    }
197                    else if (containsSharedAttribute(name)) {
198                            return _portalSession;
199                    }
200                    else {
201                            return _portletSession;
202                    }
203            }
204    
205            private static Log _log = LogFactoryUtil.getLog(SharedSessionWrapper.class);
206    
207            private static Map<String, String> _sharedSessionAttributesExcludes;
208    
209            static {
210                    _sharedSessionAttributesExcludes = new HashMap<String, String>();
211    
212                    for (String name : PropsValues.SESSION_SHARED_ATTRIBUTES_EXCLUDES) {
213                            _sharedSessionAttributesExcludes.put(name, name);
214                    }
215            }
216    
217            private HttpSession _portalSession;
218            private HttpSession _portletSession;
219    
220    }