001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Caucho Technology (http://www.caucho.com/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * info@caucho.com.
029: *
030: * 5. Products derived from this software may not be called "Resin"
031: * nor may "Resin" appear in their names without prior written
032: * permission of Caucho Technology.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
038: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
039: * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
040: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
041: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
042: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
043: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
044: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
045: *
046: * @author Sam
047: */
048:
049: package com.caucho.portal.generic.context;
050:
051: import com.caucho.portal.generic.Renderer;
052:
053: import javax.portlet.RenderRequest;
054: import javax.portlet.RenderResponse;
055: import java.io.IOException;
056: import java.io.OutputStream;
057: import java.io.PrintWriter;
058: import java.util.logging.Logger;
059:
060: public class RendererResponseHandler extends AbstractResponseHandler {
061: protected static final Logger log = Logger
062: .getLogger(RendererResponseHandler.class.getName());
063:
064: private ConnectionContext _context;
065: private Renderer _renderer;
066: private RenderRequest _renderRequest;
067: private RenderResponse _renderResponse;
068: private String _namespace;
069:
070: private PrintWriter _writer;
071: private boolean _writerIsWrapped;
072: private OutputStream _outputStream;
073: private boolean _outputStreamIsWrapped;
074:
075: private boolean _wasReset;
076:
077: public RendererResponseHandler() {
078: }
079:
080: public RendererResponseHandler(ConnectionContext context,
081: ResponseHandler responseHandler, Renderer renderer,
082: RenderRequest renderRequest, RenderResponse renderResponse,
083: String namespace) {
084: open(context, responseHandler, renderer, renderRequest,
085: renderResponse, namespace);
086: }
087:
088: public void open(ConnectionContext context,
089: ResponseHandler responseHandler, Renderer renderer,
090: RenderRequest renderRequest, RenderResponse renderResponse,
091: String namespace) {
092: if (_renderer != null)
093: throw new IllegalStateException("already open");
094:
095: super .open(responseHandler);
096:
097: _context = context;
098: _renderer = renderer;
099: _renderRequest = renderRequest;
100: _renderResponse = renderResponse;
101: _namespace = namespace;
102: }
103:
104: public void finish() throws IOException {
105: try {
106: finishWriter(false);
107: finishOutputStream(false);
108: } catch (Exception ex) {
109: setError(ex);
110: } finally {
111: _wasReset = false;
112: _writer = null;
113: _writerIsWrapped = false;
114: _outputStream = null;
115: _outputStreamIsWrapped = false;
116: _namespace = null;
117: _renderRequest = null;
118: _renderResponse = null;
119: _renderer = null;
120: }
121: }
122:
123: public void finishWriter(boolean isDiscarded) throws IOException {
124: if (_writerIsWrapped) {
125: PrintWriter writer = _writer;
126: _writer = null;
127: _writerIsWrapped = false;
128:
129: _renderer.finish(writer, _renderRequest, _namespace,
130: isDiscarded);
131: }
132: }
133:
134: public void finishOutputStream(boolean isDiscarded)
135: throws IOException {
136: if (_outputStreamIsWrapped) {
137: OutputStream outputStream = _outputStream;
138: _outputStream = null;
139: _outputStreamIsWrapped = false;
140:
141: _renderer.finish(outputStream, _renderRequest, _namespace,
142: isDiscarded);
143: }
144: }
145:
146: public String getDefaultContentType() {
147: return _renderer == null ? null : _renderer
148: .getDefaultContentType();
149: }
150:
151: public boolean isAlwaysWrite() {
152: return _renderer != null && _renderer.isAlwaysWrite();
153: }
154:
155: public PrintWriter getWriter() throws IOException {
156: if (_writer != null)
157: return _writer;
158:
159: PrintWriter writer = super .getWriter();
160:
161: if (_renderer != null) {
162:
163: PrintWriter rendererWriter = _renderer.getWriter(writer,
164: _renderRequest, _namespace);
165:
166: if (rendererWriter != null) {
167: writer = rendererWriter;
168: _writerIsWrapped = true;
169: }
170: }
171:
172: return _writer = writer;
173: }
174:
175: public boolean isWriter() {
176: return _writerIsWrapped;
177: }
178:
179: public boolean isAlwaysStream() {
180: return _renderer != null && _renderer.isAlwaysStream();
181: }
182:
183: public OutputStream getOutputStream() throws IOException {
184: if (_outputStream != null)
185: return _outputStream;
186:
187: OutputStream outputStream = super .getOutputStream();
188:
189: if (_renderer != null) {
190: OutputStream rendererStream = _renderer.getOutputStream(
191: outputStream, _renderRequest, _namespace);
192:
193: if (rendererStream != null) {
194: outputStream = rendererStream;
195: _outputStreamIsWrapped = true;
196: }
197: }
198:
199: return _outputStream = outputStream;
200: }
201:
202: public boolean isOutputStream() {
203: return _outputStreamIsWrapped;
204: }
205:
206: public void flushBuffer() throws IOException {
207: if (!_wasReset && !isWriter() && !isOutputStream()) {
208: if (_renderer.isAlwaysWrite()) {
209: getWriter();
210: } else if (_renderer.isAlwaysStream()) {
211: getOutputStream();
212: }
213: }
214: }
215:
216: public void resetBuffer() {
217: _wasReset = true;
218:
219: try {
220: finishWriter(true);
221: finishOutputStream(true);
222: } catch (IOException ex) {
223: setError(ex);
224: }
225: }
226:
227: public void reset() {
228: _wasReset = true;
229:
230: try {
231: finishWriter(true);
232: finishOutputStream(true);
233: } catch (IOException ex) {
234: setError(ex);
235: }
236: }
237:
238: /**
239: * @param renderAgain if true, immediately call getWriter()
240: * or getOutputStream() again
241: */
242: public void reset(boolean renderAgain) {
243: try {
244: boolean isWriter = isWriter();
245: boolean isOutputStream = isOutputStream();
246:
247: finishWriter(true);
248: finishOutputStream(true);
249:
250: if (isWriter)
251: getWriter();
252:
253: if (isOutputStream)
254: getOutputStream();
255:
256: } catch (IOException ex) {
257: setError(ex);
258: }
259: }
260: }
|