001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.common.web.util;
025:
026: import java.io.ByteArrayOutputStream;
027: import java.io.IOException;
028: import java.io.PrintWriter;
029: import java.io.StringWriter;
030:
031: import javax.servlet.ServletOutputStream;
032: import javax.servlet.ServletResponse;
033: import javax.servlet.http.HttpServletResponse;
034: import javax.servlet.http.HttpServletResponseWrapper;
035:
036: import org.springframework.util.FileCopyUtils;
037:
038: /**
039: * ResponseWrapper that buffers the output and defers the rendering until
040: * {@link #renderResponse()} is invoked.
041: *
042: * @author Felix Gnass [fgnass at neteye dot de]
043: */
044: public class DeferredRenderingResponseWrapper extends
045: HttpServletResponseWrapper {
046:
047: private ByteArrayOutputStream outputStream;
048:
049: private StringWriter writer;
050:
051: private PrintWriter printWriter;
052:
053: private boolean redirectSent;
054:
055: private boolean flush;
056:
057: public DeferredRenderingResponseWrapper(HttpServletResponse response) {
058: super (response);
059: }
060:
061: public void sendError(int sc) throws IOException {
062: redirectSent = true;
063: super .sendError(sc);
064: }
065:
066: public void sendError(int sc, String msg) throws IOException {
067: redirectSent = true;
068: super .sendError(sc, msg);
069: }
070:
071: public void sendRedirect(String location) throws IOException {
072: redirectSent = true;
073: super .sendRedirect(location);
074: }
075:
076: public PrintWriter getWriter() throws IOException {
077: if (outputStream == null) {
078: if (writer == null) {
079: writer = new StringWriter();
080: printWriter = new PrintWriter(writer);
081: }
082: return printWriter;
083: } else {
084: throw new IllegalStateException(
085: "getOutputStream() has been called already");
086: }
087: }
088:
089: public ServletOutputStream getOutputStream() throws IOException {
090: if (writer == null) {
091: if (outputStream == null) {
092: outputStream = new ByteArrayOutputStream();
093: }
094: return new DelegatingServletOutputStream(outputStream);
095: } else {
096: throw new IllegalStateException(
097: "getWriter() has been called already");
098: }
099: }
100:
101: public void flushBuffer() throws IOException {
102: flush = true;
103: }
104:
105: public boolean isRedirectSent() {
106: return redirectSent;
107: }
108:
109: public void renderResponse() throws IOException {
110: renderResponse(getResponse());
111: }
112:
113: public void renderResponse(ServletResponse response)
114: throws IOException {
115: if (outputStream != null) {
116: FileCopyUtils.copy(outputStream.toByteArray(), response
117: .getOutputStream());
118: } else if (writer != null) {
119: response.getWriter().write(writer.toString());
120: }
121: if (flush) {
122: response.flushBuffer();
123: }
124: }
125:
126: }
|