KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > filters > CauchoResponseWrapper


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.filters;
30
31 import com.caucho.log.Log;
32 import com.caucho.server.connection.AbstractResponseStream;
33 import com.caucho.server.connection.CauchoResponse;
34 import com.caucho.server.connection.ResponseWrapper;
35 import com.caucho.server.connection.ResponseWriter;
36 import com.caucho.server.connection.ServletOutputStreamImpl;
37 import com.caucho.vfs.FlushBuffer;
38
39 import javax.servlet.ServletOutputStream JavaDoc;
40 import javax.servlet.ServletResponse JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.OutputStream JavaDoc;
44 import java.io.PrintWriter JavaDoc;
45 import java.io.UnsupportedEncodingException JavaDoc;
46 import java.util.Locale JavaDoc;
47 import java.util.logging.Logger JavaDoc;
48
49 /**
50  * Response wrapper that can take advantage of Resin's streams.
51  */

52 public class CauchoResponseWrapper extends ResponseWrapper
53   implements CauchoResponse {
54   private static final Logger JavaDoc log = Log.open(CauchoResponseWrapper.class);
55
56   private FlushBuffer _flushBuffer;
57   
58   private final FilterWrapperResponseStream _originalStream;
59   protected AbstractResponseStream _stream;
60
61   private ResponseWriter _writer;
62   private ServletOutputStreamImpl _os;
63   
64   private boolean _hasStream;
65   
66   private boolean _hasError;
67
68   public CauchoResponseWrapper()
69   {
70     _os = new ServletOutputStreamImpl();
71     _writer = new ResponseWriter();
72     
73     _originalStream = new FilterWrapperResponseStream();
74   }
75
76   public CauchoResponseWrapper(HttpServletResponse JavaDoc response)
77   {
78     this();
79     
80     init(response);
81   }
82
83   /**
84    * Initialize the response.
85    */

86   public void init(HttpServletResponse JavaDoc response)
87   {
88     setResponse(response);
89
90     _stream = _originalStream;
91     _os.init(_originalStream);
92     _writer.init(_originalStream);
93
94     _hasStream = false;
95     _hasError = false;
96
97     _originalStream.init(this);
98     /*
99     try {
100       _originalStream.init(getStream());//response.getOutputStream());
101     } catch (IOException e) {
102       log.log(Level.FINER, e.toString(), e);
103     }
104     */

105
106     _originalStream.start();
107   }
108
109   /**
110    * complete the response.
111    */

112   public void close()
113     throws IOException JavaDoc
114   {
115     if (_stream != null)
116       _stream.close();
117     else
118       _originalStream.close();
119   }
120
121   public ServletResponse JavaDoc getResponse()
122   {
123     return _response;
124   }
125
126   /**
127    * Sets the response content type.
128    */

129   public void setContentType(String JavaDoc value)
130   {
131     _response.setContentType(value);
132     
133     try {
134       _stream.setEncoding(getCharacterEncoding());
135     } catch (UnsupportedEncodingException JavaDoc e) {
136     }
137   }
138
139   /**
140    * Sets the ResponseStream
141    */

142   public void setResponseStream(AbstractResponseStream stream)
143   {
144     _stream = stream;
145
146     _os.init(stream);
147     _writer.init(stream);
148   }
149
150   /**
151    * Gets the response stream.
152    */

153   public AbstractResponseStream getResponseStream()
154   {
155     return _stream;
156   }
157
158   /**
159    * Returns true for a caucho response stream.
160    */

161   public boolean isCauchoResponseStream()
162   {
163     return true;
164   }
165
166   /**
167    * Returns the servlet output stream.
168    */

169   public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc
170   {
171     return _os;
172   }
173
174   /**
175    * Returns the print writer.
176    */

177   public PrintWriter JavaDoc getWriter() throws IOException JavaDoc
178   {
179     return _writer;
180   }
181
182   /**
183    * Returns the output stream for this wrapper.
184    */

185   protected OutputStream JavaDoc getStream() throws IOException JavaDoc
186   {
187     return _response.getOutputStream();
188   }
189
190   /**
191    * Sets the flush buffer
192    */

193   public void setFlushBuffer(FlushBuffer flushBuffer)
194   {
195     _flushBuffer = flushBuffer;
196   }
197
198   /**
199    * Gets the flush buffer
200    */

201   public FlushBuffer getFlushBuffer()
202   {
203     return _flushBuffer;
204   }
205
206   public void flushBuffer()
207     throws IOException JavaDoc
208   {
209     if (_flushBuffer != null)
210       _flushBuffer.flushBuffer();
211     
212     _stream.flushBuffer();
213     
214     //_response.flushBuffer();
215
}
216
217   public void resetBuffer()
218   {
219     if (_stream != null)
220       _stream.clearBuffer();
221     
222     _response.resetBuffer();
223   }
224
225   public void clearBuffer()
226   {
227     resetBuffer();
228   }
229
230   public void setLocale(Locale JavaDoc locale)
231   {
232     _response.setLocale(locale);
233     
234     try {
235       _stream.setLocale(_response.getLocale());
236     } catch (UnsupportedEncodingException JavaDoc e) {
237     }
238   }
239
240   /*
241    * caucho
242    */

243
244   public String JavaDoc getHeader(String JavaDoc key)
245   {
246     return null;
247   }
248   
249   public boolean disableHeaders(boolean disable)
250   {
251     return false;
252   }
253
254   public int getRemaining()
255   {
256     /*
257     if (_response instanceof CauchoResponse)
258       return ((CauchoResponse) _response).getRemaining();
259     else
260       return 0;
261     */

262     return _stream.getRemaining();
263   }
264
265   /**
266    * When set to true, RequestDispatcher.forward() is disallowed on
267    * this stream.
268    */

269   public void setForbidForward(boolean forbid)
270   {
271   }
272
273   /**
274    * Returns true if RequestDispatcher.forward() is disallowed on
275    * this stream.
276    */

277   public boolean getForbidForward()
278   {
279     return false;
280   }
281
282
283   /**
284    * Set to true while processing an error.
285    */

286   public void setHasError(boolean hasError)
287   {
288     _hasError = hasError;
289   }
290
291   /**
292    * Returns true if we're processing an error.
293    */

294   public boolean hasError()
295   {
296     return _hasError;
297   }
298
299   /**
300    * Kills the cache for an error.
301    */

302   public void killCache()
303   {
304     if (_response instanceof CauchoResponse)
305       ((CauchoResponse) _response).killCache();
306   }
307   
308   public void setSessionId(String JavaDoc id)
309   {
310     if (_response instanceof CauchoResponse)
311       ((CauchoResponse) _response).setSessionId(id);
312   }
313   
314   public void setPrivateCache(boolean isPrivate)
315   {
316     if (_response instanceof CauchoResponse)
317       ((CauchoResponse) _response).setPrivateCache(isPrivate);
318   }
319   
320   public void setNoCache(boolean isPrivate)
321   {
322     if (_response instanceof CauchoResponse)
323       ((CauchoResponse) _response).setNoCache(isPrivate);
324   }
325 }
326
Popular Tags