CacheServletResponse is a serialized representation of a response
| Constructor: |
public CacheHttpServletResponseWrapper(HttpServletResponse response) {
this(response, false, Long.MAX_VALUE, CacheFilter.EXPIRES_ON, CacheFilter.LAST_MODIFIED_INITIAL, -60);
}
Parameters:
response - The servlet response
|
public CacheHttpServletResponseWrapper(HttpServletResponse response,
boolean fragment,
long time,
long lastModified,
long expires,
long cacheControl) {
super(response);
result = new ResponseContent();
this.fragment = fragment;
this.expires = expires;
this.lastModified = lastModified;
this.cacheControl = cacheControl;
// only set inital values for last modified and expires, when a complete page is cached
if (!fragment) {
// setting a default last modified value based on object creation and remove the millis
if (lastModified == CacheFilter.LAST_MODIFIED_INITIAL) {
long current = System.currentTimeMillis();
current = current - (current % 1000);
result.setLastModified(current);
super.setDateHeader(CacheFilter.HEADER_LAST_MODIFIED, result.getLastModified());
}
// setting the expires value
if (expires == CacheFilter.EXPIRES_TIME) {
result.setExpires(result.getLastModified() + time);
super.setDateHeader(CacheFilter.HEADER_EXPIRES, result.getExpires());
}
// setting the cache control with max-age
if (this.cacheControl == CacheFilter.MAX_AGE_TIME) {
// set the count down
long maxAge = System.currentTimeMillis();
maxAge = maxAge - (maxAge % 1000) + time;
result.setMaxAge(maxAge);
super.addHeader(CacheFilter.HEADER_CACHE_CONTROL, "max-age=" + time / 1000);
} else if (this.cacheControl != CacheFilter.MAX_AGE_NO_INIT) {
result.setMaxAge(this.cacheControl);
super.addHeader(CacheFilter.HEADER_CACHE_CONTROL, "max-age=" + (-this.cacheControl));
} else if (this.cacheControl == CacheFilter.MAX_AGE_NO_INIT ) {
result.setMaxAge(this.cacheControl);
}
}
}
Parameters:
response - The servlet response
fragment - true if the repsonse indicates that it is a fragement of a page
time - the refresh time in millis
lastModified - defines if last modified header will be send, @see CacheFilter
expires - defines if expires header will be send, @see CacheFilter
cacheControl - defines if cache control header will be send, @see CacheFilter
|
| Method from com.opensymphony.oscache.web.filter.CacheHttpServletResponseWrapper Detail: |
public void addDateHeader(String name,
long value) {
if (log.isDebugEnabled()) {
log.debug("dateheader: " + name + ": " + value);
}
// only set the last modified value, if a complete page is cached
if ((lastModified != CacheFilter.LAST_MODIFIED_OFF) && (CacheFilter.HEADER_LAST_MODIFIED.equalsIgnoreCase(name))) {
if (!fragment) {
result.setLastModified(value);
} // TODO should we return now by fragments to avoid putting the header to the response?
}
// implement RFC 2616 14.21 Expires (without max-age)
if ((expires != CacheFilter.EXPIRES_OFF) && (CacheFilter.HEADER_EXPIRES.equalsIgnoreCase(name))) {
if (!fragment) {
result.setExpires(value);
} // TODO should we return now by fragments to avoid putting the header to the response?
}
super.addDateHeader(name, value);
}
|
public void addHeader(String name,
String value) {
if (log.isDebugEnabled()) {
log.debug("header: " + name + ": " + value);
}
if (CacheFilter.HEADER_CONTENT_TYPE.equalsIgnoreCase(name)) {
result.setContentType(value);
}
if (CacheFilter.HEADER_CONTENT_ENCODING.equalsIgnoreCase(name)) {
result.setContentEncoding(value);
}
super.addHeader(name, value);
}
|
public void flushBuffer() throws IOException {
super.flushBuffer();
flush();
}
|
public ResponseContent getContent() {
// Flush the buffer
try {
flush();
} catch (IOException ignore) {
}
// Create the byte array
result.commit();
// Return the result from this response
return result;
}
|
public ServletOutputStream getOutputStream() throws IOException {
// Pass this faked servlet output stream that captures what is sent
if (cacheOut == null) {
cacheOut = new SplitServletOutputStream(result.getOutputStream(), super.getOutputStream());
}
return cacheOut;
}
|
public int getStatus() {
return status;
}
Retrieves the captured HttpResponse status. |
public PrintWriter getWriter() throws IOException {
if (cachedWriter == null) {
String encoding = getCharacterEncoding();
if (encoding != null) {
cachedWriter = new PrintWriter(new OutputStreamWriter(getOutputStream(), encoding));
} else { // using the default character encoding
cachedWriter = new PrintWriter(new OutputStreamWriter(getOutputStream()));
}
}
return cachedWriter;
}
|
public boolean isCommitted() {
return super.isCommitted(); // || (result.getOutputStream() == null);
}
Returns a boolean indicating if the response has been committed.
A commited response has already had its status code and headers written. |
public void reset() {
log.info("CacheHttpServletResponseWrapper:reset()");
super.reset();
/*
cachedWriter = null;
result = new ResponseContent();
cacheOut = null;
fragment = false;
status = SC_OK;
expires = CacheFilter.EXPIRES_ON;
lastModified = CacheFilter.LAST_MODIFIED_INITIAL;
cacheControl = -60;
// time ?
*/
}
Clears any data that exists in the buffer as well as the status code and headers.
If the response has been committed, this method throws an IllegalStateException. |
public void resetBuffer() {
log.info("CacheHttpServletResponseWrapper:resetBuffer()");
super.resetBuffer();
/*
//cachedWriter = null;
result = new ResponseContent();
//cacheOut = null;
//fragment = false;
*/
}
Clears the content of the underlying buffer in the response without clearing headers or status code.
If the response has been committed, this method throws an IllegalStateException. |
public void sendError(int status) throws IOException {
super.sendError(status);
this.status = status;
}
We override this so we can catch the response status. Only
responses with a status of 200 (SC_OK) will
be cached. |
public void sendError(int status,
String string) throws IOException {
super.sendError(status, string);
this.status = status;
}
We override this so we can catch the response status. Only
responses with a status of 200 (SC_OK) will
be cached. |
public void sendRedirect(String location) throws IOException {
this.status = SC_MOVED_TEMPORARILY;
super.sendRedirect(location);
}
We override this so we can catch the response status. Only
responses with a status of 200 (SC_OK) will
be cached. |
public void setContentType(String value) {
if (log.isDebugEnabled()) {
log.debug("ContentType: " + value);
}
super.setContentType(value);
result.setContentType(value);
}
|
public void setDateHeader(String name,
long value) {
if (log.isDebugEnabled()) {
log.debug("dateheader: " + name + ": " + value);
}
// only set the last modified value, if a complete page is cached
if ((lastModified != CacheFilter.LAST_MODIFIED_OFF) && (CacheFilter.HEADER_LAST_MODIFIED.equalsIgnoreCase(name))) {
if (!fragment) {
result.setLastModified(value);
} // TODO should we return now by fragments to avoid putting the header to the response?
}
// implement RFC 2616 14.21 Expires (without max-age)
if ((expires != CacheFilter.EXPIRES_OFF) && (CacheFilter.HEADER_EXPIRES.equalsIgnoreCase(name))) {
if (!fragment) {
result.setExpires(value);
} // TODO should we return now by fragments to avoid putting the header to the response?
}
super.setDateHeader(name, value);
}
|
public void setHeader(String name,
String value) {
if (log.isDebugEnabled()) {
log.debug("header: " + name + ": " + value);
}
if (CacheFilter.HEADER_CONTENT_TYPE.equalsIgnoreCase(name)) {
result.setContentType(value);
}
if (CacheFilter.HEADER_CONTENT_ENCODING.equalsIgnoreCase(name)) {
result.setContentEncoding(value);
}
super.setHeader(name, value);
}
|
public void setIntHeader(String name,
int value) {
if (log.isDebugEnabled()) {
log.debug("intheader: " + name + ": " + value);
}
super.setIntHeader(name, value);
}
Set the int value of the header |
public void setLocale(Locale value) {
super.setLocale(value);
result.setLocale(value);
}
|
public void setStatus(int status) {
super.setStatus(status);
this.status = status;
}
We override this so we can catch the response status. Only
responses with a status of 200 (SC_OK) will
be cached. |
public void setStatus(int status,
String string) {
super.setStatus(status, string);
this.status = status;
}
We override this so we can catch the response status. Only
responses with a status of 200 (SC_OK) will
be cached. |