1 /* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.xml.internal.messaging.saaj.util; 27 28 // Imported from: org.apache.xerces.util 29 // Needed to work around differences in JDK1.2 and 1.3 and deal with userInfo 30 31 import java.io.IOException; 32 import java.io.Serializable; 33 34 35 /********************************************************************** 36 * A class to represent a Uniform Resource Identifier (URI). This class 37 * is designed to handle the parsing of URIs and provide access to 38 * the various components (scheme, host, port, userinfo, path, query 39 * string and fragment) that may constitute a URI. 40 * <p> 41 * Parsing of a URI specification is done according to the URI 42 * syntax described in <a href="http://www.ietf.org/rfc/rfc2396.txt?number=2396">RFC 2396</a>. 43 * Every URI consists of a scheme, followed by a colon (':'), followed by a scheme-specific 44 * part. For URIs that follow the "generic URI" syntax, the scheme- 45 * specific part begins with two slashes ("//") and may be followed 46 * by an authority segment (comprised of user information, host, and 47 * port), path segment, query segment and fragment. Note that RFC 2396 48 * no longer specifies the use of the parameters segment and excludes 49 * the "user:password" syntax as part of the authority segment. If 50 * "user:password" appears in a URI, the entire user/password string 51 * is stored as userinfo. 52 * <p> 53 * For URIs that do not follow the "generic URI" syntax (e.g. mailto), 54 * the entire scheme-specific part is treated as the "path" portion 55 * of the URI. 56 * <p> 57 * Note that, unlike the java.net.URL class, this class does not provide 58 * any built-in network access functionality nor does it provide any 59 * scheme-specific functionality (for example, it does not know a 60 * default port for a specific scheme). Rather, it only knows the 61 * grammar and basic set of operations that can be applied to a URI. 62 * 63 * @version 64 * 65 **********************************************************************/ 66 public class JaxmURI implements Serializable { 67 68 /******************************************************************* 69 * MalformedURIExceptions are thrown in the process of building a URI 70 * or setting fields on a URI when an operation would result in an 71 * invalid URI specification. 72 * 73 ********************************************************************/ 74 public static class MalformedURIException extends IOException { 75 76 /****************************************************************** 77 * Constructs a <code>MalformedURIException</code> with no specified 78 * detail message. 79 ******************************************************************/ 80 public MalformedURIException() { 81 super(); 82 } 83 84 /***************************************************************** 85 * Constructs a <code>MalformedURIException</code> with the 86 * specified detail message. 87 * 88 * @param p_msg the detail message. 89 ******************************************************************/ 90 public MalformedURIException(String p_msg) { 91 super(p_msg); 92 } 93 } 94 95 /** reserved characters */ 96 private static final String RESERVED_CHARACTERS = ";/?:@&=+$,"; 97 98 /** URI punctuation mark characters - these, combined with 99 alphanumerics, constitute the "unreserved" characters */ 100 private static final String MARK_CHARACTERS = "-_.!~*'() "; 101 102 /** scheme can be composed of alphanumerics and these characters */ 103 private static final String SCHEME_CHARACTERS = "+-."; 104 105 /** userinfo can be composed of unreserved, escaped and these 106 characters */ 107 private static final String USERINFO_CHARACTERS = ";:&=+$,"; 108 109 /** Stores the scheme (usually the protocol) for this URI. */ 110 private String m_scheme = null; 111 112 /** If specified, stores the userinfo for this URI; otherwise null */ 113 private String m_userinfo = null; 114 115 /** If specified, stores the host for this URI; otherwise null */ 116 private String m_host = null; 117 118 /** If specified, stores the port for this URI; otherwise -1 */ 119 private int m_port = -1; 120 121 /** If specified, stores the path for this URI; otherwise null */ 122 private String m_path = null; 123 124 /** If specified, stores the query string for this URI; otherwise 125 null. */ 126 private String m_queryString = null; 127 128 /** If specified, stores the fragment for this URI; otherwise null */ 129 private String m_fragment = null; 130 131 private static boolean DEBUG = false; 132 133 /** 134 * Construct a new and uninitialized URI. 135 */ 136 public JaxmURI() { 137 } 138 139 /** 140 * Construct a new URI from another URI. All fields for this URI are 141 * set equal to the fields of the URI passed in. 142 * 143 * @param p_other the URI to copy (cannot be null) 144 */ 145 public JaxmURI(JaxmURI p_other) { 146 initialize(p_other); 147 } 148 149 /** 150 * Construct a new URI from a URI specification string. If the 151 * specification follows the "generic URI" syntax, (two slashes 152 * following the first colon), the specification will be parsed 153 * accordingly - setting the scheme, userinfo, host,port, path, query 154 * string and fragment fields as necessary. If the specification does 155 * not follow the "generic URI" syntax, the specification is parsed 156 * into a scheme and scheme-specific part (stored as the path) only. 157 * 158 * @param p_uriSpec the URI specification string (cannot be null or 159 * empty) 160 * 161 * @exception MalformedURIException if p_uriSpec violates any syntax 162 * rules 163 */ 164 public JaxmURI(String p_uriSpec) throws MalformedURIException { 165 this((JaxmURI)null, p_uriSpec); 166 } 167 168 /** 169 * Construct a new URI from a base URI and a URI specification string. 170 * The URI specification string may be a relative URI. 171 * 172 * @param p_base the base URI (cannot be null if p_uriSpec is null or 173 * empty) 174 * @param p_uriSpec the URI specification string (cannot be null or 175 * empty if p_base is null) 176 * 177 * @exception MalformedURIException if p_uriSpec violates any syntax 178 * rules 179 */ 180 public JaxmURI(JaxmURI p_base, String p_uriSpec) throws MalformedURIException { 181 initialize(p_base, p_uriSpec); 182 } 183 184 /** 185 * Construct a new URI that does not follow the generic URI syntax. 186 * Only the scheme and scheme-specific part (stored as the path) are 187 * initialized. 188 * 189 * @param p_scheme the URI scheme (cannot be null or empty) 190 * @param p_schemeSpecificPart the scheme-specific part (cannot be 191 * null or empty) 192 * 193 * @exception MalformedURIException if p_scheme violates any 194 * syntax rules 195 */ 196 public JaxmURI(String p_scheme, String p_schemeSpecificPart) 197 throws MalformedURIException { 198 if (p_scheme == null || p_scheme.trim().length() == 0) { 199 throw new MalformedURIException( 200 "Cannot construct URI with null/empty scheme!"); 201 } 202 if (p_schemeSpecificPart == null || 203 p_schemeSpecificPart.trim().length() == 0) { 204 throw new MalformedURIException( 205 "Cannot construct URI with null/empty scheme-specific part!"); 206 } 207 setScheme(p_scheme); 208 setPath(p_schemeSpecificPart); 209 } 210 211 /** 212 * Construct a new URI that follows the generic URI syntax from its 213 * component parts. Each component is validated for syntax and some 214 * basic semantic checks are performed as well. See the individual 215 * setter methods for specifics. 216 * 217 * @param p_scheme the URI scheme (cannot be null or empty) 218 * @param p_host the hostname or IPv4 address for the URI 219 * @param p_path the URI path - if the path contains '?' or '#', 220 * then the query string and/or fragment will be 221 * set from the path; however, if the query and 222 * fragment are specified both in the path and as 223 * separate parameters, an exception is thrown 224 * @param p_queryString the URI query string (cannot be specified 225 * if path is null) 226 * @param p_fragment the URI fragment (cannot be specified if path 227 * is null) 228 * 229 * @exception MalformedURIException if any of the parameters violates 230 * syntax rules or semantic rules 231 */ 232 public JaxmURI(String p_scheme, String p_host, String p_path, 233 String p_queryString, String p_fragment) 234 throws MalformedURIException { 235 this(p_scheme, null, p_host, -1, p_path, p_queryString, p_fragment); 236 } 237 238 /** 239 * Construct a new URI that follows the generic URI syntax from its 240 * component parts. Each component is validated for syntax and some 241 * basic semantic checks are performed as well. See the individual 242 * setter methods for specifics. 243 * 244 * @param p_scheme the URI scheme (cannot be null or empty) 245 * @param p_userinfo the URI userinfo (cannot be specified if host 246 * is null) 247 * @param p_host the hostname or IPv4 address for the URI 248 * @param p_port the URI port (may be -1 for "unspecified"; cannot 249 * be specified if host is null) 250 * @param p_path the URI path - if the path contains '?' or '#', 251 * then the query string and/or fragment will be 252 * set from the path; however, if the query and 253 * fragment are specified both in the path and as 254 * separate parameters, an exception is thrown 255 * @param p_queryString the URI query string (cannot be specified 256 * if path is null) 257 * @param p_fragment the URI fragment (cannot be specified if path 258 * is null) 259 * 260 * @exception MalformedURIException if any of the parameters violates 261 * syntax rules or semantic rules 262 */ 263 public JaxmURI(String p_scheme, String p_userinfo, 264 String p_host, int p_port, String p_path, 265 String p_queryString, String p_fragment) 266 throws MalformedURIException { 267 if (p_scheme == null || p_scheme.trim().length() == 0) { 268 throw new MalformedURIException("Scheme is required!"); 269 } 270 271 if (p_host == null) { 272 if (p_userinfo != null) { 273 throw new MalformedURIException( 274 "Userinfo may not be specified if host is not specified!"); 275 } 276 if (p_port != -1) { 277 throw new MalformedURIException( 278 "Port may not be specified if host is not specified!"); 279 } 280 } 281 282 if (p_path != null) { 283 if (p_path.indexOf('?') != -1 && p_queryString != null) { 284 throw new MalformedURIException( 285 "Query string cannot be specified in path and query string!"); 286 } 287 288 if (p_path.indexOf('#') != -1 && p_fragment != null) { 289 throw new MalformedURIException( 290 "Fragment cannot be specified in both the path and fragment!"); 291 } 292 } 293 294 setScheme(p_scheme); 295 setHost(p_host); 296 setPort(p_port); 297 setUserinfo(p_userinfo); 298 setPath(p_path); 299 setQueryString(p_queryString); 300 setFragment(p_fragment); 301 } 302 303 /** 304 * Initialize all fields of this URI from another URI. 305 * 306 * @param p_other the URI to copy (cannot be null) 307 */ 308 private void initialize(JaxmURI p_other) { 309 m_scheme = p_other.getScheme(); 310 m_userinfo = p_other.getUserinfo(); 311 m_host = p_other.getHost(); 312 m_port = p_other.getPort(); 313 m_path = p_other.getPath(); 314 m_queryString = p_other.getQueryString(); 315 m_fragment = p_other.getFragment(); 316 } 317 318 /** 319 * Initializes this URI from a base URI and a URI specification string. 320 * See RFC 2396 Section 4 and Appendix B for specifications on parsing 321 * the URI and Section 5 for specifications on resolving relative URIs 322 * and relative paths. 323 * 324 * @param p_base the base URI (may be null if p_uriSpec is an absolute 325 * URI) 326 * @param p_uriSpec the URI spec string which may be an absolute or 327 * relative URI (can only be null/empty if p_base 328 * is not null) 329 * 330 * @exception MalformedURIException if p_base is null and p_uriSpec 331 * is not an absolute URI or if 332 * p_uriSpec violates syntax rules 333 */ 334 private void initialize(JaxmURI p_base, String p_uriSpec) 335 throws MalformedURIException { 336 if (p_base == null && 337 (p_uriSpec == null || p_uriSpec.trim().length() == 0)) { 338 throw new MalformedURIException( 339 "Cannot initialize URI with empty parameters."); 340 } 341 342 // just make a copy of the base if spec is empty 343 if (p_uriSpec == null || p_uriSpec.trim().length() == 0) { 344 initialize(p_base); 345 return; 346 } 347 348 String uriSpec = p_uriSpec.trim(); 349 int uriSpecLen = uriSpec.length(); 350 int index = 0; 351 352 // Check for scheme, which must be before `/'. Also handle names with 353 // DOS drive letters ('D:'), so 1-character schemes are not allowed. 354 int colonIdx = uriSpec.indexOf(':'); 355 int slashIdx = uriSpec.indexOf('/'); 356 if ((colonIdx < 2) || (colonIdx > slashIdx && slashIdx != -1)) { 357 int fragmentIdx = uriSpec.indexOf('#'); 358 // A standalone base is a valid URI according to spec 359 if (p_base == null && fragmentIdx != 0 ) { 360 throw new MalformedURIException("No scheme found in URI."); 361 } 362 } 363 else { 364 initializeScheme(uriSpec); 365 index = m_scheme.length()+1; 366 } 367 368 // two slashes means generic URI syntax, so we get the authority 369 if (((index+1) < uriSpecLen) && 370 (uriSpec.substring(index).startsWith("//"))) { 371 index += 2; 372 int startPos = index; 373 374 // get authority - everything up to path, query or fragment 375 char testChar = '\0'; 376 while (index < uriSpecLen) { 377 testChar = uriSpec.charAt(index); 378 if (testChar == '/' || testChar == '?' || testChar == '#') { 379 break; 380 } 381 index++; 382 } 383 384 // if we found authority, parse it out, otherwise we set the 385 // host to empty string 386 if (index > startPos) { 387 initializeAuthority(uriSpec.substring(startPos, index)); 388 } 389 else { 390 m_host = ""; 391 } 392 } 393 394 initializePath(uriSpec.substring(index)); 395 396 // Resolve relative URI to base URI - see RFC 2396 Section 5.2 397 // In some cases, it might make more sense to throw an exception 398 // (when scheme is specified is the string spec and the base URI 399 // is also specified, for example), but we're just following the 400 // RFC specifications 401 if (p_base != null) { 402 403 // check to see if this is the current doc - RFC 2396 5.2 #2 404 // note that this is slightly different from the RFC spec in that 405 // we don't include the check for query string being null 406 // - this handles cases where the urispec is just a query 407 // string or a fragment (e.g. "?y" or "#s") - 408 // see <http://www.ics.uci.edu/~fielding/url/test1.html> which 409 // identified this as a bug in the RFC 410 if (m_path.length() == 0 && m_scheme == null && 411 m_host == null) { 412 m_scheme = p_base.getScheme(); 413 m_userinfo = p_base.getUserinfo(); 414 m_host = p_base.getHost(); 415 m_port = p_base.getPort(); 416 m_path = p_base.getPath(); 417 418 if (m_queryString == null) { 419 m_queryString = p_base.getQueryString(); 420 } 421 return; 422 } 423 424 // check for scheme - RFC 2396 5.2 #3 425 // if we found a scheme, it means absolute URI, so we're done 426 if (m_scheme == null) { 427 m_scheme = p_base.getScheme(); 428 } 429 else { 430 return; 431 } 432 433 // check for authority - RFC 2396 5.2 #4 434 // if we found a host, then we've got a network path, so we're done 435 if (m_host == null) { 436 m_userinfo = p_base.getUserinfo(); 437 m_host = p_base.getHost(); 438 m_port = p_base.getPort(); 439 } 440 else { 441 return; 442 } 443 444 // check for absolute path - RFC 2396 5.2 #5 445 if (m_path.length() > 0 && 446 m_path.startsWith("/")) { 447 return; 448 } 449 450 // if we get to this point, we need to resolve relative path 451 // RFC 2396 5.2 #6 452 String path = ""; 453 String basePath = p_base.getPath(); 454 455 // 6a - get all but the last segment of the base URI path 456 if (basePath != null) { 457 int lastSlash = basePath.lastIndexOf('/'); 458 if (lastSlash != -1) { 459 path = basePath.substring(0, lastSlash+1); 460 } 461 } 462 463 // 6b - append the relative URI path 464 path = path.concat(m_path); 465 466 // 6c - remove all "./" where "." is a complete path segment 467 index = -1; 468 while ((index = path.indexOf("/./")) != -1) { 469 path = path.substring(0, index+1).concat(path.substring(index+3)); 470 } 471 472 // 6d - remove "." if path ends with "." as a complete path segment 473 if (path.endsWith("/.")) { 474 path = path.substring(0, path.length()-1); 475 } 476 477 // 6e - remove all "<segment>/../" where "<segment>" is a complete 478 // path segment not equal to ".." 479 index = 1; 480 int segIndex = -1; 481 String tempString = null; 482 483 while ((index = path.indexOf("/../", index)) > 0) { 484 tempString = path.substring(0, path.indexOf("/../")); 485 segIndex = tempString.lastIndexOf('/'); 486 if (segIndex != -1) { 487 if (!tempString.substring(segIndex++).equals("..")) { 488 path = path.substring(0, segIndex).concat(path.substring(index+4)); 489 } 490 else 491 index += 4; 492 } 493 else 494 index += 4; 495 } 496 497 // 6f - remove ending "<segment>/.." where "<segment>" is a 498 // complete path segment 499 if (path.endsWith("/..")) { 500 tempString = path.substring(0, path.length()-3); 501 segIndex = tempString.lastIndexOf('/'); 502 if (segIndex != -1) { 503 path = path.substring(0, segIndex+1); 504 } 505 } 506 m_path = path; 507 } 508 } 509 510 /** 511 * Initialize the scheme for this URI from a URI string spec. 512 * 513 * @param p_uriSpec the URI specification (cannot be null) 514 * 515 * @exception MalformedURIException if URI does not have a conformant 516 * scheme 517 */ 518 private void initializeScheme(String p_uriSpec) 519 throws MalformedURIException { 520 int uriSpecLen = p_uriSpec.length(); 521 int index = 0; 522 String scheme = null; 523 char testChar = '\0'; 524 525 while (index < uriSpecLen) { 526 testChar = p_uriSpec.charAt(index); 527 if (testChar == ':' || testChar == '/' || 528 testChar == '?' || testChar == '#') { 529 break; 530 } 531 index++; 532 } 533 scheme = p_uriSpec.substring(0, index); 534 535 if (scheme.length() == 0) { 536 throw new MalformedURIException("No scheme found in URI."); 537 } 538 else { 539 setScheme(scheme); 540 } 541 } 542 543 /** 544 * Initialize the authority (userinfo, host and port) for this 545 * URI from a URI string spec. 546 * 547 * @param p_uriSpec the URI specification (cannot be null) 548 * 549 * @exception MalformedURIException if p_uriSpec violates syntax rules 550 */ 551 private void initializeAuthority(String p_uriSpec) 552 throws MalformedURIException { 553 int index = 0; 554 int start = 0; 555 int end = p_uriSpec.length(); 556 char testChar = '\0'; 557 String userinfo = null; 558 559 // userinfo is everything up @ 560 if (p_uriSpec.indexOf('@', start) != -1) { 561 while (index < end) { 562 testChar = p_uriSpec.charAt(index); 563 if (testChar == '@') { 564 break; 565 } 566 index++; 567 } 568 userinfo = p_uriSpec.substring(start, index); 569 index++; 570 } 571 572 // host is everything up to ':' 573 String host = null; 574 start = index; 575 while (index < end) { 576 testChar = p_uriSpec.charAt(index); 577 if (testChar == ':') { 578 break; 579 } 580 index++; 581 } 582 host = p_uriSpec.substring(start, index); 583 int port = -1; 584 if (host.length() > 0) { 585 // port 586 if (testChar == ':') { 587 index++; 588 start = index; 589 while (index < end) { 590 index++; 591 } 592 String portStr = p_uriSpec.substring(start, index); 593 if (portStr.length() > 0) { 594 for (int i = 0; i < portStr.length(); i++) { 595 if (!isDigit(portStr.charAt(i))) { 596 throw new MalformedURIException( 597 portStr + 598 " is invalid. Port should only contain digits!"); 599 } 600 } 601 try { 602 port = Integer.parseInt(portStr); 603 } 604 catch (NumberFormatException nfe) { 605 // can't happen 606 } 607 } 608 } 609 } 610 setHost(host); 611 setPort(port); 612 setUserinfo(userinfo); 613 } 614 615 /** 616 * Initialize the path for this URI from a URI string spec. 617 * 618 * @param p_uriSpec the URI specification (cannot be null) 619 * 620 * @exception MalformedURIException if p_uriSpec violates syntax rules 621 */ 622 private void initializePath(String p_uriSpec) 623 throws MalformedURIException { 624 if (p_uriSpec == null) { 625 throw new MalformedURIException( 626 "Cannot initialize path from null string!"); 627 } 628 629 int index = 0; 630 int start = 0; 631 int end = p_uriSpec.length(); 632 char testChar = '\0'; 633 634 // path - everything up to query string or fragment 635 while (index < end) { 636 testChar = p_uriSpec.charAt(index); 637 if (testChar == '?' || testChar == '#') { 638 break; 639 } 640 // check for valid escape sequence 641 if (testChar == '%') { 642 if (index+2 >= end || 643 !isHex(p_uriSpec.charAt(index+1)) || 644 !isHex(p_uriSpec.charAt(index+2))) { 645 throw new MalformedURIException( 646 "Path contains invalid escape sequence!"); 647 } 648 } 649 else if (!isReservedCharacter(testChar) && 650 !isUnreservedCharacter(testChar)) { 651 throw new MalformedURIException( 652 "Path contains invalid character: " + testChar); 653 } 654 index++; 655 } 656 m_path = p_uriSpec.substring(start, index); 657 658 // query - starts with ? and up to fragment or end 659 if (testChar == '?') { 660 index++; 661 start = index; 662 while (index < end) { 663 testChar = p_uriSpec.charAt(index); 664 if (testChar == '#') { 665 break; 666 } 667 if (testChar == '%') { 668 if (index+2 >= end || 669 !isHex(p_uriSpec.charAt(index+1)) || 670 !isHex(p_uriSpec.charAt(index+2))) { 671 throw new MalformedURIException( 672 "Query string contains invalid escape sequence!"); 673 } 674 } 675 else if (!isReservedCharacter(testChar) && 676 !isUnreservedCharacter(testChar)) { 677 throw new MalformedURIException( 678 "Query string contains invalid character:" + testChar); 679 } 680 index++; 681 } 682 m_queryString = p_uriSpec.substring(start, index); 683 } 684 685 // fragment - starts with # 686 if (testChar == '#') { 687 index++; 688 start = index; 689 while (index < end) { 690 testChar = p_uriSpec.charAt(index); 691 692 if (testChar == '%') { 693 if (index+2 >= end || 694 !isHex(p_uriSpec.charAt(index+1)) || 695 !isHex(p_uriSpec.charAt(index+2))) { 696 throw new MalformedURIException( 697 "Fragment contains invalid escape sequence!"); 698 } 699 } 700 else if (!isReservedCharacter(testChar) && 701 !isUnreservedCharacter(testChar)) { 702 throw new MalformedURIException( 703 "Fragment contains invalid character:"+testChar); 704 } 705 index++; 706 } 707 m_fragment = p_uriSpec.substring(start, index); 708 } 709 } 710 711 /** 712 * Get the scheme for this URI. 713 * 714 * @return the scheme for this URI 715 */ 716 public String getScheme() { 717 return m_scheme; 718 } 719 720 /** 721 * Get the scheme-specific part for this URI (everything following the 722 * scheme and the first colon). See RFC 2396 Section 5.2 for spec. 723 * 724 * @return the scheme-specific part for this URI 725 */ 726 public String getSchemeSpecificPart() { 727 StringBuffer schemespec = new StringBuffer(); 728 729 if (m_userinfo != null || m_host != null || m_port != -1) { 730 schemespec.append("//"); 731 } 732 733 if (m_userinfo != null) { 734 schemespec.append(m_userinfo); 735 schemespec.append('@'); 736 } 737 738 if (m_host != null) { 739 schemespec.append(m_host); 740 } 741 742 if (m_port != -1) { 743 schemespec.append(':'); 744 schemespec.append(m_port); 745 } 746 747 if (m_path != null) { 748 schemespec.append((m_path)); 749 } 750 751 if (m_queryString != null) { 752 schemespec.append('?'); 753 schemespec.append(m_queryString); 754 } 755 756 if (m_fragment != null) { 757 schemespec.append('#'); 758 schemespec.append(m_fragment); 759 } 760 761 return schemespec.toString(); 762 } 763 764 /** 765 * Get the userinfo for this URI. 766 * 767 * @return the userinfo for this URI (null if not specified). 768 */ 769 public String getUserinfo() { 770 return m_userinfo; 771 } 772 773 /** 774 * Get the host for this URI. 775 * 776 * @return the host for this URI (null if not specified). 777 */ 778 public String getHost() { 779 return m_host; 780 } 781 782 /** 783 * Get the port for this URI. 784 * 785 * @return the port for this URI (-1 if not specified). 786 */ 787 public int getPort() { 788 return m_port; 789 } 790 791 /** 792 * Get the path for this URI (optionally with the query string and 793 * fragment). 794 * 795 * @param p_includeQueryString if true (and query string is not null), 796 * then a "?" followed by the query string 797 * will be appended 798 * @param p_includeFragment if true (and fragment is not null), 799 * then a "#" followed by the fragment 800 * will be appended 801 * 802 * @return the path for this URI possibly including the query string 803 * and fragment 804 */ 805 public String getPath(boolean p_includeQueryString, 806 boolean p_includeFragment) { 807 StringBuffer pathString = new StringBuffer(m_path); 808 809 if (p_includeQueryString && m_queryString != null) { 810 pathString.append('?'); 811 pathString.append(m_queryString); 812 } 813 814 if (p_includeFragment && m_fragment != null) { 815 pathString.append('#'); 816 pathString.append(m_fragment); 817 } 818 return pathString.toString(); 819 } 820 821 /** 822 * Get the path for this URI. Note that the value returned is the path 823 * only and does not include the query string or fragment. 824 * 825 * @return the path for this URI. 826 */ 827 public String getPath() { 828 return m_path; 829 } 830 831 /** 832 * Get the query string for this URI. 833 * 834 * @return the query string for this URI. Null is returned if there 835 * was no "?" in the URI spec, empty string if there was a 836 * "?" but no query string following it. 837 */ 838 public String getQueryString() { 839 return m_queryString; 840 } 841 842 /** 843 * Get the fragment for this URI. 844 * 845 * @return the fragment for this URI. Null is returned if there 846 * was no "#" in the URI spec, empty string if there was a 847 * "#" but no fragment following it. 848 */ 849 public String getFragment() { 850 return m_fragment; 851 } 852 853 /** 854 * Set the scheme for this URI. The scheme is converted to lowercase 855 * before it is set. 856 * 857 * @param p_scheme the scheme for this URI (cannot be null) 858 * 859 * @exception MalformedURIException if p_scheme is not a conformant 860 * scheme name 861 */ 862 public void setScheme(String p_scheme) throws MalformedURIException { 863 if (p_scheme == null) { 864 throw new MalformedURIException( 865 "Cannot set scheme from null string!"); 866 } 867 if (!isConformantSchemeName(p_scheme)) { 868 throw new MalformedURIException("The scheme is not conformant."); 869 } 870 871 m_scheme = p_scheme.toLowerCase(); 872 } 873 874 /** 875 * Set the userinfo for this URI. If a non-null value is passed in and 876 * the host value is null, then an exception is thrown. 877 * 878 * @param p_userinfo the userinfo for this URI 879 * 880 * @exception MalformedURIException if p_userinfo contains invalid 881 * characters 882 */ 883 public void setUserinfo(String p_userinfo) throws MalformedURIException { 884 if (p_userinfo == null) { 885 m_userinfo = null; 886 } 887 else { 888 if (m_host == null) { 889 throw new MalformedURIException( 890 "Userinfo cannot be set when host is null!"); 891 } 892 893 // userinfo can contain alphanumerics, mark characters, escaped 894 // and ';',':','&','=','+','$',',' 895 int index = 0; 896 int end = p_userinfo.length(); 897 char testChar = '\0'; 898 while (index < end) { 899 testChar = p_userinfo.charAt(index); 900 if (testChar == '%') { 901 if (index+2 >= end || 902 !isHex(p_userinfo.charAt(index+1)) || 903 !isHex(p_userinfo.charAt(index+2))) { 904 throw new MalformedURIException( 905 "Userinfo contains invalid escape sequence!"); 906 } 907 } 908 else if (!isUnreservedCharacter(testChar) && 909 USERINFO_CHARACTERS.indexOf(testChar) == -1) { 910 throw new MalformedURIException( 911 "Userinfo contains invalid character:"+testChar); 912 } 913 index++; 914 } 915 } 916 m_userinfo = p_userinfo; 917 } 918 919 /** 920 * Set the host for this URI. If null is passed in, the userinfo 921 * field is also set to null and the port is set to -1. 922 * 923 * @param p_host the host for this URI 924 * 925 * @exception MalformedURIException if p_host is not a valid IP 926 * address or DNS hostname. 927 */ 928 public void setHost(String p_host) throws MalformedURIException { 929 if (p_host == null || p_host.trim().length() == 0) { 930 m_host = p_host; 931 m_userinfo = null; 932 m_port = -1; 933 } 934 else if (!isWellFormedAddress(p_host)) { 935 throw new MalformedURIException("Host is not a well formed address!"); 936 } 937 m_host = p_host; 938 } 939 940 /** 941 * Set the port for this URI. -1 is used to indicate that the port is 942 * not specified, otherwise valid port numbers are between 0 and 65535. 943 * If a valid port number is passed in and the host field is null, 944 * an exception is thrown. 945 * 946 * @param p_port the port number for this URI 947 * 948 * @exception MalformedURIException if p_port is not -1 and not a 949 * valid port number 950 */ 951 public void setPort(int p_port) throws MalformedURIException { 952 if (p_port >= 0 && p_port <= 65535) { 953 if (m_host == null) { 954 throw new MalformedURIException( 955 "Port cannot be set when host is null!"); 956 } 957 } 958 else if (p_port != -1) { 959 throw new MalformedURIException("Invalid port number!"); 960 } 961 m_port = p_port; 962 } 963 964 /** 965 * Set the path for this URI. If the supplied path is null, then the 966 * query string and fragment are set to null as well. If the supplied 967 * path includes a query string and/or fragment, these fields will be 968 * parsed and set as well. Note that, for URIs following the "generic 969 * URI" syntax, the path specified should start with a slash. 970 * For URIs that do not follow the generic URI syntax, this method 971 * sets the scheme-specific part. 972 * 973 * @param p_path the path for this URI (may be null) 974 * 975 * @exception MalformedURIException if p_path contains invalid 976 * characters 977 */ 978 public void setPath(String p_path) throws MalformedURIException { 979 if (p_path == null) { 980 m_path = null; 981 m_queryString = null; 982 m_fragment = null; 983 } 984 else { 985 initializePath(p_path); 986 } 987 } 988 989 /** 990 * Append to the end of the path of this URI. If the current path does 991 * not end in a slash and the path to be appended does not begin with 992 * a slash, a slash will be appended to the current path before the 993 * new segment is added. Also, if the current path ends in a slash 994 * and the new segment begins with a slash, the extra slash will be 995 * removed before the new segment is appended. 996 * 997 * @param p_addToPath the new segment to be added to the current path 998 * 999 * @exception MalformedURIException if p_addToPath contains syntax 1000 * errors 1001 */ 1002 public void appendPath(String p_addToPath) 1003 throws MalformedURIException { 1004 if (p_addToPath == null || p_addToPath.trim().length() == 0) { 1005 return; 1006 } 1007 1008 if (!isURIString(p_addToPath)) { 1009 throw new MalformedURIException( 1010 "Path contains invalid character!"); 1011 } 1012 1013 if (m_path == null || m_path.trim().length() == 0) { 1014 if (p_addToPath.startsWith("/")) { 1015 m_path = p_addToPath; 1016 } 1017 else { 1018 m_path = "/" + p_addToPath; 1019 } 1020 } 1021 else if (m_path.endsWith("/")) { 1022 if (p_addToPath.startsWith("/")) { 1023 m_path = m_path.concat(p_addToPath.substring(1)); 1024 } 1025 else { 1026 m_path = m_path.concat(p_addToPath); 1027 } 1028 } 1029 else { 1030 if (p_addToPath.startsWith("/")) { 1031 m_path = m_path.concat(p_addToPath); 1032 } 1033 else { 1034 m_path = m_path.concat("/" + p_addToPath); 1035 } 1036 } 1037 } 1038 1039 /** 1040 * Set the query string for this URI. A non-null value is valid only 1041 * if this is an URI conforming to the generic URI syntax and 1042 * the path value is not null. 1043 * 1044 * @param p_queryString the query string for this URI 1045 * 1046 * @exception MalformedURIException if p_queryString is not null and this 1047 * URI does not conform to the generic 1048 * URI syntax or if the path is null 1049 */ 1050 public void setQueryString(String p_queryString) throws MalformedURIException { 1051 if (p_queryString == null) { 1052 m_queryString = null; 1053 } 1054 else if (!isGenericURI()) { 1055 throw new MalformedURIException( 1056 "Query string can only be set for a generic URI!"); 1057 } 1058 else if (getPath() == null) { 1059 throw new MalformedURIException( 1060 "Query string cannot be set when path is null!"); 1061 } 1062 else if (!isURIString(p_queryString)) { 1063 throw new MalformedURIException( 1064 "Query string contains invalid character!"); 1065 } 1066 else { 1067 m_queryString = p_queryString; 1068 } 1069 } 1070 1071 /** 1072 * Set the fragment for this URI. A non-null value is valid only 1073 * if this is a URI conforming to the generic URI syntax and 1074 * the path value is not null. 1075 * 1076 * @param p_fragment the fragment for this URI 1077 * 1078 * @exception MalformedURIException if p_fragment is not null and this 1079 * URI does not conform to the generic 1080 * URI syntax or if the path is null 1081 */ 1082 public void setFragment(String p_fragment) throws MalformedURIException { 1083 if (p_fragment == null) { 1084 m_fragment = null; 1085 } 1086 else if (!isGenericURI()) { 1087 throw new MalformedURIException( 1088 "Fragment can only be set for a generic URI!"); 1089 } 1090 else if (getPath() == null) { 1091 throw new MalformedURIException( 1092 "Fragment cannot be set when path is null!"); 1093 } 1094 else if (!isURIString(p_fragment)) { 1095 throw new MalformedURIException( 1096 "Fragment contains invalid character!"); 1097 } 1098 else { 1099 m_fragment = p_fragment; 1100 } 1101 } 1102 1103 /** 1104 * Determines if the passed-in Object is equivalent to this URI. 1105 * 1106 * @param p_test the Object to test for equality. 1107 * 1108 * @return true if p_test is a URI with all values equal to this 1109 * URI, false otherwise 1110 */ 1111 public boolean equals(Object p_test) { 1112 if (p_test instanceof JaxmURI) { 1113 JaxmURI testURI = (JaxmURI) p_test; 1114 if (((m_scheme == null && testURI.m_scheme == null) || 1115 (m_scheme != null && testURI.m_scheme != null && 1116 m_scheme.equals(testURI.m_scheme))) && 1117 ((m_userinfo == null && testURI.m_userinfo == null) || 1118 (m_userinfo != null && testURI.m_userinfo != null && 1119 m_userinfo.equals(testURI.m_userinfo))) && 1120 ((m_host == null && testURI.m_host == null) || 1121 (m_host != null && testURI.m_host != null && 1122 m_host.equals(testURI.m_host))) && 1123 m_port == testURI.m_port && 1124 ((m_path == null && testURI.m_path == null) || 1125 (m_path != null && testURI.m_path != null && 1126 m_path.equals(testURI.m_path))) && 1127 ((m_queryString == null && testURI.m_queryString == null) || 1128 (m_queryString != null && testURI.m_queryString != null && 1129 m_queryString.equals(testURI.m_queryString))) && 1130 ((m_fragment == null && testURI.m_fragment == null) || 1131 (m_fragment != null && testURI.m_fragment != null && 1132 m_fragment.equals(testURI.m_fragment)))) { 1133 return true; 1134 } 1135 } 1136 return false; 1137 } 1138 1139 public int hashCode() { 1140 // No members safe to use, just default to a constant. 1141 return 153214; 1142 } 1143 1144 /** 1145 * Get the URI as a string specification. See RFC 2396 Section 5.2. 1146 * 1147 * @return the URI string specification 1148 */ 1149 public String toString() { 1150 StringBuffer uriSpecString = new StringBuffer(); 1151 1152 if (m_scheme != null) { 1153 uriSpecString.append(m_scheme); 1154 uriSpecString.append(':'); 1155 } 1156 uriSpecString.append(getSchemeSpecificPart()); 1157 return uriSpecString.toString(); 1158 } 1159 1160 /** 1161 * Get the indicator as to whether this URI uses the "generic URI" 1162 * syntax. 1163 * 1164 * @return true if this URI uses the "generic URI" syntax, false 1165 * otherwise 1166 */ 1167 public boolean isGenericURI() { 1168 // presence of the host (whether valid or empty) means 1169 // double-slashes which means generic uri 1170 return (m_host != null); 1171 } 1172 1173 /** 1174 * Determine whether a scheme conforms to the rules for a scheme name. 1175 * A scheme is conformant if it starts with an alphanumeric, and 1176 * contains only alphanumerics, '+','-' and '.'. 1177 * 1178 * @return true if the scheme is conformant, false otherwise 1179 */ 1180 public static boolean isConformantSchemeName(String p_scheme) { 1181 if (p_scheme == null || p_scheme.trim().length() == 0) { 1182 return false; 1183 } 1184 1185 if (!isAlpha(p_scheme.charAt(0))) { 1186 return false; 1187 } 1188 1189 char testChar; 1190 for (int i = 1; i < p_scheme.length(); i++) { 1191 testChar = p_scheme.charAt(i); 1192 if (!isAlphanum(testChar) && 1193 SCHEME_CHARACTERS.indexOf(testChar) == -1) { 1194 return false; 1195 } 1196 } 1197 1198 return true; 1199 } 1200 1201 /** 1202 * Determine whether a string is syntactically capable of representing 1203 * a valid IPv4 address or the domain name of a network host. A valid 1204 * IPv4 address consists of four decimal digit groups separated by a 1205 * '.'. A hostname consists of domain labels (each of which must 1206 * begin and end with an alphanumeric but may contain '-') separated 1207 & by a '.'. See RFC 2396 Section 3.2.2. 1208 * 1209 * @return true if the string is a syntactically valid IPv4 address 1210 * or hostname 1211 */ 1212 public static boolean isWellFormedAddress(String p_address) { 1213 if (p_address == null) { 1214 return false; 1215 } 1216 1217 String address = p_address.trim(); 1218 int addrLength = address.length(); 1219 if (addrLength == 0 || addrLength > 255) { 1220 return false; 1221 } 1222 1223 if (address.startsWith(".") || address.startsWith("-")) { 1224 return false; 1225 } 1226 1227 // rightmost domain label starting with digit indicates IP address 1228 // since top level domain label can only start with an alpha 1229 // see RFC 2396 Section 3.2.2 1230 int index = address.lastIndexOf('.'); 1231 if (address.endsWith(".")) { 1232 index = address.substring(0, index).lastIndexOf('.'); 1233 } 1234 1235 if (index+1 < addrLength && isDigit(p_address.charAt(index+1))) { 1236 char testChar; 1237 int numDots = 0; 1238 1239 // make sure that 1) we see only digits and dot separators, 2) that 1240 // any dot separator is preceded and followed by a digit and 1241 // 3) that we find 3 dots 1242 for (int i = 0; i < addrLength; i++) { 1243 testChar = address.charAt(i); 1244 if (testChar == '.') { 1245 if (!isDigit(address.charAt(i-1)) || 1246 (i+1 < addrLength && !isDigit(address.charAt(i+1)))) { 1247 return false; 1248 } 1249 numDots++; 1250 } 1251 else if (!isDigit(testChar)) { 1252 return false; 1253 } 1254 } 1255 if (numDots != 3) { 1256 return false; 1257 } 1258 } 1259 else { 1260 // domain labels can contain alphanumerics and '-" 1261 // but must start and end with an alphanumeric 1262 char testChar; 1263 1264 for (int i = 0; i < addrLength; i++) { 1265 testChar = address.charAt(i); 1266 if (testChar == '.') { 1267 if (!isAlphanum(address.charAt(i-1))) { 1268 return false; 1269 } 1270 if (i+1 < addrLength && !isAlphanum(address.charAt(i+1))) { 1271 return false; 1272 } 1273 } 1274 else if (!isAlphanum(testChar) && testChar != '-') { 1275 return false; 1276 } 1277 } 1278 } 1279 return true; 1280 } 1281 1282 1283 /** 1284 * Determine whether a char is a digit. 1285 * 1286 * @return true if the char is betweeen '0' and '9', false otherwise 1287 */ 1288 private static boolean isDigit(char p_char) { 1289 return p_char >= '0' && p_char <= '9'; 1290 } 1291 1292 /** 1293 * Determine whether a character is a hexadecimal character. 1294 * 1295 * @return true if the char is betweeen '0' and '9', 'a' and 'f' 1296 * or 'A' and 'F', false otherwise 1297 */ 1298 private static boolean isHex(char p_char) { 1299 return (isDigit(p_char) || 1300 (p_char >= 'a' && p_char <= 'f') || 1301 (p_char >= 'A' && p_char <= 'F')); 1302 } 1303 1304 /** 1305 * Determine whether a char is an alphabetic character: a-z or A-Z 1306 * 1307 * @return true if the char is alphabetic, false otherwise 1308 */ 1309 private static boolean isAlpha(char p_char) { 1310 return ((p_char >= 'a' && p_char <= 'z') || 1311 (p_char >= 'A' && p_char <= 'Z' )); 1312 } 1313 1314 /** 1315 * Determine whether a char is an alphanumeric: 0-9, a-z or A-Z 1316 * 1317 * @return true if the char is alphanumeric, false otherwise 1318 */ 1319 private static boolean isAlphanum(char p_char) { 1320 return (isAlpha(p_char) || isDigit(p_char)); 1321 } 1322 1323 /** 1324 * Determine whether a character is a reserved character: 1325 * ';', '/', '?', ':', '@', '&', '=', '+', '$' or ',' 1326 * 1327 * @return true if the string contains any reserved characters 1328 */ 1329 private static boolean isReservedCharacter(char p_char) { 1330 return RESERVED_CHARACTERS.indexOf(p_char) != -1; 1331 } 1332 1333 /** 1334 * Determine whether a char is an unreserved character. 1335 * 1336 * @return true if the char is unreserved, false otherwise 1337 */ 1338 private static boolean isUnreservedCharacter(char p_char) { 1339 return (isAlphanum(p_char) || 1340 MARK_CHARACTERS.indexOf(p_char) != -1); 1341 } 1342 1343 /** 1344 * Determine whether a given string contains only URI characters (also 1345 * called "uric" in RFC 2396). uric consist of all reserved 1346 * characters, unreserved characters and escaped characters. 1347 * 1348 * @return true if the string is comprised of uric, false otherwise 1349 */ 1350 private static boolean isURIString(String p_uric) { 1351 if (p_uric == null) { 1352 return false; 1353 } 1354 int end = p_uric.length(); 1355 char testChar = '\0'; 1356 for (int i = 0; i < end; i++) { 1357 testChar = p_uric.charAt(i); 1358 if (testChar == '%') { 1359 if (i+2 >= end || 1360 !isHex(p_uric.charAt(i+1)) || 1361 !isHex(p_uric.charAt(i+2))) { 1362 return false; 1363 } 1364 else { 1365 i += 2; 1366 continue; 1367 } 1368 } 1369 if (isReservedCharacter(testChar) || 1370 isUnreservedCharacter(testChar)) { 1371 continue; 1372 } 1373 else { 1374 return false; 1375 } 1376 } 1377 return true; 1378 } 1379 }