KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > connection > IncludeResponseStream


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.connection;
31
32 import com.caucho.log.Log;
33 import com.caucho.util.L10N;
34
35 import javax.servlet.ServletOutputStream JavaDoc;
36 import javax.servlet.ServletResponse JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.OutputStream JavaDoc;
39 import java.io.PrintWriter JavaDoc;
40 import java.io.Writer JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 public class IncludeResponseStream extends ToByteResponseStream {
45   static final Logger JavaDoc log = Log.open(IncludeResponseStream.class);
46   
47   static final L10N L = new L10N(IncludeResponseStream.class);
48
49   private final AbstractHttpResponse _response;
50
51   private boolean _isCauchoResponseStream;
52   private AbstractResponseStream _nextResponseStream;
53   
54   private ServletResponse JavaDoc _next;
55   private ServletOutputStream JavaDoc _os;
56   private PrintWriter JavaDoc _writer;
57   
58   private OutputStream JavaDoc _cacheStream;
59   private Writer JavaDoc _cacheWriter;
60   
61   public IncludeResponseStream(AbstractHttpResponse response)
62   {
63     _response = response;
64   }
65
66   public void init(ServletResponse JavaDoc next)
67   {
68     _next = next;
69
70     if (_os != null || _writer != null)
71       throw new IllegalStateException JavaDoc();
72
73     if (_next instanceof CauchoResponse) {
74       CauchoResponse response = (CauchoResponse) next;
75
76       _isCauchoResponseStream = response.isCauchoResponseStream();
77
78       if (_isCauchoResponseStream)
79     _nextResponseStream = response.getResponseStream();
80     }
81     else
82       _isCauchoResponseStream = false;
83   }
84
85   public void start()
86   {
87     super.start();
88
89     try {
90       setEncoding(_next.getCharacterEncoding());
91     } catch (Throwable JavaDoc e) {
92       log.log(Level.FINE, e.toString(), e);
93     }
94   }
95
96   /**
97    * Returns true for a caucho response stream.
98    */

99   public boolean isCauchoResponseStream()
100   {
101     return _isCauchoResponseStream;
102   }
103
104   /**
105    * Set true for a caucho response stream.
106    */

107   public void setCauchoResponseStream(boolean isCaucho)
108   {
109     _isCauchoResponseStream = isCaucho;
110   }
111
112   /**
113    * Sets any cache stream.
114    */

115   public void setByteCacheStream(OutputStream JavaDoc cacheStream)
116   {
117     _cacheStream = cacheStream;
118   }
119
120   /**
121    * Sets any cache stream.
122    */

123   public void setCharCacheStream(Writer JavaDoc cacheWriter)
124   {
125     _cacheWriter = cacheWriter;
126   }
127
128   /**
129    * Converts the char buffer.
130    */

131   protected void flushCharBuffer()
132     throws IOException JavaDoc
133   {
134     if (_isCauchoResponseStream && _nextResponseStream == null) {
135       // jsp/18ek
136
super.flushCharBuffer();
137       
138       return;
139     }
140     
141     try {
142       if (_writer == null)
143     _writer = _next.getWriter();
144     } catch (Throwable JavaDoc e) {
145       log.log(Level.FINER, e.toString(), e);
146     }
147
148     if (_writer != null) {
149       int charLength = getCharOffset();
150       setCharOffset(0);
151       char []buffer = getCharBuffer();
152
153       _response.startCaching(false);
154
155       _writer.write(buffer, 0, charLength);
156     
157       if (_cacheWriter != null)
158     _cacheWriter.write(buffer, 0, charLength);
159     }
160     else
161       super.flushCharBuffer();
162   }
163
164   /**
165    * Sets the byte buffer offset.
166    */

167   public void setBufferOffset(int offset)
168     throws IOException JavaDoc
169   {
170     super.setBufferOffset(offset);
171
172     if (! _isCauchoResponseStream)
173       flushByteBuffer();
174   }
175
176   /**
177    * Writes the next chunk of data to the response stream.
178    *
179    * @param buf the buffer containing the data
180    * @param offset start offset into the buffer
181    * @param length length of the data in the buffer
182    */

183   public void write(int ch)
184     throws IOException JavaDoc
185   {
186     flushCharBuffer();
187     
188     if (_isCauchoResponseStream) {
189       super.write(ch);
190     }
191     else {
192       if (_os == null)
193     _os = _next.getOutputStream();
194
195       _os.write(ch);
196     }
197   }
198
199   /**
200    * Writes the next chunk of data to the response stream.
201    *
202    * @param buf the buffer containing the data
203    * @param offset start offset into the buffer
204    * @param length length of the data in the buffer
205    */

206   public void write(byte []buf, int offset, int length)
207     throws IOException JavaDoc
208   {
209     flushCharBuffer();
210       
211     if (false && _isCauchoResponseStream) // jsp/15dv
212
super.write(buf, offset, length);
213     else {
214       if (_os == null)
215     _os = _next.getOutputStream();
216
217       _os.write(buf, offset, length);
218     }
219   }
220
221   /**
222    * Writes the next chunk of data to the response stream.
223    *
224    * @param buf the buffer containing the data
225    * @param offset start offset into the buffer
226    * @param length length of the data in the buffer
227    */

228   protected void writeNext(byte []buf, int offset, int length, boolean isEnd)
229     throws IOException JavaDoc
230   {
231     try {
232       if (_response != null)
233     _response.writeHeaders(null, -1);
234     
235       if (length == 0)
236     return;
237     
238       if (_cacheStream != null)
239     _cacheStream.write(buf, offset, length);
240
241       if (_os == null)
242     _os = _next.getOutputStream();
243     
244       _os.write(buf, offset, length);
245     } catch (IOException JavaDoc e) {
246       if (_next instanceof CauchoResponse)
247     ((CauchoResponse) _next).killCache();
248       
249       if (_response != null)
250     _response.killCache();
251
252       throw e;
253     }
254   }
255
256   /**
257    * flushing
258    */

259   public void flushByte()
260     throws IOException JavaDoc
261   {
262     flushBuffer();
263
264     if (_os == null)
265       _os = _next.getOutputStream();
266
267     _os.flush();
268   }
269
270   /**
271    * flushing
272    */

273   public void flushChar()
274     throws IOException JavaDoc
275   {
276     flushBuffer();
277
278     if (_writer == null)
279       _writer = _next.getWriter();
280     
281     _writer.flush();
282   }
283
284   /**
285    * Finish.
286    */

287   public void finish()
288     throws IOException JavaDoc
289   {
290     flushBuffer();
291
292     /*
293     if (_writer != null)
294       _writer.flush();
295     
296     if (_os != null)
297       _os.flush();
298     */

299
300     _os = null;
301     _writer = null;
302     _next = null;
303     
304     _cacheStream = null;
305     _cacheWriter = null;
306   }
307
308   /**
309    * Close.
310    */

311   public void close()
312     throws IOException JavaDoc
313   {
314     super.close();
315
316     /*
317     if (_writer != null)
318       _writer.close();
319     
320     if (_os != null)
321       _os.close();
322     */

323
324     _os = null;
325     _writer = null;
326     _next = null;
327
328     _cacheStream = null;
329     _cacheWriter = null;
330   }
331 }
332
Popular Tags