1 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.util.StringManager; 34 import org.apache.catalina.security.SecurityUtil; 35 36 44 @SuppressWarnings ("deprecation") 45 public class ResponseFacade 46 implements HttpServletResponse { 47 48 49 51 private final class SetContentTypePrivilegedAction 52 implements PrivilegedAction { 53 54 private String contentType; 55 56 public SetContentTypePrivilegedAction(String contentType){ 57 this.contentType = contentType; 58 } 59 60 public Object run() { 61 response.setContentType(contentType); 62 return null; 63 } 64 } 65 66 private final class DateHeaderPrivilegedAction 67 implements PrivilegedAction { 68 69 private String name; 70 private long value; 71 private boolean add; 72 73 DateHeaderPrivilegedAction(String name, long value, boolean add) { 74 this.name = name; 75 this.value = value; 76 this.add = add; 77 } 78 79 public Object run() { 80 if(add) { 81 response.addDateHeader(name, value); 82 } else { 83 response.setDateHeader(name, value); 84 } 85 return null; 86 } 87 } 88 89 91 92 97 public ResponseFacade(Response response) { 98 99 this.response = response; 100 } 101 102 103 105 106 109 protected static StringManager sm = 110 StringManager.getManager(Constants.Package); 111 112 113 116 protected Response response = null; 117 118 119 121 122 125 public void clear() { 126 response = null; 127 } 128 129 130 133 protected Object clone() 134 throws CloneNotSupportedException { 135 throw new CloneNotSupportedException (); 136 } 137 138 139 public void finish() { 140 141 if (response == null) { 142 throw new IllegalStateException ( 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 ( 154 sm.getString("responseFacade.nullResponse")); 155 } 156 157 return response.isSuspended(); 158 } 159 160 161 163 164 public String getCharacterEncoding() { 165 166 if (response == null) { 167 throw new IllegalStateException ( 168 sm.getString("responseFacade.nullResponse")); 169 } 170 171 return response.getCharacterEncoding(); 172 } 173 174 175 public ServletOutputStream getOutputStream() 176 throws IOException { 177 178 182 ServletOutputStream sos = response.getOutputStream(); 183 if (isFinished()) 184 response.setSuspended(true); 185 return (sos); 186 187 } 188 189 190 public PrintWriter getWriter() 191 throws IOException { 192 193 197 PrintWriter 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 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 232 (); 233 234 response.setBufferSize(size); 235 236 } 237 238 239 public int getBufferSize() { 240 241 if (response == null) { 242 throw new IllegalStateException ( 243 sm.getString("responseFacade.nullResponse")); 244 } 245 246 return response.getBufferSize(); 247 } 248 249 250 public void flushBuffer() 251 throws IOException { 252 253 if (isFinished()) 254 return; 257 258 if (SecurityUtil.isPackageProtectionEnabled()){ 259 try{ 260 AccessController.doPrivileged(new PrivilegedExceptionAction (){ 261 262 public Object run() throws IOException { 263 response.setAppCommitted(true); 264 265 response.flushBuffer(); 266 return null; 267 } 268 }); 269 } catch(PrivilegedActionException e){ 270 Exception ex = e.getException(); 271 if (ex instanceof IOException ){ 272 throw (IOException )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 288 (); 289 290 response.resetBuffer(); 291 292 } 293 294 295 public boolean isCommitted() { 296 297 if (response == null) { 298 throw new IllegalStateException ( 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 310 (); 311 312 response.reset(); 313 314 } 315 316 317 public void setLocale(Locale loc) { 318 319 if (isCommitted()) 320 return; 321 322 response.setLocale(loc); 323 } 324 325 326 public Locale getLocale() { 327 328 if (response == null) { 329 throw new IllegalStateException ( 330 sm.getString("responseFacade.nullResponse")); 331 } 332 333 return response.getLocale(); 334 } 335 336 337 public void addCookie(Cookie cookie) { 338 339 if (isCommitted()) 340 return; 341 342 response.addCookie(cookie); 343 344 } 345 346 347 public boolean containsHeader(String name) { 348 349 if (response == null) { 350 throw new IllegalStateException ( 351 sm.getString("responseFacade.nullResponse")); 352 } 353 354 return response.containsHeader(name); 355 } 356 357 358 public String encodeURL(String url) { 359 360 if (response == null) { 361 throw new IllegalStateException ( 362 sm.getString("responseFacade.nullResponse")); 363 } 364 365 return response.encodeURL(url); 366 } 367 368 369 public String encodeRedirectURL(String url) { 370 371 if (response == null) { 372 throw new IllegalStateException ( 373 sm.getString("responseFacade.nullResponse")); 374 } 375 376 return response.encodeRedirectURL(url); 377 } 378 379 380 public String encodeUrl(String url) { 381 382 if (response == null) { 383 throw new IllegalStateException ( 384 sm.getString("responseFacade.nullResponse")); 385 } 386 387 return response.encodeURL(url); 388 } 389 390 391 public String encodeRedirectUrl(String url) { 392 393 if (response == null) { 394 throw new IllegalStateException ( 395 sm.getString("responseFacade.nullResponse")); 396 } 397 398 return response.encodeRedirectURL(url); 399 } 400 401 402 public void sendError(int sc, String msg) 403 throws IOException { 404 405 if (isCommitted()) 406 throw new IllegalStateException 407 (); 408 409 response.setAppCommitted(true); 410 411 response.sendError(sc, msg); 412 413 } 414 415 416 public void sendError(int sc) 417 throws IOException { 418 419 if (isCommitted()) 420 throw new IllegalStateException 421 (); 422 423 response.setAppCommitted(true); 424 425 response.sendError(sc); 426 427 } 428 429 430 public void sendRedirect(String location) 431 throws IOException { 432 433 if (isCommitted()) 434 throw new IllegalStateException 435 (); 436 437 response.setAppCommitted(true); 438 439 response.sendRedirect(location); 440 441 } 442 443 444 public void setDateHeader(String 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 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 name, String value) { 475 476 if (isCommitted()) 477 return; 478 479 response.setHeader(name, value); 480 481 } 482 483 484 public void addHeader(String name, String value) { 485 486 if (isCommitted()) 487 return; 488 489 response.addHeader(name, value); 490 491 } 492 493 494 public void setIntHeader(String name, int value) { 495 496 if (isCommitted()) 497 return; 498 499 response.setIntHeader(name, value); 500 501 } 502 503 504 public void addIntHeader(String 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 sm) { 525 526 if (isCommitted()) 527 return; 528 529 response.setStatus(sc, sm); 530 } 531 532 533 public String getContentType() { 534 535 if (response == null) { 536 throw new IllegalStateException ( 537 sm.getString("responseFacade.nullResponse")); 538 } 539 540 return response.getContentType(); 541 } 542 543 544 public void setCharacterEncoding(String arg0) { 545 546 if (response == null) { 547 throw new IllegalStateException ( 548 sm.getString("responseFacade.nullResponse")); 549 } 550 551 response.setCharacterEncoding(arg0); 552 } 553 554 } 555 | Popular Tags |