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