|
|||||||||||||||||||
| 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 |
|
|
| 13 |
/**
|
|
| 14 |
* Holds the servlet response in a byte array so that it can be held
|
|
| 15 |
* in the cache (and, since this class is serializable, optionally
|
|
| 16 |
* persisted to disk).
|
|
| 17 |
*
|
|
| 18 |
* @version $Revision: 1.1 $
|
|
| 19 |
* @author <a href="mailto:sergek@lokitech.com">Serge Knystautas</a>
|
|
| 20 |
*/
|
|
| 21 |
public class ResponseContent implements Serializable { |
|
| 22 |
private transient ByteArrayOutputStream bout = new ByteArrayOutputStream(1000); |
|
| 23 |
private Locale locale = null; |
|
| 24 |
private String contentType = null; |
|
| 25 |
private byte[] content = null; |
|
| 26 |
|
|
| 27 |
/**
|
|
| 28 |
* Set the content type. We capture this so that when we serve this
|
|
| 29 |
* data from cache, we can set the correct content type on the response.
|
|
| 30 |
*/
|
|
| 31 | 0 |
public void setContentType(String value) { |
| 32 | 0 |
contentType = value; |
| 33 |
} |
|
| 34 |
|
|
| 35 |
/**
|
|
| 36 |
* Set the Locale. We capture this so that when we serve this data from
|
|
| 37 |
* cache, we can set the correct locale on the response.
|
|
| 38 |
*/
|
|
| 39 | 0 |
public void setLocale(Locale value) { |
| 40 | 0 |
locale = value; |
| 41 |
} |
|
| 42 |
|
|
| 43 |
/**
|
|
| 44 |
* Get an output stream. This is used by the {@link SplitServletOutputStream}
|
|
| 45 |
* to capture the original (uncached) response into a byte array.
|
|
| 46 |
*/
|
|
| 47 | 0 |
public OutputStream getOutputStream() {
|
| 48 | 0 |
return bout;
|
| 49 |
} |
|
| 50 |
|
|
| 51 |
/**
|
|
| 52 |
* Gets the size of this cached content.
|
|
| 53 |
*
|
|
| 54 |
* @return The size of the content, in bytes. If no content
|
|
| 55 |
* exists, this method returns <code>-1</code>.
|
|
| 56 |
*/
|
|
| 57 | 0 |
public int getSize() { |
| 58 | 0 |
return (content != null) ? content.length : (-1); |
| 59 |
} |
|
| 60 |
|
|
| 61 |
/**
|
|
| 62 |
* Called once the response has been written in its entirety. This
|
|
| 63 |
* method commits the response output stream by converting the output
|
|
| 64 |
* stream into a byte array.
|
|
| 65 |
*/
|
|
| 66 | 0 |
public void commit() { |
| 67 | 0 |
content = bout.toByteArray(); |
| 68 |
} |
|
| 69 |
|
|
| 70 |
/**
|
|
| 71 |
* Writes this cached data out to the supplied <code>ServletResponse</code>.
|
|
| 72 |
*
|
|
| 73 |
* @param response The servlet response to output the cached content to.
|
|
| 74 |
* @throws IOException
|
|
| 75 |
*/
|
|
| 76 | 0 |
public void writeTo(ServletResponse response) throws IOException { |
| 77 |
//Send the content type and data to this response
|
|
| 78 | 0 |
if (contentType != null) { |
| 79 | 0 |
response.setContentType(contentType); |
| 80 |
} |
|
| 81 |
|
|
| 82 | 0 |
response.setContentLength(content.length); |
| 83 |
|
|
| 84 | 0 |
if (locale != null) { |
| 85 | 0 |
response.setLocale(locale); |
| 86 |
} |
|
| 87 |
|
|
| 88 | 0 |
OutputStream out = new BufferedOutputStream(response.getOutputStream());
|
| 89 | 0 |
out.write(content); |
| 90 | 0 |
out.flush(); |
| 91 |
} |
|
| 92 |
} |
|
| 93 |
|
|
||||||||||