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