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.concurrent.ConcurrentHashSet;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    import com.liferay.portal.util.PropsValues;
020    
021    import java.util.Set;
022    
023    import javax.servlet.http.HttpSession;
024    import javax.servlet.http.HttpSessionAttributeListener;
025    import javax.servlet.http.HttpSessionBindingEvent;
026    import javax.servlet.http.HttpSessionEvent;
027    import javax.servlet.http.HttpSessionListener;
028    
029    /**
030     * <p>
031     * Listener used to help manage shared session attributes into a cache. This
032     * cache is more thread safe than the HttpSession and leads to fewer problems
033     * with shared session attributes being modified out of sequence.
034     * </p>
035     *
036     * @author     Michael C. Han
037     * @deprecated As of 7.0.0, with no direct replacement
038     */
039    @Deprecated
040    public class SharedSessionAttributeListener
041            implements HttpSessionAttributeListener, HttpSessionListener {
042    
043            public SharedSessionAttributeListener() {
044                    _sessionIds = new ConcurrentHashSet<>();
045            }
046    
047            @Override
048            public void attributeAdded(HttpSessionBindingEvent event) {
049                    if (PropsValues.SESSION_DISABLED) {
050                            return;
051                    }
052    
053                    HttpSession session = event.getSession();
054    
055                    if (!_sessionIds.contains(session.getId())) {
056                            return;
057                    }
058    
059                    SharedSessionAttributeCache sharedSessionAttributeCache =
060                            SharedSessionAttributeCache.getInstance(session);
061    
062                    String name = event.getName();
063    
064                    if (ArrayUtil.contains(
065                                    PropsValues.SESSION_SHARED_ATTRIBUTES_EXCLUDES, name)) {
066    
067                            return;
068                    }
069    
070                    for (String sharedName : PropsValues.SESSION_SHARED_ATTRIBUTES) {
071                            if (!name.startsWith(sharedName)) {
072                                    continue;
073                            }
074    
075                            sharedSessionAttributeCache.setAttribute(name, event.getValue());
076    
077                            return;
078                    }
079            }
080    
081            @Override
082            public void attributeRemoved(HttpSessionBindingEvent event) {
083                    if (PropsValues.SESSION_DISABLED) {
084                            return;
085                    }
086    
087                    HttpSession session = event.getSession();
088    
089                    if (!_sessionIds.contains(session.getId())) {
090                            return;
091                    }
092    
093                    SharedSessionAttributeCache sharedSessionAttributeCache =
094                            SharedSessionAttributeCache.getInstance(session);
095    
096                    sharedSessionAttributeCache.removeAttribute(event.getName());
097            }
098    
099            @Override
100            public void attributeReplaced(HttpSessionBindingEvent event) {
101                    if (PropsValues.SESSION_DISABLED) {
102                            return;
103                    }
104    
105                    HttpSession session = event.getSession();
106    
107                    if (!_sessionIds.contains(session.getId())) {
108                            return;
109                    }
110    
111                    SharedSessionAttributeCache sharedSessionAttributeCache =
112                            SharedSessionAttributeCache.getInstance(session);
113    
114                    if (sharedSessionAttributeCache.contains(event.getName())) {
115                            Object value = session.getAttribute(event.getName());
116    
117                            sharedSessionAttributeCache.setAttribute(event.getName(), value);
118                    }
119            }
120    
121            @Override
122            public void sessionCreated(HttpSessionEvent event) {
123                    if (PropsValues.SESSION_DISABLED) {
124                            return;
125                    }
126    
127                    HttpSession session = event.getSession();
128    
129                    SharedSessionAttributeCache.getInstance(session);
130    
131                    _sessionIds.add(session.getId());
132            }
133    
134            @Override
135            public void sessionDestroyed(HttpSessionEvent event) {
136                    if (PropsValues.SESSION_DISABLED) {
137                            return;
138                    }
139    
140                    HttpSession session = event.getSession();
141    
142                    _sessionIds.remove(session.getId());
143            }
144    
145            private final Set<String> _sessionIds;
146    
147    }