|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| CacheHttpServletResponseWrapper.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 org.apache.commons.logging.Log;
|
|
| 8 |
import org.apache.commons.logging.LogFactory;
|
|
| 9 |
|
|
| 10 |
import java.io.IOException;
|
|
| 11 |
import java.io.PrintWriter;
|
|
| 12 |
|
|
| 13 |
import java.util.Locale;
|
|
| 14 |
|
|
| 15 |
import javax.servlet.ServletOutputStream;
|
|
| 16 |
import javax.servlet.http.HttpServletResponse;
|
|
| 17 |
import javax.servlet.http.HttpServletResponseWrapper;
|
|
| 18 |
|
|
| 19 |
/**
|
|
| 20 |
* CacheServletResponse is a serialized representation of a response
|
|
| 21 |
*
|
|
| 22 |
* @author <a href="mailto:sergek@lokitech.com">Serge Knystautas</a>
|
|
| 23 |
* @version $Revision: 1.1 $
|
|
| 24 |
*/
|
|
| 25 |
public class CacheHttpServletResponseWrapper extends HttpServletResponseWrapper { |
|
| 26 |
private final Log log = LogFactory.getLog(this.getClass()); |
|
| 27 |
|
|
| 28 |
/**
|
|
| 29 |
* We cache the printWriter so we can maintain a single instance
|
|
| 30 |
* of it no matter how many times it is requested.
|
|
| 31 |
*/
|
|
| 32 |
private PrintWriter cachedWriter;
|
|
| 33 |
private ResponseContent result = null; |
|
| 34 |
private SplitServletOutputStream cacheOut = null; |
|
| 35 |
private int status = SC_OK; |
|
| 36 |
|
|
| 37 |
/**
|
|
| 38 |
* Constructor
|
|
| 39 |
*
|
|
| 40 |
* @param response The servlet response
|
|
| 41 |
*/
|
|
| 42 | 0 |
public CacheHttpServletResponseWrapper(HttpServletResponse response) {
|
| 43 | 0 |
super(response);
|
| 44 | 0 |
result = new ResponseContent();
|
| 45 |
} |
|
| 46 |
|
|
| 47 |
/**
|
|
| 48 |
* Get a response content
|
|
| 49 |
*
|
|
| 50 |
* @return The content
|
|
| 51 |
*/
|
|
| 52 | 0 |
public ResponseContent getContent() {
|
| 53 |
//Create the byte array
|
|
| 54 | 0 |
result.commit(); |
| 55 |
|
|
| 56 |
//Return the result from this response
|
|
| 57 | 0 |
return result;
|
| 58 |
} |
|
| 59 |
|
|
| 60 |
/**
|
|
| 61 |
* Set the content type
|
|
| 62 |
*
|
|
| 63 |
* @param value The content type
|
|
| 64 |
*/
|
|
| 65 | 0 |
public void setContentType(String value) { |
| 66 | 0 |
super.setContentType(value);
|
| 67 | 0 |
result.setContentType(value); |
| 68 |
} |
|
| 69 |
|
|
| 70 |
/**
|
|
| 71 |
* Set the date of a header
|
|
| 72 |
*
|
|
| 73 |
* @param name The header name
|
|
| 74 |
* @param value The date
|
|
| 75 |
*/
|
|
| 76 | 0 |
public void setDateHeader(String name, long value) { |
| 77 | 0 |
if (log.isDebugEnabled()) {
|
| 78 | 0 |
log.debug("dateheader: " + name + ": " + value); |
| 79 |
} |
|
| 80 |
|
|
| 81 | 0 |
if ("last-modified".equalsIgnoreCase(name)) { |
| 82 | 0 |
result.setLastModified(value); |
| 83 |
} |
|
| 84 |
|
|
| 85 | 0 |
super.setDateHeader(name, value);
|
| 86 |
} |
|
| 87 |
|
|
| 88 |
/**
|
|
| 89 |
* Set a header field
|
|
| 90 |
*
|
|
| 91 |
* @param name The header name
|
|
| 92 |
* @param value The header value
|
|
| 93 |
*/
|
|
| 94 | 0 |
public void setHeader(String name, String value) { |
| 95 | 0 |
if (log.isDebugEnabled()) {
|
| 96 | 0 |
log.debug("header: " + name + ": " + value); |
| 97 |
} |
|
| 98 |
|
|
| 99 | 0 |
super.setHeader(name, value);
|
| 100 |
} |
|
| 101 |
|
|
| 102 |
/**
|
|
| 103 |
* Set the int value of the header
|
|
| 104 |
*
|
|
| 105 |
* @param name The header name
|
|
| 106 |
* @param value The int value
|
|
| 107 |
*/
|
|
| 108 | 0 |
public void setIntHeader(String name, int value) { |
| 109 | 0 |
if (log.isDebugEnabled()) {
|
| 110 | 0 |
log.debug("intheader: " + name + ": " + value); |
| 111 |
} |
|
| 112 |
|
|
| 113 | 0 |
super.setIntHeader(name, value);
|
| 114 |
} |
|
| 115 |
|
|
| 116 |
/**
|
|
| 117 |
* We override this so we can catch the response status. Only
|
|
| 118 |
* responses with a status of 200 (<code>SC_OK</code>) will
|
|
| 119 |
* be cached.
|
|
| 120 |
*/
|
|
| 121 | 0 |
public void setStatus(int status) { |
| 122 | 0 |
super.setStatus(status);
|
| 123 | 0 |
this.status = status;
|
| 124 |
} |
|
| 125 |
|
|
| 126 |
/**
|
|
| 127 |
* We override this so we can catch the response status. Only
|
|
| 128 |
* responses with a status of 200 (<code>SC_OK</code>) will
|
|
| 129 |
* be cached.
|
|
| 130 |
*/
|
|
| 131 | 0 |
public void sendError(int status, String string) throws IOException { |
| 132 | 0 |
super.sendError(status, string);
|
| 133 | 0 |
this.status = status;
|
| 134 |
} |
|
| 135 |
|
|
| 136 |
/**
|
|
| 137 |
* We override this so we can catch the response status. Only
|
|
| 138 |
* responses with a status of 200 (<code>SC_OK</code>) will
|
|
| 139 |
* be cached.
|
|
| 140 |
*/
|
|
| 141 | 0 |
public void sendError(int status) throws IOException { |
| 142 | 0 |
super.sendError(status);
|
| 143 | 0 |
this.status = status;
|
| 144 |
} |
|
| 145 |
|
|
| 146 |
/**
|
|
| 147 |
* We override this so we can catch the response status. Only
|
|
| 148 |
* responses with a status of 200 (<code>SC_OK</code>) will
|
|
| 149 |
* be cached.
|
|
| 150 |
*/
|
|
| 151 | 0 |
public void setStatus(int status, String string) { |
| 152 | 0 |
super.setStatus(status, string);
|
| 153 | 0 |
this.status = status;
|
| 154 |
} |
|
| 155 |
|
|
| 156 | 0 |
public void sendRedirect(String location) throws IOException { |
| 157 | 0 |
this.status = SC_MOVED_TEMPORARILY;
|
| 158 | 0 |
super.sendRedirect(location);
|
| 159 |
} |
|
| 160 |
|
|
| 161 |
/**
|
|
| 162 |
* Retrieves the captured HttpResponse status.
|
|
| 163 |
*/
|
|
| 164 | 0 |
public int getStatus() { |
| 165 | 0 |
return status;
|
| 166 |
} |
|
| 167 |
|
|
| 168 |
/**
|
|
| 169 |
* Set the locale
|
|
| 170 |
*
|
|
| 171 |
* @param value The locale
|
|
| 172 |
*/
|
|
| 173 | 0 |
public void setLocale(Locale value) { |
| 174 | 0 |
super.setLocale(value);
|
| 175 | 0 |
result.setLocale(value); |
| 176 |
} |
|
| 177 |
|
|
| 178 |
/**
|
|
| 179 |
* Get an output stream
|
|
| 180 |
*
|
|
| 181 |
* @throws IOException
|
|
| 182 |
*/
|
|
| 183 | 0 |
public ServletOutputStream getOutputStream() throws IOException { |
| 184 |
// Pass this faked servlet output stream that captures what is sent
|
|
| 185 | 0 |
if (cacheOut == null) { |
| 186 | 0 |
cacheOut = new SplitServletOutputStream(result.getOutputStream(), super.getOutputStream()); |
| 187 |
} |
|
| 188 |
|
|
| 189 | 0 |
return cacheOut;
|
| 190 |
} |
|
| 191 |
|
|
| 192 |
/**
|
|
| 193 |
* Get a print writer
|
|
| 194 |
*
|
|
| 195 |
* @throws IOException
|
|
| 196 |
*/
|
|
| 197 | 0 |
public PrintWriter getWriter() throws IOException { |
| 198 | 0 |
if (cachedWriter == null) { |
| 199 | 0 |
cachedWriter = new PrintWriter(getOutputStream());
|
| 200 |
} |
|
| 201 |
|
|
| 202 | 0 |
return cachedWriter;
|
| 203 |
} |
|
| 204 |
|
|
| 205 | 0 |
public void flushBuffer() throws IOException { |
| 206 | 0 |
super.flushBuffer();
|
| 207 |
|
|
| 208 | 0 |
if (cacheOut != null) { |
| 209 | 0 |
cacheOut.flush(); |
| 210 |
} |
|
| 211 |
|
|
| 212 | 0 |
if (cachedWriter != null) { |
| 213 | 0 |
cachedWriter.flush(); |
| 214 |
} |
|
| 215 |
} |
|
| 216 |
} |
|
| 217 |
|
|
||||||||||