KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > coyote > tomcat5 > CoyoteResponseFacade


1
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the "License"). You may not use this file except
7  * in compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * glassfish/bootstrap/legal/CDDLv1.0.txt or
11  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * HEADER in each file and include the License file at
17  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
18  * add the following below this CDDL HEADER, with the
19  * fields enclosed by brackets "[]" replaced with your
20  * own identifying information: Portions Copyright [yyyy]
21  * [name of copyright owner]
22  *
23  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24  *
25  * Portions Copyright Apache Software Foundation.
26  */

27
28
29 package org.apache.coyote.tomcat5;
30
31 import java.io.IOException JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34 import java.security.AccessController JavaDoc;
35 import java.security.PrivilegedAction JavaDoc;
36 import java.security.PrivilegedExceptionAction JavaDoc;
37 import java.security.PrivilegedActionException JavaDoc;
38 import java.security.AccessControlException JavaDoc;
39 import java.security.Permission JavaDoc;
40 import java.security.SecurityPermission JavaDoc;
41 import java.util.Locale JavaDoc;
42 import javax.servlet.ServletException JavaDoc;
43 import javax.servlet.ServletOutputStream JavaDoc;
44 import javax.servlet.ServletResponse JavaDoc;
45 import javax.servlet.http.Cookie JavaDoc;
46 import javax.servlet.http.HttpServletResponse JavaDoc;
47
48 import org.apache.catalina.connector.ResponseFacade;
49 import org.apache.catalina.security.SecurityUtil;
50 import org.apache.catalina.util.StringManager;
51
52 /**
53  * Facade class that wraps a Coyote response object.
54  * All methods are delegated to the wrapped response.
55  *
56  * @author Remy Maucherat
57  * @author Jean-Francois Arcand
58  * @version $Revision: 1.5 $ $Date: 2006/03/10 17:46:38 $
59  */

60
61
62 public class CoyoteResponseFacade
63     extends ResponseFacade
64     implements HttpServletResponse JavaDoc {
65
66     // ----------------------------------------------------------- DoPrivileged
67

68     private final class SetContentTypePrivilegedAction
69             implements PrivilegedAction JavaDoc {
70
71         private String JavaDoc contentType;
72
73         public SetContentTypePrivilegedAction(String JavaDoc contentType){
74             this.contentType = contentType;
75         }
76         
77         public Object JavaDoc run() {
78             response.setContentType(contentType);
79             return null;
80         }
81     }
82      
83     
84     // ----------------------------------------------------------- Constructors
85

86
87     /**
88      * Construct a wrapper for the specified response.
89      *
90      * @param response The response to be wrapped
91      */

92     public CoyoteResponseFacade(CoyoteResponse response) {
93
94         super(response);
95         this.response = response;
96
97     }
98
99
100     // ----------------------------------------------- Class/Instance Variables
101

102
103     /**
104      * The string manager for this package.
105      */

106     protected static StringManager sm =
107         StringManager.getManager(Constants.Package);
108
109
110     /**
111      * The wrapped response.
112      */

113     protected CoyoteResponse response = null;
114
115
116     // --------------------------------------------------------- Public Methods
117

118     
119     /**
120     * Prevent cloning the facade.
121     */

122     protected Object JavaDoc clone()
123         throws CloneNotSupportedException JavaDoc {
124         throw new CloneNotSupportedException JavaDoc();
125     }
126       
127     
128     /**
129      * Clear facade.
130      */

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

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

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

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

541
542     //START S1AS 4703023
543
/**
544      * Return the original <code>CoyoteRequest</code> object.
545      */

546     public CoyoteResponse getUnwrappedCoyoteResponse()
547         throws AccessControlException JavaDoc {
548
549         // tomcat does not have any Permission types so instead of
550
// creating a TomcatPermission for this, use SecurityPermission.
551
SecurityManager JavaDoc sm = System.getSecurityManager();
552         if (sm != null) {
553             Permission JavaDoc perm =
554                 new SecurityPermission JavaDoc("getUnwrappedCoyoteResponse");
555             AccessController.checkPermission(perm);
556         }
557
558         return response;
559     }
560     //START S1AS 4703023
561
}
562
Popular Tags