/
CompressionServletResponseWrapper.java
224 lines (184 loc) · 7.17 KB
/
CompressionServletResponseWrapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jumpmind.symmetric.web.compression;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.zip.Deflater;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Implementation of <b>HttpServletResponseWrapper</b> that works with the
* CompressionServletResponseStream implementation..
*
* This package is derived from the Jakarta <a
* href="http://jakarta.apache.org/tomcat">Tomcat</a> examples compression
* filter and is distributed in SymmetricDS for convenience.
*
* @author Amy Roh
* @author Dmitri Valdin
* @version $Revision: 496190 $, $Date: 2007-01-14 16:21:45 -0700 (Sun, 14 Jan
* 2007) $
*/
public class CompressionServletResponseWrapper extends HttpServletResponseWrapper {
static final Log logger = LogFactory.getLog(CompressionServletResponseWrapper.class);
int compressionLevel = Deflater.DEFAULT_COMPRESSION;
int compressionStrategy = Deflater.DEFAULT_STRATEGY;
/**
* Calls the parent constructor which creates a ServletResponse adaptor
* wrapping the given response object.
*/
public CompressionServletResponseWrapper(HttpServletResponse response, int compressionLevel, int compressionStrategy) {
super(response);
this.compressionLevel = compressionLevel;
this.compressionStrategy = compressionStrategy;
origResponse = response;
if (logger.isDebugEnabled()) {
logger.debug("CompressionServletResponseWrapper constructor gets called");
}
}
/**
* Original response
*/
protected HttpServletResponse origResponse = null;
/**
* Descriptive information about this Response implementation.
*/
protected static final String info = "CompressionServletResponseWrapper";
/**
* The ServletOutputStream that has been returned by
* <code>getOutputStream()</code>, if any.
*/
protected ServletOutputStream stream = null;
/**
* The PrintWriter that has been returned by <code>getWriter()</code>, if
* any.
*/
protected PrintWriter writer = null;
/**
* Content type
*/
protected String contentType = null;
// --------------------------------------------------------- Public Methods
/**
* Set content type
*/
public void setContentType(String contentType) {
if (logger.isDebugEnabled()) {
logger.debug("setContentType to " + contentType);
}
this.contentType = contentType;
origResponse.setContentType(contentType);
}
/**
* Create and return a ServletOutputStream to write the content associated
* with this Response.
*
* @exception IOException
* if an input/output error occurs
*/
public ServletOutputStream createOutputStream() throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("createOutputStream gets called");
}
CompressionResponseStream stream = new CompressionResponseStream(origResponse, compressionLevel, compressionStrategy);
return stream;
}
/**
* Finish a response.
*/
public void finishResponse() {
try {
if (writer != null) {
writer.close();
} else {
if (stream != null)
stream.close();
}
} catch (IOException e) {
}
}
// ------------------------------------------------ ServletResponse Methods
/**
* Flush the buffer and commit this response.
*
* @exception IOException
* if an input/output error occurs
*/
public void flushBuffer() throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("flush buffer @ CompressionServletResponseWrapper");
}
((CompressionResponseStream) stream).flush();
}
/**
* Return the servlet output stream associated with this Response.
*
* @exception IllegalStateException
* if <code>getWriter</code> has already been called
* for this response
* @exception IOException
* if an input/output error occurs
*/
public ServletOutputStream getOutputStream() throws IOException {
if (writer != null)
throw new IllegalStateException("getWriter() has already been called for this response");
if (stream == null)
stream = createOutputStream();
if (logger.isDebugEnabled()) {
logger.debug("stream is set to " + stream + " in getOutputStream");
}
return (stream);
}
/**
* Return the writer associated with this Response.
*
* @exception IllegalStateException
* if <code>getOutputStream</code> has already been
* called for this response
* @exception IOException
* if an input/output error occurs
*/
public PrintWriter getWriter() throws IOException {
if (writer != null)
return (writer);
if (stream != null)
throw new IllegalStateException("getOutputStream() has already been called for this response");
stream = createOutputStream();
if (logger.isDebugEnabled()) {
logger.debug("stream is set to " + stream + " in getWriter");
}
// String charset = getCharsetFromContentType(contentType);
String charEnc = origResponse.getCharacterEncoding();
if (logger.isDebugEnabled()) {
logger.debug("character encoding is " + charEnc);
}
// HttpServletResponse.getCharacterEncoding() shouldn't return null
// according the spec, so feel free to remove that "if"
if (charEnc != null) {
writer = new PrintWriter(new OutputStreamWriter(stream, charEnc));
} else {
writer = new PrintWriter(stream);
}
return (writer);
}
public void setContentLength(int length) {
}
}