KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > webapp > DispatchResponse


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.webapp;
31
32 import com.caucho.server.connection.AbstractHttpResponse;
33 import com.caucho.server.connection.AbstractResponseStream;
34 import com.caucho.server.connection.CauchoResponse;
35 import com.caucho.server.connection.IncludeResponseStream;
36 import com.caucho.util.FreeList;
37 import com.caucho.vfs.WriteStream;
38
39 import javax.servlet.ServletResponse JavaDoc;
40 import javax.servlet.ServletResponseWrapper JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42 import java.io.IOException JavaDoc;
43
44 /**
45  * Internal response for an include() or forward()
46  */

47 class DispatchResponse extends AbstractHttpResponse {
48   private static final FreeList<DispatchResponse> _freeList =
49     new FreeList<DispatchResponse>(32);
50
51   private IncludeResponseStream _stream;
52   
53   private HttpServletResponse JavaDoc _next;
54   
55   protected DispatchResponse()
56   {
57   }
58
59   /**
60    * Creates a dispatch request.
61    */

62   public static DispatchResponse createDispatch()
63   {
64     DispatchResponse res = _freeList.allocate();
65     if (res == null)
66       res = new DispatchResponse();
67
68     return res;
69   }
70
71   /**
72    * Creates the response stream.
73    */

74   protected AbstractResponseStream createResponseStream()
75   {
76     _stream = new IncludeResponseStream(this);
77
78     return _stream;
79   }
80
81   /**
82    * Sets the next response.
83    */

84   public void setNextResponse(HttpServletResponse JavaDoc next)
85   {
86     _next = next;
87
88     _stream.init(next);
89   }
90
91   /**
92    * Gets the next response.
93    */

94   public ServletResponse JavaDoc getResponse()
95   {
96     return _next;
97   }
98
99   /**
100    * Starts the response.
101    */

102   public void start()
103     throws IOException JavaDoc
104   {
105     super.start();
106
107     setResponseStream(_stream);
108
109     _stream.start();
110   }
111
112   /**
113    * included response can't set the content type.
114    */

115   public void setContentType(String JavaDoc type)
116   {
117   }
118
119   /**
120    * Wrapped calls.
121    */

122   public String JavaDoc encodeURL(String JavaDoc url)
123   {
124     return _next.encodeURL(url);
125   }
126
127   /**
128    * Wrapped calls.
129    */

130   public String JavaDoc encodeRedirectURL(String JavaDoc url)
131   {
132     return _next.encodeRedirectURL(url);
133   }
134
135   /**
136    * included() responses don't print the headers.
137    */

138   protected boolean writeHeadersInt(WriteStream os, int length)
139     throws IOException JavaDoc
140   {
141     return false;
142   }
143
144   /**
145    * This is not a top response.
146    */

147   public boolean isTop()
148   {
149     return false;
150   }
151
152   @Override JavaDoc
153   public boolean isCommitted()
154   {
155     // jsp/15m2
156
return _next.isCommitted();
157   }
158
159   /**
160    * Returns true for a caucho response.
161    */

162   public boolean isCauchoResponse()
163   {
164     return _next instanceof CauchoResponse;
165   }
166
167   /**
168    * Set true for a caucho response stream.
169    */

170   public void setCauchoResponseStream(boolean isCaucho)
171   {
172     _stream.setCauchoResponseStream(isCaucho);
173   }
174
175   /**
176    * Kills the cache.
177    */

178   public void killCache()
179   {
180     super.killCache();
181
182     ServletResponse JavaDoc next = _next;
183     while (next != null && next != this) {
184       if (next instanceof CauchoResponse) {
185     ((CauchoResponse) next).killCache();
186     break;
187       }
188
189       if (next instanceof ServletResponseWrapper JavaDoc)
190     next = ((ServletResponseWrapper JavaDoc) next).getResponse();
191       else if (next instanceof DispatchResponse)
192     next = ((DispatchResponse) next).getResponse();
193       else
194     break;
195     }
196   }
197
198   /**
199    * Frees the response.
200    */

201   public void free()
202   {
203     super.free();
204     
205     _next = null;
206   }
207
208   /**
209    * Frees the request.
210    */

211   public static void free(DispatchResponse res)
212   {
213     res.free();
214
215     _freeList.free(res);
216   }
217 }
218
Popular Tags