001: /* HttpBufferedResponse.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Mon Jan 17 14:02:29 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.web.servlet.http;
020:
021: import java.io.Writer;
022: import java.io.PrintWriter;
023: import java.io.StringWriter;
024: import java.io.OutputStream;
025: import java.io.OutputStreamWriter;
026: import java.io.ByteArrayOutputStream;
027: import java.io.IOException;
028:
029: import javax.servlet.ServletOutputStream;
030: import javax.servlet.http.HttpServletResponse;
031: import javax.servlet.http.HttpServletResponseWrapper;
032:
033: import org.zkoss.web.servlet.ServletOutputStreamWrapper;
034:
035: /**
036: * A servlet response that uses another writer or stream as
037: * the output.
038: *
039: * @author tomyeh
040: */
041: public class HttpBufferedResponse extends HttpServletResponseWrapper {
042: private Writer _writer;
043: private OutputStream _stream;
044:
045: private PrintWriter _pwt;
046: private ServletOutputStream _sos;
047: private boolean _bSendRedirect;
048:
049: /** Returns a buffered response with a writer, if writer is not null;
050: * or the original response if writer is null.
051: * It is smart enough not to wrap the same writer twice.
052: */
053: public static final HttpServletResponse getInstance(
054: HttpServletResponse response, Writer writer) {
055: if (writer != null
056: && (!(response instanceof HttpBufferedResponse) || ((HttpBufferedResponse) response)._writer != writer))
057: return new HttpBufferedResponse(response, writer);
058: return response;
059: }
060:
061: /** Returns a buffered response with a output stream, if stream is not null;
062: * or the original response if stream is null.
063: * It is smart enough not to wrap the same stream twice.
064: */
065: public static final HttpServletResponse getInstance(
066: HttpServletResponse response, OutputStream stream) {
067: if (stream != null
068: && (!(response instanceof HttpBufferedResponse) || ((HttpBufferedResponse) response)._stream != stream))
069: return new HttpBufferedResponse(response, stream);
070: return response;
071: }
072:
073: /** Constructs a buffered response with a writer. */
074: private HttpBufferedResponse(HttpServletResponse response,
075: Writer writer) {
076: super (response);
077: if (writer == null)
078: throw new IllegalArgumentException("null writer");
079: _writer = writer;
080: }
081:
082: /** Constructs a buffered response with an output stream. */
083: private HttpBufferedResponse(HttpServletResponse response,
084: OutputStream stream) {
085: super (response);
086: if (stream == null)
087: throw new IllegalArgumentException("null stream");
088: _stream = stream;
089: }
090:
091: //extra//
092: /** Returns whether {@link #sendRedirect} was called.
093: */
094: public boolean isSendRedirect() {
095: return _bSendRedirect;
096: }
097:
098: //super//
099: public PrintWriter getWriter() throws IOException {
100: if (_sos != null)
101: throw new IllegalStateException(
102: "getOutputStream was called");
103:
104: if (_pwt == null) {
105: if (_writer != null) {
106: if (_writer instanceof PrintWriter) {
107: _pwt = (PrintWriter) _writer;
108: } else {
109: _pwt = new PrintWriter(_writer);
110: }
111: } else {
112: _pwt = new PrintWriter(new OutputStreamWriter(_stream,
113: getCharacterEncoding()));
114: }
115: }
116: return _pwt;
117: }
118:
119: public ServletOutputStream getOutputStream() throws IOException {
120: if (_pwt != null)
121: throw new IllegalStateException("getWriter was called");
122:
123: if (_sos == null) {
124: if (_stream != null) {
125: _sos = ServletOutputStreamWrapper.getInstance(_stream);
126: } else {
127: _sos = ServletOutputStreamWrapper.getInstance(_writer,
128: getCharacterEncoding());
129: }
130: }
131: return _sos;
132: }
133:
134: public void flushBuffer() throws IOException {
135: if (_writer != null)
136: _writer.flush();
137: else
138: _stream.flush();
139: }
140:
141: /** Useful only if StringWriter or ByteArrayOutputStream is used
142: * to construct this object.
143: */
144: public void resetBuffer() {
145: if (_writer instanceof StringWriter) {
146: ((StringWriter) _writer).getBuffer().setLength(0);
147: } else if (_stream instanceof ByteArrayOutputStream) {
148: ((ByteArrayOutputStream) _stream).reset();
149: }
150: }
151:
152: public void sendRedirect(String location) throws IOException {
153: super .sendRedirect(location);
154: _bSendRedirect = true;
155: }
156:
157: //deprecated//
158: /**
159: * @deprecated
160: */
161: public String encodeRedirectUrl(String url) {
162: return super .encodeRedirectUrl(url);
163: }
164:
165: /**
166: * @deprecated
167: */
168: public String encodeUrl(String url) {
169: return super .encodeUrl(url);
170: }
171:
172: /**
173: * @deprecated
174: */
175: public void setStatus(int sc, String sm) {
176: super.setStatus(sc, sm);
177: }
178: }
|