001    /**
002     * Copyright (c) 2000-2011 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, and
042                    // Tomcat, but may break on other servers
043    
044                    if (super.isFilterEnabled()) {
045                            if (ServerDetector.isJBoss() || ServerDetector.isJetty() ||
046                                    ServerDetector.isJOnAS() || ServerDetector.isOC4J() ||
047                                    ServerDetector.isTomcat()) {
048    
049                                    _filterEnabled = true;
050                            }
051                            else {
052                                    _filterEnabled = false;
053                            }
054                    }
055            }
056    
057            protected boolean isAlreadyFiltered(HttpServletRequest request) {
058                    if (request.getAttribute(SKIP_FILTER) != null) {
059                            return true;
060                    }
061                    else {
062                            return false;
063                    }
064            }
065    
066            protected boolean isCompress(HttpServletRequest request) {
067                    if (ParamUtil.getBoolean(request, _COMPRESS, true)) {
068                            return true;
069                    }
070                    else {
071                            return false;
072                    }
073            }
074    
075            protected boolean isFilterEnabled() {
076                    return _filterEnabled;
077            }
078    
079            protected boolean isInclude(HttpServletRequest request) {
080                    String uri = (String)request.getAttribute(
081                            JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI);
082    
083                    if (uri == null) {
084                            return false;
085                    }
086                    else {
087                            return true;
088                    }
089            }
090    
091            protected void processFilter(
092                            HttpServletRequest request, HttpServletResponse response,
093                            FilterChain filterChain)
094                    throws Exception {
095    
096                    if (isCompress(request) && !isInclude(request) &&
097                            BrowserSnifferUtil.acceptsGzip(request) &&
098                            !isAlreadyFiltered(request)) {
099    
100                            if (_log.isDebugEnabled()) {
101                                    String completeURL = HttpUtil.getCompleteURL(request);
102    
103                                    _log.debug("Compressing " + completeURL);
104                            }
105    
106                            request.setAttribute(SKIP_FILTER, Boolean.TRUE);
107    
108                            GZipResponse gZipResponse = new GZipResponse(request, response);
109    
110                            processFilter(GZipFilter.class, request, gZipResponse, filterChain);
111    
112                            gZipResponse.finishResponse();
113                    }
114                    else {
115                            if (_log.isDebugEnabled()) {
116                                    String completeURL = HttpUtil.getCompleteURL(request);
117    
118                                    _log.debug("Not compressing " + completeURL);
119                            }
120    
121                            processFilter(
122                                    GZipFilter.class, request, response, filterChain);
123                    }
124            }
125    
126            private static final String _COMPRESS = "compress";
127    
128            private static Log _log = LogFactoryUtil.getLog(GZipFilter.class);
129    
130            private boolean _filterEnabled;
131    
132    }