KickJava   Java API By Example, From Geeks To Geeks.

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


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.server.connection;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.FreeList;
33 import com.caucho.vfs.FlushBuffer;
34
35 import javax.servlet.ServletOutputStream JavaDoc;
36 import javax.servlet.ServletResponse JavaDoc;
37 import javax.servlet.http.Cookie JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.PrintWriter JavaDoc;
41 import java.io.UnsupportedEncodingException JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 public class ResponseAdapter extends ResponseWrapper
45   implements CauchoResponse {
46   private static final Logger JavaDoc log = Log.open(ResponseAdapter.class);
47
48   private static final FreeList<ResponseAdapter> _freeList =
49     new FreeList<ResponseAdapter>(32);
50
51   protected RequestAdapter _request;
52
53   protected FlushBuffer _flushBuffer;
54
55   protected AbstractResponseStream _originalResponseStream;
56   protected AbstractResponseStream _responseStream;
57
58   private ServletOutputStreamImpl _os;
59   private ResponseWriter _writer;
60
61   private boolean _hasOutputStream;
62   private boolean _hasError;
63
64   private ResponseAdapter()
65   {
66   }
67
68   protected ResponseAdapter(HttpServletResponse JavaDoc response)
69   {
70     setResponse(response);
71
72     _originalResponseStream = createWrapperResponseStream();
73
74     _os = new ServletOutputStreamImpl();
75     _writer = new ResponseWriter();
76   }
77
78   /**
79    * Creates a new ResponseAdapter.
80    */

81   public static ResponseAdapter create(HttpServletResponse JavaDoc response)
82   {
83     ResponseAdapter resAdapt = _freeList.allocate();
84
85     if (resAdapt == null)
86       resAdapt = new ResponseAdapter(response);
87     else
88       resAdapt.setResponse(response);
89
90     resAdapt.init(response);
91
92     return resAdapt;
93   }
94
95   void setRequest(RequestAdapter request)
96   {
97     _request = request;
98   }
99
100   protected AbstractResponseStream createWrapperResponseStream()
101   {
102     return new WrapperResponseStream();
103   }
104
105   public void init(HttpServletResponse JavaDoc response)
106   {
107     setResponse(response);
108     _hasError = false;
109
110     _responseStream = _originalResponseStream;
111     if (_originalResponseStream instanceof WrapperResponseStream) {
112       WrapperResponseStream wrapper = (WrapperResponseStream) _originalResponseStream;
113       wrapper.init(response);
114     }
115
116     _originalResponseStream.start();
117
118     _os.init(_originalResponseStream);
119     _writer.init(_originalResponseStream);
120   }
121
122   public AbstractResponseStream getResponseStream()
123   {
124     return _responseStream;
125   }
126
127   public boolean isCauchoResponseStream()
128   {
129     return false;
130   }
131
132   public void setResponseStream(AbstractResponseStream responseStream)
133   {
134     _responseStream = responseStream;
135
136     _os.init(responseStream);
137     _writer.init(responseStream);
138   }
139
140   public boolean isTop()
141   {
142     return false;
143   }
144
145   public void resetBuffer()
146   {
147     super.resetBuffer();
148
149     _responseStream.clearBuffer();
150   }
151
152   public void sendRedirect(String JavaDoc url)
153     throws IOException JavaDoc
154   {
155     resetBuffer();
156
157     super.sendRedirect(url);
158   }
159
160   public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc
161   {
162     return _os;
163   }
164
165   /**
166    * Sets the flush buffer
167    */

168   public void setFlushBuffer(FlushBuffer flushBuffer)
169   {
170     _flushBuffer = flushBuffer;
171   }
172
173   /**
174    * Gets the flush buffer
175    */

176   public FlushBuffer getFlushBuffer()
177   {
178     return _flushBuffer;
179   }
180
181   public PrintWriter JavaDoc getWriter() throws IOException JavaDoc
182   {
183     return _writer;
184   }
185
186   public void setContentType(String JavaDoc value)
187   {
188     super.setContentType(value);
189
190     try {
191       _responseStream.setEncoding(getCharacterEncoding());
192     } catch (UnsupportedEncodingException JavaDoc e) {
193     }
194   }
195
196   public void addCookie(Cookie JavaDoc cookie)
197   {
198     if (_request != null)
199       _request.setHasCookie();
200
201     super.addCookie(cookie);
202   }
203
204   /*
205    * caucho
206    */

207
208   public String JavaDoc getHeader(String JavaDoc key)
209   {
210     return null;
211   }
212
213   public boolean disableHeaders(boolean disable)
214   {
215     return false;
216   }
217
218   public int getRemaining()
219   {
220     return _responseStream.getRemaining();
221   }
222
223   /**
224    * When set to true, RequestDispatcher.forward() is disallowed on
225    * this stream.
226    */

227   public void setForbidForward(boolean forbid)
228   {
229   }
230
231   /**
232    * Returns true if RequestDispatcher.forward() is disallowed on
233    * this stream.
234    */

235   public boolean getForbidForward()
236   {
237     return false;
238   }
239
240
241   /**
242    * Set to true while processing an error.
243    */

244   public void setHasError(boolean hasError)
245   {
246     _hasError = hasError;
247   }
248
249   /**
250    * Returns true if we're processing an error.
251    */

252   public boolean hasError()
253   {
254     return _hasError;
255   }
256
257   /**
258    * Kills the cache for an error.
259    */

260   public void killCache()
261   {
262     if (getResponse() instanceof CauchoResponse)
263       ((CauchoResponse) getResponse()).killCache();
264   }
265
266   /**
267    * Sets private caching
268    */

269   public void setPrivateCache(boolean isPrivate)
270   {
271     if (getResponse() instanceof CauchoResponse)
272       ((CauchoResponse) getResponse()).setPrivateCache(isPrivate);
273   }
274
275   /**
276    * Sets no caching
277    */

278   public void setNoCache(boolean isPrivate)
279   {
280     if (getResponse() instanceof CauchoResponse)
281       ((CauchoResponse) getResponse()).setNoCache(isPrivate);
282   }
283
284   public void setSessionId(String JavaDoc id)
285   {
286     if (getResponse() instanceof CauchoResponse)
287       ((CauchoResponse) getResponse()).setSessionId(id);
288   }
289
290   public void finish()
291     throws IOException JavaDoc
292   {
293     if (_responseStream != null)
294       _responseStream.flushBuffer();
295
296     _responseStream = _originalResponseStream;
297   }
298
299   public void close()
300     throws IOException JavaDoc
301   {
302     ServletResponse JavaDoc response = getResponse();
303
304     AbstractResponseStream responseStream = _responseStream;
305     _responseStream = _originalResponseStream;
306
307     if (responseStream != null)
308       responseStream.close();
309
310     if (_originalResponseStream != responseStream)
311       _originalResponseStream.close();
312
313     if (response instanceof CauchoResponse) {
314       ((CauchoResponse) response).close();
315     }
316     /*
317     else {
318       try {
319     PrintWriter writer = response.getWriter();
320     writer.close();
321       } catch (Throwable e) {
322       }
323
324       try {
325     OutputStream os = response.getOutputStream();
326     os.close();
327       } catch (Throwable e) {
328       }
329     }
330     */

331   }
332
333   public static void free(ResponseAdapter resAdapt)
334   {
335     resAdapt.free();
336
337     _freeList.free(resAdapt);
338   }
339
340   /**
341    * Clears the adapter.
342    */

343   protected void free()
344   {
345     _request = null;
346     _responseStream = null;
347
348     setResponse(null);
349   }
350 }
351
Popular Tags