KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > filters > gzip > GzipResponseWrapper


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.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStreamWriter JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.util.zip.GZIPOutputStream JavaDoc;
25
26 import javax.servlet.ServletContext JavaDoc;
27 import javax.servlet.ServletOutputStream JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
30
31 /**
32  * Wraps the response object with an output buffer, and which waits until
33  * the full response is written, after which it commits and writes a
34  * gzip compressed version, with the transfer encoding header set to gzipped.
35  *
36  * @author <a HREF="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
37  * @version $Id: GzipResponseWrapper.java,v 1.2 2005/09/10 06:34:35 rickknowles Exp $
38  */

39 public class GzipResponseWrapper extends HttpServletResponseWrapper JavaDoc {
40
41     private ByteArrayServletOutputStream bufferStream;
42     private PrintWriter JavaDoc bufferWriter;
43     private ServletContext JavaDoc context;
44     
45     public GzipResponseWrapper(HttpServletResponse JavaDoc response, ServletContext JavaDoc context) {
46         super(response);
47         this.context = context;
48         setHeader("Content-Encoding", "gzip");
49     }
50     
51     public void close() throws IOException JavaDoc {
52         
53         if (this.bufferWriter != null) {
54             this.bufferWriter.flush();
55         }
56         
57         // reset content length, and echo out
58
if (this.bufferStream != null) {
59             byte content[] = this.bufferStream.getContent();
60             long startTime = System.currentTimeMillis();
61             
62             int initialContentLength = content.length;
63             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
64             GZIPOutputStream JavaDoc gzip = new GZIPOutputStream JavaDoc(buffer);
65             gzip.write(content);
66             gzip.close();
67             content = buffer.toByteArray();
68             buffer.close();
69             
70             context.log("Gzipped " + initialContentLength + " bytes of " +
71                     "response content to " + content.length + " bytes in " +
72                     (System.currentTimeMillis() - startTime) + "ms");
73             
74             // Set content length and write out
75
super.setContentLength(content.length);
76             ServletOutputStream JavaDoc out = null;
77             try {
78                 out = super.getOutputStream();
79             } catch (IllegalStateException JavaDoc err) {
80                 throw new IOException JavaDoc("Can't use a gzip response wrapper on included files");
81             }
82             out.write(content);
83             out.flush();
84         }
85     }
86     
87     public void setContentLength(int length) {/* do nothing */}
88     
89     /**
90      * Returns a stream wrapper
91      */

92     public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
93         if (this.bufferStream != null) {
94             return this.bufferStream;
95         } else {
96             // Assumes we only map this filter to appropriate objects
97
this.bufferStream = new ByteArrayServletOutputStream();
98             return this.bufferStream;
99         }
100     }
101     
102     /**
103      * Returns a writer wrapper
104      */

105     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
106         if (this.bufferWriter != null) {
107             return this.bufferWriter;
108         } else {
109             // this is actually against servlet spec, but return a
110
// writer only, if the stream has already been requested
111
if (this.bufferStream == null) {
112                 this.bufferStream = new ByteArrayServletOutputStream();
113             }
114             this.bufferWriter = new PrintWriter JavaDoc(
115                     new OutputStreamWriter JavaDoc(this.bufferStream,
116                             getCharacterEncoding()), true);
117             return this.bufferWriter;
118         }
119     }
120 }
121
Popular Tags