01: package com.openedit.servlet.gzip;
02:
03: import java.io.IOException;
04: import java.io.OutputStreamWriter;
05: import java.io.PrintWriter;
06:
07: import javax.servlet.ServletOutputStream;
08: import javax.servlet.http.HttpServletResponse;
09: import javax.servlet.http.HttpServletResponseWrapper;
10:
11: public class GzipResponseWrapper extends HttpServletResponseWrapper {
12: protected HttpServletResponse origResponse = null;
13: protected ServletOutputStream stream = null;
14: protected PrintWriter writer = null;
15:
16: public GzipResponseWrapper(HttpServletResponse response) {
17: super (response);
18: origResponse = response;
19: }
20:
21: public ServletOutputStream createOutputStream() throws IOException {
22: return (new GzipResponseStream(origResponse));
23: }
24:
25: public void finishResponse() {
26: try {
27: if (writer != null) {
28: writer.close();
29: } else {
30: if (stream != null) {
31: stream.close();
32: }
33: }
34: } catch (IOException e) {
35: }
36: }
37:
38: public void flushBuffer() throws IOException {
39: stream.flush();
40: }
41:
42: public ServletOutputStream getOutputStream() throws IOException {
43: if (writer != null) {
44: throw new IllegalStateException(
45: "getWriter() has already been called!");
46: }
47:
48: if (stream == null)
49: stream = createOutputStream();
50: return (stream);
51: }
52:
53: public PrintWriter getWriter() throws IOException {
54: if (writer != null) {
55: return (writer);
56: }
57:
58: if (stream != null) {
59: throw new IllegalStateException(
60: "getOutputStream() has already been called!");
61: }
62:
63: stream = createOutputStream();
64: // BUG FIX 2003-12-01 Reuse content's encoding, don't assume UTF-8
65: writer = new PrintWriter(new OutputStreamWriter(stream,
66: origResponse.getCharacterEncoding()));
67: return (writer);
68: }
69:
70: public void setContentLength(int length) {
71: }
72: }
|