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