KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > cachius > CachiusResponseWrapper


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.cachius;
25
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.io.Writer JavaDoc;
30
31 import javax.servlet.ServletOutputStream JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
34
35 import org.riotfamily.cachius.support.MultiplexPrintWriter;
36 import org.riotfamily.cachius.support.MultiplexServletOutputStream;
37
38
39 /**
40  * A HttpServletResponseWrapper that captures the response and updates
41  * the accociated CacheItem in case no error occures during request processing.
42  *
43  * @author Felix Gnass
44  */

45 public class CachiusResponseWrapper extends HttpServletResponseWrapper JavaDoc {
46
47     private ItemUpdater cacheItemUpdate;
48     
49     private ServletOutputStream JavaDoc outputStream;
50     
51     private PrintWriter JavaDoc writer;
52         
53  
54     public CachiusResponseWrapper(HttpServletResponse JavaDoc response,
55             ItemUpdater cacheItemUpdate) {
56         
57         super(response);
58         this.cacheItemUpdate = cacheItemUpdate;
59     }
60         
61     /**
62      * Set the HTTP status code
63      *
64      * @param sc The status
65      */

66     public void setStatus(int sc) {
67         super.setStatus(sc);
68         if (sc != 0 && sc != HttpServletResponse.SC_OK) {
69             cacheItemUpdate.discard();
70         }
71     }
72        
73     
74     public void setContentType(String JavaDoc contentType) {
75         super.setContentType(contentType);
76         cacheItemUpdate.setContentType(contentType);
77     }
78         
79     
80     /**
81      * Get an OutputStream
82      *
83      * @throws IllegalStateException If getWriter() has been called before
84      * @throws IOException
85      */

86     public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
87         if (writer != null) {
88             throw new IllegalStateException JavaDoc();
89         }
90         if (outputStream == null) {
91             OutputStream JavaDoc captureStream = cacheItemUpdate.getOutputStream();
92             if (captureStream != null) {
93                 outputStream = new MultiplexServletOutputStream(
94                         captureStream, super.getOutputStream());
95             }
96             else {
97                 // Fail gracefully - continue without caching
98
outputStream = super.getOutputStream();
99             }
100         }
101         return outputStream;
102     }
103
104     /**
105      * Get a PrintWriter
106      *
107      * @throws IllegalStateExcepion If getOutputStream() has been called before
108      * @throws IOException
109      */

110     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
111         if (writer == null) {
112             if (outputStream != null) {
113                 throw new IllegalStateException JavaDoc();
114             }
115             Writer JavaDoc captureWriter = cacheItemUpdate.getWriter();
116             if (captureWriter != null) {
117                 writer = new MultiplexPrintWriter(
118                         captureWriter,
119                         super.getWriter());
120             }
121             else {
122                 // Fail gracefully - continue without caching
123
writer = super.getWriter();
124             }
125         }
126         return writer;
127     }
128     
129     public void flushBuffer() throws IOException JavaDoc {
130         if (writer != null) {
131             writer.flush();
132         }
133         else if (outputStream != null) {
134             outputStream.flush();
135         }
136     }
137     
138 }
139
Popular Tags