KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > portal > generic > context > RendererResponseHandler


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Sam
47  */

48
49
50 package com.caucho.portal.generic.context;
51
52 import com.caucho.portal.generic.Renderer;
53
54 import javax.portlet.RenderRequest;
55 import javax.portlet.RenderResponse;
56 import java.io.IOException JavaDoc;
57 import java.io.OutputStream JavaDoc;
58 import java.io.PrintWriter JavaDoc;
59 import java.util.logging.Logger JavaDoc;
60
61 public class RendererResponseHandler
62   extends AbstractResponseHandler
63 {
64   protected static final Logger JavaDoc log =
65     Logger.getLogger(RendererResponseHandler.class.getName());
66
67   private ConnectionContext _context;
68   private Renderer _renderer;
69   private RenderRequest _renderRequest;
70   private RenderResponse _renderResponse;
71   private String JavaDoc _namespace;
72
73   private PrintWriter JavaDoc _writer;
74   private boolean _writerIsWrapped;
75   private OutputStream JavaDoc _outputStream;
76   private boolean _outputStreamIsWrapped;
77
78   private boolean _wasReset;
79
80   public RendererResponseHandler()
81   {
82   }
83
84   public RendererResponseHandler( ConnectionContext context,
85                                   ResponseHandler responseHandler,
86                                   Renderer renderer,
87                                   RenderRequest renderRequest,
88                                   RenderResponse renderResponse,
89                                   String JavaDoc namespace )
90   {
91     open(context, responseHandler, renderer, renderRequest, renderResponse, namespace);
92   }
93
94   public void open( ConnectionContext context,
95                     ResponseHandler responseHandler,
96                     Renderer renderer,
97                     RenderRequest renderRequest,
98                     RenderResponse renderResponse,
99                     String JavaDoc namespace )
100   {
101     if (_renderer != null)
102       throw new IllegalStateException JavaDoc("already open");
103
104     super.open(responseHandler);
105
106     _context = context;
107     _renderer = renderer;
108     _renderRequest = renderRequest;
109     _renderResponse = renderResponse;
110     _namespace = namespace;
111   }
112
113   public void finish()
114     throws IOException JavaDoc
115   {
116     try {
117       finishWriter(false);
118       finishOutputStream(false);
119     }
120     catch (Exception JavaDoc ex) {
121       setError(ex);
122     }
123     finally {
124       _wasReset = false;
125       _writer = null;
126       _writerIsWrapped = false;
127       _outputStream = null;
128       _outputStreamIsWrapped = false;
129       _namespace = null;
130       _renderRequest = null;
131       _renderResponse = null;
132       _renderer = null;
133     }
134   }
135
136   public void finishWriter( boolean isDiscarded )
137     throws IOException JavaDoc
138   {
139     if (_writerIsWrapped) {
140       PrintWriter JavaDoc writer = _writer;
141       _writer = null;
142       _writerIsWrapped = false;
143
144       _renderer.finish( writer, _renderRequest, _namespace, isDiscarded );
145     }
146   }
147
148   public void finishOutputStream( boolean isDiscarded )
149     throws IOException JavaDoc
150   {
151     if (_outputStreamIsWrapped) {
152       OutputStream JavaDoc outputStream = _outputStream;
153       _outputStream = null;
154       _outputStreamIsWrapped = false;
155
156       _renderer.finish( outputStream, _renderRequest, _namespace, isDiscarded );
157     }
158   }
159
160   public String JavaDoc getDefaultContentType()
161   {
162     return _renderer == null ? null : _renderer.getDefaultContentType();
163   }
164
165   public boolean isAlwaysWrite()
166   {
167     return _renderer != null && _renderer.isAlwaysWrite();
168   }
169
170   public PrintWriter JavaDoc getWriter()
171     throws IOException JavaDoc
172   {
173     if (_writer != null)
174       return _writer;
175
176     PrintWriter JavaDoc writer = super.getWriter();
177
178     if (_renderer != null) {
179
180       PrintWriter JavaDoc rendererWriter
181         = _renderer.getWriter( writer, _renderRequest, _namespace );
182
183       if (rendererWriter != null) {
184         writer = rendererWriter;
185         _writerIsWrapped = true;
186       }
187     }
188
189     return _writer = writer;
190   }
191
192   public boolean isWriter()
193   {
194     return _writerIsWrapped;
195   }
196
197   public boolean isAlwaysStream()
198   {
199     return _renderer != null && _renderer.isAlwaysStream();
200   }
201
202   public OutputStream JavaDoc getOutputStream()
203     throws IOException JavaDoc
204   {
205     if (_outputStream != null)
206       return _outputStream;
207
208     OutputStream JavaDoc outputStream = super.getOutputStream();
209
210     if (_renderer != null) {
211       OutputStream JavaDoc rendererStream
212         = _renderer.getOutputStream(outputStream, _renderRequest, _namespace );
213
214       if (rendererStream != null) {
215         outputStream = rendererStream;
216         _outputStreamIsWrapped = true;
217       }
218     }
219
220     return _outputStream = outputStream;
221   }
222
223   public boolean isOutputStream()
224   {
225     return _outputStreamIsWrapped;
226   }
227
228   public void flushBuffer()
229     throws IOException JavaDoc
230   {
231     if (!_wasReset && !isWriter() && !isOutputStream()) {
232       if (_renderer.isAlwaysWrite()) {
233         getWriter();
234       }
235       else if (_renderer.isAlwaysStream()) {
236         getOutputStream();
237       }
238     }
239   }
240
241   public void resetBuffer()
242   {
243     _wasReset = true;
244
245     try {
246       finishWriter(true);
247       finishOutputStream(true);
248     }
249     catch (IOException JavaDoc ex) {
250       setError(ex);
251     }
252   }
253
254   public void reset()
255   {
256     _wasReset = true;
257
258     try {
259       finishWriter(true);
260       finishOutputStream(true);
261     }
262     catch (IOException JavaDoc ex) {
263       setError(ex);
264     }
265   }
266
267   /**
268    * @param renderAgain if true, immediately call getWriter()
269    * or getOutputStream() again
270    */

271   public void reset(boolean renderAgain)
272   {
273     try {
274       boolean isWriter = isWriter();
275       boolean isOutputStream = isOutputStream();
276
277       finishWriter(true);
278       finishOutputStream(true);
279
280       if (isWriter)
281         getWriter();
282
283       if (isOutputStream)
284         getOutputStream();
285
286     }
287     catch (IOException JavaDoc ex) {
288       setError(ex);
289     }
290   }
291 }
292
Popular Tags