1   /**
2    * Copyright (c) 2000-2009 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.gzip;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.portlet.LiferayWindowState;
28  import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
29  import com.liferay.portal.kernel.util.HttpUtil;
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.portal.servlet.filters.BasePortalFilter;
34  
35  import javax.servlet.FilterChain;
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  
39  /**
40   * <a href="GZipFilter.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   * @author Raymond Augé
44   *
45   */
46  public class GZipFilter extends BasePortalFilter {
47  
48      public static final String SKIP_FILTER =
49          GZipFilter.class.getName() + "SKIP_FILTER";
50  
51      public GZipFilter() {
52  
53          // The compression filter will work on JBoss, Jetty, JOnAS, OC4J, Orion,
54          // and Tomcat, but may break on other servers
55  
56          if (super.isFilterEnabled()) {
57              if (ServerDetector.isJBoss() || ServerDetector.isJetty() ||
58                  ServerDetector.isJOnAS() || ServerDetector.isOC4J() ||
59                  ServerDetector.isOrion() || ServerDetector.isTomcat()) {
60  
61                  _filterEnabled = true;
62              }
63              else {
64                  _filterEnabled = false;
65              }
66          }
67      }
68  
69      protected boolean isAlreadyFiltered(HttpServletRequest request) {
70          if (request.getAttribute(SKIP_FILTER) != null) {
71              return true;
72          }
73          else {
74              return false;
75          }
76      }
77  
78      protected boolean isCompress(HttpServletRequest request) {
79          if (!ParamUtil.get(request, _COMPRESS, true)) {
80              return false;
81          }
82          else {
83  
84              // Modifying binary content through a servlet filter under certain
85              // conditions is bad on performance the user will not start
86              // downloading the content until the entire content is modified.
87  
88              String lifecycle = ParamUtil.getString(request, "p_p_lifecycle");
89  
90              if ((lifecycle.equals("1") &&
91                   LiferayWindowState.isExclusive(request)) ||
92                  lifecycle.equals("2")) {
93  
94                  return false;
95              }
96              else {
97                  return true;
98              }
99          }
100     }
101 
102     protected boolean isFilterEnabled() {
103         return _filterEnabled;
104     }
105 
106     protected boolean isInclude(HttpServletRequest request) {
107         String uri = (String)request.getAttribute(
108             JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI);
109 
110         if (uri == null) {
111             return false;
112         }
113         else {
114             return true;
115         }
116     }
117 
118     protected void processFilter(
119             HttpServletRequest request, HttpServletResponse response,
120             FilterChain filterChain)
121         throws Exception {
122 
123         if (isCompress(request) && !isInclude(request) &&
124             BrowserSnifferUtil.acceptsGzip(request) &&
125             !isAlreadyFiltered(request)) {
126 
127             if (_log.isDebugEnabled()) {
128                 String completeURL = HttpUtil.getCompleteURL(request);
129 
130                 _log.debug("Compressing " + completeURL);
131             }
132 
133             request.setAttribute(SKIP_FILTER, Boolean.TRUE);
134 
135             GZipResponse gZipResponse = new GZipResponse(response);
136 
137             processFilter(GZipFilter.class, request, gZipResponse, filterChain);
138 
139             gZipResponse.finishResponse();
140         }
141         else {
142             if (_log.isDebugEnabled()) {
143                 String completeURL = HttpUtil.getCompleteURL(request);
144 
145                 _log.debug("Not compressing " + completeURL);
146             }
147 
148             processFilter(
149                 GZipFilter.class, request, response, filterChain);
150         }
151     }
152 
153     private static final String _COMPRESS = "compress";
154 
155     private static Log _log = LogFactoryUtil.getLog(GZipFilter.class);
156 
157     private boolean _filterEnabled;
158 
159 }