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            /**
134             * @deprecated As of 7.0.0
135             */
136            @Deprecated
137            @Override
138            public Object getValue(String name) {
139                    return getAttribute(name);
140            }
141    
142            /**
143             * @deprecated As of 7.0.0
144             */
145            @Deprecated
146            @Override
147            public String[] getValueNames() {
148                    List<String> names = ListUtil.fromEnumeration(getAttributeNames());
149    
150                    return names.toArray(new String[names.size()]);
151            }
152    
153            @Override
154            public void invalidate() {
155                    HttpSession session = getSessionDelegate();
156    
157                    session.invalidate();
158            }
159    
160            @Override
161            public boolean isNew() {
162                    HttpSession session = getSessionDelegate();
163    
164                    return session.isNew();
165            }
166    
167            /**
168             * @deprecated As of 7.0.0
169             */
170            @Deprecated
171            @Override
172            public void putValue(String name, Object value) {
173                    setAttribute(name, value);
174            }
175    
176            @Override
177            public void removeAttribute(String name) {
178                    HttpSession session = getSessionDelegate(name);
179    
180                    session.removeAttribute(name);
181            }
182    
183            /**
184             * @deprecated As of 7.0.0
185             */
186            @Deprecated
187            @Override
188            public void removeValue(String name) {
189                    removeAttribute(name);
190            }
191    
192            @Override
193            public void setAttribute(String name, Object value) {
194                    HttpSession session = getSessionDelegate(name);
195    
196                    session.setAttribute(name, value);
197            }
198    
199            @Override
200            public void setMaxInactiveInterval(int maxInactiveInterval) {
201                    HttpSession session = getSessionDelegate();
202    
203                    session.setMaxInactiveInterval(maxInactiveInterval);
204            }
205    
206            protected boolean containsSharedAttribute(String name) {
207                    for (String sharedName : PropsValues.SESSION_SHARED_ATTRIBUTES) {
208                            if (name.startsWith(sharedName)) {
209                                    return true;
210                            }
211                    }
212    
213                    return false;
214            }
215    
216            protected HttpSession getSessionDelegate() {
217                    if (_portletSession != null) {
218                            return _portletSession;
219                    }
220                    else {
221                            return _portalSession;
222                    }
223            }
224    
225            protected HttpSession getSessionDelegate(String name) {
226                    if (_portletSession == null) {
227                            return _portalSession;
228                    }
229    
230                    if (_sharedSessionAttributesExcludes.containsKey(name)) {
231                            return _portletSession;
232                    }
233                    else if (containsSharedAttribute(name)) {
234                            return _portalSession;
235                    }
236                    else {
237                            return _portletSession;
238                    }
239            }
240    
241            private static final Log _log = LogFactoryUtil.getLog(
242                    SharedSessionWrapper.class);
243    
244            private static final Map<String, String> _sharedSessionAttributesExcludes;
245    
246            static {
247                    _sharedSessionAttributesExcludes = new HashMap<>();
248    
249                    for (String name : PropsValues.SESSION_SHARED_ATTRIBUTES_EXCLUDES) {
250                            _sharedSessionAttributesExcludes.put(name, name);
251                    }
252            }
253    
254            private final HttpSession _portalSession;
255            private HttpSession _portletSession;
256    
257    }