1 7 8 package java.net; 9 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.OutputStream ; 13 import java.util.Hashtable ; 14 import java.util.StringTokenizer ; 15 import sun.security.util.SecurityConstants; 16 17 116 public final class URL implements java.io.Serializable { 117 118 static final long serialVersionUID = -7627629688361524110L; 119 120 132 private static final String protocolPathProp = "java.protocol.handler.pkgs"; 133 134 138 private String protocol; 139 140 144 private String host; 145 146 150 private int port = -1; 151 152 157 private String file; 158 159 162 private transient String query; 163 164 168 private String authority; 169 170 173 private transient String path; 174 175 178 private transient String userInfo; 179 180 184 private String ref; 185 186 190 transient InetAddress hostAddress; 191 192 195 transient URLStreamHandler handler; 196 197 200 private int hashCode = -1; 201 202 280 public URL(String protocol, String host, int port, String file) 281 throws MalformedURLException 282 { 283 this(protocol, host, port, file, null); 284 } 285 286 304 public URL(String protocol, String host, String file) 305 throws MalformedURLException { 306 this(protocol, host, -1, file); 307 } 308 309 348 public URL(String protocol, String host, int port, String file, 349 URLStreamHandler handler) throws MalformedURLException { 350 if (handler != null) { 351 SecurityManager sm = System.getSecurityManager(); 352 if (sm != null) { 353 checkSpecifyHandler(sm); 355 } 356 } 357 358 protocol = protocol.toLowerCase(); 359 this.protocol = protocol; 360 if (host != null) { 361 362 366 if (host != null && host.indexOf(':') >= 0 367 && !host.startsWith("[")) { 368 host = "["+host+"]"; 369 } 370 this.host = host; 371 372 if (port < -1) { 373 throw new MalformedURLException ("Invalid port number :" + 374 port); 375 } 376 this.port = port; 377 authority = (port == -1) ? host : host + ":" + port; 378 } 379 380 Parts parts = new Parts(file); 381 path = parts.getPath(); 382 query = parts.getQuery(); 383 384 if (query != null) { 385 this.file = path + "?" + query; 386 } else { 387 this.file = path; 388 } 389 ref = parts.getRef(); 390 391 if (handler == null && 394 (handler = getURLStreamHandler(protocol)) == null) { 395 throw new MalformedURLException ("unknown protocol: " + protocol); 396 } 397 this.handler = handler; 398 } 399 400 412 public URL(String spec) throws MalformedURLException { 413 this(null, spec); 414 } 415 416 463 public URL(URL context, String spec) throws MalformedURLException { 464 this(context, spec, null); 465 } 466 467 487 public URL(URL context, String spec, URLStreamHandler handler) 488 throws MalformedURLException 489 { 490 String original = spec; 491 int i, limit, c; 492 int start = 0; 493 String newProtocol = null; 494 boolean aRef=false; 495 boolean isRelative = false; 496 497 if (handler != null) { 499 SecurityManager sm = System.getSecurityManager(); 500 if (sm != null) { 501 checkSpecifyHandler(sm); 502 } 503 } 504 505 try { 506 limit = spec.length(); 507 while ((limit > 0) && (spec.charAt(limit - 1) <= ' ')) { 508 limit--; } 510 while ((start < limit) && (spec.charAt(start) <= ' ')) { 511 start++; } 513 514 if (spec.regionMatches(true, start, "url:", 0, 4)) { 515 start += 4; 516 } 517 if (start < spec.length() && spec.charAt(start) == '#') { 518 522 aRef=true; 523 } 524 for (i = start ; !aRef && (i < limit) && 525 ((c = spec.charAt(i)) != '/') ; i++) { 526 if (c == ':') { 527 528 String s = spec.substring(start, i).toLowerCase(); 529 if (isValidProtocol(s)) { 530 newProtocol = s; 531 start = i + 1; 532 } 533 break; 534 } 535 } 536 537 protocol = newProtocol; 539 if ((context != null) && ((newProtocol == null) || 540 newProtocol.equalsIgnoreCase(context.protocol))) { 541 if (handler == null) { 544 handler = context.handler; 545 } 546 547 if (context.path != null && context.path.startsWith("/")) 552 newProtocol = null; 553 554 if (newProtocol == null) { 555 protocol = context.protocol; 556 authority = context.authority; 557 userInfo = context.userInfo; 558 host = context.host; 559 port = context.port; 560 file = context.file; 561 path = context.path; 562 isRelative = true; 563 } 564 } 565 566 if (protocol == null) { 567 throw new MalformedURLException ("no protocol: "+original); 568 } 569 570 if (handler == null && 573 (handler = getURLStreamHandler(protocol)) == null) { 574 throw new MalformedURLException ("unknown protocol: "+protocol); 575 } 576 577 this.handler = handler; 578 579 i = spec.indexOf('#', start); 580 if (i >= 0) { 581 ref = spec.substring(i + 1, limit); 582 limit = i; 583 } 584 585 589 if (isRelative && start == limit) { 590 query = context.query; 591 if (ref == null) { 592 ref = context.ref; 593 } 594 } 595 596 handler.parseURL(this, spec, start, limit); 597 598 } catch(MalformedURLException e) { 599 throw e; 600 } catch(Exception e) { 601 throw new MalformedURLException (e.getMessage()); 602 } 603 } 604 605 608 private boolean isValidProtocol(String protocol) { 609 int len = protocol.length(); 610 if (len < 1) 611 return false; 612 char c = protocol.charAt(0); 613 if (!Character.isLetter(c)) 614 return false; 615 for (int i = 1; i < len; i++) { 616 c = protocol.charAt(i); 617 if (!Character.isLetterOrDigit(c) && c != '.' && c != '+' && 618 c != '-') { 619 return false; 620 } 621 } 622 return true; 623 } 624 625 628 private void checkSpecifyHandler(SecurityManager sm) { 629 sm.checkPermission(SecurityConstants.SPECIFY_HANDLER_PERMISSION); 630 } 631 632 643 protected void set(String protocol, String host, 644 int port, String file, String ref) { 645 synchronized (this) { 646 this.protocol = protocol; 647 this.host = host; 648 authority = port == -1 ? host : host + ":" + port; 649 this.port = port; 650 this.file = file; 651 this.ref = ref; 652 654 hashCode = -1; 655 hostAddress = null; 656 int q = file.lastIndexOf('?'); 657 if (q != -1) { 658 query = file.substring(q+1); 659 path = file.substring(0, q); 660 } else 661 path = file; 662 } 663 } 664 665 680 protected void set(String protocol, String host, int port, 681 String authority, String userInfo, String path, 682 String query, String ref) { 683 synchronized (this) { 684 this.protocol = protocol; 685 this.host = host; 686 this.port = port; 687 this.file = query == null ? path : path + "?" + query; 688 this.userInfo = userInfo; 689 this.path = path; 690 this.ref = ref; 691 693 hashCode = -1; 694 hostAddress = null; 695 this.query = query; 696 this.authority = authority; 697 } 698 } 699 700 707 public String getQuery() { 708 return query; 709 } 710 711 718 public String getPath() { 719 return path; 720 } 721 722 728 public String getUserInfo() { 729 return userInfo; 730 } 731 732 738 public String getAuthority() { 739 return authority; 740 } 741 742 747 public int getPort() { 748 return port; 749 } 750 751 759 public int getDefaultPort() { 760 return handler.getDefaultPort(); 761 } 762 763 768 public String getProtocol() { 769 return protocol; 770 } 771 772 780 public String getHost() { 781 return host; 782 } 783 784 795 public String getFile() { 796 return file; 797 } 798 799 806 public String getRef() { 807 return ref; 808 } 809 810 835 public boolean equals(Object obj) { 836 if (!(obj instanceof URL )) 837 return false; 838 URL u2 = (URL )obj; 839 840 return handler.equals(this, u2); 841 } 842 843 851 public synchronized int hashCode() { 852 if (hashCode != -1) 853 return hashCode; 854 855 hashCode = handler.hashCode(this); 856 return hashCode; 857 } 858 859 870 public boolean sameFile(URL other) { 871 return handler.sameFile(this, other); 872 } 873 874 884 public String toString() { 885 return toExternalForm(); 886 } 887 888 898 public String toExternalForm() { 899 return handler.toExternalForm(this); 900 } 901 902 915 public URI toURI() throws URISyntaxException { 916 return new URI (toString()); 917 } 918 919 942 public URLConnection openConnection() throws java.io.IOException { 943 return handler.openConnection(this); 944 } 945 946 975 public URLConnection openConnection(Proxy proxy) 976 throws java.io.IOException { 977 if (proxy == null) { 978 throw new IllegalArgumentException ("proxy can not be null"); 979 } 980 981 SecurityManager sm = System.getSecurityManager(); 982 if (proxy.type() != Proxy.Type.DIRECT && sm != null) { 983 InetSocketAddress epoint = (InetSocketAddress ) proxy.address(); 984 if (epoint.isUnresolved()) 985 sm.checkConnect(epoint.getHostName(), epoint.getPort()); 986 else 987 sm.checkConnect(epoint.getAddress().getHostAddress(), 988 epoint.getPort()); 989 } 990 return handler.openConnection(this, proxy); 991 } 992 993 1006 public final InputStream openStream() throws java.io.IOException { 1007 return openConnection().getInputStream(); 1008 } 1009 1010 1020 public final Object getContent() throws java.io.IOException { 1021 return openConnection().getContent(); 1022 } 1023 1024 1038 public final Object getContent(Class [] classes) 1039 throws java.io.IOException { 1040 return openConnection().getContent(classes); 1041 } 1042 1043 1046 static URLStreamHandlerFactory factory; 1047 1048 1071 public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) { 1072 synchronized (streamHandlerLock) { 1073 if (factory != null) { 1074 throw new Error ("factory already defined"); 1075 } 1076 SecurityManager security = System.getSecurityManager(); 1077 if (security != null) { 1078 security.checkSetFactory(); 1079 } 1080 handlers.clear(); 1081 factory = fac; 1082 } 1083 } 1084 1085 1088 static Hashtable handlers = new Hashtable (); 1089 private static Object streamHandlerLock = new Object (); 1090 1091 1095 static URLStreamHandler getURLStreamHandler(String protocol) { 1096 1097 URLStreamHandler handler = (URLStreamHandler )handlers.get(protocol); 1098 if (handler == null) { 1099 1100 boolean checkedWithFactory = false; 1101 1102 if (factory != null) { 1104 handler = factory.createURLStreamHandler(protocol); 1105 checkedWithFactory = true; 1106 } 1107 1108 if (handler == null) { 1110 String packagePrefixList = null; 1111 1112 packagePrefixList 1113 = (String ) java.security.AccessController.doPrivileged( 1114 new sun.security.action.GetPropertyAction( 1115 protocolPathProp,"")); 1116 if (packagePrefixList != "") { 1117 packagePrefixList += "|"; 1118 } 1119 1120 packagePrefixList += "sun.net.www.protocol"; 1123 1124 StringTokenizer packagePrefixIter = 1125 new StringTokenizer (packagePrefixList, "|"); 1126 1127 while (handler == null && 1128 packagePrefixIter.hasMoreTokens()) { 1129 1130 String packagePrefix = 1131 packagePrefixIter.nextToken().trim(); 1132 try { 1133 String clsName = packagePrefix + "." + protocol + 1134 ".Handler"; 1135 Class cls = null; 1136 try { 1137 cls = Class.forName(clsName); 1138 } catch (ClassNotFoundException e) { 1139 ClassLoader cl = ClassLoader.getSystemClassLoader(); 1140 if (cl != null) { 1141 cls = cl.loadClass(clsName); 1142 } 1143 } 1144 if (cls != null) { 1145 handler = 1146 (URLStreamHandler )cls.newInstance(); 1147 } 1148 } catch (Exception e) { 1149 } 1151 } 1152 } 1153 1154 synchronized (streamHandlerLock) { 1155 1156 URLStreamHandler handler2 = null; 1157 1158 handler2 = (URLStreamHandler )handlers.get(protocol); 1161 1162 if (handler2 != null) { 1163 return handler2; 1164 } 1165 1166 if (!checkedWithFactory && factory != null) { 1169 handler2 = factory.createURLStreamHandler(protocol); 1170 } 1171 1172 if (handler2 != null) { 1173 handler = handler2; 1177 } 1178 1179 if (handler != null) { 1181 handlers.put(protocol, handler); 1182 } 1183 1184 } 1185 } 1186 1187 return handler; 1188 1189 } 1190 1191 1201 private synchronized void writeObject(java.io.ObjectOutputStream s) 1202 throws IOException 1203 { 1204 s.defaultWriteObject(); } 1206 1207 1212 private synchronized void readObject(java.io.ObjectInputStream s) 1213 throws IOException , ClassNotFoundException 1214 { 1215 s.defaultReadObject(); if ((handler = getURLStreamHandler(protocol)) == null) { 1217 throw new IOException ("unknown protocol: " + protocol); 1218 } 1219 1220 if (authority == null && 1222 ((host != null && host.length() > 0) || port != -1)) { 1223 if (host == null) 1224 host = ""; 1225 authority = (port == -1) ? host : host + ":" + port; 1226 1227 int at = host.lastIndexOf('@'); 1229 if (at != -1) { 1230 userInfo = host.substring(0, at); 1231 host = host.substring(at+1); 1232 } 1233 } else if (authority != null) { 1234 int ind = authority.indexOf('@'); 1236 if (ind != -1) 1237 userInfo = authority.substring(0, ind); 1238 } 1239 1240 path = null; 1242 query = null; 1243 if (file != null) { 1244 int q = file.lastIndexOf('?'); 1246 if (q != -1) { 1247 query = file.substring(q+1); 1248 path = file.substring(0, q); 1249 } else 1250 path = file; 1251 } 1252 } 1253} 1254 1255class Parts { 1256 String path, query, ref; 1257 1258 Parts(String file) { 1259 int ind = file.indexOf('#'); 1260 ref = ind < 0 ? null: file.substring(ind + 1); 1261 file = ind < 0 ? file: file.substring(0, ind); 1262 int q = file.lastIndexOf('?'); 1263 if (q != -1) { 1264 query = file.substring(q+1); 1265 path = file.substring(0, q); 1266 } else { 1267 path = file; 1268 } 1269 } 1270 1271 String getPath() { 1272 return path; 1273 } 1274 1275 String getQuery() { 1276 return query; 1277 } 1278 1279 String getRef() { 1280 return ref; 1281 } 1282} 1283 1284 | Popular Tags |