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.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            @Override
058            public boolean isFilterEnabled() {
059                    return _filterEnabled;
060            }
061    
062            @Override
063            public boolean isFilterEnabled(
064                    HttpServletRequest request, HttpServletResponse response) {
065    
066                    if (isCompress(request) && !isInclude(request) &&
067                            BrowserSnifferUtil.acceptsGzip(request) &&
068                            !isAlreadyFiltered(request)) {
069    
070                            return true;
071                    }
072                    else {
073                            return false;
074                    }
075            }
076    
077            protected boolean isAlreadyFiltered(HttpServletRequest request) {
078                    if (request.getAttribute(SKIP_FILTER) != null) {
079                            return true;
080                    }
081                    else {
082                            return false;
083                    }
084            }
085    
086            protected boolean isCompress(HttpServletRequest request) {
087                    if (ParamUtil.getBoolean(request, _COMPRESS, true)) {
088                            return true;
089                    }
090                    else {
091                            return false;
092                    }
093            }
094    
095            protected boolean isInclude(HttpServletRequest request) {
096                    String uri = (String)request.getAttribute(
097                            JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI);
098    
099                    if (uri == null) {
100                            return false;
101                    }
102                    else {
103                            return true;
104                    }
105            }
106    
107            @Override
108            protected void processFilter(
109                            HttpServletRequest request, HttpServletResponse response,
110                            FilterChain filterChain)
111                    throws Exception {
112    
113                    if (_log.isDebugEnabled()) {
114                            String completeURL = HttpUtil.getCompleteURL(request);
115    
116                            _log.debug("Compressing " + completeURL);
117                    }
118    
119                    request.setAttribute(SKIP_FILTER, Boolean.TRUE);
120    
121                    GZipResponse gZipResponse = new GZipResponse(request, response);
122    
123                    processFilter(GZipFilter.class, request, gZipResponse, filterChain);
124    
125                    gZipResponse.finishResponse(true);
126            }
127    
128            private static final String _COMPRESS = "compress";
129    
130            private static Log _log = LogFactoryUtil.getLog(GZipFilter.class);
131    
132            private boolean _filterEnabled;
133    
134    }