2
* Copyright (c) 2002-2003 by OpenSymphony
5
package com.opensymphony.oscache.web.filter;
9
import java.util.Locale;
11
import javax.servlet.ServletResponse;
14
* Holds the servlet response in a byte array so that it can be held
15
* in the cache (and, since this class is serializable, optionally
18
* @version $Revision: 1.1 $
19
* @author <a href="mailto:sergek@lokitech.com">Serge Knystautas</a>
21
public class ResponseContent implements Serializable {
22
private transient ByteArrayOutputStream bout = new ByteArrayOutputStream(1000);
23
private Locale locale = null;
24
private String contentType = null;
25
private byte[] content = null;
28
* Set the content type. We capture this so that when we serve this
29
* data from cache, we can set the correct content type on the response.
31
public void setContentType(String value) {
36
* Set the Locale. We capture this so that when we serve this data from
37
* cache, we can set the correct locale on the response.
39
public void setLocale(Locale value) {
44
* Get an output stream. This is used by the {@link SplitServletOutputStream}
45
* to capture the original (uncached) response into a byte array.
47
public OutputStream getOutputStream() {
52
* Gets the size of this cached content.
54
* @return The size of the content, in bytes. If no content
55
* exists, this method returns <code>-1</code>.
57
public int getSize() {
58
return (content != null) ? content.length : (-1);
62
* Called once the response has been written in its entirety. This
63
* method commits the response output stream by converting the output
64
* stream into a byte array.
66
public void commit() {
67
content = bout.toByteArray();
71
* Writes this cached data out to the supplied <code>ServletResponse</code>.
73
* @param response The servlet response to output the cached content to.
76
public void writeTo(ServletResponse response) throws IOException {
77
//Send the content type and data to this response
78
if (contentType != null) {
79
response.setContentType(contentType);
82
response.setContentLength(content.length);
85
response.setLocale(locale);
88
OutputStream out = new BufferedOutputStream(response.getOutputStream());