1 20 package org.apache.cactus.server; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.IOException ; 25 26 import java.security.Principal ; 27 28 import java.util.Enumeration ; 29 import java.util.Locale ; 30 31 import javax.servlet.RequestDispatcher ; 32 import javax.servlet.ServletInputStream ; 33 import javax.servlet.http.Cookie ; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpSession ; 36 37 import org.apache.cactus.ServletURL; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 55 public abstract class AbstractHttpServletRequestWrapper 56 implements HttpServletRequest 57 { 58 61 private static final Log LOGGER = 62 LogFactory.getLog(AbstractHttpServletRequestWrapper.class); 63 64 67 protected HttpServletRequest request; 68 69 72 protected ServletURL url; 73 74 78 protected String remoteIPAddress; 79 80 84 protected String remoteHostName; 85 86 90 protected String remoteUser; 91 92 94 103 public AbstractHttpServletRequestWrapper(HttpServletRequest theRequest, 104 ServletURL theURL) 105 { 106 this.request = theRequest; 107 this.url = theURL; 108 } 109 110 113 public HttpServletRequest getOriginalRequest() 114 { 115 return this.request; 116 } 117 118 124 public void setRemoteIPAddress(String theRemoteIPAddress) 125 { 126 this.remoteIPAddress = theRemoteIPAddress; 127 } 128 129 135 public void setRemoteHostName(String theRemoteHostName) 136 { 137 this.remoteHostName = theRemoteHostName; 138 } 139 140 145 public void setRemoteUser(String theRemoteUser) 146 { 147 this.remoteUser = theRemoteUser; 148 } 149 150 152 158 public String getContextPath() 159 { 160 String result = this.request.getContextPath(); 161 162 if ((this.url != null) && (this.url.getContextPath() != null)) 163 { 164 result = this.url.getContextPath(); 165 LOGGER.debug("Using simulated context : [" + result + "]"); 166 } 167 168 return result; 169 } 170 171 175 public String getPathInfo() 176 { 177 String result; 178 179 if (this.url != null) 180 { 181 result = this.url.getPathInfo(); 182 LOGGER.debug("Using simulated PathInfo : [" + result + "]"); 183 } 184 else 185 { 186 result = this.request.getPathInfo(); 187 } 188 189 return result; 190 } 191 192 198 public String getServerName() 199 { 200 String result = this.request.getServerName(); 201 202 if ((this.url != null) && (this.url.getHost() != null)) 203 { 204 result = this.url.getHost(); 205 LOGGER.debug("Using simulated server name : [" + result + "]"); 206 } 207 208 return result; 209 } 210 211 218 public int getServerPort() 219 { 220 int result = this.request.getServerPort(); 221 222 if ((this.url != null) && (this.url.getServerName() != null)) 223 { 224 result = (this.url.getPort() == -1) ? 80 : this.url.getPort(); 225 LOGGER.debug("Using simulated server port : [" + result + "]"); 226 } 227 228 return result; 229 } 230 231 235 public String getRequestURI() 236 { 237 String result; 238 239 if (this.url != null) 240 { 241 result = getContextPath() 242 + ((getServletPath() == null) ? "" : getServletPath()) 243 + ((getPathInfo() == null) ? "" : getPathInfo()); 244 245 LOGGER.debug("Using simulated request URI : [" + result + "]"); 246 } 247 else 248 { 249 result = this.request.getRequestURI(); 250 } 251 252 return result; 253 } 254 255 261 public String getServletPath() 262 { 263 String result = this.request.getServletPath(); 264 265 if ((this.url != null) && (this.url.getServletPath() != null)) 266 { 267 result = this.url.getServletPath(); 268 LOGGER.debug("Using simulated servlet path : [" + result + "]"); 269 } 270 271 return result; 272 } 273 274 279 public String getPathTranslated() 280 { 281 String pathTranslated; 282 283 if ((this.url != null) && (this.url.getPathInfo() != null)) 284 { 285 String pathInfo = this.url.getPathInfo(); 286 287 if (this.request.getRealPath("/") == null) 290 { 291 pathTranslated = null; 292 } 293 else 294 { 295 String newPathInfo = (pathInfo.startsWith("/") 297 ? pathInfo.substring(1) : pathInfo); 298 299 if (this.request.getRealPath("/").endsWith("/")) 300 { 301 pathTranslated = this.request.getRealPath("/") 302 + newPathInfo.replace('/', File.separatorChar); 303 } 304 else 305 { 306 pathTranslated = this.request.getRealPath("/") 307 + File.separatorChar + newPathInfo.replace('/', 308 File.separatorChar); 309 } 310 } 311 } 312 else 313 { 314 pathTranslated = this.request.getPathTranslated(); 315 } 316 317 return pathTranslated; 318 } 319 320 324 public String getQueryString() 325 { 326 String result; 327 328 if (this.url != null) 329 { 330 result = this.url.getQueryString(); 331 LOGGER.debug("Using simulated query string : [" + result + "]"); 332 } 333 else 334 { 335 result = this.request.getQueryString(); 336 } 337 338 return result; 339 } 340 341 350 public RequestDispatcher getRequestDispatcher(String thePath) 351 { 352 if (thePath == null) 358 { 359 return null; 360 } 361 362 RequestDispatcher dispatcher = null; 363 String fullPath; 364 365 if (thePath.startsWith("/")) 369 { 370 fullPath = thePath; 371 } 372 else 373 { 374 String pI = getPathInfo(); 375 376 if (pI == null) 377 { 378 fullPath = catPath(getServletPath(), thePath); 379 } 380 else 381 { 382 fullPath = catPath(getServletPath() + pI, thePath); 383 } 384 385 if (fullPath == null) 386 { 387 return null; 388 } 389 } 390 391 LOGGER.debug("Computed full path : [" + fullPath + "]"); 392 393 dispatcher = new RequestDispatcherWrapper( 394 this.request.getRequestDispatcher(fullPath)); 395 396 return dispatcher; 397 } 398 399 408 private String catPath(String theLookupPath, String thePath) 409 { 410 int index = theLookupPath.lastIndexOf("/"); 412 413 theLookupPath = theLookupPath.substring(0, index); 414 415 while (thePath.startsWith("../")) 417 { 418 if (theLookupPath.length() > 0) 419 { 420 index = theLookupPath.lastIndexOf("/"); 421 theLookupPath = theLookupPath.substring(0, index); 422 } 423 else 424 { 425 return null; 427 } 428 429 index = thePath.indexOf("../") + 3; 430 thePath = thePath.substring(index); 431 } 432 433 return theLookupPath + "/" + thePath; 434 } 435 436 441 public String getRemoteAddr() 442 { 443 String remoteIPAddress; 444 445 if (this.remoteIPAddress != null) 446 { 447 remoteIPAddress = this.remoteIPAddress; 448 } 449 else 450 { 451 remoteIPAddress = this.request.getRemoteAddr(); 452 } 453 454 return remoteIPAddress; 455 } 456 457 462 public String getRemoteHost() 463 { 464 String remoteHostName; 465 466 if (this.remoteHostName != null) 467 { 468 remoteHostName = this.remoteHostName; 469 } 470 else 471 { 472 remoteHostName = this.request.getRemoteHost(); 473 } 474 475 return remoteHostName; 476 } 477 478 483 public String getRemoteUser() 484 { 485 String remoteUser; 486 487 if (this.remoteUser != null) 488 { 489 remoteUser = this.remoteUser; 490 } 491 else 492 { 493 remoteUser = this.request.getRemoteUser(); 494 } 495 496 return remoteUser; 497 } 498 499 501 504 public boolean isRequestedSessionIdFromURL() 505 { 506 return this.request.isRequestedSessionIdFromURL(); 507 } 508 509 512 public boolean isRequestedSessionIdFromUrl() 513 { 514 return this.request.isRequestedSessionIdFromURL(); 515 } 516 517 520 public boolean isUserInRole(String theRole) 521 { 522 return this.request.isUserInRole(theRole); 523 } 524 525 528 public boolean isRequestedSessionIdValid() 529 { 530 return this.request.isRequestedSessionIdValid(); 531 } 532 533 536 public boolean isRequestedSessionIdFromCookie() 537 { 538 return this.request.isRequestedSessionIdFromCookie(); 539 } 540 541 544 public Enumeration getLocales() 545 { 546 return this.request.getLocales(); 547 } 548 549 552 public String getHeader(String theName) 553 { 554 return this.request.getHeader(theName); 555 } 556 557 560 public Enumeration getHeaders(String theName) 561 { 562 return this.request.getHeaders(theName); 563 } 564 565 568 public Enumeration getHeaderNames() 569 { 570 return this.request.getHeaderNames(); 571 } 572 573 576 public String getScheme() 577 { 578 return this.request.getScheme(); 579 } 580 581 584 public String getAuthType() 585 { 586 return this.request.getAuthType(); 587 } 588 589 592 public String getRealPath(String thePath) 593 { 594 return this.request.getRealPath(thePath); 595 } 596 597 600 public HttpSession getSession() 601 { 602 return this.request.getSession(); 603 } 604 605 608 public HttpSession getSession(boolean isCreate) 609 { 610 return this.request.getSession(isCreate); 611 } 612 613 616 public BufferedReader getReader() throws IOException 617 { 618 return this.request.getReader(); 619 } 620 621 624 public int getContentLength() 625 { 626 return this.request.getContentLength(); 627 } 628 629 632 public String [] getParameterValues(String theName) 633 { 634 return this.request.getParameterValues(theName); 635 } 636 637 640 public String getContentType() 641 { 642 return this.request.getContentType(); 643 } 644 645 648 public Locale getLocale() 649 { 650 return this.request.getLocale(); 651 } 652 653 656 public void removeAttribute(String theName) 657 { 658 this.request.removeAttribute(theName); 659 } 660 661 664 public String getParameter(String theName) 665 { 666 return this.request.getParameter(theName); 667 } 668 669 672 public ServletInputStream getInputStream() throws IOException 673 { 674 return this.request.getInputStream(); 675 } 676 677 680 public Principal getUserPrincipal() 681 { 682 return this.request.getUserPrincipal(); 683 } 684 685 688 public boolean isSecure() 689 { 690 return this.request.isSecure(); 691 } 692 693 696 public String getCharacterEncoding() 697 { 698 return this.request.getCharacterEncoding(); 699 } 700 701 704 public Enumeration getParameterNames() 705 { 706 return this.request.getParameterNames(); 707 } 708 709 712 public String getMethod() 713 { 714 return this.request.getMethod(); 715 } 716 717 720 public void setAttribute(String theName, Object theAttribute) 721 { 722 this.request.setAttribute(theName, theAttribute); 723 } 724 725 728 public Object getAttribute(String theName) 729 { 730 return this.request.getAttribute(theName); 731 } 732 733 736 public int getIntHeader(String theName) 737 { 738 return this.request.getIntHeader(theName); 739 } 740 741 744 public long getDateHeader(String theName) 745 { 746 return this.request.getDateHeader(theName); 747 } 748 749 752 public Enumeration getAttributeNames() 753 { 754 return this.request.getAttributeNames(); 755 } 756 757 760 public String getRequestedSessionId() 761 { 762 return this.request.getRequestedSessionId(); 763 } 764 765 768 public Cookie [] getCookies() 769 { 770 return this.request.getCookies(); 771 } 772 773 776 public String getProtocol() 777 { 778 return this.request.getProtocol(); 779 } 780 } 781 | Popular Tags |