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