KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > util > ResponseContent


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package org.apache.roller.ui.core.util;
6
7 import java.io.*;
8
9 import java.util.Locale JavaDoc;
10
11 import javax.servlet.ServletResponse JavaDoc;
12 import javax.servlet.http.HttpServletResponse JavaDoc;
13
14 /**
15  * Holds the servlet response in a byte array so that it can be held
16  * in the cache (and, since this class is serializable, optionally
17  * persisted to disk).
18  *
19  * @version $Revision: 1.3 $
20  * @author <a HREF="mailto:sergek@lokitech.com">Serge Knystautas</a>
21  */

22 public class ResponseContent implements Serializable {
23     private transient ByteArrayOutputStream bout = new ByteArrayOutputStream(1000);
24     private Locale JavaDoc locale = null;
25     private String JavaDoc contentType = null;
26     private byte[] content = null;
27     private long lastModified = -1;
28
29     /**
30      * Set the content type. We capture this so that when we serve this
31      * data from cache, we can set the correct content type on the response.
32      */

33     public void setContentType(String JavaDoc value) {
34         contentType = value;
35     }
36
37     public long getLastModified() {
38         return lastModified;
39     }
40
41     public void setLastModified(long value) {
42         lastModified = value;
43     }
44
45     /**
46      * Set the Locale. We capture this so that when we serve this data from
47      * cache, we can set the correct locale on the response.
48      */

49     public void setLocale(Locale JavaDoc value) {
50         locale = value;
51     }
52
53     /**
54      * Get an output stream. This is used by the {@link SplitServletOutputStream}
55      * to capture the original (uncached) response into a byte array.
56      */

57     public OutputStream getOutputStream() {
58         return bout;
59     }
60
61     /**
62      * Gets the size of this cached content.
63      *
64      * @return The size of the content, in bytes. If no content
65      * exists, this method returns <code>-1</code>.
66      */

67     public int getSize() {
68         return (content != null) ? content.length : (-1);
69     }
70
71     /**
72      * Called once the response has been written in its entirety. This
73      * method commits the response output stream by converting the output
74      * stream into a byte array.
75      */

76     public void commit() {
77         content = bout.toByteArray();
78     }
79
80     /**
81      * Writes this cached data out to the supplied <code>ServletResponse</code>.
82      *
83      * @param response The servlet response to output the cached content to.
84      * @throws IOException
85      */

86     public void writeTo(ServletResponse JavaDoc response) throws IOException {
87         //Send the content type and data to this response
88
if (contentType != null) {
89             response.setContentType(contentType);
90         }
91
92         response.setContentLength(content.length);
93
94         if (locale != null) {
95             response.setLocale(locale);
96         }
97
98         OutputStream out = new BufferedOutputStream(response.getOutputStream());
99         out.write(content);
100         out.flush();
101     }
102 }
103
Popular Tags