1.1 --- a/emul/mini/src/main/java/java/net/URLStreamHandler.java Tue Feb 26 14:55:55 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,568 +0,0 @@
1.4 -/*
1.5 - * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 - *
1.8 - * This code is free software; you can redistribute it and/or modify it
1.9 - * under the terms of the GNU General Public License version 2 only, as
1.10 - * published by the Free Software Foundation. Oracle designates this
1.11 - * particular file as subject to the "Classpath" exception as provided
1.12 - * by Oracle in the LICENSE file that accompanied this code.
1.13 - *
1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 - * version 2 for more details (a copy is included in the LICENSE file that
1.18 - * accompanied this code).
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License version
1.21 - * 2 along with this work; if not, write to the Free Software Foundation,
1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 - *
1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 - * or visit www.oracle.com if you need additional information or have any
1.26 - * questions.
1.27 - */
1.28 -
1.29 -package java.net;
1.30 -
1.31 -
1.32 -/**
1.33 - * The abstract class <code>URLStreamHandler</code> is the common
1.34 - * superclass for all stream protocol handlers. A stream protocol
1.35 - * handler knows how to make a connection for a particular protocol
1.36 - * type, such as <code>http</code>, <code>ftp</code>, or
1.37 - * <code>gopher</code>.
1.38 - * <p>
1.39 - * In most cases, an instance of a <code>URLStreamHandler</code>
1.40 - * subclass is not created directly by an application. Rather, the
1.41 - * first time a protocol name is encountered when constructing a
1.42 - * <code>URL</code>, the appropriate stream protocol handler is
1.43 - * automatically loaded.
1.44 - *
1.45 - * @author James Gosling
1.46 - * @see java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
1.47 - * @since JDK1.0
1.48 - */
1.49 -public abstract class URLStreamHandler {
1.50 - /**
1.51 - * Opens a connection to the object referenced by the
1.52 - * <code>URL</code> argument.
1.53 - * This method should be overridden by a subclass.
1.54 - *
1.55 - * <p>If for the handler's protocol (such as HTTP or JAR), there
1.56 - * exists a public, specialized URLConnection subclass belonging
1.57 - * to one of the following packages or one of their subpackages:
1.58 - * java.lang, java.io, java.util, java.net, the connection
1.59 - * returned will be of that subclass. For example, for HTTP an
1.60 - * HttpURLConnection will be returned, and for JAR a
1.61 - * JarURLConnection will be returned.
1.62 - *
1.63 - * @param u the URL that this connects to.
1.64 - * @return a <code>URLConnection</code> object for the <code>URL</code>.
1.65 - * @exception IOException if an I/O error occurs while opening the
1.66 - * connection.
1.67 - */
1.68 -// abstract protected URLConnection openConnection(URL u) throws IOException;
1.69 -
1.70 - /**
1.71 - * Same as openConnection(URL), except that the connection will be
1.72 - * made through the specified proxy; Protocol handlers that do not
1.73 - * support proxying will ignore the proxy parameter and make a
1.74 - * normal connection.
1.75 - *
1.76 - * Calling this method preempts the system's default ProxySelector
1.77 - * settings.
1.78 - *
1.79 - * @param u the URL that this connects to.
1.80 - * @param p the proxy through which the connection will be made.
1.81 - * If direct connection is desired, Proxy.NO_PROXY
1.82 - * should be specified.
1.83 - * @return a <code>URLConnection</code> object for the <code>URL</code>.
1.84 - * @exception IOException if an I/O error occurs while opening the
1.85 - * connection.
1.86 - * @exception IllegalArgumentException if either u or p is null,
1.87 - * or p has the wrong type.
1.88 - * @exception UnsupportedOperationException if the subclass that
1.89 - * implements the protocol doesn't support this method.
1.90 - * @since 1.5
1.91 - */
1.92 -// protected URLConnection openConnection(URL u, Proxy p) throws IOException {
1.93 -// throw new UnsupportedOperationException("Method not implemented.");
1.94 -// }
1.95 -
1.96 - /**
1.97 - * Parses the string representation of a <code>URL</code> into a
1.98 - * <code>URL</code> object.
1.99 - * <p>
1.100 - * If there is any inherited context, then it has already been
1.101 - * copied into the <code>URL</code> argument.
1.102 - * <p>
1.103 - * The <code>parseURL</code> method of <code>URLStreamHandler</code>
1.104 - * parses the string representation as if it were an
1.105 - * <code>http</code> specification. Most URL protocol families have a
1.106 - * similar parsing. A stream protocol handler for a protocol that has
1.107 - * a different syntax must override this routine.
1.108 - *
1.109 - * @param u the <code>URL</code> to receive the result of parsing
1.110 - * the spec.
1.111 - * @param spec the <code>String</code> representing the URL that
1.112 - * must be parsed.
1.113 - * @param start the character index at which to begin parsing. This is
1.114 - * just past the '<code>:</code>' (if there is one) that
1.115 - * specifies the determination of the protocol name.
1.116 - * @param limit the character position to stop parsing at. This is the
1.117 - * end of the string or the position of the
1.118 - * "<code>#</code>" character, if present. All information
1.119 - * after the sharp sign indicates an anchor.
1.120 - */
1.121 - protected void parseURL(URL u, String spec, int start, int limit) {
1.122 - // These fields may receive context content if this was relative URL
1.123 - String protocol = u.getProtocol();
1.124 - String authority = u.getAuthority();
1.125 - String userInfo = u.getUserInfo();
1.126 - String host = u.getHost();
1.127 - int port = u.getPort();
1.128 - String path = u.getPath();
1.129 - String query = u.getQuery();
1.130 -
1.131 - // This field has already been parsed
1.132 - String ref = u.getRef();
1.133 -
1.134 - boolean isRelPath = false;
1.135 - boolean queryOnly = false;
1.136 -
1.137 -// FIX: should not assume query if opaque
1.138 - // Strip off the query part
1.139 - if (start < limit) {
1.140 - int queryStart = spec.indexOf('?');
1.141 - queryOnly = queryStart == start;
1.142 - if ((queryStart != -1) && (queryStart < limit)) {
1.143 - query = spec.substring(queryStart+1, limit);
1.144 - if (limit > queryStart)
1.145 - limit = queryStart;
1.146 - spec = spec.substring(0, queryStart);
1.147 - }
1.148 - }
1.149 -
1.150 - int i = 0;
1.151 - // Parse the authority part if any
1.152 - boolean isUNCName = (start <= limit - 4) &&
1.153 - (spec.charAt(start) == '/') &&
1.154 - (spec.charAt(start + 1) == '/') &&
1.155 - (spec.charAt(start + 2) == '/') &&
1.156 - (spec.charAt(start + 3) == '/');
1.157 - if (!isUNCName && (start <= limit - 2) && (spec.charAt(start) == '/') &&
1.158 - (spec.charAt(start + 1) == '/')) {
1.159 - start += 2;
1.160 - i = spec.indexOf('/', start);
1.161 - if (i < 0) {
1.162 - i = spec.indexOf('?', start);
1.163 - if (i < 0)
1.164 - i = limit;
1.165 - }
1.166 -
1.167 - host = authority = spec.substring(start, i);
1.168 -
1.169 - int ind = authority.indexOf('@');
1.170 - if (ind != -1) {
1.171 - userInfo = authority.substring(0, ind);
1.172 - host = authority.substring(ind+1);
1.173 - } else {
1.174 - userInfo = null;
1.175 - }
1.176 - if (host != null) {
1.177 - // If the host is surrounded by [ and ] then its an IPv6
1.178 - // literal address as specified in RFC2732
1.179 - if (host.length()>0 && (host.charAt(0) == '[')) {
1.180 - if ((ind = host.indexOf(']')) > 2) {
1.181 -
1.182 - String nhost = host ;
1.183 - host = nhost.substring(0,ind+1);
1.184 -// if (!IPAddressUtil.
1.185 -// isIPv6LiteralAddress(host.substring(1, ind))) {
1.186 -// throw new IllegalArgumentException(
1.187 -// "Invalid host: "+ host);
1.188 -// }
1.189 -
1.190 - port = -1 ;
1.191 - if (nhost.length() > ind+1) {
1.192 - if (nhost.charAt(ind+1) == ':') {
1.193 - ++ind ;
1.194 - // port can be null according to RFC2396
1.195 - if (nhost.length() > (ind + 1)) {
1.196 - port = Integer.parseInt(nhost.substring(ind+1));
1.197 - }
1.198 - } else {
1.199 - throw new IllegalArgumentException(
1.200 - "Invalid authority field: " + authority);
1.201 - }
1.202 - }
1.203 - } else {
1.204 - throw new IllegalArgumentException(
1.205 - "Invalid authority field: " + authority);
1.206 - }
1.207 - } else {
1.208 - ind = host.indexOf(':');
1.209 - port = -1;
1.210 - if (ind >= 0) {
1.211 - // port can be null according to RFC2396
1.212 - if (host.length() > (ind + 1)) {
1.213 - port = Integer.parseInt(host.substring(ind + 1));
1.214 - }
1.215 - host = host.substring(0, ind);
1.216 - }
1.217 - }
1.218 - } else {
1.219 - host = "";
1.220 - }
1.221 - if (port < -1)
1.222 - throw new IllegalArgumentException("Invalid port number :" +
1.223 - port);
1.224 - start = i;
1.225 - // If the authority is defined then the path is defined by the
1.226 - // spec only; See RFC 2396 Section 5.2.4.
1.227 - if (authority != null && authority.length() > 0)
1.228 - path = "";
1.229 - }
1.230 -
1.231 - if (host == null) {
1.232 - host = "";
1.233 - }
1.234 -
1.235 - // Parse the file path if any
1.236 - if (start < limit) {
1.237 - if (spec.charAt(start) == '/') {
1.238 - path = spec.substring(start, limit);
1.239 - } else if (path != null && path.length() > 0) {
1.240 - isRelPath = true;
1.241 - int ind = path.lastIndexOf('/');
1.242 - String seperator = "";
1.243 - if (ind == -1 && authority != null)
1.244 - seperator = "/";
1.245 - path = path.substring(0, ind + 1) + seperator +
1.246 - spec.substring(start, limit);
1.247 -
1.248 - } else {
1.249 - String seperator = (authority != null) ? "/" : "";
1.250 - path = seperator + spec.substring(start, limit);
1.251 - }
1.252 - } else if (queryOnly && path != null) {
1.253 - int ind = path.lastIndexOf('/');
1.254 - if (ind < 0)
1.255 - ind = 0;
1.256 - path = path.substring(0, ind) + "/";
1.257 - }
1.258 - if (path == null)
1.259 - path = "";
1.260 -
1.261 - if (isRelPath) {
1.262 - // Remove embedded /./
1.263 - while ((i = path.indexOf("/./")) >= 0) {
1.264 - path = path.substring(0, i) + path.substring(i + 2);
1.265 - }
1.266 - // Remove embedded /../ if possible
1.267 - i = 0;
1.268 - while ((i = path.indexOf("/../", i)) >= 0) {
1.269 - /*
1.270 - * A "/../" will cancel the previous segment and itself,
1.271 - * unless that segment is a "/../" itself
1.272 - * i.e. "/a/b/../c" becomes "/a/c"
1.273 - * but "/../../a" should stay unchanged
1.274 - */
1.275 - if (i > 0 && (limit = path.lastIndexOf('/', i - 1)) >= 0 &&
1.276 - (path.indexOf("/../", limit) != 0)) {
1.277 - path = path.substring(0, limit) + path.substring(i + 3);
1.278 - i = 0;
1.279 - } else {
1.280 - i = i + 3;
1.281 - }
1.282 - }
1.283 - // Remove trailing .. if possible
1.284 - while (path.endsWith("/..")) {
1.285 - i = path.indexOf("/..");
1.286 - if ((limit = path.lastIndexOf('/', i - 1)) >= 0) {
1.287 - path = path.substring(0, limit+1);
1.288 - } else {
1.289 - break;
1.290 - }
1.291 - }
1.292 - // Remove starting .
1.293 - if (path.startsWith("./") && path.length() > 2)
1.294 - path = path.substring(2);
1.295 -
1.296 - // Remove trailing .
1.297 - if (path.endsWith("/."))
1.298 - path = path.substring(0, path.length() -1);
1.299 - }
1.300 -
1.301 - setURL(u, protocol, host, port, authority, userInfo, path, query, ref);
1.302 - }
1.303 -
1.304 - /**
1.305 - * Returns the default port for a URL parsed by this handler. This method
1.306 - * is meant to be overidden by handlers with default port numbers.
1.307 - * @return the default port for a <code>URL</code> parsed by this handler.
1.308 - * @since 1.3
1.309 - */
1.310 - protected int getDefaultPort() {
1.311 - return -1;
1.312 - }
1.313 -
1.314 - /**
1.315 - * Provides the default equals calculation. May be overidden by handlers
1.316 - * for other protocols that have different requirements for equals().
1.317 - * This method requires that none of its arguments is null. This is
1.318 - * guaranteed by the fact that it is only called by java.net.URL class.
1.319 - * @param u1 a URL object
1.320 - * @param u2 a URL object
1.321 - * @return <tt>true</tt> if the two urls are
1.322 - * considered equal, ie. they refer to the same
1.323 - * fragment in the same file.
1.324 - * @since 1.3
1.325 - */
1.326 - protected boolean equals(URL u1, URL u2) {
1.327 - String ref1 = u1.getRef();
1.328 - String ref2 = u2.getRef();
1.329 - return (ref1 == ref2 || (ref1 != null && ref1.equals(ref2))) &&
1.330 - sameFile(u1, u2);
1.331 - }
1.332 -
1.333 - /**
1.334 - * Provides the default hash calculation. May be overidden by handlers for
1.335 - * other protocols that have different requirements for hashCode
1.336 - * calculation.
1.337 - * @param u a URL object
1.338 - * @return an <tt>int</tt> suitable for hash table indexing
1.339 - * @since 1.3
1.340 - */
1.341 - protected int hashCode(URL u) {
1.342 - int h = 0;
1.343 -
1.344 - // Generate the protocol part.
1.345 - String protocol = u.getProtocol();
1.346 - if (protocol != null)
1.347 - h += protocol.hashCode();
1.348 -
1.349 - // Generate the host part.
1.350 - Object addr = getHostAddress(u);
1.351 - if (addr != null) {
1.352 - h += addr.hashCode();
1.353 - } else {
1.354 - String host = u.getHost();
1.355 - if (host != null)
1.356 - h += host.toLowerCase().hashCode();
1.357 - }
1.358 -
1.359 - // Generate the file part.
1.360 - String file = u.getFile();
1.361 - if (file != null)
1.362 - h += file.hashCode();
1.363 -
1.364 - // Generate the port part.
1.365 - if (u.getPort() == -1)
1.366 - h += getDefaultPort();
1.367 - else
1.368 - h += u.getPort();
1.369 -
1.370 - // Generate the ref part.
1.371 - String ref = u.getRef();
1.372 - if (ref != null)
1.373 - h += ref.hashCode();
1.374 -
1.375 - return h;
1.376 - }
1.377 -
1.378 - /**
1.379 - * Compare two urls to see whether they refer to the same file,
1.380 - * i.e., having the same protocol, host, port, and path.
1.381 - * This method requires that none of its arguments is null. This is
1.382 - * guaranteed by the fact that it is only called indirectly
1.383 - * by java.net.URL class.
1.384 - * @param u1 a URL object
1.385 - * @param u2 a URL object
1.386 - * @return true if u1 and u2 refer to the same file
1.387 - * @since 1.3
1.388 - */
1.389 - protected boolean sameFile(URL u1, URL u2) {
1.390 - // Compare the protocols.
1.391 - if (!((u1.getProtocol() == u2.getProtocol()) ||
1.392 - (u1.getProtocol() != null &&
1.393 - u1.getProtocol().equalsIgnoreCase(u2.getProtocol()))))
1.394 - return false;
1.395 -
1.396 - // Compare the files.
1.397 - if (!(u1.getFile() == u2.getFile() ||
1.398 - (u1.getFile() != null && u1.getFile().equals(u2.getFile()))))
1.399 - return false;
1.400 -
1.401 - // Compare the ports.
1.402 - int port1, port2;
1.403 - port1 = (u1.getPort() != -1) ? u1.getPort() : u1.handler.getDefaultPort();
1.404 - port2 = (u2.getPort() != -1) ? u2.getPort() : u2.handler.getDefaultPort();
1.405 - if (port1 != port2)
1.406 - return false;
1.407 -
1.408 - // Compare the hosts.
1.409 - if (!hostsEqual(u1, u2))
1.410 - return false;
1.411 -
1.412 - return true;
1.413 - }
1.414 -
1.415 - /**
1.416 - * Get the IP address of our host. An empty host field or a DNS failure
1.417 - * will result in a null return.
1.418 - *
1.419 - * @param u a URL object
1.420 - * @return an <code>InetAddress</code> representing the host
1.421 - * IP address.
1.422 - * @since 1.3
1.423 - */
1.424 - private synchronized Object getHostAddress(URL u) {
1.425 - return u.hostAddress;
1.426 - }
1.427 -
1.428 - /**
1.429 - * Compares the host components of two URLs.
1.430 - * @param u1 the URL of the first host to compare
1.431 - * @param u2 the URL of the second host to compare
1.432 - * @return <tt>true</tt> if and only if they
1.433 - * are equal, <tt>false</tt> otherwise.
1.434 - * @since 1.3
1.435 - */
1.436 - protected boolean hostsEqual(URL u1, URL u2) {
1.437 - Object a1 = getHostAddress(u1);
1.438 - Object a2 = getHostAddress(u2);
1.439 - // if we have internet address for both, compare them
1.440 - if (a1 != null && a2 != null) {
1.441 - return a1.equals(a2);
1.442 - // else, if both have host names, compare them
1.443 - } else if (u1.getHost() != null && u2.getHost() != null)
1.444 - return u1.getHost().equalsIgnoreCase(u2.getHost());
1.445 - else
1.446 - return u1.getHost() == null && u2.getHost() == null;
1.447 - }
1.448 -
1.449 - /**
1.450 - * Converts a <code>URL</code> of a specific protocol to a
1.451 - * <code>String</code>.
1.452 - *
1.453 - * @param u the URL.
1.454 - * @return a string representation of the <code>URL</code> argument.
1.455 - */
1.456 - protected String toExternalForm(URL u) {
1.457 -
1.458 - // pre-compute length of StringBuffer
1.459 - int len = u.getProtocol().length() + 1;
1.460 - if (u.getAuthority() != null && u.getAuthority().length() > 0)
1.461 - len += 2 + u.getAuthority().length();
1.462 - if (u.getPath() != null) {
1.463 - len += u.getPath().length();
1.464 - }
1.465 - if (u.getQuery() != null) {
1.466 - len += 1 + u.getQuery().length();
1.467 - }
1.468 - if (u.getRef() != null)
1.469 - len += 1 + u.getRef().length();
1.470 -
1.471 - StringBuffer result = new StringBuffer(len);
1.472 - result.append(u.getProtocol());
1.473 - result.append(":");
1.474 - if (u.getAuthority() != null && u.getAuthority().length() > 0) {
1.475 - result.append("//");
1.476 - result.append(u.getAuthority());
1.477 - }
1.478 - if (u.getPath() != null) {
1.479 - result.append(u.getPath());
1.480 - }
1.481 - if (u.getQuery() != null) {
1.482 - result.append('?');
1.483 - result.append(u.getQuery());
1.484 - }
1.485 - if (u.getRef() != null) {
1.486 - result.append("#");
1.487 - result.append(u.getRef());
1.488 - }
1.489 - return result.toString();
1.490 - }
1.491 -
1.492 - /**
1.493 - * Sets the fields of the <code>URL</code> argument to the indicated values.
1.494 - * Only classes derived from URLStreamHandler are supposed to be able
1.495 - * to call the set method on a URL.
1.496 - *
1.497 - * @param u the URL to modify.
1.498 - * @param protocol the protocol name.
1.499 - * @param host the remote host value for the URL.
1.500 - * @param port the port on the remote machine.
1.501 - * @param authority the authority part for the URL.
1.502 - * @param userInfo the userInfo part of the URL.
1.503 - * @param path the path component of the URL.
1.504 - * @param query the query part for the URL.
1.505 - * @param ref the reference.
1.506 - * @exception SecurityException if the protocol handler of the URL is
1.507 - * different from this one
1.508 - * @see java.net.URL#set(java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)
1.509 - * @since 1.3
1.510 - */
1.511 - protected void setURL(URL u, String protocol, String host, int port,
1.512 - String authority, String userInfo, String path,
1.513 - String query, String ref) {
1.514 - if (this != u.handler) {
1.515 - throw new SecurityException("handler for url different from " +
1.516 - "this handler");
1.517 - }
1.518 - // ensure that no one can reset the protocol on a given URL.
1.519 - u.set(u.getProtocol(), host, port, authority, userInfo, path, query, ref);
1.520 - }
1.521 -
1.522 - /**
1.523 - * Sets the fields of the <code>URL</code> argument to the indicated values.
1.524 - * Only classes derived from URLStreamHandler are supposed to be able
1.525 - * to call the set method on a URL.
1.526 - *
1.527 - * @param u the URL to modify.
1.528 - * @param protocol the protocol name. This value is ignored since 1.2.
1.529 - * @param host the remote host value for the URL.
1.530 - * @param port the port on the remote machine.
1.531 - * @param file the file.
1.532 - * @param ref the reference.
1.533 - * @exception SecurityException if the protocol handler of the URL is
1.534 - * different from this one
1.535 - * @deprecated Use setURL(URL, String, String, int, String, String, String,
1.536 - * String);
1.537 - */
1.538 - @Deprecated
1.539 - protected void setURL(URL u, String protocol, String host, int port,
1.540 - String file, String ref) {
1.541 - /*
1.542 - * Only old URL handlers call this, so assume that the host
1.543 - * field might contain "user:passwd@host". Fix as necessary.
1.544 - */
1.545 - String authority = null;
1.546 - String userInfo = null;
1.547 - if (host != null && host.length() != 0) {
1.548 - authority = (port == -1) ? host : host + ":" + port;
1.549 - int at = host.lastIndexOf('@');
1.550 - if (at != -1) {
1.551 - userInfo = host.substring(0, at);
1.552 - host = host.substring(at+1);
1.553 - }
1.554 - }
1.555 -
1.556 - /*
1.557 - * Assume file might contain query part. Fix as necessary.
1.558 - */
1.559 - String path = null;
1.560 - String query = null;
1.561 - if (file != null) {
1.562 - int q = file.lastIndexOf('?');
1.563 - if (q != -1) {
1.564 - query = file.substring(q+1);
1.565 - path = file.substring(0, q);
1.566 - } else
1.567 - path = file;
1.568 - }
1.569 - setURL(u, protocol, host, port, authority, userInfo, path, query, ref);
1.570 - }
1.571 -}