001    /**
002     * Copyright (c) 2000-2013 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.kernel.servlet.filters.compoundsessionid.CompoundSessionIdServletRequest;
019    import com.liferay.portal.kernel.servlet.filters.compoundsessionid.CompoundSessionIdSplitterUtil;
020    import com.liferay.portal.servlet.filters.BasePortalFilter;
021    
022    import javax.servlet.FilterConfig;
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.http.HttpServletRequestWrapper;
025    import javax.servlet.http.HttpServletResponse;
026    
027    /**
028     * <p>
029     * See http://issues.liferay.com/browse/LPS-18587.
030     * </p>
031     *
032     * @author Michael C. Han
033     */
034    public class CompoundSessionIdFilter
035            extends BasePortalFilter implements WrapHttpServletRequestFilter {
036    
037            @Override
038            public HttpServletRequest getWrappedHttpServletRequest(
039                    HttpServletRequest request, HttpServletResponse response) {
040    
041                    HttpServletRequest wrappedRequest = request;
042    
043                    while (wrappedRequest instanceof HttpServletRequestWrapper) {
044                            if (wrappedRequest instanceof CompoundSessionIdServletRequest) {
045                                    return request;
046                            }
047    
048                            HttpServletRequestWrapper httpServletRequestWrapper =
049                                    (HttpServletRequestWrapper)wrappedRequest;
050    
051                            wrappedRequest =
052                                    (HttpServletRequest)httpServletRequestWrapper.getRequest();
053                    }
054    
055                    return new CompoundSessionIdServletRequest(request);
056            }
057    
058            @Override
059            public void init(FilterConfig filterConfig) {
060                    super.init(filterConfig);
061    
062                    if (CompoundSessionIdSplitterUtil.hasSessionDelimiter()) {
063                            _filterEnabled = true;
064                    }
065                    else {
066                            _filterEnabled = false;
067                    }
068            }
069    
070            @Override
071            public boolean isFilterEnabled() {
072                    return _filterEnabled;
073            }
074    
075            private static boolean _filterEnabled;
076    
077    }