KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > filters > gzip > GzipFilter


1 /*
2  * Generator Runtime Servlet Framework
3  * Copyright (C) 2004 Rick Knowles
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public License
7  * Version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License Version 2 for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * Version 2 along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */

18 package filters.gzip;
19
20 import java.io.IOException JavaDoc;
21 import java.util.Enumeration JavaDoc;
22
23 import javax.servlet.Filter JavaDoc;
24 import javax.servlet.FilterChain JavaDoc;
25 import javax.servlet.FilterConfig JavaDoc;
26 import javax.servlet.ServletContext JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.ServletRequest JavaDoc;
29 import javax.servlet.ServletResponse JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 /**
34  * A filter that checks if the request will accept a gzip encoded response, and
35  * if so wraps the response in a gzip encoding response wrapper.
36  *
37  * @author <a HREF="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
38  * @version $Id: GzipFilter.java,v 1.1 2005/08/24 06:43:34 rickknowles Exp $
39  */

40 public class GzipFilter implements Filter JavaDoc {
41
42     private static final String JavaDoc ACCEPT_ENCODING = "Accept-Encoding";
43     
44     private ServletContext JavaDoc context;
45
46     public void init(FilterConfig JavaDoc config) throws ServletException JavaDoc {
47         this.context = config.getServletContext();
48     }
49
50     public void destroy() {
51         this.context = null;
52     }
53
54     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
55             FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc {
56         Enumeration JavaDoc headers = ((HttpServletRequest JavaDoc) request).getHeaders(ACCEPT_ENCODING);
57         boolean acceptsGzipEncoding = false;
58         while (headers.hasMoreElements() && !acceptsGzipEncoding) {
59             acceptsGzipEncoding = (((String JavaDoc) headers.nextElement()).indexOf("gzip") != -1);
60         }
61         if (acceptsGzipEncoding) {
62             GzipResponseWrapper encodedResponse = new GzipResponseWrapper(
63                     (HttpServletResponse JavaDoc) response, this.context);
64             chain.doFilter(request, encodedResponse);
65             encodedResponse.close();
66         } else {
67             chain.doFilter(request, response);
68         }
69     }
70 }
71
Popular Tags