1
22
23 package com.liferay.filters.compression;
24
25 import java.io.IOException;
26 import java.io.OutputStreamWriter;
27 import java.io.PrintWriter;
28
29 import javax.servlet.ServletOutputStream;
30 import javax.servlet.http.HttpServletResponse;
31 import javax.servlet.http.HttpServletResponseWrapper;
32
33
40 public class CompressionResponse extends HttpServletResponseWrapper {
41
42 public CompressionResponse(HttpServletResponse res) {
43 super(res);
44
45 _res = res;
46 }
47
48 public void finishResponse() {
49 try {
50 if (_writer != null) {
51 _writer.close();
52 }
53 else if (_stream != null) {
54 _stream.close();
55 }
56 }
57 catch (IOException e) {
58 }
59 }
60
61 public void flushBuffer() throws IOException {
62 if (_stream != null) {
63 _stream.flush();
64 }
65 }
66
67 public ServletOutputStream getOutputStream() throws IOException {
68 if (_writer != null) {
69 throw new IllegalStateException();
70 }
71
72 if (_stream == null) {
73 _stream = _createOutputStream();
74 }
75
76 return _stream;
77 }
78
79 public PrintWriter getWriter() throws IOException {
80 if (_writer != null) {
81 return _writer;
82 }
83
84 if (_stream != null) {
85 throw new IllegalStateException();
86 }
87
88 _stream = _createOutputStream();
89
90 _writer = new PrintWriter(new OutputStreamWriter(
91 _stream, CompressionFilter.ENCODING));
93
94 return _writer;
95 }
96
97 private ServletOutputStream _createOutputStream() throws IOException {
98 return new CompressionStream(_res);
99 }
100
101 private HttpServletResponse _res = null;
102 private ServletOutputStream _stream = null;
103 private PrintWriter _writer = null;
104
105 }