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