1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.filters.compression;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.servlet.BaseFilter;
28  import com.liferay.portal.kernel.servlet.BrowserSniffer;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.JavaConstants;
31  import com.liferay.portal.kernel.util.ParamUtil;
32  import com.liferay.portal.kernel.util.ServerDetector;
33  import com.liferay.util.Http;
34  import com.liferay.util.SystemProperties;
35  
36  import java.io.IOException;
37  
38  import javax.servlet.FilterChain;
39  import javax.servlet.ServletException;
40  import javax.servlet.ServletRequest;
41  import javax.servlet.ServletResponse;
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  /**
46   * <a href="CompressionFilter.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Raymond Augé
50   *
51   */
52  public class CompressionFilter extends BaseFilter {
53  
54      static boolean useFilter = GetterUtil.getBoolean(
55          SystemProperties.get(CompressionFilter.class.getName()), true);
56  
57      public static final String ENCODING = GetterUtil.getString(
58          SystemProperties.get("file.encoding"), "UTF-8");
59  
60      static {
61  
62          // The compression filter will work on JBoss, Jetty, JOnAS, OC4J, Orion,
63          // and Tomcat, but may break on other servers
64  
65          if (useFilter) {
66              if (ServerDetector.isJBoss() || ServerDetector.isJetty() ||
67                  ServerDetector.isJOnAS() || ServerDetector.isOC4J() ||
68                  ServerDetector.isOrion() || ServerDetector.isTomcat()) {
69  
70                  useFilter = true;
71              }
72              else {
73                  useFilter = false;
74              }
75          }
76      }
77  
78      public void doFilter(
79              ServletRequest req, ServletResponse res, FilterChain chain)
80          throws IOException, ServletException {
81  
82          if (_log.isDebugEnabled()) {
83              if (useFilter) {
84                  _log.debug("Compression is enabled");
85              }
86              else {
87                  _log.debug("Compression is disabled");
88              }
89          }
90  
91          HttpServletRequest httpReq = (HttpServletRequest)req;
92          HttpServletResponse httpRes = (HttpServletResponse)res;
93  
94          String completeURL = Http.getCompleteURL(httpReq);
95  
96          if (useFilter && isCompress(httpReq) && !isInclude(httpReq) &&
97              BrowserSniffer.acceptsGzip(httpReq) &&
98              !isAlreadyFiltered(httpReq)) {
99  
100             if (_log.isDebugEnabled()) {
101                 _log.debug("Compressing " + completeURL);
102             }
103 
104             httpReq.setAttribute(_ALREADY_FILTERED, Boolean.TRUE);
105 
106             CompressionResponse compressionResponse =
107                 new CompressionResponse(httpRes);
108 
109             doFilter(CompressionFilter.class, req, compressionResponse, chain);
110 
111             compressionResponse.finishResponse();
112         }
113         else {
114             if (_log.isDebugEnabled()) {
115                 _log.debug("Not compressing " + completeURL);
116             }
117 
118             doFilter(CompressionFilter.class, req, res, chain);
119         }
120     }
121 
122     protected boolean isAlreadyFiltered(HttpServletRequest req) {
123         if (req.getAttribute(_ALREADY_FILTERED) != null) {
124             return true;
125         }
126         else {
127             return false;
128         }
129     }
130 
131     protected boolean isCompress(HttpServletRequest req) {
132         if (!ParamUtil.get(req, _COMPRESS, true)) {
133             return false;
134         }
135         else {
136 
137             // The exclusive state is used to stream binary content.
138             // Compressing binary content through a servlet filter is bad on
139             // performance because the user will not start downloading the
140             // content until the entire content is compressed.
141 
142             String windowState = ParamUtil.getString(req, "p_p_state");
143 
144             if (windowState.equals("exclusive")) {
145                 return false;
146             }
147             else {
148                 return true;
149             }
150         }
151     }
152 
153     protected boolean isInclude(HttpServletRequest req) {
154         String uri = (String)req.getAttribute(
155             JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI);
156 
157         if (uri == null) {
158             return false;
159         }
160         else {
161             return true;
162         }
163     }
164 
165     private static final String _ALREADY_FILTERED =
166         CompressionFilter.class.getName() + "_ALREADY_FILTERED";
167 
168     private static final String _COMPRESS = "compress";
169 
170     private static Log _log = LogFactoryUtil.getLog(CompressionFilter.class);
171 
172 }