1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.portlet.LiferayWindowState;
20  import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
21  import com.liferay.portal.kernel.util.HttpUtil;
22  import com.liferay.portal.kernel.util.JavaConstants;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.kernel.util.ServerDetector;
25  import com.liferay.portal.servlet.filters.BasePortalFilter;
26  
27  import javax.servlet.FilterChain;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  /**
32   * <a href="GZipFilter.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   * @author Raymond Augé
36   */
37  public class GZipFilter extends BasePortalFilter {
38  
39      public static final String SKIP_FILTER =
40          GZipFilter.class.getName() + "SKIP_FILTER";
41  
42      public GZipFilter() {
43  
44          // The compression filter will work on JBoss, Jetty, JOnAS, OC4J, and
45          // Tomcat, but may break on other servers
46  
47          if (super.isFilterEnabled()) {
48              if (ServerDetector.isJBoss() || ServerDetector.isJetty() ||
49                  ServerDetector.isJOnAS() || ServerDetector.isOC4J() ||
50                  ServerDetector.isTomcat()) {
51  
52                  _filterEnabled = true;
53              }
54              else {
55                  _filterEnabled = false;
56              }
57          }
58      }
59  
60      protected boolean isAlreadyFiltered(HttpServletRequest request) {
61          if (request.getAttribute(SKIP_FILTER) != null) {
62              return true;
63          }
64          else {
65              return false;
66          }
67      }
68  
69      protected boolean isCompress(HttpServletRequest request) {
70          if (!ParamUtil.get(request, _COMPRESS, true)) {
71              return false;
72          }
73          else {
74  
75              // Modifying binary content through a servlet filter under certain
76              // conditions is bad on performance the user will not start
77              // downloading the content until the entire content is modified.
78  
79              String lifecycle = ParamUtil.getString(request, "p_p_lifecycle");
80  
81              if ((lifecycle.equals("1") &&
82                   LiferayWindowState.isExclusive(request)) ||
83                  lifecycle.equals("2")) {
84  
85                  return false;
86              }
87              else {
88                  return true;
89              }
90          }
91      }
92  
93      protected boolean isFilterEnabled() {
94          return _filterEnabled;
95      }
96  
97      protected boolean isInclude(HttpServletRequest request) {
98          String uri = (String)request.getAttribute(
99              JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI);
100 
101         if (uri == null) {
102             return false;
103         }
104         else {
105             return true;
106         }
107     }
108 
109     protected void processFilter(
110             HttpServletRequest request, HttpServletResponse response,
111             FilterChain filterChain)
112         throws Exception {
113 
114         if (isCompress(request) && !isInclude(request) &&
115             BrowserSnifferUtil.acceptsGzip(request) &&
116             !isAlreadyFiltered(request)) {
117 
118             if (_log.isDebugEnabled()) {
119                 String completeURL = HttpUtil.getCompleteURL(request);
120 
121                 _log.debug("Compressing " + completeURL);
122             }
123 
124             request.setAttribute(SKIP_FILTER, Boolean.TRUE);
125 
126             GZipResponse gZipResponse = new GZipResponse(response);
127 
128             processFilter(GZipFilter.class, request, gZipResponse, filterChain);
129 
130             gZipResponse.finishResponse();
131         }
132         else {
133             if (_log.isDebugEnabled()) {
134                 String completeURL = HttpUtil.getCompleteURL(request);
135 
136                 _log.debug("Not compressing " + completeURL);
137             }
138 
139             processFilter(
140                 GZipFilter.class, request, response, filterChain);
141         }
142     }
143 
144     private static final String _COMPRESS = "compress";
145 
146     private static Log _log = LogFactoryUtil.getLog(GZipFilter.class);
147 
148     private boolean _filterEnabled;
149 
150 }