001: package org.apache.myfaces.application.jsp;
002:
003: import javax.servlet.ServletOutputStream;
004: import javax.servlet.http.HttpServletResponse;
005: import javax.servlet.http.HttpServletResponseWrapper;
006: import java.io.*;
007:
008: /**
009: * @author Bruno Aranda (latest modification by $Author: baranda $)
010: * @version $Revision: 542008 $ $Date: 2007-05-27 19:45:19 +0200 (So, 27 Mai 2007) $
011: */
012: public class ViewResponseWrapper extends HttpServletResponseWrapper {
013: private PrintWriter _writer;
014: private CharArrayWriter _charArrayWriter;
015: private int _status = HttpServletResponse.SC_OK;
016: private WrappedServletOutputStream _byteArrayWriter;
017:
018: public ViewResponseWrapper(HttpServletResponse httpServletResponse) {
019: super (httpServletResponse);
020: }
021:
022: @Override
023: public void sendError(int status) throws IOException {
024: super .sendError(status);
025: _status = status;
026: }
027:
028: @Override
029: public void sendError(int status, String errorMessage)
030: throws IOException {
031: super .sendError(status, errorMessage);
032: _status = status;
033: }
034:
035: @Override
036: public void setStatus(int status) {
037: super .setStatus(status);
038: _status = status;
039: }
040:
041: @Override
042: public void setStatus(int status, String errorMessage) {
043: super .setStatus(status, errorMessage);
044: _status = status;
045: }
046:
047: public int getStatus() {
048: return _status;
049: }
050:
051: public void flushToWrappedResponse() throws IOException {
052: if (_charArrayWriter != null) {
053: _charArrayWriter.writeTo(getResponse().getWriter());
054: _charArrayWriter.reset();
055: _writer.flush();
056: } else if (_byteArrayWriter != null) {
057: getResponse().getOutputStream().write(
058: _byteArrayWriter.toByteArray());
059: _byteArrayWriter.reset();
060: _byteArrayWriter.flush();
061: }
062: }
063:
064: public void flushToWriter(Writer writer) throws IOException {
065: if (_charArrayWriter != null) {
066: _charArrayWriter.writeTo(writer);
067: _charArrayWriter.reset();
068: _writer.flush();
069: } else if (_byteArrayWriter != null) {
070: getOutputStream().write(_byteArrayWriter.toByteArray());
071: _byteArrayWriter.reset();
072: _byteArrayWriter.flush();
073: }
074: writer.flush();
075: }
076:
077: @Override
078: public ServletOutputStream getOutputStream() throws IOException {
079: if (_charArrayWriter != null)
080: throw new IllegalStateException();
081: if (_byteArrayWriter == null) {
082: _byteArrayWriter = new WrappedServletOutputStream();
083: }
084: return _byteArrayWriter;
085: }
086:
087: @Override
088: public PrintWriter getWriter() throws IOException {
089: if (_byteArrayWriter != null)
090: throw new IllegalStateException();
091: if (_writer == null) {
092: _charArrayWriter = new CharArrayWriter(4096);
093: _writer = new PrintWriter(_charArrayWriter);
094: }
095: return _writer;
096: }
097:
098: @Override
099: public void reset() {
100: if (_charArrayWriter != null) {
101: _charArrayWriter.reset();
102: }
103: }
104:
105: @Override
106: public String toString() {
107: if (_charArrayWriter != null) {
108: return _charArrayWriter.toString();
109: }
110: return null;
111: }
112:
113: static class WrappedServletOutputStream extends ServletOutputStream {
114: private ByteArrayOutputStream _byteArrayOutputStream;
115:
116: public WrappedServletOutputStream() {
117: _byteArrayOutputStream = new ByteArrayOutputStream(1024);
118: }
119:
120: public void write(int i) throws IOException {
121: _byteArrayOutputStream.write(i);
122: }
123:
124: public byte[] toByteArray() {
125: return _byteArrayOutputStream.toByteArray();
126: }
127:
128: public void reset() {
129: _byteArrayOutputStream.reset();
130: }
131:
132: }
133: }
|