KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > connector > ResponseFacade


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.connector;
20
21 import java.io.IOException JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.security.AccessController JavaDoc;
24 import java.security.PrivilegedAction JavaDoc;
25 import java.security.PrivilegedActionException JavaDoc;
26 import java.security.PrivilegedExceptionAction JavaDoc;
27 import java.util.Locale JavaDoc;
28
29 import javax.servlet.ServletOutputStream JavaDoc;
30 import javax.servlet.http.Cookie JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import org.apache.catalina.util.StringManager;
34 import org.apache.catalina.security.SecurityUtil;
35
36 /**
37  * Facade class that wraps a Coyote response object.
38  * All methods are delegated to the wrapped response.
39  *
40  * @author Remy Maucherat
41  * @author Jean-Francois Arcand
42  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
43  */

44 @SuppressWarnings JavaDoc("deprecation")
45 public class ResponseFacade
46     implements HttpServletResponse JavaDoc {
47
48
49     // ----------------------------------------------------------- DoPrivileged
50

51     private final class SetContentTypePrivilegedAction
52             implements PrivilegedAction JavaDoc {
53
54         private String JavaDoc contentType;
55
56         public SetContentTypePrivilegedAction(String JavaDoc contentType){
57             this.contentType = contentType;
58         }
59         
60         public Object JavaDoc run() {
61             response.setContentType(contentType);
62             return null;
63         }
64     }
65
66     private final class DateHeaderPrivilegedAction
67             implements PrivilegedAction JavaDoc {
68
69         private String JavaDoc name;
70         private long value;
71         private boolean add;
72
73         DateHeaderPrivilegedAction(String JavaDoc name, long value, boolean add) {
74             this.name = name;
75             this.value = value;
76             this.add = add;
77         }
78
79         public Object JavaDoc run() {
80             if(add) {
81                 response.addDateHeader(name, value);
82             } else {
83                 response.setDateHeader(name, value);
84             }
85             return null;
86         }
87     }
88     
89     // ----------------------------------------------------------- Constructors
90

91
92     /**
93      * Construct a wrapper for the specified response.
94      *
95      * @param response The response to be wrapped
96      */

97     public ResponseFacade(Response response) {
98
99          this.response = response;
100     }
101
102
103     // ----------------------------------------------- Class/Instance Variables
104

105
106     /**
107      * The string manager for this package.
108      */

109     protected static StringManager sm =
110         StringManager.getManager(Constants.Package);
111
112
113     /**
114      * The wrapped response.
115      */

116     protected Response response = null;
117
118
119     // --------------------------------------------------------- Public Methods
120

121
122     /**
123      * Clear facade.
124      */

125     public void clear() {
126         response = null;
127     }
128
129
130     /**
131      * Prevent cloning the facade.
132      */

133     protected Object JavaDoc clone()
134         throws CloneNotSupportedException JavaDoc {
135         throw new CloneNotSupportedException JavaDoc();
136     }
137
138
139     public void finish() {
140
141         if (response == null) {
142             throw new IllegalStateException JavaDoc(
143                             sm.getString("responseFacade.nullResponse"));
144         }
145
146         response.setSuspended(true);
147     }
148
149
150     public boolean isFinished() {
151
152         if (response == null) {
153             throw new IllegalStateException JavaDoc(
154                             sm.getString("responseFacade.nullResponse"));
155         }
156
157         return response.isSuspended();
158     }
159
160
161     // ------------------------------------------------ ServletResponse Methods
162

163
164     public String JavaDoc getCharacterEncoding() {
165
166         if (response == null) {
167             throw new IllegalStateException JavaDoc(
168                             sm.getString("responseFacade.nullResponse"));
169         }
170
171         return response.getCharacterEncoding();
172     }
173
174
175     public ServletOutputStream JavaDoc getOutputStream()
176         throws IOException JavaDoc {
177
178         // if (isFinished())
179
// throw new IllegalStateException
180
// (/*sm.getString("responseFacade.finished")*/);
181

182         ServletOutputStream JavaDoc sos = response.getOutputStream();
183         if (isFinished())
184             response.setSuspended(true);
185         return (sos);
186
187     }
188
189
190     public PrintWriter JavaDoc getWriter()
191         throws IOException JavaDoc {
192
193         // if (isFinished())
194
// throw new IllegalStateException
195
// (/*sm.getString("responseFacade.finished")*/);
196

197         PrintWriter JavaDoc writer = response.getWriter();
198         if (isFinished())
199             response.setSuspended(true);
200         return (writer);
201
202     }
203
204
205     public void setContentLength(int len) {
206
207         if (isCommitted())
208             return;
209
210         response.setContentLength(len);
211
212     }
213
214
215     public void setContentType(String JavaDoc type) {
216
217         if (isCommitted())
218             return;
219         
220         if (SecurityUtil.isPackageProtectionEnabled()){
221             AccessController.doPrivileged(new SetContentTypePrivilegedAction(type));
222         } else {
223             response.setContentType(type);
224         }
225     }
226
227
228     public void setBufferSize(int size) {
229
230         if (isCommitted())
231             throw new IllegalStateException JavaDoc
232                 (/*sm.getString("responseBase.reset.ise")*/);
233
234         response.setBufferSize(size);
235
236     }
237
238
239     public int getBufferSize() {
240
241         if (response == null) {
242             throw new IllegalStateException JavaDoc(
243                             sm.getString("responseFacade.nullResponse"));
244         }
245
246         return response.getBufferSize();
247     }
248
249
250     public void flushBuffer()
251         throws IOException JavaDoc {
252
253         if (isFinished())
254             // throw new IllegalStateException
255
// (/*sm.getString("responseFacade.finished")*/);
256
return;
257
258         if (SecurityUtil.isPackageProtectionEnabled()){
259             try{
260                 AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc(){
261
262                     public Object JavaDoc run() throws IOException JavaDoc{
263                         response.setAppCommitted(true);
264
265                         response.flushBuffer();
266                         return null;
267                     }
268                 });
269             } catch(PrivilegedActionException JavaDoc e){
270                 Exception JavaDoc ex = e.getException();
271                 if (ex instanceof IOException JavaDoc){
272                     throw (IOException JavaDoc)ex;
273                 }
274             }
275         } else {
276             response.setAppCommitted(true);
277
278             response.flushBuffer();
279         }
280
281     }
282
283
284     public void resetBuffer() {
285
286         if (isCommitted())
287             throw new IllegalStateException JavaDoc
288                 (/*sm.getString("responseBase.reset.ise")*/);
289
290         response.resetBuffer();
291
292     }
293
294
295     public boolean isCommitted() {
296
297         if (response == null) {
298             throw new IllegalStateException JavaDoc(
299                             sm.getString("responseFacade.nullResponse"));
300         }
301
302         return (response.isAppCommitted());
303     }
304
305
306     public void reset() {
307
308         if (isCommitted())
309             throw new IllegalStateException JavaDoc
310                 (/*sm.getString("responseBase.reset.ise")*/);
311
312         response.reset();
313
314     }
315
316
317     public void setLocale(Locale JavaDoc loc) {
318
319         if (isCommitted())
320             return;
321
322         response.setLocale(loc);
323     }
324
325
326     public Locale JavaDoc getLocale() {
327
328         if (response == null) {
329             throw new IllegalStateException JavaDoc(
330                             sm.getString("responseFacade.nullResponse"));
331         }
332
333         return response.getLocale();
334     }
335
336
337     public void addCookie(Cookie JavaDoc cookie) {
338
339         if (isCommitted())
340             return;
341
342         response.addCookie(cookie);
343
344     }
345
346
347     public boolean containsHeader(String JavaDoc name) {
348
349         if (response == null) {
350             throw new IllegalStateException JavaDoc(
351                             sm.getString("responseFacade.nullResponse"));
352         }
353
354         return response.containsHeader(name);
355     }
356
357
358     public String JavaDoc encodeURL(String JavaDoc url) {
359
360         if (response == null) {
361             throw new IllegalStateException JavaDoc(
362                             sm.getString("responseFacade.nullResponse"));
363         }
364
365         return response.encodeURL(url);
366     }
367
368
369     public String JavaDoc encodeRedirectURL(String JavaDoc url) {
370
371         if (response == null) {
372             throw new IllegalStateException JavaDoc(
373                             sm.getString("responseFacade.nullResponse"));
374         }
375
376         return response.encodeRedirectURL(url);
377     }
378
379
380     public String JavaDoc encodeUrl(String JavaDoc url) {
381
382         if (response == null) {
383             throw new IllegalStateException JavaDoc(
384                             sm.getString("responseFacade.nullResponse"));
385         }
386
387         return response.encodeURL(url);
388     }
389
390
391     public String JavaDoc encodeRedirectUrl(String JavaDoc url) {
392
393         if (response == null) {
394             throw new IllegalStateException JavaDoc(
395                             sm.getString("responseFacade.nullResponse"));
396         }
397
398         return response.encodeRedirectURL(url);
399     }
400
401
402     public void sendError(int sc, String JavaDoc msg)
403         throws IOException JavaDoc {
404
405         if (isCommitted())
406             throw new IllegalStateException JavaDoc
407                 (/*sm.getString("responseBase.reset.ise")*/);
408
409         response.setAppCommitted(true);
410
411         response.sendError(sc, msg);
412
413     }
414
415
416     public void sendError(int sc)
417         throws IOException JavaDoc {
418
419         if (isCommitted())
420             throw new IllegalStateException JavaDoc
421                 (/*sm.getString("responseBase.reset.ise")*/);
422
423         response.setAppCommitted(true);
424
425         response.sendError(sc);
426
427     }
428
429
430     public void sendRedirect(String JavaDoc location)
431         throws IOException JavaDoc {
432
433         if (isCommitted())
434             throw new IllegalStateException JavaDoc
435                 (/*sm.getString("responseBase.reset.ise")*/);
436
437         response.setAppCommitted(true);
438
439         response.sendRedirect(location);
440
441     }
442
443
444     public void setDateHeader(String JavaDoc name, long date) {
445
446         if (isCommitted())
447             return;
448
449         if(System.getSecurityManager() != null) {
450             AccessController.doPrivileged(new DateHeaderPrivilegedAction
451                                              (name, date, false));
452         } else {
453             response.setDateHeader(name, date);
454         }
455
456     }
457
458
459     public void addDateHeader(String JavaDoc name, long date) {
460
461         if (isCommitted())
462             return;
463
464         if(System.getSecurityManager() != null) {
465             AccessController.doPrivileged(new DateHeaderPrivilegedAction
466                                              (name, date, true));
467         } else {
468             response.addDateHeader(name, date);
469         }
470
471     }
472
473
474     public void setHeader(String JavaDoc name, String JavaDoc value) {
475
476         if (isCommitted())
477             return;
478
479         response.setHeader(name, value);
480
481     }
482
483
484     public void addHeader(String JavaDoc name, String JavaDoc value) {
485
486         if (isCommitted())
487             return;
488
489         response.addHeader(name, value);
490
491     }
492
493
494     public void setIntHeader(String JavaDoc name, int value) {
495
496         if (isCommitted())
497             return;
498
499         response.setIntHeader(name, value);
500
501     }
502
503
504     public void addIntHeader(String JavaDoc name, int value) {
505
506         if (isCommitted())
507             return;
508
509         response.addIntHeader(name, value);
510
511     }
512
513
514     public void setStatus(int sc) {
515
516         if (isCommitted())
517             return;
518
519         response.setStatus(sc);
520
521     }
522
523
524     public void setStatus(int sc, String JavaDoc sm) {
525
526         if (isCommitted())
527             return;
528
529         response.setStatus(sc, sm);
530     }
531
532
533     public String JavaDoc getContentType() {
534
535         if (response == null) {
536             throw new IllegalStateException JavaDoc(
537                             sm.getString("responseFacade.nullResponse"));
538         }
539
540         return response.getContentType();
541     }
542
543
544     public void setCharacterEncoding(String JavaDoc arg0) {
545
546         if (response == null) {
547             throw new IllegalStateException JavaDoc(
548                             sm.getString("responseFacade.nullResponse"));
549         }
550
551         response.setCharacterEncoding(arg0);
552     }
553
554 }
555
Popular Tags