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.gzip;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
020    import com.liferay.portal.kernel.util.HttpUtil;
021    import com.liferay.portal.kernel.util.JavaConstants;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.ServerDetector;
024    import com.liferay.portal.servlet.filters.BasePortalFilter;
025    
026    import javax.servlet.FilterChain;
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Raymond Aug??
033     */
034    public class GZipFilter extends BasePortalFilter {
035    
036            public static final String SKIP_FILTER =
037                    GZipFilter.class.getName() + "#SKIP_FILTER";
038    
039            public GZipFilter() {
040    
041                    // The compression filter will work on JBoss, Jetty, JOnAS, OC4J,
042                    // Tomcat, WebLogic, and WebSphere, but may break on other servers
043    
044                    boolean filterEnabled = false;
045    
046                    if (super.isFilterEnabled()) {
047                            if (ServerDetector.isJBoss() || ServerDetector.isJetty() ||
048                                    ServerDetector.isJOnAS() || ServerDetector.isOC4J() ||
049                                    ServerDetector.isTomcat() || ServerDetector.isWebLogic() ||
050                                    ServerDetector.isWebSphere()) {
051    
052                                    filterEnabled = true;
053                            }
054                    }
055    
056                    _filterEnabled = filterEnabled;
057            }
058    
059            @Override
060            public boolean isFilterEnabled() {
061                    return _filterEnabled;
062            }
063    
064            @Override
065            public boolean isFilterEnabled(
066                    HttpServletRequest request, HttpServletResponse response) {
067    
068                    if (isCompress(request) && !isInclude(request) &&
069                            BrowserSnifferUtil.acceptsGzip(request) &&
070                            !isAlreadyFiltered(request)) {
071    
072                            return true;
073                    }
074                    else {
075                            return false;
076                    }
077            }
078    
079            protected boolean isAlreadyFiltered(HttpServletRequest request) {
080                    if (request.getAttribute(SKIP_FILTER) != null) {
081                            return true;
082                    }
083                    else {
084                            return false;
085                    }
086            }
087    
088            protected boolean isCompress(HttpServletRequest request) {
089                    if (ParamUtil.getBoolean(request, _COMPRESS, true)) {
090                            return true;
091                    }
092                    else {
093                            return false;
094                    }
095            }
096    
097            protected boolean isInclude(HttpServletRequest request) {
098                    String uri = (String)request.getAttribute(
099                            JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI);
100    
101                    if (uri == null) {
102                            return false;
103                    }
104                    else {
105                            return true;
106                    }
107            }
108    
109            @Override
110            protected void processFilter(
111                            HttpServletRequest request, HttpServletResponse response,
112                            FilterChain filterChain)
113                    throws Exception {
114    
115                    if (_log.isDebugEnabled()) {
116                            String completeURL = HttpUtil.getCompleteURL(request);
117    
118                            _log.debug("Compressing " + completeURL);
119                    }
120    
121                    request.setAttribute(SKIP_FILTER, Boolean.TRUE);
122    
123                    GZipResponse gZipResponse = new GZipResponse(request, response);
124    
125                    processFilter(
126                            GZipFilter.class.getName(), request, gZipResponse, filterChain);
127    
128                    gZipResponse.finishResponse(true);
129            }
130    
131            private static final String _COMPRESS = "compress";
132    
133            private static final Log _log = LogFactoryUtil.getLog(GZipFilter.class);
134    
135            private final boolean _filterEnabled;
136    
137    }