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;
22 import java.io.PrintWriter;
23 import java.security.AccessController;
24 import java.security.PrivilegedAction;
25 import java.security.PrivilegedActionException;
26 import java.security.PrivilegedExceptionAction;
27 import java.util.Locale;
28
29 import javax.servlet.ServletOutputStream;
30 import javax.servlet.http.Cookie;
31 import javax.servlet.http.HttpServletResponse;
32
33 import org.apache.catalina.Globals;
34 import org.apache.catalina.util.StringManager;
35 import org.apache.catalina.security.SecurityUtil;
36
37 /**
38 * Facade class that wraps a Coyote response object.
39 * All methods are delegated to the wrapped response.
40 *
41 * @author Remy Maucherat
42 * @author Jean-Francois Arcand
43 * @version $Revision: 505593 $ $Date: 2007-02-10 01:54:56 +0100 (sam., 10 févr. 2007) $
44 */
45 @SuppressWarnings("deprecation")
46 public class ResponseFacade
47 implements HttpServletResponse {
48
49
50 // ----------------------------------------------------------- DoPrivileged
51
52 private final class SetContentTypePrivilegedAction
53 implements PrivilegedAction {
54
55 private String contentType;
56
57 public SetContentTypePrivilegedAction(String contentType){
58 this.contentType = contentType;
59 }
60
61 public Object run() {
62 response.setContentType(contentType);
63 return null;
64 }
65 }
66
67 private final class DateHeaderPrivilegedAction
68 implements PrivilegedAction {
69
70 private String name;
71 private long value;
72 private boolean add;
73
74 DateHeaderPrivilegedAction(String name, long value, boolean add) {
75 this.name = name;
76 this.value = value;
77 this.add = add;
78 }
79
80 public Object run() {
81 if(add) {
82 response.addDateHeader(name, value);
83 } else {
84 response.setDateHeader(name, value);
85 }
86 return null;
87 }
88 }
89
90 // ----------------------------------------------------------- Constructors
91
92
93 /**
94 * Construct a wrapper for the specified response.
95 *
96 * @param response The response to be wrapped
97 */
98 public ResponseFacade(Response response) {
99
100 this.response = response;
101 }
102
103
104 // ----------------------------------------------- Class/Instance Variables
105
106
107 /**
108 * The string manager for this package.
109 */
110 protected static StringManager sm =
111 StringManager.getManager(Constants.Package);
112
113
114 /**
115 * The wrapped response.
116 */
117 protected Response response = null;
118
119
120 // --------------------------------------------------------- Public Methods
121
122
123 /**
124 * Clear facade.
125 */
126 public void clear() {
127 response = null;
128 }
129
130
131 /**
132 * Prevent cloning the facade.
133 */
134 protected Object clone()
135 throws CloneNotSupportedException {
136 throw new CloneNotSupportedException();
137 }
138
139
140 public void finish() {
141
142 if (response == null) {
143 throw new IllegalStateException(
144 sm.getString("responseFacade.nullResponse"));
145 }
146
147 response.setSuspended(true);
148 }
149
150
151 public boolean isFinished() {
152
153 if (response == null) {
154 throw new IllegalStateException(
155 sm.getString("responseFacade.nullResponse"));
156 }
157
158 return response.isSuspended();
159 }
160
161
162 // ------------------------------------------------ ServletResponse Methods
163
164
165 public String getCharacterEncoding() {
166
167 if (response == null) {
168 throw new IllegalStateException(
169 sm.getString("responseFacade.nullResponse"));
170 }
171
172 return response.getCharacterEncoding();
173 }
174
175
176 public ServletOutputStream getOutputStream()
177 throws IOException {
178
179 // if (isFinished())
180 // throw new IllegalStateException
181 // (/*sm.getString("responseFacade.finished")*/);
182
183 ServletOutputStream sos = response.getOutputStream();
184 if (isFinished())
185 response.setSuspended(true);
186 return (sos);
187
188 }
189
190
191 public PrintWriter getWriter()
192 throws IOException {
193
194 // if (isFinished())
195 // throw new IllegalStateException
196 // (/*sm.getString("responseFacade.finished")*/);
197
198 PrintWriter writer = response.getWriter();
199 if (isFinished())
200 response.setSuspended(true);
201 return (writer);
202
203 }
204
205
206 public void setContentLength(int len) {
207
208 if (isCommitted())
209 return;
210
211 response.setContentLength(len);
212
213 }
214
215
216 public void setContentType(String type) {
217
218 if (isCommitted())
219 return;
220
221 if (SecurityUtil.isPackageProtectionEnabled()){
222 AccessController.doPrivileged(new SetContentTypePrivilegedAction(type));
223 } else {
224 response.setContentType(type);
225 }
226 }
227
228
229 public void setBufferSize(int size) {
230
231 if (isCommitted())
232 throw new IllegalStateException
233 (/*sm.getString("responseBase.reset.ise")*/);
234
235 response.setBufferSize(size);
236
237 }
238
239
240 public int getBufferSize() {
241
242 if (response == null) {
243 throw new IllegalStateException(
244 sm.getString("responseFacade.nullResponse"));
245 }
246
247 return response.getBufferSize();
248 }
249
250
251 public void flushBuffer()
252 throws IOException {
253
254 if (isFinished())
255 // throw new IllegalStateException
256 // (/*sm.getString("responseFacade.finished")*/);
257 return;
258
259 if (SecurityUtil.isPackageProtectionEnabled()){
260 try{
261 AccessController.doPrivileged(new PrivilegedExceptionAction(){
262
263 public Object run() throws IOException{
264 response.setAppCommitted(true);
265
266 response.flushBuffer();
267 return null;
268 }
269 });
270 } catch(PrivilegedActionException e){
271 Exception ex = e.getException();
272 if (ex instanceof IOException){
273 throw (IOException)ex;
274 }
275 }
276 } else {
277 response.setAppCommitted(true);
278
279 response.flushBuffer();
280 }
281
282 }
283
284
285 public void resetBuffer() {
286
287 if (isCommitted())
288 throw new IllegalStateException
289 (/*sm.getString("responseBase.reset.ise")*/);
290
291 response.resetBuffer();
292
293 }
294
295
296 public boolean isCommitted() {
297
298 if (response == null) {
299 throw new IllegalStateException(
300 sm.getString("responseFacade.nullResponse"));
301 }
302
303 return (response.isAppCommitted());
304 }
305
306
307 public void reset() {
308
309 if (isCommitted())
310 throw new IllegalStateException
311 (/*sm.getString("responseBase.reset.ise")*/);
312
313 response.reset();
314
315 }
316
317
318 public void setLocale(Locale loc) {
319
320 if (isCommitted())
321 return;
322
323 response.setLocale(loc);
324 }
325
326
327 public Locale getLocale() {
328
329 if (response == null) {
330 throw new IllegalStateException(
331 sm.getString("responseFacade.nullResponse"));
332 }
333
334 return response.getLocale();
335 }
336
337
338 public void addCookie(Cookie cookie) {
339
340 if (isCommitted())
341 return;
342
343 response.addCookie(cookie);
344
345 }
346
347
348 public boolean containsHeader(String name) {
349
350 if (response == null) {
351 throw new IllegalStateException(
352 sm.getString("responseFacade.nullResponse"));
353 }
354
355 return response.containsHeader(name);
356 }
357
358
359 public String encodeURL(String url) {
360
361 if (response == null) {
362 throw new IllegalStateException(
363 sm.getString("responseFacade.nullResponse"));
364 }
365
366 return response.encodeURL(url);
367 }
368
369
370 public String encodeRedirectURL(String url) {
371
372 if (response == null) {
373 throw new IllegalStateException(
374 sm.getString("responseFacade.nullResponse"));
375 }
376
377 return response.encodeRedirectURL(url);
378 }
379
380
381 public String encodeUrl(String url) {
382
383 if (response == null) {
384 throw new IllegalStateException(
385 sm.getString("responseFacade.nullResponse"));
386 }
387
388 return response.encodeURL(url);
389 }
390
391
392 public String encodeRedirectUrl(String url) {
393
394 if (response == null) {
395 throw new IllegalStateException(
396 sm.getString("responseFacade.nullResponse"));
397 }
398
399 return response.encodeRedirectURL(url);
400 }
401
402
403 public void sendError(int sc, String msg)
404 throws IOException {
405
406 if (isCommitted())
407 throw new IllegalStateException
408 (/*sm.getString("responseBase.reset.ise")*/);
409
410 response.setAppCommitted(true);
411
412 response.sendError(sc, msg);
413
414 }
415
416
417 public void sendError(int sc)
418 throws IOException {
419
420 if (isCommitted())
421 throw new IllegalStateException
422 (/*sm.getString("responseBase.reset.ise")*/);
423
424 response.setAppCommitted(true);
425
426 response.sendError(sc);
427
428 }
429
430
431 public void sendRedirect(String location)
432 throws IOException {
433
434 if (isCommitted())
435 throw new IllegalStateException
436 (/*sm.getString("responseBase.reset.ise")*/);
437
438 response.setAppCommitted(true);
439
440 response.sendRedirect(location);
441
442 }
443
444
445 public void setDateHeader(String name, long date) {
446
447 if (isCommitted())
448 return;
449
450 if(Globals.IS_SECURITY_ENABLED) {
451 AccessController.doPrivileged(new DateHeaderPrivilegedAction
452 (name, date, false));
453 } else {
454 response.setDateHeader(name, date);
455 }
456
457 }
458
459
460 public void addDateHeader(String name, long date) {
461
462 if (isCommitted())
463 return;
464
465 if(Globals.IS_SECURITY_ENABLED) {
466 AccessController.doPrivileged(new DateHeaderPrivilegedAction
467 (name, date, true));
468 } else {
469 response.addDateHeader(name, date);
470 }
471
472 }
473
474
475 public void setHeader(String name, String value) {
476
477 if (isCommitted())
478 return;
479
480 response.setHeader(name, value);
481
482 }
483
484
485 public void addHeader(String name, String value) {
486
487 if (isCommitted())
488 return;
489
490 response.addHeader(name, value);
491
492 }
493
494
495 public void setIntHeader(String name, int value) {
496
497 if (isCommitted())
498 return;
499
500 response.setIntHeader(name, value);
501
502 }
503
504
505 public void addIntHeader(String name, int value) {
506
507 if (isCommitted())
508 return;
509
510 response.addIntHeader(name, value);
511
512 }
513
514
515 public void setStatus(int sc) {
516
517 if (isCommitted())
518 return;
519
520 response.setStatus(sc);
521
522 }
523
524
525 public void setStatus(int sc, String sm) {
526
527 if (isCommitted())
528 return;
529
530 response.setStatus(sc, sm);
531 }
532
533
534 public String getContentType() {
535
536 if (response == null) {
537 throw new IllegalStateException(
538 sm.getString("responseFacade.nullResponse"));
539 }
540
541 return response.getContentType();
542 }
543
544
545 public void setCharacterEncoding(String arg0) {
546
547 if (response == null) {
548 throw new IllegalStateException(
549 sm.getString("responseFacade.nullResponse"));
550 }
551
552 response.setCharacterEncoding(arg0);
553 }
554
555 }