|
|||||||||||||||||||
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.2.2.3 $
|
|
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 contentEncoding = null; |
|
26 |
private String contentType = null; |
|
27 |
private byte[] content = null; |
|
28 |
private long lastModified = -1; |
|
29 |
|
|
30 |
/**
|
|
31 |
* Set the content type. We capture this so that when we serve this
|
|
32 |
* data from cache, we can set the correct content type on the response.
|
|
33 |
*/
|
|
34 | 0 |
public void setContentType(String value) { |
35 | 0 |
contentType = value; |
36 |
} |
|
37 |
|
|
38 | 0 |
public String getContentEncoding() {
|
39 | 0 |
return contentEncoding;
|
40 |
} |
|
41 |
|
|
42 | 0 |
public void setContentEncoding(String contentEncoding) { |
43 | 0 |
this.contentEncoding = contentEncoding;
|
44 |
} |
|
45 |
|
|
46 | 0 |
public long getLastModified() { |
47 | 0 |
return lastModified;
|
48 |
} |
|
49 |
|
|
50 | 0 |
public void setLastModified(long value) { |
51 | 0 |
lastModified = value; |
52 |
} |
|
53 |
|
|
54 |
/**
|
|
55 |
* Set the Locale. We capture this so that when we serve this data from
|
|
56 |
* cache, we can set the correct locale on the response.
|
|
57 |
*/
|
|
58 | 0 |
public void setLocale(Locale value) { |
59 | 0 |
locale = value; |
60 |
} |
|
61 |
|
|
62 |
/**
|
|
63 |
* Get an output stream. This is used by the {@link SplitServletOutputStream}
|
|
64 |
* to capture the original (uncached) response into a byte array.
|
|
65 |
*/
|
|
66 | 0 |
public OutputStream getOutputStream() {
|
67 | 0 |
return bout;
|
68 |
} |
|
69 |
|
|
70 |
/**
|
|
71 |
* Gets the size of this cached content.
|
|
72 |
*
|
|
73 |
* @return The size of the content, in bytes. If no content
|
|
74 |
* exists, this method returns <code>-1</code>.
|
|
75 |
*/
|
|
76 | 0 |
public int getSize() { |
77 | 0 |
return (content != null) ? content.length : (-1); |
78 |
} |
|
79 |
|
|
80 |
/**
|
|
81 |
* Called once the response has been written in its entirety. This
|
|
82 |
* method commits the response output stream by converting the output
|
|
83 |
* stream into a byte array.
|
|
84 |
*/
|
|
85 | 0 |
public void commit() { |
86 | 0 |
content = bout.toByteArray(); |
87 |
} |
|
88 |
|
|
89 |
/**
|
|
90 |
* Writes this cached data out to the supplied <code>ServletResponse</code>.
|
|
91 |
*
|
|
92 |
* @param response The servlet response to output the cached content to.
|
|
93 |
* @throws IOException
|
|
94 |
*/
|
|
95 | 0 |
public void writeTo(ServletResponse response) throws IOException { |
96 |
//Send the content type and data to this response
|
|
97 | 0 |
if (contentType != null) { |
98 | 0 |
response.setContentType(contentType); |
99 |
} |
|
100 |
|
|
101 | 0 |
if ((lastModified != -1) && (response instanceof HttpServletResponse)) { |
102 | 0 |
((HttpServletResponse) response).setDateHeader(CacheFilter.HEADER_LAST_MODIFIED, lastModified); |
103 |
} |
|
104 |
|
|
105 | 0 |
response.setContentLength(content.length); |
106 |
|
|
107 | 0 |
if (locale != null) { |
108 | 0 |
response.setLocale(locale); |
109 |
} |
|
110 |
|
|
111 | 0 |
OutputStream out = new BufferedOutputStream(response.getOutputStream());
|
112 | 0 |
out.write(content); |
113 | 0 |
out.flush(); |
114 |
} |
|
115 |
} |
|
116 |
|
|