KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > core > CacheResponseWrapper


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.core;
24
25 import java.io.*;
26 import javax.servlet.*;
27 import javax.servlet.http.*;
28
29 /**
30  * Modifies the response to write the page to both the browser and the cache.
31  * This class is a slightly modified version of the GZIPResponseWrapper found
32  * in <a HREF="http://www.onjava.com/pub/a/onjava/2003/11/19/filters.html">this
33  * article</a>.
34  *
35  * @see CacheResponseStream
36  */

37 public class CacheResponseWrapper extends HttpServletResponseWrapper {
38   HttpServletResponse response;
39   ServletOutputStream stream;
40   PrintWriter writer;
41   OutputStream cacheOutput;
42
43   /**
44    * Creates a new wrapper.
45    *
46    * @param response the original response
47    * @param cacheOutput the output stream to write the cached page to
48    */

49   public CacheResponseWrapper(HttpServletResponse response,
50       OutputStream cacheOutput) {
51     super(response);
52     this.response = response;
53     this.cacheOutput = cacheOutput;
54     //response.setHeader("Content-Encoding", "gzip");
55
}
56
57   /**
58    * Creates the output stream.
59    *
60    * @see CacheResponseStream
61    */

62   public ServletOutputStream createOutputStream() throws IOException {
63     return new CacheResponseStream(response, cacheOutput);
64   }
65
66   /**
67    * Closes the stream.
68    */

69   public void finishResponse() {
70     try {
71       if (writer != null) {
72         writer.close();
73       } else if (stream != null) {
74         stream.close();
75       }
76     } catch (IOException e) {}
77   }
78
79   public void flushBuffer() throws IOException {
80     stream.flush();
81   }
82
83   public ServletOutputStream getOutputStream() throws IOException {
84     if (writer != null) {
85       throw new IllegalStateException JavaDoc("getWriter() has already been called!");
86     }
87
88     if (stream == null) {
89       stream = createOutputStream();
90     }
91
92     return stream;
93   }
94
95   public PrintWriter getWriter() throws IOException {
96     if (writer != null) {
97       return writer;
98     }
99
100     if (stream != null) {
101       throw new IllegalStateException JavaDoc("getOutputStream() has already been called!");
102     }
103
104     stream = createOutputStream();
105     writer = new PrintWriter(stream);
106     return writer;
107   }
108 }
109
Popular Tags