1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.servlet.filters.gzip;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
20  import com.liferay.portal.kernel.util.HttpUtil;
21  import com.liferay.portal.kernel.util.JavaConstants;
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.kernel.util.ServerDetector;
24  import com.liferay.portal.servlet.filters.BasePortalFilter;
25  
26  import javax.servlet.FilterChain;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  /**
31   * <a href="GZipFilter.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   * @author Raymond Augé
35   */
36  public class GZipFilter extends BasePortalFilter {
37  
38      public static final String SKIP_FILTER =
39          GZipFilter.class.getName() + "SKIP_FILTER";
40  
41      public GZipFilter() {
42  
43          // The compression filter will work on JBoss, Jetty, JOnAS, OC4J, and
44          // Tomcat, but may break on other servers
45  
46          if (super.isFilterEnabled()) {
47              if (ServerDetector.isJBoss() || ServerDetector.isJetty() ||
48                  ServerDetector.isJOnAS() || ServerDetector.isOC4J() ||
49                  ServerDetector.isTomcat()) {
50  
51                  _filterEnabled = true;
52              }
53              else {
54                  _filterEnabled = false;
55              }
56          }
57      }
58  
59      protected boolean isAlreadyFiltered(HttpServletRequest request) {
60          if (request.getAttribute(SKIP_FILTER) != null) {
61              return true;
62          }
63          else {
64              return false;
65          }
66      }
67  
68      protected boolean isCompress(HttpServletRequest request) {
69          if (!ParamUtil.get(request, _COMPRESS, true)) {
70              return false;
71          }
72          else {
73              return false;
74          }
75      }
76  
77      protected boolean isFilterEnabled() {
78          return _filterEnabled;
79      }
80  
81      protected boolean isInclude(HttpServletRequest request) {
82          String uri = (String)request.getAttribute(
83              JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI);
84  
85          if (uri == null) {
86              return false;
87          }
88          else {
89              return true;
90          }
91      }
92  
93      protected void processFilter(
94              HttpServletRequest request, HttpServletResponse response,
95              FilterChain filterChain)
96          throws Exception {
97  
98          if (isCompress(request) && !isInclude(request) &&
99              BrowserSnifferUtil.acceptsGzip(request) &&
100             !isAlreadyFiltered(request)) {
101 
102             if (_log.isDebugEnabled()) {
103                 String completeURL = HttpUtil.getCompleteURL(request);
104 
105                 _log.debug("Compressing " + completeURL);
106             }
107 
108             request.setAttribute(SKIP_FILTER, Boolean.TRUE);
109 
110             GZipResponse gZipResponse = new GZipResponse(response);
111 
112             processFilter(GZipFilter.class, request, gZipResponse, filterChain);
113 
114             gZipResponse.finishResponse();
115         }
116         else {
117             if (_log.isDebugEnabled()) {
118                 String completeURL = HttpUtil.getCompleteURL(request);
119 
120                 _log.debug("Not compressing " + completeURL);
121             }
122 
123             processFilter(
124                 GZipFilter.class, request, response, filterChain);
125         }
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 }