KickJava   Java API By Example, From Geeks To Geeks.

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


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.FastPrintWriter;
53
54 import java.io.IOException JavaDoc;
55 import java.io.OutputStream JavaDoc;
56 import java.io.PrintWriter JavaDoc;
57 import java.io.UnsupportedEncodingException JavaDoc;
58 import java.util.Locale JavaDoc;
59 import java.util.logging.Level JavaDoc;
60 import java.util.logging.Logger JavaDoc;
61
62 /**
63  * A ResponseHandler that wraps another ResponseHandler. Derived classes
64  * override the print() and write() methods to intercept the output.
65  *
66  * flushBuffer(), reset(), and resetBuffer() DO NOT propagate to the
67  * wrapped stream (they do nothing in the implementations for this class)
68  */

69 public class AbstractResponseHandler implements ResponseHandler
70 {
71   protected static final Logger JavaDoc log =
72     Logger.getLogger(AbstractResponseHandler.class.getName());
73
74   private PrintWriter _internalWriter;
75   private OutputStream JavaDoc _internalOutputStream;
76
77   private ResponseHandler _successor;
78
79   protected PrintWriter _writer;
80   protected OutputStream JavaDoc _outputStream;
81
82   protected PrintWriter _writerOut;
83   protected OutputStream JavaDoc _outputStreamOut;
84
85   private Exception JavaDoc _errorCause;
86
87
88   public AbstractResponseHandler()
89   {
90   }
91
92   public AbstractResponseHandler(ResponseHandler successor)
93   {
94     open(successor);
95   }
96
97   public void open(ResponseHandler successor)
98   {
99     if (_successor != null)
100       throw new IllegalStateException JavaDoc("already open");
101
102     _successor = successor;
103   }
104
105   public void finish()
106     throws IOException JavaDoc
107   {
108     Exception JavaDoc errorCause = _errorCause;
109
110     _writerOut = null;
111     _outputStreamOut = null;
112     _successor = null;
113     _errorCause = null;
114
115     if (errorCause != null) {
116       if (errorCause instanceof IOException JavaDoc)
117         throw (IOException JavaDoc) errorCause;
118       else {
119         IOException JavaDoc ex = new IOException JavaDoc();
120         ex.initCause(errorCause);
121         throw ex;
122       }
123     }
124
125   }
126
127   public ResponseHandler getSuccessor()
128   {
129     return _successor;
130   }
131
132   public void setProperty(String JavaDoc name, String JavaDoc value)
133   {
134     _successor.setProperty(name, value);
135   }
136
137   public void addProperty(String JavaDoc name, String JavaDoc value)
138   {
139     _successor.addProperty(name, value);
140   }
141
142   public void setCharacterEncoding(String JavaDoc enc)
143     throws UnsupportedEncodingException JavaDoc
144   {
145     if (_writer == null && _outputStream == null)
146       _successor.setCharacterEncoding(enc);
147   }
148
149   public String JavaDoc getCharacterEncoding()
150   {
151     return _successor.getCharacterEncoding();
152   }
153
154   public void setContentType(String JavaDoc contentType)
155   {
156     if (_writer == null && _outputStream == null)
157       _successor.setContentType(contentType);
158   }
159
160   public String JavaDoc getContentType()
161   {
162     return _successor.getContentType();
163   }
164
165   public void setLocale(Locale JavaDoc locale)
166   {
167     if (_writer == null && _outputStream == null)
168       _successor.setLocale(locale);
169   }
170
171   public Locale JavaDoc getLocale()
172   {
173     return _successor.getLocale();
174   }
175
176   public boolean isCommitted()
177   {
178     return _successor.isCommitted();
179   }
180
181   public PrintWriter getWriter()
182     throws IOException JavaDoc
183   {
184     checkErrorOrFail();
185
186     if (_writer != null)
187       return _writer;
188
189     if (_outputStream != null)
190       throw new IllegalStateException JavaDoc("getOutputStream() already called");
191
192     if (getContentType() == null)
193       throw new IllegalStateException JavaDoc(
194           "response.setContentType() must be called before getWriter()");
195
196     try {
197       PrintWriter writerOut = _successor.getWriter();
198
199       if (_internalWriter == null)
200         _internalWriter = new GenericPrintWriter(this);
201
202       _writerOut = writerOut;
203       _writer = _internalWriter;
204
205     }
206     catch (Exception JavaDoc ex) {
207       setError(ex);
208     }
209
210     checkErrorOrFail();
211
212
213     return _writer;
214   }
215
216   protected PrintWriter getUnderlyingWriter()
217   {
218     return _writerOut;
219   }
220
221   public OutputStream JavaDoc getOutputStream()
222     throws IOException JavaDoc
223   {
224     checkErrorOrFail();
225
226     if (_outputStream != null)
227       return _outputStream;
228
229     if (_writer != null)
230       throw new IllegalStateException JavaDoc("getWriter() already called");
231
232     if (getContentType() == null)
233       throw new IllegalStateException JavaDoc(
234           "response.setContentType() must be called before getOutputStream()");
235
236     boolean fail = true;
237
238     try {
239       OutputStream JavaDoc outputStreamOut = _successor.getOutputStream();
240
241       if (_internalOutputStream == null)
242         _internalOutputStream = new GenericOutputStream(this);
243
244       _outputStreamOut = outputStreamOut;
245       _outputStream = _internalOutputStream;
246
247       fail = false;
248     }
249     catch (Exception JavaDoc ex) {
250       setError(ex);
251     }
252
253     checkErrorOrFail();
254
255     return _outputStream;
256   }
257
258   protected OutputStream JavaDoc getUnderlyingOutputStream()
259   {
260     return _outputStreamOut;
261   }
262
263   /**
264    * Set an error with a cause.
265    */

266   protected void setError(Exception JavaDoc cause)
267   {
268     if (cause == null)
269       throw new NullPointerException JavaDoc();
270
271     if (_errorCause != null) {
272       _errorCause = cause;
273       log.log(Level.FINEST, _errorCause.toString(), cause);
274     }
275   }
276
277   /**
278    * Return an exception if this ResponseHandler has failed.
279    */

280   public Exception JavaDoc getErrorCause()
281   {
282     return _errorCause;
283   }
284
285   public boolean isError()
286   {
287     return _errorCause != null;
288   }
289
290   protected void checkErrorOrFail()
291     throws IOException JavaDoc
292   {
293     if (_errorCause != null) {
294       if (_errorCause instanceof IOException JavaDoc)
295         throw (IOException JavaDoc) _errorCause;
296       else {
297         IOException JavaDoc ex = new IOException JavaDoc();
298         ex.initCause(_errorCause);
299         throw ex;
300       }
301     }
302   }
303
304   public void setBufferSize(int bufferSize)
305   {
306     _successor.setBufferSize(bufferSize);
307   }
308
309   public int getBufferSize()
310   {
311     return _successor.getBufferSize();
312   }
313
314   /**
315    * flushBuffer(), reset(), and resetBuffer() DO NOT propagate to the
316    * wrapped stream (they do nothing in the implementations for this class)
317    */

318   public void reset()
319   {
320   }
321
322   /**
323    * flushBuffer(), reset(), and resetBuffer() DO NOT propagate to the
324    * wrapped stream (they do nothing in the implementations for this class)
325    */

326   public void resetBuffer()
327   {
328   }
329
330   /**
331    * flushBuffer(), reset(), and resetBuffer() DO NOT propagate to the
332    * wrapped stream (they do nothing in the implementations for this class)
333    */

334   public void flushBuffer()
335     throws IOException JavaDoc
336   {
337     checkErrorOrFail();
338   }
339
340   /**
341    * Write chars out to the underlying Writer
342    */

343   protected void print(char buf[], int off, int len)
344     throws IOException JavaDoc
345   {
346     if (len == 0)
347       return;
348
349     checkErrorOrFail();
350
351     _writerOut.write(buf, off, len);
352   }
353
354   /**
355    * Write chars out to the underlying Writer
356    */

357   protected void print(String JavaDoc str, int off, int len)
358     throws IOException JavaDoc
359   {
360     if (len == 0)
361       return;
362
363     checkErrorOrFail();
364
365     _writerOut.write(str, off, len);
366   }
367
368   /**
369    * Write a char out to the underlying Writer
370    */

371   protected void print(char c)
372     throws IOException JavaDoc
373   {
374     checkErrorOrFail();
375
376     _writerOut.write((int)c);
377   }
378
379   /**
380    * Write bytes out to the underlying OutputStream
381    */

382   protected void write(byte[] buf, int off, int len)
383     throws IOException JavaDoc
384   {
385     checkErrorOrFail();
386
387     _outputStreamOut.write(buf, off, len);
388   }
389
390   /**
391    * Write a byte out to the underlying OutputStream
392    */

393   protected void write(byte b)
394     throws IOException JavaDoc
395   {
396     checkErrorOrFail();
397
398     _outputStreamOut.write((int)b);
399   }
400
401   private static class GenericPrintWriter
402       extends FastPrintWriter
403   {
404     private AbstractResponseHandler _successor;
405
406     public GenericPrintWriter(AbstractResponseHandler successor)
407     {
408       _successor = successor;
409     }
410
411     protected void setError()
412     {
413       _successor.setError(new IOException JavaDoc());
414     }
415
416     protected void setError(Exception JavaDoc errorCause)
417     {
418       _successor.setError(errorCause);
419     }
420
421     public boolean checkError()
422     {
423       return _successor.getErrorCause() != null;
424     }
425
426     public Exception JavaDoc getErrorCause()
427     {
428       return _successor.getErrorCause();
429     }
430
431
432     public void writeOut(char buf[], int off, int len)
433       throws IOException JavaDoc
434     {
435       _successor.print(buf, off, len);
436     }
437
438     public void writeOut(String JavaDoc str, int off, int len)
439       throws IOException JavaDoc
440     {
441       _successor.print(str, off, len);
442     }
443
444     public void writeOut(char c)
445       throws IOException JavaDoc
446     {
447       _successor.print((char)c);
448     }
449
450     public void close()
451     {
452     }
453   }
454
455   static private class GenericOutputStream extends OutputStream JavaDoc
456   {
457     private AbstractResponseHandler _successor;
458
459     public GenericOutputStream(AbstractResponseHandler successor)
460     {
461       _successor = successor;
462     }
463
464     public void flush()
465       throws IOException JavaDoc
466     {
467       _successor.flushBuffer();
468     }
469
470     public void write(byte[] buf)
471       throws IOException JavaDoc
472     {
473       _successor.write(buf, 0, buf.length);
474     }
475
476     public void write(byte[] buf, int off, int len)
477       throws IOException JavaDoc
478     {
479       _successor.write(buf, off, len);
480     }
481
482     public void write(int b)
483       throws IOException JavaDoc
484     {
485       _successor.write((byte)b);
486     }
487
488     public void close()
489     {
490     }
491   }
492 }
493
Popular Tags