1 2 3 27 28 29 package org.apache.coyote.tomcat5; 30 31 import java.io.IOException ; 32 import java.io.OutputStream ; 33 import java.io.PrintWriter ; 34 import java.security.AccessController ; 35 import java.security.PrivilegedAction ; 36 import java.security.PrivilegedExceptionAction ; 37 import java.security.PrivilegedActionException ; 38 import java.security.AccessControlException ; 39 import java.security.Permission ; 40 import java.security.SecurityPermission ; 41 import java.util.Locale ; 42 import javax.servlet.ServletException ; 43 import javax.servlet.ServletOutputStream ; 44 import javax.servlet.ServletResponse ; 45 import javax.servlet.http.Cookie ; 46 import javax.servlet.http.HttpServletResponse ; 47 48 import org.apache.catalina.connector.ResponseFacade; 49 import org.apache.catalina.security.SecurityUtil; 50 import org.apache.catalina.util.StringManager; 51 52 60 61 62 public class CoyoteResponseFacade 63 extends ResponseFacade 64 implements HttpServletResponse { 65 66 68 private final class SetContentTypePrivilegedAction 69 implements PrivilegedAction { 70 71 private String contentType; 72 73 public SetContentTypePrivilegedAction(String contentType){ 74 this.contentType = contentType; 75 } 76 77 public Object run() { 78 response.setContentType(contentType); 79 return null; 80 } 81 } 82 83 84 86 87 92 public CoyoteResponseFacade(CoyoteResponse response) { 93 94 super(response); 95 this.response = response; 96 97 } 98 99 100 102 103 106 protected static StringManager sm = 107 StringManager.getManager(Constants.Package); 108 109 110 113 protected CoyoteResponse response = null; 114 115 116 118 119 122 protected Object clone() 123 throws CloneNotSupportedException { 124 throw new CloneNotSupportedException (); 125 } 126 127 128 131 public void clear() { 132 response = null; 133 } 134 135 136 public void finish() { 137 138 if (response == null) { 139 throw new IllegalStateException ( 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 ( 152 sm.getString("responseFacade.nullResponse")); 153 } 154 155 return response.isSuspended(); 156 157 } 158 159 160 162 163 public String getCharacterEncoding() { 164 165 if (response == null) { 166 throw new IllegalStateException ( 167 sm.getString("responseFacade.nullResponse")); 168 } 169 170 return response.getCharacterEncoding(); 171 } 172 173 174 public ServletOutputStream getOutputStream() 175 throws IOException { 176 177 181 ServletOutputStream sos = response.getOutputStream(); 182 if (isFinished()) 183 response.setSuspended(true); 184 return (sos); 185 186 } 187 188 189 public PrintWriter getWriter() 190 throws IOException { 191 192 196 PrintWriter 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 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 231 (); 232 233 response.setBufferSize(size); 234 235 } 236 237 238 public int getBufferSize() { 239 240 if (response == null) { 241 throw new IllegalStateException ( 242 sm.getString("responseFacade.nullResponse")); 243 } 244 245 return response.getBufferSize(); 246 } 247 248 249 public void flushBuffer() 250 throws IOException { 251 252 if (isFinished()) 253 return; 256 257 if (SecurityUtil.isPackageProtectionEnabled()){ 258 try{ 259 AccessController.doPrivileged(new PrivilegedExceptionAction (){ 260 261 public Object run() throws IOException { 262 response.setAppCommitted(true); 263 264 response.flushBuffer(); 265 return null; 266 } 267 }); 268 } catch(PrivilegedActionException e){ 269 Exception ex = e.getException(); 270 if (ex instanceof IOException ){ 271 throw (IOException )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 287 (); 288 289 response.resetBuffer(); 290 291 } 292 293 294 public boolean isCommitted() { 295 296 if (response == null) { 297 throw new IllegalStateException ( 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 309 (); 310 311 response.reset(); 312 313 } 314 315 316 public void setLocale(Locale loc) { 317 318 if (isCommitted()) 319 return; 320 321 response.setLocale(loc); 322 } 323 324 325 public Locale getLocale() { 326 327 if (response == null) { 328 throw new IllegalStateException ( 329 sm.getString("responseFacade.nullResponse")); 330 } 331 332 return response.getLocale(); 333 } 334 335 336 public void addCookie(Cookie cookie) { 337 338 if (isCommitted()) 339 return; 340 341 response.addCookie(cookie); 342 343 } 344 345 346 public boolean containsHeader(String name) { 347 348 if (response == null) { 349 throw new IllegalStateException ( 350 sm.getString("responseFacade.nullResponse")); 351 } 352 353 return response.containsHeader(name); 354 } 355 356 357 public String encodeURL(String url) { 358 359 if (response == null) { 360 throw new IllegalStateException ( 361 sm.getString("responseFacade.nullResponse")); 362 } 363 364 return response.encodeURL(url); 365 } 366 367 368 public String encodeRedirectURL(String url) { 369 370 if (response == null) { 371 throw new IllegalStateException ( 372 sm.getString("responseFacade.nullResponse")); 373 } 374 375 return response.encodeRedirectURL(url); 376 } 377 378 379 public String encodeUrl(String url) { 380 381 if (response == null) { 382 throw new IllegalStateException ( 383 sm.getString("responseFacade.nullResponse")); 384 } 385 386 return response.encodeURL(url); 387 } 388 389 390 public String encodeRedirectUrl(String url) { 391 392 if (response == null) { 393 throw new IllegalStateException ( 394 sm.getString("responseFacade.nullResponse")); 395 } 396 397 return response.encodeRedirectURL(url); 398 } 399 400 401 public void sendError(int sc, String msg) 402 throws IOException { 403 404 if (isCommitted()) 405 throw new IllegalStateException 406 (); 407 408 response.setAppCommitted(true); 409 410 response.sendError(sc, msg); 411 412 } 413 414 415 public void sendError(int sc) 416 throws IOException { 417 418 if (isCommitted()) 419 throw new IllegalStateException 420 (); 421 422 response.setAppCommitted(true); 423 424 response.sendError(sc); 425 426 } 427 428 429 public void sendRedirect(String location) 430 throws IOException { 431 432 if (isCommitted()) 433 throw new IllegalStateException 434 (); 435 436 response.setAppCommitted(true); 437 438 response.sendRedirect(location); 439 440 } 441 442 443 public void setDateHeader(String name, long date) { 444 445 if (isCommitted()) 446 return; 447 448 response.setDateHeader(name, date); 449 450 } 451 452 453 public void addDateHeader(String name, long date) { 454 455 if (isCommitted()) 456 return; 457 458 response.addDateHeader(name, date); 459 460 } 461 462 463 public void setHeader(String name, String value) { 464 465 if (isCommitted()) 466 return; 467 468 response.setHeader(name, value); 469 470 } 471 472 473 public void addHeader(String name, String value) { 474 475 if (isCommitted()) 476 return; 477 478 response.addHeader(name, value); 479 480 } 481 482 483 public void setIntHeader(String name, int value) { 484 485 if (isCommitted()) 486 return; 487 488 response.setIntHeader(name, value); 489 490 } 491 492 493 public void addIntHeader(String 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 sm) { 514 515 if (isCommitted()) 516 return; 517 518 response.setStatus(sc, sm); 519 520 } 521 522 523 public int getStatus() { 525 return response.getStatus(); 526 } 527 528 public String 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 541 542 546 public CoyoteResponse getUnwrappedCoyoteResponse() 547 throws AccessControlException { 548 549 SecurityManager sm = System.getSecurityManager(); 552 if (sm != null) { 553 Permission perm = 554 new SecurityPermission ("getUnwrappedCoyoteResponse"); 555 AccessController.checkPermission(perm); 556 } 557 558 return response; 559 } 560 } 562 | Popular Tags |