1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class GZipFilter extends BasePortalFilter {
46  
47      public static final String SKIP_FILTER =
48          GZipFilter.class.getName() + "SKIP_FILTER";
49  
50      public GZipFilter() {
51  
52          // The compression filter will work on JBoss, Jetty, JOnAS, OC4J, Orion,
53          // and Tomcat, but may break on other servers
54  
55          if (super.isFilterEnabled()) {
56              if (ServerDetector.isJBoss() || ServerDetector.isJetty() ||
57                  ServerDetector.isJOnAS() || ServerDetector.isOC4J() ||
58                  ServerDetector.isOrion() || ServerDetector.isTomcat()) {
59  
60                  _filterEnabled = true;
61              }
62              else {
63                  _filterEnabled = false;
64              }
65          }
66      }
67  
68      protected boolean isAlreadyFiltered(HttpServletRequest request) {
69          if (request.getAttribute(SKIP_FILTER) != null) {
70              return true;
71          }
72          else {
73              return false;
74          }
75      }
76  
77      protected boolean isCompress(HttpServletRequest request) {
78          if (!ParamUtil.get(request, _COMPRESS, true)) {
79              return false;
80          }
81          else {
82  
83              // Modifying binary content through a servlet filter under certain
84              // conditions is bad on performance the user will not start
85              // downloading the content until the entire content is modified.
86  
87              String lifecycle = ParamUtil.getString(request, "p_p_lifecycle");
88  
89              if ((lifecycle.equals("1") &&
90                   LiferayWindowState.isExclusive(request)) ||
91                  lifecycle.equals("2")) {
92  
93                  return false;
94              }
95              else {
96                  return true;
97              }
98          }
99      }
100 
101     protected boolean isFilterEnabled() {
102         return _filterEnabled;
103     }
104 
105     protected boolean isInclude(HttpServletRequest request) {
106         String uri = (String)request.getAttribute(
107             JavaConstants.JAVAX_SERVLET_INCLUDE_REQUEST_URI);
108 
109         if (uri == null) {
110             return false;
111         }
112         else {
113             return true;
114         }
115     }
116 
117     protected void processFilter(
118             HttpServletRequest request, HttpServletResponse response,
119             FilterChain filterChain)
120         throws Exception {
121 
122         if (isCompress(request) && !isInclude(request) &&
123             BrowserSnifferUtil.acceptsGzip(request) &&
124             !isAlreadyFiltered(request)) {
125 
126             if (_log.isDebugEnabled()) {
127                 String completeURL = HttpUtil.getCompleteURL(request);
128 
129                 _log.debug("Compressing " + completeURL);
130             }
131 
132             request.setAttribute(SKIP_FILTER, Boolean.TRUE);
133 
134             GZipResponse gZipResponse = new GZipResponse(response);
135 
136             processFilter(GZipFilter.class, request, gZipResponse, filterChain);
137 
138             gZipResponse.finishResponse();
139         }
140         else {
141             if (_log.isDebugEnabled()) {
142                 String completeURL = HttpUtil.getCompleteURL(request);
143 
144                 _log.debug("Not compressing " + completeURL);
145             }
146 
147             processFilter(
148                 GZipFilter.class, request, response, filterChain);
149         }
150     }
151 
152     private static final String _COMPRESS = "compress";
153 
154     private static Log _log = LogFactoryUtil.getLog(GZipFilter.class);
155 
156     private boolean _filterEnabled;
157 
158 }