001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }