~ubuntu-branches/ubuntu/gutsy/oscache/gutsy

« back to all changes in this revision

Viewing changes to src/core/java/com/opensymphony/oscache/web/filter/ResponseContent.java

  • Committer: Bazaar Package Importer
  • Author(s): Kalle Kivimaa
  • Date: 2004-08-13 14:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20040813140000-lyugvinublk1x8y2
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
    public void setContentType(String value) {
 
32
        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
    public void setLocale(Locale value) {
 
40
        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
    public OutputStream getOutputStream() {
 
48
        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
    public int getSize() {
 
58
        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
    public void commit() {
 
67
        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
    public void writeTo(ServletResponse response) throws IOException {
 
77
        //Send the content type and data to this response
 
78
        if (contentType != null) {
 
79
            response.setContentType(contentType);
 
80
        }
 
81
 
 
82
        response.setContentLength(content.length);
 
83
 
 
84
        if (locale != null) {
 
85
            response.setLocale(locale);
 
86
        }
 
87
 
 
88
        OutputStream out = new BufferedOutputStream(response.getOutputStream());
 
89
        out.write(content);
 
90
        out.flush();
 
91
    }
 
92
}