|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ResponseContent.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
* Copyright (c) 2002-2003 by OpenSymphony
|
|
3 |
* All rights reserved.
|
|
4 |
*/
|
|
5 |
package com.opensymphony.oscache.web.filter;
|
|
6 |
|
|
7 |
import java.io.*;
|
|
8 |
|
|
9 |
import java.util.Locale;
|
|
10 |
|
|
11 |
import javax.servlet.ServletResponse;
|
|
12 |
import javax.servlet.http.HttpServletResponse;
|
|
13 |
|
|
14 |
/**
|
|
15 |
* Holds the servlet response in a byte array so that it can be held
|
|
16 |
* in the cache (and, since this class is serializable, optionally
|
|
17 |
* persisted to disk).
|
|
18 |
*
|
|
19 |
* @version $Revision: 1.1 $
|
|
20 |
* @author <a href="mailto:sergek@lokitech.com">Serge Knystautas</a>
|
|
21 |
*/
|
|
22 |
public class ResponseContent implements Serializable { |
|
23 |
private transient ByteArrayOutputStream bout = new ByteArrayOutputStream(1000); |
|
24 |
private Locale locale = null; |
|
25 |
private String contentType = null; |
|
26 |
private byte[] content = null; |
|
27 |
private long lastModified = -1; |
|
28 |
|
|
29 |
/**
|
|
30 |
* Set the content type. We capture this so that when we serve this
|
|
31 |
* data from cache, we can set the correct content type on the response.
|
|
32 |
*/
|
|
33 | 0 |
public void setContentType(String value) { |
34 | 0 |
contentType = value; |
35 |
} |
|
36 |
|
|
37 | 0 |
public long getLastModified() { |
38 | 0 |
return lastModified;
|
39 |
} |
|
40 |
|
|
41 | 0 |
public void setLastModified(long value) { |
42 | 0 |
lastModified = value; |
43 |
} |
|
44 |
|
|
45 |
/**
|
|
46 |
* Set the Locale. We capture this so that when we serve this data from
|
|
47 |
* cache, we can set the correct locale on the response.
|
|
48 |
*/
|
|
49 | 0 |
public void setLocale(Locale value) { |
50 | 0 |
locale = value; |
51 |
} |
|
52 |
|
|
53 |
/**
|
|
54 |
* Get an output stream. This is used by the {@link SplitServletOutputStream}
|
|
55 |
* to capture the original (uncached) response into a byte array.
|
|
56 |
*/
|
|
57 | 0 |
public OutputStream getOutputStream() {
|
58 | 0 |
return bout;
|
59 |
} |
|
60 |
|
|
61 |
/**
|
|
62 |
* Gets the size of this cached content.
|
|
63 |
*
|
|
64 |
* @return The size of the content, in bytes. If no content
|
|
65 |
* exists, this method returns <code>-1</code>.
|
|
66 |
*/
|
|
67 | 0 |
public int getSize() { |
68 | 0 |
return (content != null) ? content.length : (-1); |
69 |
} |
|
70 |
|
|
71 |
/**
|
|
72 |
* Called once the response has been written in its entirety. This
|
|
73 |
* method commits the response output stream by converting the output
|
|
74 |
* stream into a byte array.
|
|
75 |
*/
|
|
76 | 0 |
public void commit() { |
77 | 0 |
content = bout.toByteArray(); |
78 |
} |
|
79 |
|
|
80 |
/**
|
|
81 |
* Writes this cached data out to the supplied <code>ServletResponse</code>.
|
|
82 |
*
|
|
83 |
* @param response The servlet response to output the cached content to.
|
|
84 |
* @throws IOException
|
|
85 |
*/
|
|
86 | 0 |
public void writeTo(ServletResponse response) throws IOException { |
87 |
//Send the content type and data to this response
|
|
88 | 0 |
if (contentType != null) { |
89 | 0 |
response.setContentType(contentType); |
90 |
} |
|
91 |
|
|
92 | 0 |
if (response instanceof HttpServletResponse) { |
93 | 0 |
((HttpServletResponse) response).setDateHeader("Last-Modified", lastModified);
|
94 |
} |
|
95 |
|
|
96 | 0 |
response.setContentLength(content.length); |
97 |
|
|
98 | 0 |
if (locale != null) { |
99 | 0 |
response.setLocale(locale); |
100 |
} |
|
101 |
|
|
102 | 0 |
OutputStream out = new BufferedOutputStream(response.getOutputStream());
|
103 | 0 |
out.write(content); |
104 | 0 |
out.flush(); |
105 |
} |
|
106 |
} |
|
107 |
|
|