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.ServiceTracker;
022    
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.http.HttpServletResponse;
025    
026    /**
027     * <p>
028     * See https://issues.liferay.com/browse/LPS-18587.
029     * </p>
030     *
031     * @author Michael C. Han
032     */
033    public class CompoundSessionIdFilter
034            extends BasePortalFilter implements WrapHttpServletRequestFilter {
035    
036            public CompoundSessionIdFilter() {
037                    Registry registry = RegistryUtil.getRegistry();
038    
039                    _serviceTracker = registry.trackServices(
040                            CompoundSessionIdServletRequestFactory.class);
041    
042                    _serviceTracker.open();
043            }
044    
045            @Override
046            public void destroy() {
047                    _serviceTracker.close();
048    
049                    super.destroy();
050            }
051    
052            @Override
053            public HttpServletRequest getWrappedHttpServletRequest(
054                    HttpServletRequest request, HttpServletResponse response) {
055    
056                    CompoundSessionIdServletRequestFactory
057                            compoundSessionIdServletRequestFactory =
058                                    _serviceTracker.getService();
059    
060                    if (compoundSessionIdServletRequestFactory != null) {
061                            return compoundSessionIdServletRequestFactory.create(request);
062                    }
063    
064                    return request;
065            }
066    
067            @Override
068            public boolean isFilterEnabled() {
069                    if (_serviceTracker.isEmpty()) {
070                            return false;
071                    }
072    
073                    return true;
074            }
075    
076            private final ServiceTracker
077                    <CompoundSessionIdServletRequestFactory,
078                            CompoundSessionIdServletRequestFactory> _serviceTracker;
079    
080    }