/
GZIPResponseStream.java
177 lines (142 loc) · 4.95 KB
/
GZIPResponseStream.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
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.web.filter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
/**
* Wraps Response Stream for GZipFilter
*
* @author Matt Raible
* @version $Revision: 1.3 $ $Date: 2004/05/16 02:17:00 $
*/
public class GZIPResponseStream extends ServletOutputStream {
// abstraction of the output stream used for compression
protected OutputStream bufferedOutput;
// state keeping variable for if close() has been called
protected boolean closed;
// reference to original response
protected HttpServletResponse response;
// reference to the output stream to the client's browser
protected ServletOutputStream output;
// default size of the in-memory buffer
private int bufferSize = 50000;
public GZIPResponseStream(HttpServletResponse response) throws IOException {
super();
closed = false;
this.response = response;
this.output = response.getOutputStream();
bufferedOutput = new ByteArrayOutputStream();
}
@Override
public void close() throws IOException {
// verify the stream is yet to be closed
if (closed) {
throw new IOException("This output stream has already been closed");
}
// if we buffered everything in memory, gzip it
if (bufferedOutput instanceof ByteArrayOutputStream) {
// get the content
ByteArrayOutputStream baos = (ByteArrayOutputStream) bufferedOutput;
// prepare a gzip stream
ByteArrayOutputStream compressedContent = new ByteArrayOutputStream();
GZIPOutputStream gzipstream = new GZIPOutputStream(compressedContent);
byte[] bytes = baos.toByteArray();
gzipstream.write(bytes);
gzipstream.finish();
// get the compressed content
byte[] compressedBytes = compressedContent.toByteArray();
// set appropriate HTTP headers
response.setContentLength(compressedBytes.length);
response.addHeader("Content-Encoding", "gzip");
output.write(compressedBytes);
output.flush();
output.close();
closed = true;
}
// if things were not buffered in memory, finish the GZIP stream and response
else if (bufferedOutput instanceof GZIPOutputStream) {
// cast to appropriate type
GZIPOutputStream gzipstream = (GZIPOutputStream) bufferedOutput;
// finish the compression
gzipstream.finish();
// finish the response
output.flush();
output.close();
closed = true;
}
}
@Override
public void flush() throws IOException {
if (closed) {
throw new IOException("Cannot flush a closed output stream");
}
bufferedOutput.flush();
}
@Override
public void write(int b) throws IOException {
if (closed) {
throw new IOException("Cannot write to a closed output stream");
}
// make sure we aren't over the buffer's limit
checkBufferSize(1);
// write the byte to the temporary output
bufferedOutput.write((byte) b);
}
private void checkBufferSize(int length) throws IOException {
// check if we are buffering too large of a file
if (bufferedOutput instanceof ByteArrayOutputStream) {
ByteArrayOutputStream baos = (ByteArrayOutputStream) bufferedOutput;
if ((baos.size() + length) > bufferSize) {
// files too large to keep in memory are sent to the client without Content-Length specified
response.addHeader("Content-Encoding", "gzip");
// get existing bytes
byte[] bytes = baos.toByteArray();
// make new gzip stream using the response output stream
GZIPOutputStream gzipstream = new GZIPOutputStream(output);
gzipstream.write(bytes);
// we are no longer buffering, send content via gzipstream
bufferedOutput = gzipstream;
}
}
}
@Override
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
if (closed) {
throw new IOException("Cannot write to a closed output stream");
}
// make sure we aren't over the buffer's limit
checkBufferSize(len);
// write the content to the buffer
bufferedOutput.write(b, off, len);
}
public boolean closed() {
return this.closed;
}
public void reset() {
//noop
}
@Override
public boolean isReady() {
throw new UnsupportedOperationException("Asynchonous operation is not supported.");
}
@Override
public void setWriteListener(WriteListener writeListener) {
throw new UnsupportedOperationException("Asynchonous operation is not supported.");
}
}