KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > servlet > filter > GZipFilter


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar.servlet.filter;
27
28
29 import javax.servlet.FilterChain JavaDoc;
30 import javax.servlet.ServletRequest JavaDoc;
31 import javax.servlet.ServletResponse JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import java.io.ByteArrayOutputStream JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.util.zip.GZIPOutputStream JavaDoc;
36
37
38 public class GZipFilter extends GenericFilter
39 {
40     public GZipFilter()
41     {
42     }
43
44
45     public void doFilter(final ServletRequest JavaDoc request, final ServletResponse JavaDoc response, FilterChain JavaDoc chain) throws java.io.IOException JavaDoc, javax.servlet.ServletException JavaDoc
46     {
47         GenericResponseWrapper wrapper = new GenericResponseWrapper((HttpServletResponse JavaDoc)response);
48         chain.doFilter(request, wrapper);
49         OutputStream JavaDoc out = response.getOutputStream();
50
51
52         if (!request.getProtocol().equals("HTTP/1.0") && !cached(wrapper) && !included(request))
53         {
54             ByteArrayOutputStream JavaDoc compressed = new ByteArrayOutputStream JavaDoc();
55             GZIPOutputStream JavaDoc gzout = new GZIPOutputStream JavaDoc(compressed);
56             gzout.write(wrapper.getData());
57             gzout.close();
58
59
60             HttpServletResponse JavaDoc httpResponse = (HttpServletResponse JavaDoc)response;
61             httpResponse.setHeader("Content-Encoding","gzip");
62             out.write(compressed.toByteArray());
63             response.setContentLength(compressed.size());
64         }
65         else
66         {
67             out.write(wrapper.getData());
68         }
69     }
70
71
72     protected boolean included(ServletRequest JavaDoc request)
73     {
74         return null != request.getAttribute("javax.servlet.include.request_uri");
75     }
76
77
78     protected boolean cached(GenericResponseWrapper wrapper)
79     {
80         return !(wrapper.getData().length > 0);
81     }
82 }
Popular Tags