Holds the servlet response in a byte array so that it can be held
in the cache (and, since this class is serializable, optionally
persisted to disk).
| Method from com.opensymphony.oscache.web.filter.ResponseContent Detail: |
public void commit() {
if (bout != null) {
content = bout.toByteArray();
bout = null;
}
}
Called once the response has been written in its entirety. This
method commits the response output stream by converting the output
stream into a byte array. |
public String getContentEncoding() {
return contentEncoding;
}
|
public String getContentType() {
return contentType;
}
|
public long getExpires() {
return expires;
}
|
public long getLastModified() {
return lastModified;
}
|
public long getMaxAge() {
return maxAge;
}
Returns the max age of the content in miliseconds. If expires header and cache control are
enabled both, both will be equal. |
public OutputStream getOutputStream() {
return bout;
}
Get an output stream. This is used by the SplitServletOutputStream
to capture the original (uncached) response into a byte array. |
public int getSize() {
return (content != null) ? content.length : (-1);
}
Gets the size of this cached content. |
public boolean isContentGZiped() {
return "gzip".equals(contentEncoding);
}
|
public void setContentEncoding(String contentEncoding) {
this.contentEncoding = contentEncoding;
}
|
public void setContentType(String value) {
contentType = value;
}
Set the content type. We capture this so that when we serve this
data from cache, we can set the correct content type on the response. |
public void setExpires(long value) {
expires = value;
}
Sets the expires date and time in miliseconds. |
public void setLastModified(long value) {
lastModified = value;
}
|
public void setLocale(Locale value) {
locale = value;
}
Set the Locale. We capture this so that when we serve this data from
cache, we can set the correct locale on the response. |
public void setMaxAge(long value) {
maxAge = value;
}
Sets the max age date and time in miliseconds. If the parameter is -1, the max-age parameter
won't be set by default in the Cache-Control header. |
public void writeTo(ServletResponse response) throws IOException {
writeTo(response, false, false);
}
Writes this cached data out to the supplied ServletResponse. |
public void writeTo(ServletResponse response,
boolean fragment,
boolean acceptsGZip) throws IOException {
//Send the content type and data to this response
if (contentType != null) {
response.setContentType(contentType);
}
if (fragment) {
// Don't support gzip compression if the content is a fragment of a page
acceptsGZip = false;
} else {
// add special headers for a complete page
if (response instanceof HttpServletResponse) {
HttpServletResponse httpResponse = (HttpServletResponse) response;
// add the last modified header
if (lastModified != -1) {
httpResponse.setDateHeader(CacheFilter.HEADER_LAST_MODIFIED, lastModified);
}
// add the expires header
if (expires != Long.MAX_VALUE) {
httpResponse.setDateHeader(CacheFilter.HEADER_EXPIRES, expires);
}
// add the cache-control header for max-age
if (maxAge == CacheFilter.MAX_AGE_NO_INIT || maxAge == CacheFilter.MAX_AGE_TIME) {
// do nothing
} else if (maxAge > 0) { // set max-age based on life time
long currentMaxAge = maxAge / 1000 - System.currentTimeMillis() / 1000;
if (currentMaxAge < 0) {
currentMaxAge = 0;
}
httpResponse.addHeader(CacheFilter.HEADER_CACHE_CONTROL, "max-age=" + currentMaxAge);
} else {
httpResponse.addHeader(CacheFilter.HEADER_CACHE_CONTROL, "max-age=" + (-maxAge));
}
}
}
if (locale != null) {
response.setLocale(locale);
}
OutputStream out = new BufferedOutputStream(response.getOutputStream());
if (isContentGZiped()) {
if (acceptsGZip) {
((HttpServletResponse) response).addHeader(CacheFilter.HEADER_CONTENT_ENCODING, "gzip");
response.setContentLength(content.length);
out.write(content);
} else {
// client doesn't support, so we have to uncompress it
ByteArrayInputStream bais = new ByteArrayInputStream(content);
GZIPInputStream zis = new GZIPInputStream(bais);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int numBytesRead = 0;
byte[] tempBytes = new byte[4196];
while ((numBytesRead = zis.read(tempBytes, 0, tempBytes.length)) != -1) {
baos.write(tempBytes, 0, numBytesRead);
}
byte[] result = baos.toByteArray();
response.setContentLength(result.length);
out.write(result);
}
} else {
// the content isn't compressed
// regardless if the client browser supports gzip we will just return the content
response.setContentLength(content.length);
out.write(content);
}
out.flush();
}
Writes this cached data out to the supplied ServletResponse. |