| 1 16 17 package org.apache.axis.types; 18 19 import java.io.IOException ; 20 import java.io.Serializable ; 21 22 58 public class URI implements Serializable { 59 60 66 public static class MalformedURIException extends IOException { 67 68 69 static final long serialVersionUID = -6695054834342951930L; 70 71 75 public MalformedURIException() { 76 super(); 77 } 78 79 85 public MalformedURIException(String p_msg) { 86 super(p_msg); 87 } 88 } 89 90 91 static final long serialVersionUID = 1601921774685357214L; 92 93 private static final byte [] fgLookupTable = new byte[128]; 94 95 98 99 100 private static final int RESERVED_CHARACTERS = 0x01; 102 103 105 private static final int MARK_CHARACTERS = 0x02; 106 107 108 private static final int SCHEME_CHARACTERS = 0x04; 109 110 112 private static final int USERINFO_CHARACTERS = 0x08; 113 114 115 private static final int ASCII_ALPHA_CHARACTERS = 0x10; 116 117 118 private static final int ASCII_DIGIT_CHARACTERS = 0x20; 119 120 121 private static final int ASCII_HEX_CHARACTERS = 0x40; 122 123 124 private static final int PATH_CHARACTERS = 0x80; 125 126 127 private static final int MASK_ALPHA_NUMERIC = ASCII_ALPHA_CHARACTERS | ASCII_DIGIT_CHARACTERS; 128 129 130 private static final int MASK_UNRESERVED_MASK = MASK_ALPHA_NUMERIC | MARK_CHARACTERS; 131 132 133 private static final int MASK_URI_CHARACTER = MASK_UNRESERVED_MASK | RESERVED_CHARACTERS; 134 135 136 private static final int MASK_SCHEME_CHARACTER = MASK_ALPHA_NUMERIC | SCHEME_CHARACTERS; 137 138 139 private static final int MASK_USERINFO_CHARACTER = MASK_UNRESERVED_MASK | USERINFO_CHARACTERS; 140 141 142 private static final int MASK_PATH_CHARACTER = MASK_UNRESERVED_MASK | PATH_CHARACTERS; 143 144 static { 145 for (int i = '0'; i <= '9'; ++i) { 147 fgLookupTable[i] |= ASCII_DIGIT_CHARACTERS | ASCII_HEX_CHARACTERS; 148 } 149 150 for (int i = 'A'; i <= 'F'; ++i) { 152 fgLookupTable[i] |= ASCII_ALPHA_CHARACTERS | ASCII_HEX_CHARACTERS; 153 fgLookupTable[i+0x00000020] |= ASCII_ALPHA_CHARACTERS | ASCII_HEX_CHARACTERS; 154 } 155 156 for (int i = 'G'; i <= 'Z'; ++i) { 158 fgLookupTable[i] |= ASCII_ALPHA_CHARACTERS; 159 fgLookupTable[i+0x00000020] |= ASCII_ALPHA_CHARACTERS; 160 } 161 162 fgLookupTable[';'] |= RESERVED_CHARACTERS; 164 fgLookupTable['/'] |= RESERVED_CHARACTERS; 165 fgLookupTable['?'] |= RESERVED_CHARACTERS; 166 fgLookupTable[':'] |= RESERVED_CHARACTERS; 167 fgLookupTable['@'] |= RESERVED_CHARACTERS; 168 fgLookupTable['&'] |= RESERVED_CHARACTERS; 169 fgLookupTable['='] |= RESERVED_CHARACTERS; 170 fgLookupTable['+'] |= RESERVED_CHARACTERS; 171 fgLookupTable['$'] |= RESERVED_CHARACTERS; 172 fgLookupTable[','] |= RESERVED_CHARACTERS; 173 fgLookupTable['['] |= RESERVED_CHARACTERS; 174 fgLookupTable[']'] |= RESERVED_CHARACTERS; 175 176 fgLookupTable['-'] |= MARK_CHARACTERS; 178 fgLookupTable['_'] |= MARK_CHARACTERS; 179 fgLookupTable['.'] |= MARK_CHARACTERS; 180 fgLookupTable['!'] |= MARK_CHARACTERS; 181 fgLookupTable['~'] |= MARK_CHARACTERS; 182 fgLookupTable['*'] |= MARK_CHARACTERS; 183 fgLookupTable['\''] |= MARK_CHARACTERS; 184 fgLookupTable['('] |= MARK_CHARACTERS; 185 fgLookupTable[')'] |= MARK_CHARACTERS; 186 187 fgLookupTable['+'] |= SCHEME_CHARACTERS; 189 fgLookupTable['-'] |= SCHEME_CHARACTERS; 190 fgLookupTable['.'] |= SCHEME_CHARACTERS; 191 192 fgLookupTable[';'] |= USERINFO_CHARACTERS; 194 fgLookupTable[':'] |= USERINFO_CHARACTERS; 195 fgLookupTable['&'] |= USERINFO_CHARACTERS; 196 fgLookupTable['='] |= USERINFO_CHARACTERS; 197 fgLookupTable['+'] |= USERINFO_CHARACTERS; 198 fgLookupTable['$'] |= USERINFO_CHARACTERS; 199 fgLookupTable[','] |= USERINFO_CHARACTERS; 200 201 fgLookupTable[';'] |= PATH_CHARACTERS; 203 fgLookupTable['/'] |= PATH_CHARACTERS; 204 fgLookupTable[':'] |= PATH_CHARACTERS; 205 fgLookupTable['@'] |= PATH_CHARACTERS; 206 fgLookupTable['&'] |= PATH_CHARACTERS; 207 fgLookupTable['='] |= PATH_CHARACTERS; 208 fgLookupTable['+'] |= PATH_CHARACTERS; 209 fgLookupTable['$'] |= PATH_CHARACTERS; 210 fgLookupTable[','] |= PATH_CHARACTERS; 211 } 212 213 214 private String m_scheme = null; 215 216 217 private String m_userinfo = null; 218 219 220 private String m_host = null; 221 222 223 private int m_port = -1; 224 225 226 private String m_regAuthority = null; 227 228 229 private String m_path = null; 230 231 233 private String m_queryString = null; 234 235 236 private String m_fragment = null; 237 238 private static boolean DEBUG = false; 239 240 243 public URI() { 244 } 245 246 252 public URI(URI p_other) { 253 initialize(p_other); 254 } 255 256 271 public URI(String p_uriSpec) throws MalformedURIException { 272 this((URI)null, p_uriSpec); 273 } 274 275 294 public URI(String p_uriSpec, boolean allowNonAbsoluteURI) throws MalformedURIException { 295 this((URI)null, p_uriSpec, allowNonAbsoluteURI); 296 } 297 298 310 public URI(URI p_base, String p_uriSpec) throws MalformedURIException { 311 initialize(p_base, p_uriSpec); 312 } 313 314 331 public URI(URI p_base, String p_uriSpec, boolean allowNonAbsoluteURI) throws MalformedURIException { 332 initialize(p_base, p_uriSpec, allowNonAbsoluteURI); 333 } 334 335 347 public URI(String p_scheme, String p_schemeSpecificPart) 348 throws MalformedURIException { 349 if (p_scheme == null || p_scheme.trim().length() == 0) { 350 throw new MalformedURIException( 351 "Cannot construct URI with null/empty scheme!"); 352 } 353 if (p_schemeSpecificPart == null || 354 p_schemeSpecificPart.trim().length() == 0) { 355 throw new MalformedURIException( 356 "Cannot construct URI with null/empty scheme-specific part!"); 357 } 358 setScheme(p_scheme); 359 setPath(p_schemeSpecificPart); 360 } 361 362 383 public URI(String p_scheme, String p_host, String p_path, 384 String p_queryString, String p_fragment) 385 throws MalformedURIException { 386 this(p_scheme, null, p_host, -1, p_path, p_queryString, p_fragment); 387 } 388 389 414 public URI(String p_scheme, String p_userinfo, 415 String p_host, int p_port, String p_path, 416 String p_queryString, String p_fragment) 417 throws MalformedURIException { 418 if (p_scheme == null || p_scheme.trim().length() == 0) { 419 throw new MalformedURIException("Scheme is required!"); 420 } 421 422 if (p_host == null) { 423 if (p_userinfo != null) { 424 throw new MalformedURIException( 425 "Userinfo may not be specified if host is not specified!"); 426 } 427 if (p_port != -1) { 428 throw new MalformedURIException( 429 "Port may not be specified if host is not specified!"); 430 } 431 } 432 433 if (p_path != null) { 434 if (p_path.indexOf('?') != -1 && p_queryString != null) { 435 throw new MalformedURIException( 436 "Query string cannot be specified in path and query string!"); 437 } 438 439 if (p_path.indexOf('#') != -1 && p_fragment != null) { 440 throw new MalformedURIException( 441 "Fragment cannot be specified in both the path and fragment!"); 442 } 443 } 444 445 setScheme(p_scheme); 446 setHost(p_host); 447 setPort(p_port); 448 setUserinfo(p_userinfo); 449 setPath(p_path); 450 setQueryString(p_queryString); 451 setFragment(p_fragment); 452 } 453 454 459 private void initialize(URI p_other) { 460 m_scheme = p_other.getScheme(); 461 m_userinfo = p_other.getUserinfo(); 462 m_host = p_other.getHost(); 463 m_port = p_other.getPort(); 464 m_regAuthority = p_other.getRegBasedAuthority(); 465 m_path = p_other.getPath(); 466 m_queryString = p_other.getQueryString(); 467 m_fragment = p_other.getFragment(); 468 } 469 470 488 private void initialize(URI p_base, String p_uriSpec, boolean allowNonAbsoluteURI) 489 throws MalformedURIException { 490 491 String uriSpec = p_uriSpec; 492 int uriSpecLen = (uriSpec != null) ? uriSpec.length() : 0; 493 494 if (p_base == null && uriSpecLen == 0) { 495 if (allowNonAbsoluteURI) { 496 m_path = ""; 497 return; 498 } 499 throw new MalformedURIException("Cannot initialize URI with empty parameters."); 500 } 501 502 if (uriSpecLen == 0) { 504 initialize(p_base); 505 return; 506 } 507 508 int index = 0; 509 510 int colonIdx = uriSpec.indexOf(':'); 512 if (colonIdx != -1) { 513 final int searchFrom = colonIdx - 1; 514 int slashIdx = uriSpec.lastIndexOf('/', searchFrom); 516 int queryIdx = uriSpec.lastIndexOf('?', searchFrom); 517 int fragmentIdx = uriSpec.lastIndexOf('#', searchFrom); 518 519 if (colonIdx == 0 || slashIdx != -1 || 520 queryIdx != -1 || fragmentIdx != -1) { 521 if (colonIdx == 0 || (p_base == null && fragmentIdx != 0 && !allowNonAbsoluteURI)) { 523 throw new MalformedURIException("No scheme found in URI."); 524 } 525 } 526 else { 527 initializeScheme(uriSpec); 528 index = m_scheme.length()+1; 529 530 if (colonIdx == uriSpecLen - 1 || uriSpec.charAt(colonIdx+1) == '#') { 532 throw new MalformedURIException("Scheme specific part cannot be empty."); 533 } 534 } 535 } 536 else if (p_base == null && uriSpec.indexOf('#') != 0 && !allowNonAbsoluteURI) { 537 throw new MalformedURIException("No scheme found in URI."); 538 } 539 540 if (((index+1) < uriSpecLen) && 550 (uriSpec.charAt(index) == '/' && uriSpec.charAt(index+1) == '/')) { 551 index += 2; 552 int startPos = index; 553 554 char testChar = '\0'; 556 while (index < uriSpecLen) { 557 testChar = uriSpec.charAt(index); 558 if (testChar == '/' || testChar == '?' || testChar == '#') { 559 break; 560 } 561 index++; 562 } 563 564 if (index > startPos) { 568 if (!initializeAuthority(uriSpec.substring(startPos, index))) { 571 index = startPos - 2; 572 } 573 } 574 else { 575 m_host = ""; 576 } 577 } 578 579 initializePath(uriSpec, index); 580 581 if (p_base != null) { 587 absolutize(p_base); 588 } 589 } 590 591 607 private void initialize(URI p_base, String p_uriSpec) 608 throws MalformedURIException { 609 610 String uriSpec = p_uriSpec; 611 int uriSpecLen = (uriSpec != null) ? uriSpec.length() : 0; 612 613 if (p_base == null && uriSpecLen == 0) { 614 throw new MalformedURIException( 615 "Cannot initialize URI with empty parameters."); 616 } 617 618 if (uriSpecLen == 0) { 620 initialize(p_base); 621 return; 622 } 623 624 int index = 0; 625 626 int colonIdx = uriSpec.indexOf(':'); 628 if (colonIdx != -1) { 629 final int searchFrom = colonIdx - 1; 630 int slashIdx = uriSpec.lastIndexOf('/', searchFrom); 632 int queryIdx = uriSpec.lastIndexOf('?', searchFrom); 633 int fragmentIdx = uriSpec.lastIndexOf('#', searchFrom); 634 635 if (colonIdx == 0 || slashIdx != -1 || 636 queryIdx != -1 || fragmentIdx != -1) { 637 if (colonIdx == 0 || (p_base == null && fragmentIdx != 0)) { 639 throw new MalformedURIException("No scheme found in URI."); 640 } 641 } 642 else { 643 initializeScheme(uriSpec); 644 index = m_scheme.length()+1; 645 646 if (colonIdx == uriSpecLen - 1 || uriSpec.charAt(colonIdx+1) == '#') { 648 throw new MalformedURIException("Scheme specific part cannot be empty."); 649 } 650 } 651 } 652 else if (p_base == null && uriSpec.indexOf('#') != 0) { 653 throw new MalformedURIException("No scheme found in URI."); 654 } 655 656 if (((index+1) < uriSpecLen) && 666 (uriSpec.charAt(index) == '/' && uriSpec.charAt(index+1) == '/')) { 667 index += 2; 668 int startPos = index; 669 670 char testChar = '\0'; 672 while (index < uriSpecLen) { 673 testChar = uriSpec.charAt(index); 674 if (testChar == '/' || testChar == '?' || testChar == '#') { 675 break; 676 } 677 index++; 678 } 679 680 if (index > startPos) { 684 if (!initializeAuthority(uriSpec.substring(startPos, index))) { 687 index = startPos - 2; 688 } 689 } 690 else { 691 m_host = ""; 692 } 693 } 694 695 initializePath(uriSpec, index); 696 697 if (p_base != null) { 703 absolutize(p_base); 704 } 705 } 706 707 712 public void absolutize(URI p_base) { 713 714 if (m_path.length() == 0 && m_scheme == null && 722 m_host == null && m_regAuthority == null) { 723 m_scheme = p_base.getScheme(); 724 m_userinfo = p_base.getUserinfo(); 725 m_host = p_base.getHost(); 726 m_port = p_base.getPort(); 727 m_regAuthority = p_base.getRegBasedAuthority(); 728 m_path = p_base.getPath(); 729 730 if (m_queryString == null) { 731 m_queryString = p_base.getQueryString(); 732 733 if (m_fragment == null) { 734 m_fragment = p_base.getFragment(); 735 } 736 } 737 return; 738 } 739 740 if (m_scheme == null) { 743 m_scheme = p_base.getScheme(); 744 } 745 else { 746 return; 747 } 748 749 if (m_host == null && m_regAuthority == null) { 752 m_userinfo = p_base.getUserinfo(); 753 m_host = p_base.getHost(); 754 m_port = p_base.getPort(); 755 m_regAuthority = p_base.getRegBasedAuthority(); 756 } 757 else { 758 return; 759 } 760 761 if (m_path.length() > 0 && 763 m_path.startsWith("/")) { 764 return; 765 } 766 767 String path = ""; 770 String basePath = p_base.getPath(); 771 772 if (basePath != null && basePath.length() > 0) { 774 int lastSlash = basePath.lastIndexOf('/'); 775 if (lastSlash != -1) { 776 path = basePath.substring(0, lastSlash+1); 777 } 778 } 779 else if (m_path.length() > 0) { 780 path = "/"; 781 } 782 783 path = path.concat(m_path); 785 786 int index = -1; 788 while ((index = path.indexOf("/./")) != -1) { 789 path = path.substring(0, index+1).concat(path.substring(index+3)); 790 } 791 792 if (path.endsWith("/.")) { 794 path = path.substring(0, path.length()-1); 795 } 796 797 index = 1; 800 int segIndex = -1; 801 String tempString = null; 802 803 while ((index = path.indexOf("/../", index)) > 0) { 804 tempString = path.substring(0, path.indexOf("/../")); 805 segIndex = tempString.lastIndexOf('/'); 806 if (segIndex != -1) { 807 if (!tempString.substring(segIndex).equals("..")) { 808 path = path.substring(0, segIndex+1).concat(path.substring(index+4)); 809 index = segIndex; 810 } 811 else { 812 index += 4; 813 } 814 } 815 else { 816 index += 4; 817 } 818 } 819 820 if (path.endsWith("/..")) { 823 tempString = path.substring(0, path.length()-3); 824 segIndex = tempString.lastIndexOf('/'); 825 if (segIndex != -1) { 826 path = path.substring(0, segIndex+1); 827 } 828 } 829 m_path = path; 830 } 831 832 840 private void initializeScheme(String p_uriSpec) 841 throws MalformedURIException { 842 int uriSpecLen = p_uriSpec.length(); 843 int index = 0; 844 String scheme = null; 845 char testChar = '\0'; 846 847 while (index < uriSpecLen) { 848 testChar = p_uriSpec.charAt(index); 849 if (testChar == ':' || testChar == '/' || 850 testChar == '?' || testChar == '#') { 851 break; 852 } 853 index++; 854 } 855 scheme = p_uriSpec.substring(0, index); 856 857 if (scheme.length() == 0) { 858 throw new MalformedURIException("No scheme found in URI."); 859 } 860 else { 861 setScheme(scheme); 862 } 863 } 864 865 874 private boolean initializeAuthority(String p_uriSpec) { 875 876 int index = 0; 877 int start = 0; 878 int end = p_uriSpec.length(); 879 880 char testChar = '\0'; 881 String userinfo = null; 882 883 if (p_uriSpec.indexOf('@', start) != -1) { 885 while (index < end) { 886 testChar = p_uriSpec.charAt(index); 887 if (testChar == '@') { 888 break; 889 } 890 index++; 891 } 892 userinfo = p_uriSpec.substring(start, index); 893 index++; 894 } 895 896 String host = null; 899 start = index; 900 boolean hasPort = false; 901 if (index < end) { 902 if (p_uriSpec.charAt(start) == '[') { 903 int bracketIndex = p_uriSpec.indexOf(']', start); 904 index = (bracketIndex != -1) ? bracketIndex : end; 905 if (index+1 < end && p_uriSpec.charAt(index+1) == ':') { 906 ++index; 907 hasPort = true; 908 } 909 else { 910 index = end; 911 } 912 } 913 else { 914 int colonIndex = p_uriSpec.lastIndexOf(':', end); 915 index = (colonIndex > start) ? colonIndex : end; 916 hasPort = (index != end); 917 } 918 } 919 host = p_uriSpec.substring(start, index); 920 int port = -1; 921 if (host.length() > 0) { 922 if (hasPort) { 924 index++; 925 start = index; 926 while (index < end) { 927 index++; 928 } 929 String portStr = p_uriSpec.substring(start, index); 930 if (portStr.length() > 0) { 931 939 try { 942 port = Integer.parseInt(portStr); 943 if (port == -1) --port; 944 } 945 catch (NumberFormatException nfe) { 946 port = -2; 947 } 948 } 949 } 950 } 951 952 if (isValidServerBasedAuthority(host, port, userinfo)) { 953 m_host = host; 954 m_port = port; 955 m_userinfo = userinfo; 956 &n
|