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.filters.compoundsessionid;
016    
017    import com.liferay.portal.kernel.servlet.WrapHttpServletRequestFilter;
018    import com.liferay.portal.servlet.filters.BasePortalFilter;
019    import com.liferay.registry.Registry;
020    import com.liferay.registry.RegistryUtil;
021    import com.liferay.registry.ServiceReference;
022    import com.liferay.registry.ServiceTracker;
023    import com.liferay.registry.ServiceTrackerCustomizer;
024    
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    /**
029     * <p>
030     * See https://issues.liferay.com/browse/LPS-18587.
031     * </p>
032     *
033     * @author Michael C. Han
034     */
035    public class CompoundSessionIdFilter
036            extends BasePortalFilter implements WrapHttpServletRequestFilter {
037    
038            public CompoundSessionIdFilter() {
039                    Registry registry = RegistryUtil.getRegistry();
040    
041                    _serviceTracker = registry.trackServices(
042                            CompoundSessionIdServletRequestFactory.class,
043                            new CompoundSessionIdServletRequestFactoryTrackerCustomizer());
044    
045                    _serviceTracker.open();
046            }
047    
048            @Override
049            public HttpServletRequest getWrappedHttpServletRequest(
050                    HttpServletRequest request, HttpServletResponse response) {
051    
052                    CompoundSessionIdServletRequestFactory
053                            compoundSessionIdServletRequestFactory =
054                                    _compoundSessionIdServletRequestFactory;
055    
056                    if (compoundSessionIdServletRequestFactory != null) {
057                            return compoundSessionIdServletRequestFactory.create(request);
058                    }
059    
060                    return request;
061            }
062    
063            @Override
064            public boolean isFilterEnabled() {
065                    if (_compoundSessionIdServletRequestFactory != null) {
066                            return true;
067                    }
068    
069                    return false;
070            }
071    
072            private volatile CompoundSessionIdServletRequestFactory
073                    _compoundSessionIdServletRequestFactory;
074            private final ServiceTracker
075                    <CompoundSessionIdServletRequestFactory,
076                            CompoundSessionIdServletRequestFactory> _serviceTracker;
077    
078            private class CompoundSessionIdServletRequestFactoryTrackerCustomizer
079                    implements ServiceTrackerCustomizer
080                            <CompoundSessionIdServletRequestFactory,
081                                    CompoundSessionIdServletRequestFactory> {
082    
083                    @Override
084                    public CompoundSessionIdServletRequestFactory addingService(
085                            ServiceReference<CompoundSessionIdServletRequestFactory>
086                                    serviceReference) {
087    
088                            Registry registry = RegistryUtil.getRegistry();
089    
090                            CompoundSessionIdServletRequestFactory
091                                    compoundSessionIdServletRequestFactory = registry.getService(
092                                            serviceReference);
093    
094                            _compoundSessionIdServletRequestFactory =
095                                    compoundSessionIdServletRequestFactory;
096    
097                            return compoundSessionIdServletRequestFactory;
098                    }
099    
100                    @Override
101                    public void modifiedService(
102                            ServiceReference<CompoundSessionIdServletRequestFactory>
103                                    serviceReference,
104                            CompoundSessionIdServletRequestFactory
105                                    compoundSessionIdServletRequestFactory) {
106    
107                            removedService(
108                                    serviceReference, compoundSessionIdServletRequestFactory);
109                            addingService(serviceReference);
110                    }
111    
112                    @Override
113                    public void removedService(
114                            ServiceReference<CompoundSessionIdServletRequestFactory>
115                                    serviceReference,
116                            CompoundSessionIdServletRequestFactory
117                                    compoundSessionIdServletRequestFactory) {
118    
119                            _compoundSessionIdServletRequestFactory = null;
120                    }
121    
122            }
123    
124    }