1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| package com.opensymphony.oscache.web.filter; |
6 |
| |
7 |
| import java.io.*; |
8 |
| |
9 |
| import java.util.Locale; |
10 |
| import java.util.zip.GZIPInputStream; |
11 |
| |
12 |
| import javax.servlet.ServletResponse; |
13 |
| import javax.servlet.http.HttpServletResponse; |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| public class ResponseContent implements Serializable { |
24 |
| private transient ByteArrayOutputStream bout = new ByteArrayOutputStream(1000); |
25 |
| private Locale locale = null; |
26 |
| private String contentEncoding = null; |
27 |
| private String contentType = null; |
28 |
| private byte[] content = null; |
29 |
| private long expires = Long.MAX_VALUE; |
30 |
| private long lastModified = -1; |
31 |
| |
32 |
0
| public String getContentType() {
|
33 |
0
| return contentType;
|
34 |
| } |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
0
| public void setContentType(String value) {
|
41 |
0
| contentType = value;
|
42 |
| } |
43 |
| |
44 |
0
| public long getLastModified() {
|
45 |
0
| return lastModified;
|
46 |
| } |
47 |
| |
48 |
0
| public void setLastModified(long value) {
|
49 |
0
| lastModified = value;
|
50 |
| } |
51 |
| |
52 |
0
| public String getContentEncoding() {
|
53 |
0
| return contentEncoding;
|
54 |
| } |
55 |
| |
56 |
0
| public void setContentEncoding(String contentEncoding) {
|
57 |
0
| this.contentEncoding = contentEncoding;
|
58 |
| } |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
0
| public void setLocale(Locale value) {
|
65 |
0
| locale = value;
|
66 |
| } |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
0
| public long getExpires() {
|
72 |
0
| return expires;
|
73 |
| } |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
0
| public void setExpires(long value) {
|
80 |
0
| expires = value;
|
81 |
| } |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
0
| public OutputStream getOutputStream() {
|
88 |
0
| return bout;
|
89 |
| } |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
0
| public int getSize() {
|
98 |
0
| return (content != null) ? content.length : (-1);
|
99 |
| } |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
0
| public void commit() {
|
107 |
0
| content = bout.toByteArray();
|
108 |
| } |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
0
| public void writeTo(ServletResponse response) throws IOException {
|
117 |
0
| writeTo(response, false, false);
|
118 |
| } |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
0
| public void writeTo(ServletResponse response, boolean fragment, boolean acceptsGZip) throws IOException {
|
129 |
| |
130 |
0
| if (contentType != null) {
|
131 |
0
| response.setContentType(contentType);
|
132 |
| } |
133 |
| |
134 |
0
| if (fragment) {
|
135 |
| |
136 |
0
| acceptsGZip = false;
|
137 |
| } else { |
138 |
| |
139 |
0
| if (response instanceof HttpServletResponse) {
|
140 |
0
| HttpServletResponse httpResponse = (HttpServletResponse) response;
|
141 |
| |
142 |
| |
143 |
0
| if (lastModified != -1) {
|
144 |
0
| httpResponse.setDateHeader(CacheFilter.HEADER_LAST_MODIFIED, lastModified);
|
145 |
| } |
146 |
| |
147 |
| |
148 |
0
| if (expires != Long.MAX_VALUE) {
|
149 |
0
| httpResponse.setDateHeader(CacheFilter.HEADER_EXPIRES, expires);
|
150 |
| } |
151 |
| } |
152 |
| } |
153 |
| |
154 |
0
| if (locale != null) {
|
155 |
0
| response.setLocale(locale);
|
156 |
| } |
157 |
| |
158 |
0
| OutputStream out = new BufferedOutputStream(response.getOutputStream());
|
159 |
| |
160 |
0
| if (isContentGZiped()) {
|
161 |
0
| if (acceptsGZip) {
|
162 |
0
| ((HttpServletResponse) response).addHeader(CacheFilter.HEADER_CONTENT_ENCODING, "gzip");
|
163 |
0
| response.setContentLength(content.length);
|
164 |
0
| out.write(content);
|
165 |
| } else { |
166 |
| |
167 |
0
| ByteArrayInputStream bais = new ByteArrayInputStream(content);
|
168 |
0
| GZIPInputStream zis = new GZIPInputStream(bais);
|
169 |
| |
170 |
0
| ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
171 |
0
| int numBytesRead = 0;
|
172 |
0
| byte[] tempBytes = new byte[4196];
|
173 |
| |
174 |
0
| while ((numBytesRead = zis.read(tempBytes, 0, tempBytes.length)) != -1) {
|
175 |
0
| baos.write(tempBytes, 0, numBytesRead);
|
176 |
| } |
177 |
| |
178 |
0
| byte[] result = baos.toByteArray();
|
179 |
| |
180 |
0
| response.setContentLength(result.length);
|
181 |
0
| out.write(result);
|
182 |
| } |
183 |
| } else { |
184 |
| |
185 |
| |
186 |
0
| response.setContentLength(content.length);
|
187 |
0
| out.write(content);
|
188 |
| } |
189 |
0
| out.flush();
|
190 |
| } |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
0
| public boolean isContentGZiped() {
|
197 |
0
| return "gzip".equals(contentEncoding);
|
198 |
| } |
199 |
| } |