001: /* BufferedResponse.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;
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.ServletResponse;
031: import javax.servlet.ServletResponseWrapper;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import org.zkoss.web.servlet.ServletOutputStreamWrapper;
035: import org.zkoss.web.servlet.http.HttpBufferedResponse;
036:
037: /**
038: * A servlet response that uses another writer or stream as
039: * the output.
040: *
041: * @author tomyeh
042: */
043: public class BufferedResponse extends ServletResponseWrapper {
044: private Writer _writer;
045: private OutputStream _stream;
046:
047: private PrintWriter _pwt;
048: private ServletOutputStream _sos;
049:
050: /** Returns a buffered response with a writer, if writer is not null;
051: * or the original response if writer is null.
052: * If reponse is HttpServletResponse, {@link HttpBufferedResponse#getInstance}
053: * is returned.
054: * It is smart enough not to wrap the same writer twice.
055: */
056: public static final ServletResponse getInstance(
057: ServletResponse response, Writer writer) {
058: if (response instanceof HttpServletResponse)
059: return HttpBufferedResponse.getInstance(
060: (HttpServletResponse) response, writer);
061:
062: if (writer != null
063: && (!(response instanceof BufferedResponse) || ((BufferedResponse) response)._writer != writer))
064: return new BufferedResponse(response, writer);
065: return response;
066: }
067:
068: /** Returns a buffered response with a output stream, if stream is not null;
069: * or the original response if stream is null.
070: * If reponse is HttpServletResponse, {@link HttpBufferedResponse#getInstance}
071: * is returned.
072: * It is smart enough not to wrap the same stream twice.
073: */
074: public static final ServletResponse getInstance(
075: ServletResponse response, OutputStream stream) {
076: if (response instanceof HttpServletResponse)
077: return HttpBufferedResponse.getInstance(
078: (HttpServletResponse) response, stream);
079:
080: if (stream != null
081: && (!(response instanceof BufferedResponse) || ((BufferedResponse) response)._stream != stream))
082: return new BufferedResponse(response, stream);
083: return response;
084: }
085:
086: /** Constructs a buffered response with a writer. */
087: private BufferedResponse(ServletResponse response, Writer writer) {
088: super (response);
089: if (writer == null)
090: throw new NullPointerException("writer");
091: _writer = writer;
092: }
093:
094: /** Constructs a buffered response with an output stream. */
095: private BufferedResponse(ServletResponse response,
096: OutputStream stream) {
097: super (response);
098: if (stream == null)
099: throw new NullPointerException("stream");
100: _stream = stream;
101: }
102:
103: //super//
104: public PrintWriter getWriter() throws IOException {
105: if (_sos != null)
106: throw new IllegalStateException(
107: "getOutputStream was called");
108:
109: if (_pwt == null) {
110: if (_writer != null) {
111: if (_writer instanceof PrintWriter) {
112: _pwt = (PrintWriter) _writer;
113: } else {
114: _pwt = new PrintWriter(_writer);
115: }
116: } else {
117: _pwt = new PrintWriter(new OutputStreamWriter(_stream,
118: getCharacterEncoding()));
119: }
120: }
121: return _pwt;
122: }
123:
124: public ServletOutputStream getOutputStream() throws IOException {
125: if (_pwt != null)
126: throw new IllegalStateException("getWriter was called");
127:
128: if (_sos == null) {
129: if (_stream != null) {
130: _sos = ServletOutputStreamWrapper.getInstance(_stream);
131: } else {
132: throw new UnsupportedOperationException(
133: "ServletOutputStream cannot wrap PrintWriter");
134: }
135: }
136: return _sos;
137: }
138:
139: public void flushBuffer() throws IOException {
140: if (_writer != null)
141: _writer.flush();
142: else
143: _stream.flush();
144: }
145:
146: /** Useful only if StringWriter or ByteArrayOutputStream is used
147: * to construct this object.
148: */
149: public void resetBuffer() {
150: if (_writer instanceof StringWriter) {
151: ((StringWriter) _writer).getBuffer().setLength(0);
152: } else if (_stream instanceof ByteArrayOutputStream) {
153: ((ByteArrayOutputStream) _stream).reset();
154: }
155: }
156: }
|