1   /**
2    * Copyright (c) 2000-2007 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.servlet.BrowserSniffer;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.JavaConstants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.ServerDetector;
30  import com.liferay.util.Http;
31  import com.liferay.util.SystemProperties;
32  
33  import java.io.IOException;
34  
35  import javax.servlet.Filter;
36  import javax.servlet.FilterChain;
37  import javax.servlet.FilterConfig;
38  import javax.servlet.ServletException;
39  import javax.servlet.ServletRequest;
40  import javax.servlet.ServletResponse;
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  /**
48   * <a href="CompressionFilter.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class CompressionFilter implements Filter {
54  
55      static boolean useFilter = GetterUtil.getBoolean(
56          SystemProperties.get(CompressionFilter.class.getName()), true);
57  
58      public static final String ENCODING = GetterUtil.getString(
59          SystemProperties.get("file.encoding"), "UTF-8");
60  
61      static {
62  
63          // The compression filter will work on JBoss, Jetty, JOnAS, OC4J, Orion,
64          // and Tomcat, but may break on other servers
65  
66          if (useFilter) {
67              if (ServerDetector.isJBoss() || ServerDetector.isJetty() ||
68                  ServerDetector.isJOnAS() || ServerDetector.isOC4J() ||
69                  ServerDetector.isOrion() || ServerDetector.isTomcat()) {
70  
71                  useFilter = true;
72              }
73              else {
74                  useFilter = false;
75              }
76          }
77      }
78  
79      public void init(FilterConfig config) {
80      }
81  
82      public void doFilter(
83              ServletRequest req, ServletResponse res, FilterChain chain)
84          throws IOException, ServletException {
85  
86          if (_log.isDebugEnabled()) {
87              if (useFilter) {
88                  _log.debug("Compression is enabled");
89              }
90              else {
91                  _log.debug("Compression is disabled");
92              }
93          }
94  
95          HttpServletRequest httpReq = (HttpServletRequest)req;
96          HttpServletResponse httpRes = (HttpServletResponse)res;
97  
98          String completeURL = Http.getCompleteURL(httpReq);
99  
100         if (useFilter && isCompress(httpReq) && !isInclude(httpReq) &&
101             BrowserSniffer.acceptsGzip(httpReq) &&
102             !isAlreadyFiltered(httpReq)) {
103 
104             if (_log.isDebugEnabled()) {
105                 _log.debug("Compressing " + completeURL);
106             }
107 
108             httpReq.setAttribute(_ALREADY_FILTERED, Boolean.TRUE);
109 
110             CompressionResponse compressionResponse =
111                 new CompressionResponse(httpRes);
112 
113             chain.doFilter(req, compressionResponse);
114 
115             compressionResponse.finishResponse();
116         }
117         else {
118             if (_log.isDebugEnabled()) {
119                 _log.debug("Not compressing " + completeURL);
120             }
121 
122             chain.doFilter(req, res);
123         }
124     }
125 
126     public void destroy() {
127     }
128 
129     protected boolean isAlreadyFiltered(HttpServletRequest req) {
130         if (req.getAttribute(_ALREADY_FILTERED) != null) {
131             return true;
132         }
133         else {
134             return false;
135         }
136     }
137 
138     protected boolean isCompress(HttpServletRequest req) {
139         if (!ParamUtil.get(req, _COMPRESS, true)) {
140             return false;
141         }
142         else {
143 
144             // The exclusive state is used to stream binary content.
145             // Compressing binary content through a servlet filter is bad on
146             // performance because the user will not start downloading the
147             // content until the entire content is compressed.
148 
149             String windowState = ParamUtil.getString(req, "p_p_state");
150 
151             if (windowState.equals("exclusive")) {
152                 return false;
153             }
154             else {
155                 return true;
156             }
157         }
158     }
159 
160     protected boolean isInclude(HttpServletRequest req) {
161         String uri = (String)req.getAttribute(
162             JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI);
163 
164         if (uri == null) {
165             return false;
166         }
167         else {
168             return true;
169         }
170     }
171 
172     private static final String _ALREADY_FILTERED =
173         CompressionFilter.class.getName() + "_ALREADY_FILTERED";
174 
175     private static final String _COMPRESS = "compress";
176 
177     private static Log _log = LogFactory.getLog(CompressionFilter.class);
178 
179 }