KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > website > interceptor > pushup > DeferredRenderingResponseWrapper


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.website.interceptor.pushup;
25
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.io.StringWriter 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.common.web.util.DelegatingServletOutputStream;
36 import org.springframework.util.FileCopyUtils;
37
38 /**
39  * ResponseWrapper that buffers the output and defers the rendering until
40  * {@link #renderResponse()} is invoked.
41  *
42  * @see PushUpInterceptor
43  * @author Felix Gnass [fgnass at neteye dot de]
44  */

45 public class DeferredRenderingResponseWrapper extends HttpServletResponseWrapper JavaDoc {
46     
47     private ByteArrayOutputStream JavaDoc outputStream;
48     
49     private StringWriter JavaDoc writer;
50     
51     private boolean redirectSent = false;
52     
53     public DeferredRenderingResponseWrapper(HttpServletResponse JavaDoc response) {
54         super(response);
55     }
56     
57     public void sendError(int sc) throws IOException JavaDoc {
58         redirectSent = true;
59         super.sendError(sc);
60     }
61     
62     public void sendError(int sc, String JavaDoc msg) throws IOException JavaDoc {
63         redirectSent = true;
64         super.sendError(sc, msg);
65     }
66     
67     public void sendRedirect(String JavaDoc location) throws IOException JavaDoc {
68         redirectSent = true;
69         super.sendRedirect(location);
70     }
71         
72     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
73         if (outputStream == null) {
74             if (writer == null) {
75                 writer = new StringWriter JavaDoc();
76             }
77             return new PrintWriter JavaDoc(writer);
78         }
79         else {
80             throw new IllegalStateException JavaDoc(
81                 "getOutputStream() has been called already");
82         }
83     }
84     
85     public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
86         if (writer == null) {
87             if (outputStream == null) {
88                 outputStream = new ByteArrayOutputStream JavaDoc();
89             }
90             return new DelegatingServletOutputStream(outputStream);
91         }
92         else {
93             throw new IllegalStateException JavaDoc(
94                     "getWriter() has been called already");
95         }
96     }
97     
98     public boolean isRedirectSent() {
99         return redirectSent;
100     }
101     
102     public void renderResponse(HttpServletResponse JavaDoc response) throws IOException JavaDoc {
103         if (outputStream != null) {
104             FileCopyUtils.copy(outputStream.toByteArray(),
105                     response.getOutputStream());
106         }
107         else if (writer != null) {
108             response.getWriter().write(writer.toString());
109         }
110     }
111     
112     
113 }
114
Popular Tags