1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet.filters.gzip;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.portlet.LiferayWindowState;
25  import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
26  import com.liferay.portal.kernel.util.HttpUtil;
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.portal.servlet.filters.BasePortalFilter;
31  
32  import java.io.IOException;
33  
34  import javax.servlet.FilterChain;
35  import javax.servlet.ServletException;
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 IOException, ServletException {
122 
123         String completeURL = HttpUtil.getCompleteURL(request);
124 
125         if (isCompress(request) && !isInclude(request) &&
126             BrowserSnifferUtil.acceptsGzip(request) &&
127             !isAlreadyFiltered(request)) {
128 
129             if (_log.isDebugEnabled()) {
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                 _log.debug("Not compressing " + completeURL);
144             }
145 
146             processFilter(
147                 GZipFilter.class, request, response, filterChain);
148         }
149     }
150 
151     private static final String _COMPRESS = "compress";
152 
153     private static Log _log = LogFactoryUtil.getLog(GZipFilter.class);
154 
155     private boolean _filterEnabled;
156 
157 }