1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| package com.opensymphony.oscache.web.filter; |
6 |
| |
7 |
| import org.apache.commons.logging.Log; |
8 |
| import org.apache.commons.logging.LogFactory; |
9 |
| |
10 |
| import java.io.IOException; |
11 |
| import java.io.OutputStreamWriter; |
12 |
| import java.io.PrintWriter; |
13 |
| |
14 |
| import java.util.Locale; |
15 |
| |
16 |
| import javax.servlet.ServletOutputStream; |
17 |
| import javax.servlet.http.HttpServletResponse; |
18 |
| import javax.servlet.http.HttpServletResponseWrapper; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| public class CacheHttpServletResponseWrapper extends HttpServletResponseWrapper { |
27 |
| private final Log log = LogFactory.getLog(this.getClass()); |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| private PrintWriter cachedWriter; |
34 |
| private ResponseContent result = null; |
35 |
| private SplitServletOutputStream cacheOut = null; |
36 |
| private boolean fragment = false; |
37 |
| private int status = SC_OK; |
38 |
| private long expires = CacheFilter.EXPIRES_ON; |
39 |
| private long lastModified = CacheFilter.LAST_MODIFIED_INITIAL; |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
0
| public CacheHttpServletResponseWrapper(HttpServletResponse response) {
|
47 |
0
| this(response, false, Long.MAX_VALUE, CacheFilter.EXPIRES_ON, CacheFilter.LAST_MODIFIED_INITIAL);
|
48 |
| } |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
0
| public CacheHttpServletResponseWrapper(HttpServletResponse response, boolean fragment, long time, long lastModified, long expires) {
|
60 |
0
| super(response);
|
61 |
0
| result = new ResponseContent();
|
62 |
0
| this.fragment = fragment;
|
63 |
0
| this.expires = expires;
|
64 |
0
| this.lastModified = lastModified;
|
65 |
| |
66 |
| |
67 |
0
| if (!fragment) {
|
68 |
| |
69 |
0
| if (lastModified == CacheFilter.LAST_MODIFIED_INITIAL) {
|
70 |
0
| long current = System.currentTimeMillis() / 1000;
|
71 |
0
| result.setLastModified(current * 1000);
|
72 |
0
| super.setDateHeader(CacheFilter.HEADER_LAST_MODIFIED, result.getLastModified());
|
73 |
| } |
74 |
| |
75 |
0
| if (expires == CacheFilter.EXPIRES_TIME) {
|
76 |
0
| result.setExpires(result.getLastModified() + time);
|
77 |
0
| super.setDateHeader(CacheFilter.HEADER_EXPIRES, result.getExpires());
|
78 |
| } |
79 |
| } |
80 |
| } |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
0
| public ResponseContent getContent() {
|
88 |
| |
89 |
0
| result.commit();
|
90 |
| |
91 |
| |
92 |
0
| return result;
|
93 |
| } |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
0
| public void setContentType(String value) {
|
101 |
0
| if (log.isDebugEnabled()) {
|
102 |
0
| log.debug("ContentType: " + value);
|
103 |
| } |
104 |
| |
105 |
0
| super.setContentType(value);
|
106 |
0
| result.setContentType(value);
|
107 |
| } |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
0
| public void setDateHeader(String name, long value) {
|
116 |
0
| if (log.isDebugEnabled()) {
|
117 |
0
| log.debug("dateheader: " + name + ": " + value);
|
118 |
| } |
119 |
| |
120 |
| |
121 |
0
| if ((lastModified != CacheFilter.LAST_MODIFIED_OFF) && (CacheFilter.HEADER_LAST_MODIFIED.equalsIgnoreCase(name))) {
|
122 |
0
| if (!fragment) {
|
123 |
0
| result.setLastModified(value);
|
124 |
| } |
125 |
| } |
126 |
| |
127 |
| |
128 |
0
| if ((expires != CacheFilter.EXPIRES_OFF) && (CacheFilter.HEADER_EXPIRES.equalsIgnoreCase(name))) {
|
129 |
0
| if (!fragment) {
|
130 |
0
| result.setExpires(value);
|
131 |
| } |
132 |
| } |
133 |
| |
134 |
0
| super.setDateHeader(name, value);
|
135 |
| } |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
0
| public void addDateHeader(String name, long value) {
|
144 |
0
| if (log.isDebugEnabled()) {
|
145 |
0
| log.debug("dateheader: " + name + ": " + value);
|
146 |
| } |
147 |
| |
148 |
| |
149 |
0
| if ((lastModified != CacheFilter.LAST_MODIFIED_OFF) && (CacheFilter.HEADER_LAST_MODIFIED.equalsIgnoreCase(name))) {
|
150 |
0
| if (!fragment) {
|
151 |
0
| result.setLastModified(value);
|
152 |
| } |
153 |
| } |
154 |
| |
155 |
| |
156 |
0
| if ((expires != CacheFilter.EXPIRES_OFF) && (CacheFilter.HEADER_EXPIRES.equalsIgnoreCase(name))) {
|
157 |
0
| if (!fragment) {
|
158 |
0
| result.setExpires(value);
|
159 |
| } |
160 |
| } |
161 |
| |
162 |
0
| super.addDateHeader(name, value);
|
163 |
| } |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
0
| public void setHeader(String name, String value) {
|
172 |
0
| if (log.isDebugEnabled()) {
|
173 |
0
| log.debug("header: " + name + ": " + value);
|
174 |
| } |
175 |
| |
176 |
0
| if (CacheFilter.HEADER_CONTENT_TYPE.equalsIgnoreCase(name)) {
|
177 |
0
| result.setContentType(value);
|
178 |
| } |
179 |
| |
180 |
0
| if (CacheFilter.HEADER_CONTENT_ENCODING.equalsIgnoreCase(name)) {
|
181 |
0
| result.setContentEncoding(value);
|
182 |
| } |
183 |
| |
184 |
0
| super.setHeader(name, value);
|
185 |
| } |
186 |
| |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
0
| public void addHeader(String name, String value) {
|
194 |
0
| if (log.isDebugEnabled()) {
|
195 |
0
| log.debug("header: " + name + ": " + value);
|
196 |
| } |
197 |
| |
198 |
0
| if (CacheFilter.HEADER_CONTENT_TYPE.equalsIgnoreCase(name)) {
|
199 |
0
| result.setContentType(value);
|
200 |
| } |
201 |
| |
202 |
0
| if (CacheFilter.HEADER_CONTENT_ENCODING.equalsIgnoreCase(name)) {
|
203 |
0
| result.setContentEncoding(value);
|
204 |
| } |
205 |
| |
206 |
0
| super.addHeader(name, value);
|
207 |
| } |
208 |
| |
209 |
| |
210 |
| |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
0
| public void setIntHeader(String name, int value) {
|
216 |
0
| if (log.isDebugEnabled()) {
|
217 |
0
| log.debug("intheader: " + name + ": " + value);
|
218 |
| } |
219 |
| |
220 |
0
| super.setIntHeader(name, value);
|
221 |
| } |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
0
| public void setStatus(int status) {
|
229 |
0
| super.setStatus(status);
|
230 |
0
| this.status = status;
|
231 |
| } |
232 |
| |
233 |
| |
234 |
| |
235 |
| |
236 |
| |
237 |
| |
238 |
0
| public void sendError(int status, String string) throws IOException {
|
239 |
0
| super.sendError(status, string);
|
240 |
0
| this.status = status;
|
241 |
| } |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
| |
247 |
| |
248 |
0
| public void sendError(int status) throws IOException {
|
249 |
0
| super.sendError(status);
|
250 |
0
| this.status = status;
|
251 |
| } |
252 |
| |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
0
| public void setStatus(int status, String string) {
|
259 |
0
| super.setStatus(status, string);
|
260 |
0
| this.status = status;
|
261 |
| } |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
| |
267 |
| |
268 |
0
| public void sendRedirect(String location) throws IOException {
|
269 |
0
| this.status = SC_MOVED_TEMPORARILY;
|
270 |
0
| super.sendRedirect(location);
|
271 |
| } |
272 |
| |
273 |
| |
274 |
| |
275 |
| |
276 |
0
| public int getStatus() {
|
277 |
0
| return status;
|
278 |
| } |
279 |
| |
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
0
| public void setLocale(Locale value) {
|
286 |
0
| super.setLocale(value);
|
287 |
0
| result.setLocale(value);
|
288 |
| } |
289 |
| |
290 |
| |
291 |
| |
292 |
| |
293 |
| |
294 |
| |
295 |
0
| public ServletOutputStream getOutputStream() throws IOException {
|
296 |
| |
297 |
0
| if (cacheOut == null) {
|
298 |
0
| cacheOut = new SplitServletOutputStream(result.getOutputStream(), super.getOutputStream());
|
299 |
| } |
300 |
| |
301 |
0
| return cacheOut;
|
302 |
| } |
303 |
| |
304 |
| |
305 |
| |
306 |
| |
307 |
| |
308 |
| |
309 |
0
| public PrintWriter getWriter() throws IOException {
|
310 |
0
| if (cachedWriter == null) {
|
311 |
0
| String encoding = getCharacterEncoding();
|
312 |
0
| if (encoding != null) {
|
313 |
0
| cachedWriter = new PrintWriter(new OutputStreamWriter(getOutputStream(), encoding));
|
314 |
| } else { |
315 |
0
| cachedWriter = new PrintWriter(new OutputStreamWriter(getOutputStream()));
|
316 |
| } |
317 |
| } |
318 |
| |
319 |
0
| return cachedWriter;
|
320 |
| } |
321 |
| |
322 |
0
| public void flushBuffer() throws IOException {
|
323 |
0
| super.flushBuffer();
|
324 |
| |
325 |
0
| if (cacheOut != null) {
|
326 |
0
| cacheOut.flush();
|
327 |
| } |
328 |
| |
329 |
0
| if (cachedWriter != null) {
|
330 |
0
| cachedWriter.flush();
|
331 |
| } |
332 |
| } |
333 |
| } |