View Javadoc

1   /*
2    * $Id: GZIPFilter.java 269 2005-08-10 17:49:22Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.filters.gzip;
24  
25  import java.io.IOException;
26  
27  import javax.servlet.FilterChain;
28  import javax.servlet.ServletException;
29  import javax.servlet.ServletRequest;
30  import javax.servlet.ServletResponse;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  import org.talika.tarsis.filters.AbstractFilter;
35  
36  /**
37   * <code>GZIPFilter</code> checks if client's accepts gzip encoding and then compress
38   * output using GZIP method.
39   *
40   * @author  Jose M. Palomar
41   * @version $Revision: 269 $
42   *
43   * @todo Move to org.talika.tarsis.filters package.
44   */
45  
46  public final class GZIPFilter extends AbstractFilter {
47  
48      // Fields
49  
50      // Methods
51      /**
52       * Checks if client's accepts gzip encoding and then compress output using GZIP
53       * method.
54       *
55       * @param servletRequest ServletRequest the <code>ServletRequest</code> object
56       * that contains the client's request.
57       * @param servletResponse ServletResponse the <code>ServletResponse</code> object
58       * that contains the servlet's response.
59       * @param filterChain FilterChain invocation chain of filtered request.
60       * @throws IOException if an input or output exception occurs
61       * @throws ServletException if an exception has occurred that interferes with the
62       * filter's normal operation
63       * @see javax.servlet.Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
64       */
65      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
66      FilterChain filterChain)
67      throws IOException, ServletException {
68  
69          if (getLogger().isDebugEnabled()) {
70              getLogger().logDebug(getFilterConfig().getFilterName() + ": invoked");
71          }
72  
73          if (acceptsGZIPEncoding((HttpServletRequest) servletRequest)) {
74  
75              try {
76  
77                  GZIPResponseWrapper gzipResponse =
78                              new GZIPResponseWrapper((HttpServletResponse) servletResponse);
79  
80                  filterChain.doFilter(servletRequest, gzipResponse);
81  
82                  if (getLogger().isDebugEnabled()) {
83                      getLogger().logDebug(getFilterConfig().getFilterName() + ": Compressed output");
84                  }
85  
86              }
87              catch (ServletException se) {
88                  throw se;
89              }
90              catch (Throwable t) {
91                  throw new ServletException(t);
92              }
93  
94          }
95          else {
96              filterChain.doFilter(servletRequest, servletResponse);
97          }
98  
99      }
100 
101     /**
102      * Checks if client accepts GZIP encoding.
103      *
104      * @param request HttpServletRequest the <code>HttpServletRequest</code> object
105      * that contains the client's request.
106      * @return boolean true if client accepts GZIP encoding.
107      */
108     protected boolean acceptsGZIPEncoding(HttpServletRequest request) {
109         String encoding = request.getHeader("accept-encoding");
110         return (encoding.indexOf("gzip") != -1);
111     }
112 
113     /**
114      * Checks if client is Netscape 4.
115      *
116      * @param request HttpServletRequest the <code>HttpServletRequest</code> object
117      * that contains the client's request.
118      * @return boolean true if client is Netscape 4.
119      */
120     protected boolean isNetscape4(HttpServletRequest request) {
121         String userAgent = request.getHeader("user-agent");
122         userAgent.toLowerCase();
123         return ((userAgent.indexOf("mozilla") != -1) &&
124                 (userAgent.indexOf("spoofer") == -1) &&
125                 (userAgent.indexOf("compatible") == -1) &&
126                 (userAgent.indexOf("opera") == -1) &&
127                 (userAgent.indexOf("webtv") == -1) &&
128                 (userAgent.indexOf("hotjava") == -1));
129     }
130 
131 }