1 20 package org.apache.cactus; 21 22 import javax.servlet.http.HttpServletRequest ; 23 24 import org.apache.cactus.internal.server.ServletUtil; 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 61 public class ServletURL 62 { 63 68 public static final String URL_PROTOCOL_PARAM = "Cactus_URL_Protocol"; 69 70 75 public static final String URL_SERVER_NAME_PARAM = "Cactus_URL_Server"; 76 77 82 public static final String URL_CONTEXT_PATH_PARAM = 83 "Cactus_URL_ContextPath"; 84 85 90 public static final String URL_SERVLET_PATH_PARAM = 91 "Cactus_URL_ServletPath"; 92 93 98 public static final String URL_PATH_INFO_PARAM = "Cactus_URL_PathInfo"; 99 100 105 public static final String URL_QUERY_STRING_PARAM = 106 "Cactus_URL_QueryString"; 107 108 111 public static final String PROTOCOL_HTTP = "http"; 112 113 116 public static final String PROTOCOL_HTTPS = "https"; 117 118 121 private static final int DEFAULT_PORT_HTTP = 80; 122 123 126 private static final int DEFAULT_PORT_HTTPS = 443; 127 128 131 private static final Log LOGGER = LogFactory.getLog(ServletURL.class); 132 133 136 private String serverName; 137 138 141 private String contextPath; 142 143 146 private String servletPath; 147 148 151 private String pathInfo; 152 153 156 private String queryString; 157 158 161 private String protocol = PROTOCOL_HTTP; 162 163 167 public ServletURL() 168 { 169 } 170 171 205 public ServletURL(String theProtocol, String theServerName, 206 String theContextPath, String theServletPath, String thePathInfo, 207 String theQueryString) 208 { 209 setProtocol(theProtocol); 210 setServerName(theServerName); 211 setContextPath(theContextPath); 212 setServletPath(theServletPath); 213 setPathInfo(thePathInfo); 214 setQueryString(theQueryString); 215 } 216 217 248 public ServletURL(String theServerName, String theContextPath, 249 String theServletPath, String thePathInfo, String theQueryString) 250 { 251 this(PROTOCOL_HTTP, theServerName, theContextPath, theServletPath, 252 thePathInfo, theQueryString); 253 } 254 255 258 public String getProtocol() 259 { 260 return this.protocol; 261 } 262 263 271 public void setProtocol(String theProtocol) 272 { 273 if ((!theProtocol.equals(PROTOCOL_HTTP)) 275 && (!theProtocol.equals(PROTOCOL_HTTPS))) 276 { 277 throw new RuntimeException ("Invalid protocol [" + theProtocol 278 + "]. Currently supported protocols are [" 279 + PROTOCOL_HTTP + "] and [" 280 + PROTOCOL_HTTPS + "]."); 281 } 282 283 this.protocol = theProtocol; 284 } 285 286 289 public String getServerName() 290 { 291 return this.serverName; 292 } 293 294 305 public void setServerName(String theServerName) 306 { 307 this.serverName = theServerName; 308 } 309 310 321 public String getHost() 322 { 323 String host = getServerName(); 324 325 if (host != null) 326 { 327 int pos = host.indexOf(":"); 328 329 if (pos > 0) 330 { 331 host = host.substring(0, pos); 332 } 333 } 334 335 return host; 336 } 337 338 353 public int getPort() 354 { 355 int port = -1; 356 357 if (getServerName() != null) 358 { 359 int pos = getServerName().indexOf(":"); 360 361 if (pos < 0) 362 { 363 port = getDefaultPort(); 366 } 367 else 368 { 369 try 371 { 372 port = Integer.parseInt(getServerName().substring(pos + 1)); 373 if (port < 0) 374 { 375 port = -1; 376 } 377 } 378 catch (NumberFormatException e) 379 { 380 port = -1; 381 } 382 } 383 } 384 385 return port; 386 } 387 388 391 public String getContextPath() 392 { 393 return this.contextPath; 394 } 395 396 406 public void setContextPath(String theContextPath) 407 { 408 if ((theContextPath != null) && (theContextPath.length() > 0)) 409 { 410 if (!theContextPath.startsWith("/")) 411 { 412 throw new IllegalArgumentException ("The Context Path must" 413 + " start with a \"/\" character."); 414 } 415 if (theContextPath.endsWith("/")) 416 { 417 throw new IllegalArgumentException ("The Context Path must not" 418 + " end with a \"/\" character."); 419 } 420 } 421 422 this.contextPath = theContextPath; 423 } 424 425 428 public String getServletPath() 429 { 430 return this.servletPath; 431 } 432 433 441 public void setServletPath(String theServletPath) 442 { 443 if ((theServletPath != null) && (theServletPath.length() > 0)) 444 { 445 if (!theServletPath.startsWith("/")) 446 { 447 throw new IllegalArgumentException ("The Servlet Path must" 448 + " start with a \"/\" character."); 449 } 450 } 451 452 this.servletPath = theServletPath; 453 } 454 455 458 public String getPathInfo() 459 { 460 return this.pathInfo; 461 } 462 463 472 public void setPathInfo(String thePathInfo) 473 { 474 if ((thePathInfo != null) && (thePathInfo.length() == 0)) 475 { 476 throw new IllegalArgumentException ("The Path Info must" 477 + " not be an empty string. Use null if you don't" 478 + " want to have a path info."); 479 } 480 else if (thePathInfo != null) 481 { 482 if (!thePathInfo.startsWith("/")) 483 { 484 throw new IllegalArgumentException ("The Path Info must" 485 + " start with a \"/\" character."); 486 } 487 } 488 489 this.pathInfo = thePathInfo; 490 } 491 492 495 public String getQueryString() 496 { 497 return this.queryString; 498 } 499 500 508 public void setQueryString(String theQueryString) 509 { 510 this.queryString = theQueryString; 511 } 512 513 517 public String getPath() 518 { 519 String path; 520 521 path = (getContextPath() == null) ? "" : getContextPath(); 522 path += ((getServletPath() == null) ? "" : getServletPath()); 523 path += ((getPathInfo() == null) ? "" : getPathInfo()); 524 525 if (path.length() == 0) 526 { 527 path = null; 528 } 529 530 return path; 531 } 532 533 538 public void saveToRequest(WebRequest theRequest) 539 { 540 theRequest.addParameter(URL_PROTOCOL_PARAM, getProtocol(), 544 WebRequest.GET_METHOD); 545 546 if (getServerName() != null) 547 { 548 theRequest.addParameter(URL_SERVER_NAME_PARAM, getServerName(), 549 WebRequest.GET_METHOD); 550 } 551 552 if (getContextPath() != null) 553 { 554 theRequest.addParameter(URL_CONTEXT_PATH_PARAM, getContextPath(), 555 WebRequest.GET_METHOD); 556 } 557 558 if (getServletPath() != null) 559 { 560 theRequest.addParameter(URL_SERVLET_PATH_PARAM, getServletPath(), 561 WebRequest.GET_METHOD); 562 } 563 564 if (getPathInfo() != null) 565 { 566 theRequest.addParameter(URL_PATH_INFO_PARAM, getPathInfo(), 567 WebRequest.GET_METHOD); 568 } 569 570 if (getQueryString() != null) 571 { 572 theRequest.addParameter(URL_QUERY_STRING_PARAM, getQueryString(), 573 WebRequest.GET_METHOD); 574 } 575 } 576 577 585 public static ServletURL loadFromRequest(HttpServletRequest theRequest) 586 { 587 String qString = theRequest.getQueryString(); 588 boolean isDefined = false; 589 590 ServletURL url = new ServletURL(); 591 592 String protocol = ServletUtil.getQueryStringParameter(qString, 593 URL_PROTOCOL_PARAM); 594 595 if (protocol != null) 596 { 597 isDefined = true; 598 url.setProtocol(protocol); 599 } 600 601 String serverName = ServletUtil.getQueryStringParameter(qString, 602 URL_SERVER_NAME_PARAM); 603 604 if (serverName != null) 605 { 606 isDefined = true; 607 url.setServerName(serverName); 608 } 609 610 String contextPath = ServletUtil.getQueryStringParameter(qString, 611 URL_CONTEXT_PATH_PARAM); 612 613 if (contextPath != null) 614 { 615 isDefined = true; 616 url.setContextPath(contextPath); 617 } 618 619 String servletPath = ServletUtil.getQueryStringParameter(qString, 620 URL_SERVLET_PATH_PARAM); 621 622 if (servletPath != null) 623 { 624 isDefined = true; 625 url.setServletPath(servletPath); 626 } 627 628 String pathInfo = ServletUtil.getQueryStringParameter(qString, 629 URL_PATH_INFO_PARAM); 630 631 if (pathInfo != null) 632 { 633 isDefined = true; 634 url.setPathInfo(pathInfo); 635 } 636 637 String queryString = ServletUtil.getQueryStringParameter(qString, 638 URL_QUERY_STRING_PARAM); 639 640 if (queryString != null) 641 { 642 isDefined = true; 643 url.setQueryString(queryString); 644 } 645 646 if (!isDefined) 647 { 648 LOGGER.debug("Undefined simulation URL"); 649 url = null; 650 } 651 else 652 { 653 LOGGER.debug("Simulation URL = [" + url + "]"); 654 } 655 656 return url; 657 } 658 659 662 public String toString() 663 { 664 StringBuffer buffer = new StringBuffer (); 665 666 buffer.append("protocol = [" + getProtocol() + "], "); 667 buffer.append("host name = [" + getHost() + "], "); 668 buffer.append("port = [" + getPort() + "], "); 669 buffer.append("context path = [" + getContextPath() + "], "); 670 buffer.append("servlet path = [" + getServletPath() + "], "); 671 buffer.append("path info = [" + getPathInfo() + "], "); 672 buffer.append("query string = [" + getQueryString() + "]"); 673 674 return buffer.toString(); 675 } 676 677 682 private int getDefaultPort() 683 { 684 if (PROTOCOL_HTTPS.equals(getProtocol())) 685 { 686 return DEFAULT_PORT_HTTPS; 687 } 688 else 689 { 690 return DEFAULT_PORT_HTTP; 691 } 692 } 693 694 } 695 | Popular Tags |