KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > core > ApplicationHttpResponse


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.core;
20
21
22 import java.io.IOException JavaDoc;
23 import java.util.Locale JavaDoc;
24
25 import javax.servlet.http.Cookie JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
28
29 import org.apache.catalina.util.StringManager;
30
31
32 /**
33  * Wrapper around a <code>javax.servlet.http.HttpServletResponse</code>
34  * that transforms an application response object (which might be the original
35  * one passed to a servlet, or might be based on the 2.3
36  * <code>javax.servlet.http.HttpServletResponseWrapper</code> class)
37  * back into an internal <code>org.apache.catalina.HttpResponse</code>.
38  * <p>
39  * <strong>WARNING</strong>: Due to Java's lack of support for multiple
40  * inheritance, all of the logic in <code>ApplicationResponse</code> is
41  * duplicated in <code>ApplicationHttpResponse</code>. Make sure that you
42  * keep these two classes in synchronization when making changes!
43  *
44  * @author Craig R. McClanahan
45  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
46  */

47
48 class ApplicationHttpResponse extends HttpServletResponseWrapper JavaDoc {
49
50
51     // ----------------------------------------------------------- Constructors
52

53
54     /**
55      * Construct a new wrapped response around the specified servlet response.
56      *
57      * @param response The servlet response being wrapped
58      */

59     public ApplicationHttpResponse(HttpServletResponse JavaDoc response) {
60
61         this(response, false);
62
63     }
64
65
66     /**
67      * Construct a new wrapped response around the specified servlet response.
68      *
69      * @param response The servlet response being wrapped
70      * @param included <code>true</code> if this response is being processed
71      * by a <code>RequestDispatcher.include()</code> call
72      */

73     public ApplicationHttpResponse(HttpServletResponse JavaDoc response,
74                                    boolean included) {
75
76         super(response);
77         setIncluded(included);
78
79     }
80
81
82     // ----------------------------------------------------- Instance Variables
83

84
85     /**
86      * Is this wrapped response the subject of an <code>include()</code>
87      * call?
88      */

89     protected boolean included = false;
90
91
92     /**
93      * Descriptive information about this implementation.
94      */

95     protected static final String JavaDoc info =
96         "org.apache.catalina.core.ApplicationHttpResponse/1.0";
97
98
99     /**
100      * The string manager for this package.
101      */

102     protected static StringManager sm =
103         StringManager.getManager(Constants.Package);
104
105
106     // ------------------------------------------------ ServletResponse Methods
107

108
109     /**
110      * Disallow <code>reset()</code> calls on a included response.
111      *
112      * @exception IllegalStateException if the response has already
113      * been committed
114      */

115     public void reset() {
116
117         // If already committed, the wrapped response will throw ISE
118
if (!included || getResponse().isCommitted())
119             getResponse().reset();
120
121     }
122
123
124     /**
125      * Disallow <code>setContentLength()</code> calls on an included response.
126      *
127      * @param len The new content length
128      */

129     public void setContentLength(int len) {
130
131         if (!included)
132             getResponse().setContentLength(len);
133
134     }
135
136
137     /**
138      * Disallow <code>setContentType()</code> calls on an included response.
139      *
140      * @param type The new content type
141      */

142     public void setContentType(String JavaDoc type) {
143
144         if (!included)
145             getResponse().setContentType(type);
146
147     }
148
149
150     /**
151      * Disallow <code>setLocale()</code> calls on an included response.
152      *
153      * @param loc The new locale
154      */

155     public void setLocale(Locale JavaDoc loc) {
156
157         if (!included)
158             getResponse().setLocale(loc);
159
160     }
161
162
163     /**
164      * Ignore <code>setBufferSize()</code> calls on an included response.
165      *
166      * @param size The buffer size
167      */

168     public void setBufferSize(int size) {
169         if (!included)
170             getResponse().setBufferSize(size);
171     }
172
173
174     // -------------------------------------------- HttpServletResponse Methods
175

176
177     /**
178      * Disallow <code>addCookie()</code> calls on an included response.
179      *
180      * @param cookie The new cookie
181      */

182     public void addCookie(Cookie JavaDoc cookie) {
183
184         if (!included)
185             ((HttpServletResponse JavaDoc) getResponse()).addCookie(cookie);
186
187     }
188
189
190     /**
191      * Disallow <code>addDateHeader()</code> calls on an included response.
192      *
193      * @param name The new header name
194      * @param value The new header value
195      */

196     public void addDateHeader(String JavaDoc name, long value) {
197
198         if (!included)
199             ((HttpServletResponse JavaDoc) getResponse()).addDateHeader(name, value);
200
201     }
202
203
204     /**
205      * Disallow <code>addHeader()</code> calls on an included response.
206      *
207      * @param name The new header name
208      * @param value The new header value
209      */

210     public void addHeader(String JavaDoc name, String JavaDoc value) {
211
212         if (!included)
213             ((HttpServletResponse JavaDoc) getResponse()).addHeader(name, value);
214
215     }
216
217
218     /**
219      * Disallow <code>addIntHeader()</code> calls on an included response.
220      *
221      * @param name The new header name
222      * @param value The new header value
223      */

224     public void addIntHeader(String JavaDoc name, int value) {
225
226         if (!included)
227             ((HttpServletResponse JavaDoc) getResponse()).addIntHeader(name, value);
228
229     }
230
231
232     /**
233      * Disallow <code>sendError()</code> calls on an included response.
234      *
235      * @param sc The new status code
236      *
237      * @exception IOException if an input/output error occurs
238      */

239     public void sendError(int sc) throws IOException JavaDoc {
240
241         if (!included)
242             ((HttpServletResponse JavaDoc) getResponse()).sendError(sc);
243
244     }
245
246
247     /**
248      * Disallow <code>sendError()</code> calls on an included response.
249      *
250      * @param sc The new status code
251      * @param msg The new message
252      *
253      * @exception IOException if an input/output error occurs
254      */

255     public void sendError(int sc, String JavaDoc msg) throws IOException JavaDoc {
256
257         if (!included)
258             ((HttpServletResponse JavaDoc) getResponse()).sendError(sc, msg);
259
260     }
261
262
263     /**
264      * Disallow <code>sendRedirect()</code> calls on an included response.
265      *
266      * @param location The new location
267      *
268      * @exception IOException if an input/output error occurs
269      */

270     public void sendRedirect(String JavaDoc location) throws IOException JavaDoc {
271
272         if (!included)
273             ((HttpServletResponse JavaDoc) getResponse()).sendRedirect(location);
274
275     }
276
277
278     /**
279      * Disallow <code>setDateHeader()</code> calls on an included response.
280      *
281      * @param name The new header name
282      * @param value The new header value
283      */

284     public void setDateHeader(String JavaDoc name, long value) {
285
286         if (!included)
287             ((HttpServletResponse JavaDoc) getResponse()).setDateHeader(name, value);
288
289     }
290
291
292     /**
293      * Disallow <code>setHeader()</code> calls on an included response.
294      *
295      * @param name The new header name
296      * @param value The new header value
297      */

298     public void setHeader(String JavaDoc name, String JavaDoc value) {
299
300         if (!included)
301             ((HttpServletResponse JavaDoc) getResponse()).setHeader(name, value);
302
303     }
304
305
306     /**
307      * Disallow <code>setIntHeader()</code> calls on an included response.
308      *
309      * @param name The new header name
310      * @param value The new header value
311      */

312     public void setIntHeader(String JavaDoc name, int value) {
313
314         if (!included)
315             ((HttpServletResponse JavaDoc) getResponse()).setIntHeader(name, value);
316
317     }
318
319
320     /**
321      * Disallow <code>setStatus()</code> calls on an included response.
322      *
323      * @param sc The new status code
324      */

325     public void setStatus(int sc) {
326
327         if (!included)
328             ((HttpServletResponse JavaDoc) getResponse()).setStatus(sc);
329
330     }
331
332
333     /**
334      * Disallow <code>setStatus()</code> calls on an included response.
335      *
336      * @param sc The new status code
337      * @param msg The new message
338      */

339     public void setStatus(int sc, String JavaDoc msg) {
340
341         if (!included)
342             ((HttpServletResponse JavaDoc) getResponse()).setStatus(sc, msg);
343
344     }
345
346
347     // -------------------------------------------------------- Package Methods
348

349
350     /**
351      * Return descriptive information about this implementation.
352      */

353     public String JavaDoc getInfo() {
354
355         return (info);
356
357     }
358
359
360     /**
361      * Return the included flag for this response.
362      */

363     boolean isIncluded() {
364
365         return (this.included);
366
367     }
368
369
370     /**
371      * Set the included flag for this response.
372      *
373      * @param included The new included flag
374      */

375     void setIncluded(boolean included) {
376
377         this.included = included;
378
379     }
380
381
382     /**
383      * Set the response that we are wrapping.
384      *
385      * @param response The new wrapped response
386      */

387     void setResponse(HttpServletResponse JavaDoc response) {
388
389         super.setResponse(response);
390
391     }
392
393
394 }
395
Popular Tags