001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsf.context;
030:
031: import javax.servlet.*;
032: import javax.servlet.http.*;
033:
034: import java.io.*;
035: import java.util.*;
036: import java.util.logging.*;
037:
038: import com.caucho.filters.*;
039: import com.caucho.util.*;
040: import com.caucho.vfs.*;
041: import com.caucho.jsp.*;
042: import com.caucho.server.connection.*;
043:
044: public class JspResponseWrapper extends ResponseWrapper implements
045: CauchoResponse {
046: private static final Logger log = Logger
047: .getLogger(JspResponseWrapper.class.getName());
048: private static final L10N L = new L10N(JspResponseWrapper.class);
049:
050: private boolean _hasError;
051: private BodyResponseStream _bodyStream;
052: private AbstractResponseStream _stream;
053:
054: private HttpServletResponse _response;
055:
056: private TempStream _tempStream = new TempStream();
057: private WriteStream _out;
058:
059: private FlushBuffer _flushBuffer;
060:
061: private ResponseWriter _writer = new ResponseWriter();
062: private ServletOutputStreamImpl _os = new ServletOutputStreamImpl();
063:
064: /**
065: * Initialize the response.
066: */
067: public void init(HttpServletResponse response) {
068: _bodyStream = new BodyResponseStream();
069: _stream = _bodyStream;
070:
071: _out = new WriteStream(_tempStream);
072: _bodyStream.setWriter(_out.getPrintWriter());
073:
074: setResponse(response);
075: _response = response;
076:
077: _os.init(_stream);
078: _writer.init(_stream);
079:
080: _hasError = false;
081: }
082:
083: /**
084: * Sets the ResponseStream
085: */
086: public void setResponseStream(AbstractResponseStream stream) {
087: try {
088: _stream.flushBuffer();
089: } catch (IOException e) {
090: log.log(Level.FINER, e.toString(), e);
091: }
092:
093: _stream = stream;
094:
095: _os.init(stream);
096: _writer.init(stream);
097: }
098:
099: /**
100: * Gets the response stream.
101: */
102: public AbstractResponseStream getResponseStream() {
103: return _stream;
104: }
105:
106: /**
107: * Returns true for a caucho response stream.
108: */
109: public boolean isCauchoResponseStream() {
110: return true;
111: }
112:
113: /**
114: * Returns the servlet output stream.
115: */
116: public ServletOutputStream getOutputStream() throws IOException {
117: return _os;
118: }
119:
120: /**
121: * Returns the print writer.
122: */
123: public PrintWriter getWriter() throws IOException {
124: return _writer;
125: }
126:
127: /**
128: * Returns the output stream for this wrapper.
129: */
130: protected OutputStream getStream() throws IOException {
131: return _response.getOutputStream();
132: }
133:
134: /**
135: * Sets the flush buffer
136: */
137: public void setFlushBuffer(FlushBuffer flushBuffer) {
138: _flushBuffer = flushBuffer;
139: }
140:
141: /**
142: * Gets the flush buffer
143: */
144: public FlushBuffer getFlushBuffer() {
145: return _flushBuffer;
146: }
147:
148: public void flushBuffer() throws IOException {
149: //if (_flushBuffer != null)
150: // _flushBuffer.flushBuffer();
151:
152: //_stream.flushBuffer();
153:
154: //_response.flushBuffer();
155: }
156:
157: public void resetBuffer() {
158: if (_stream != null)
159: _stream.clearBuffer();
160: }
161:
162: public void clearBuffer() {
163: resetBuffer();
164: }
165:
166: public void setLocale(Locale locale) {
167: _response.setLocale(locale);
168:
169: try {
170: _stream.setLocale(_response.getLocale());
171: } catch (UnsupportedEncodingException e) {
172: }
173: }
174:
175: /*
176: * caucho
177: */
178:
179: public String getHeader(String key) {
180: return null;
181: }
182:
183: public boolean disableHeaders(boolean disable) {
184: return false;
185: }
186:
187: public int getRemaining() {
188: /*
189: if (_response instanceof CauchoResponse)
190: return ((CauchoResponse) _response).getRemaining();
191: else
192: return 0;
193: */
194: return _stream.getRemaining();
195: }
196:
197: /**
198: * When set to true, RequestDispatcher.forward() is disallowed on
199: * this stream.
200: */
201: public void setForbidForward(boolean forbid) {
202: }
203:
204: /**
205: * Returns true if RequestDispatcher.forward() is disallowed on
206: * this stream.
207: */
208: public boolean getForbidForward() {
209: return false;
210: }
211:
212: /**
213: * Set to true while processing an error.
214: */
215: public void setHasError(boolean hasError) {
216: _hasError = hasError;
217: }
218:
219: /**
220: * Returns true if we're processing an error.
221: */
222: public boolean hasError() {
223: return _hasError;
224: }
225:
226: /**
227: * Kills the cache for an error.
228: */
229: public void killCache() {
230: }
231:
232: public void setSessionId(String id) {
233:
234: }
235:
236: public void setPrivateCache(boolean isPrivate) {
237: }
238:
239: public void setNoCache(boolean isPrivate) {
240: }
241:
242: /**
243: * complete the response.
244: */
245: public void close() throws IOException {
246: WriteStream out = _out;
247: _out = null;
248:
249: if (out != null)
250: out.close();
251: }
252:
253: public void flushResponse() throws IOException {
254: _out.flush();
255:
256: ReadStream rs = _tempStream.openRead();
257: PrintWriter out = _response.getWriter();
258:
259: int ch;
260:
261: while ((ch = rs.readChar()) >= 0)
262: out.write((char) ch);
263:
264: rs.close();
265: }
266:
267: public String complete() throws IOException {
268: WriteStream out = _out;
269: _out = null;
270:
271: if (out != null)
272: out.flush();
273:
274: ReadStream rs = _tempStream.openRead();
275: StringBuilder sb = new StringBuilder();
276:
277: int ch;
278:
279: while ((ch = rs.readChar()) >= 0)
280: sb.append((char) ch);
281:
282: rs.close();
283:
284: return sb.toString();
285: }
286: }
|