jaroslav@327: /*
jaroslav@327: * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
jaroslav@327: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@327: *
jaroslav@327: * This code is free software; you can redistribute it and/or modify it
jaroslav@327: * under the terms of the GNU General Public License version 2 only, as
jaroslav@327: * published by the Free Software Foundation. Oracle designates this
jaroslav@327: * particular file as subject to the "Classpath" exception as provided
jaroslav@327: * by Oracle in the LICENSE file that accompanied this code.
jaroslav@327: *
jaroslav@327: * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@327: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@327: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jaroslav@327: * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@327: * accompanied this code).
jaroslav@327: *
jaroslav@327: * You should have received a copy of the GNU General Public License version
jaroslav@327: * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@327: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@327: *
jaroslav@327: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@327: * or visit www.oracle.com if you need additional information or have any
jaroslav@327: * questions.
jaroslav@327: */
jaroslav@327:
jaroslav@327: package java.net;
jaroslav@327:
jaroslav@1398: import java.io.IOException;
jaroslav@1398:
jaroslav@327:
jaroslav@327: /**
jaroslav@327: * The abstract class URLStreamHandler
is the common
jaroslav@327: * superclass for all stream protocol handlers. A stream protocol
jaroslav@327: * handler knows how to make a connection for a particular protocol
jaroslav@327: * type, such as http
, ftp
, or
jaroslav@327: * gopher
.
jaroslav@327: *
jaroslav@327: * In most cases, an instance of a URLStreamHandler
jaroslav@327: * subclass is not created directly by an application. Rather, the
jaroslav@327: * first time a protocol name is encountered when constructing a
jaroslav@327: * URL
, the appropriate stream protocol handler is
jaroslav@327: * automatically loaded.
jaroslav@327: *
jaroslav@327: * @author James Gosling
jaroslav@327: * @see java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
jaroslav@327: * @since JDK1.0
jaroslav@327: */
jaroslav@327: public abstract class URLStreamHandler {
jaroslav@327: /**
jaroslav@327: * Opens a connection to the object referenced by the
jaroslav@327: * URL
argument.
jaroslav@327: * This method should be overridden by a subclass.
jaroslav@327: *
jaroslav@327: *
If for the handler's protocol (such as HTTP or JAR), there
jaroslav@327: * exists a public, specialized URLConnection subclass belonging
jaroslav@327: * to one of the following packages or one of their subpackages:
jaroslav@327: * java.lang, java.io, java.util, java.net, the connection
jaroslav@327: * returned will be of that subclass. For example, for HTTP an
jaroslav@327: * HttpURLConnection will be returned, and for JAR a
jaroslav@327: * JarURLConnection will be returned.
jaroslav@327: *
jaroslav@327: * @param u the URL that this connects to.
jaroslav@327: * @return a URLConnection
object for the URL
.
jaroslav@327: * @exception IOException if an I/O error occurs while opening the
jaroslav@327: * connection.
jaroslav@327: */
jaroslav@1398: abstract protected URLConnection openConnection(URL u) throws IOException;
jaroslav@327:
jaroslav@327: /**
jaroslav@327: * Same as openConnection(URL), except that the connection will be
jaroslav@327: * made through the specified proxy; Protocol handlers that do not
jaroslav@327: * support proxying will ignore the proxy parameter and make a
jaroslav@327: * normal connection.
jaroslav@327: *
jaroslav@327: * Calling this method preempts the system's default ProxySelector
jaroslav@327: * settings.
jaroslav@327: *
jaroslav@327: * @param u the URL that this connects to.
jaroslav@327: * @param p the proxy through which the connection will be made.
jaroslav@327: * If direct connection is desired, Proxy.NO_PROXY
jaroslav@327: * should be specified.
jaroslav@327: * @return a URLConnection
object for the URL
.
jaroslav@327: * @exception IOException if an I/O error occurs while opening the
jaroslav@327: * connection.
jaroslav@327: * @exception IllegalArgumentException if either u or p is null,
jaroslav@327: * or p has the wrong type.
jaroslav@327: * @exception UnsupportedOperationException if the subclass that
jaroslav@327: * implements the protocol doesn't support this method.
jaroslav@327: * @since 1.5
jaroslav@327: */
jaroslav@339: // protected URLConnection openConnection(URL u, Proxy p) throws IOException {
jaroslav@339: // throw new UnsupportedOperationException("Method not implemented.");
jaroslav@339: // }
jaroslav@327:
jaroslav@327: /**
jaroslav@327: * Parses the string representation of a URL
into a
jaroslav@327: * URL
object.
jaroslav@327: *
jaroslav@327: * If there is any inherited context, then it has already been
jaroslav@327: * copied into the URL
argument.
jaroslav@327: *
jaroslav@327: * The parseURL
method of URLStreamHandler
jaroslav@327: * parses the string representation as if it were an
jaroslav@327: * http
specification. Most URL protocol families have a
jaroslav@327: * similar parsing. A stream protocol handler for a protocol that has
jaroslav@327: * a different syntax must override this routine.
jaroslav@327: *
jaroslav@327: * @param u the URL
to receive the result of parsing
jaroslav@327: * the spec.
jaroslav@327: * @param spec the String
representing the URL that
jaroslav@327: * must be parsed.
jaroslav@327: * @param start the character index at which to begin parsing. This is
jaroslav@327: * just past the ':
' (if there is one) that
jaroslav@327: * specifies the determination of the protocol name.
jaroslav@327: * @param limit the character position to stop parsing at. This is the
jaroslav@327: * end of the string or the position of the
jaroslav@327: * "#
" character, if present. All information
jaroslav@327: * after the sharp sign indicates an anchor.
jaroslav@327: */
jaroslav@327: protected void parseURL(URL u, String spec, int start, int limit) {
jaroslav@327: // These fields may receive context content if this was relative URL
jaroslav@327: String protocol = u.getProtocol();
jaroslav@327: String authority = u.getAuthority();
jaroslav@327: String userInfo = u.getUserInfo();
jaroslav@327: String host = u.getHost();
jaroslav@327: int port = u.getPort();
jaroslav@327: String path = u.getPath();
jaroslav@327: String query = u.getQuery();
jaroslav@327:
jaroslav@327: // This field has already been parsed
jaroslav@327: String ref = u.getRef();
jaroslav@327:
jaroslav@327: boolean isRelPath = false;
jaroslav@327: boolean queryOnly = false;
jaroslav@327:
jaroslav@327: // FIX: should not assume query if opaque
jaroslav@327: // Strip off the query part
jaroslav@327: if (start < limit) {
jaroslav@327: int queryStart = spec.indexOf('?');
jaroslav@327: queryOnly = queryStart == start;
jaroslav@327: if ((queryStart != -1) && (queryStart < limit)) {
jaroslav@327: query = spec.substring(queryStart+1, limit);
jaroslav@327: if (limit > queryStart)
jaroslav@327: limit = queryStart;
jaroslav@327: spec = spec.substring(0, queryStart);
jaroslav@327: }
jaroslav@327: }
jaroslav@327:
jaroslav@327: int i = 0;
jaroslav@327: // Parse the authority part if any
jaroslav@327: boolean isUNCName = (start <= limit - 4) &&
jaroslav@327: (spec.charAt(start) == '/') &&
jaroslav@327: (spec.charAt(start + 1) == '/') &&
jaroslav@327: (spec.charAt(start + 2) == '/') &&
jaroslav@327: (spec.charAt(start + 3) == '/');
jaroslav@327: if (!isUNCName && (start <= limit - 2) && (spec.charAt(start) == '/') &&
jaroslav@327: (spec.charAt(start + 1) == '/')) {
jaroslav@327: start += 2;
jaroslav@327: i = spec.indexOf('/', start);
jaroslav@327: if (i < 0) {
jaroslav@327: i = spec.indexOf('?', start);
jaroslav@327: if (i < 0)
jaroslav@327: i = limit;
jaroslav@327: }
jaroslav@327:
jaroslav@327: host = authority = spec.substring(start, i);
jaroslav@327:
jaroslav@327: int ind = authority.indexOf('@');
jaroslav@327: if (ind != -1) {
jaroslav@327: userInfo = authority.substring(0, ind);
jaroslav@327: host = authority.substring(ind+1);
jaroslav@327: } else {
jaroslav@327: userInfo = null;
jaroslav@327: }
jaroslav@327: if (host != null) {
jaroslav@327: // If the host is surrounded by [ and ] then its an IPv6
jaroslav@327: // literal address as specified in RFC2732
jaroslav@327: if (host.length()>0 && (host.charAt(0) == '[')) {
jaroslav@327: if ((ind = host.indexOf(']')) > 2) {
jaroslav@327:
jaroslav@327: String nhost = host ;
jaroslav@327: host = nhost.substring(0,ind+1);
jaroslav@339: // if (!IPAddressUtil.
jaroslav@339: // isIPv6LiteralAddress(host.substring(1, ind))) {
jaroslav@339: // throw new IllegalArgumentException(
jaroslav@339: // "Invalid host: "+ host);
jaroslav@339: // }
jaroslav@327:
jaroslav@327: port = -1 ;
jaroslav@327: if (nhost.length() > ind+1) {
jaroslav@327: if (nhost.charAt(ind+1) == ':') {
jaroslav@327: ++ind ;
jaroslav@327: // port can be null according to RFC2396
jaroslav@327: if (nhost.length() > (ind + 1)) {
jaroslav@327: port = Integer.parseInt(nhost.substring(ind+1));
jaroslav@327: }
jaroslav@327: } else {
jaroslav@327: throw new IllegalArgumentException(
jaroslav@327: "Invalid authority field: " + authority);
jaroslav@327: }
jaroslav@327: }
jaroslav@327: } else {
jaroslav@327: throw new IllegalArgumentException(
jaroslav@327: "Invalid authority field: " + authority);
jaroslav@327: }
jaroslav@327: } else {
jaroslav@327: ind = host.indexOf(':');
jaroslav@327: port = -1;
jaroslav@327: if (ind >= 0) {
jaroslav@327: // port can be null according to RFC2396
jaroslav@327: if (host.length() > (ind + 1)) {
jaroslav@327: port = Integer.parseInt(host.substring(ind + 1));
jaroslav@327: }
jaroslav@327: host = host.substring(0, ind);
jaroslav@327: }
jaroslav@327: }
jaroslav@327: } else {
jaroslav@327: host = "";
jaroslav@327: }
jaroslav@327: if (port < -1)
jaroslav@327: throw new IllegalArgumentException("Invalid port number :" +
jaroslav@327: port);
jaroslav@327: start = i;
jaroslav@327: // If the authority is defined then the path is defined by the
jaroslav@327: // spec only; See RFC 2396 Section 5.2.4.
jaroslav@327: if (authority != null && authority.length() > 0)
jaroslav@327: path = "";
jaroslav@327: }
jaroslav@327:
jaroslav@327: if (host == null) {
jaroslav@327: host = "";
jaroslav@327: }
jaroslav@327:
jaroslav@327: // Parse the file path if any
jaroslav@327: if (start < limit) {
jaroslav@327: if (spec.charAt(start) == '/') {
jaroslav@327: path = spec.substring(start, limit);
jaroslav@327: } else if (path != null && path.length() > 0) {
jaroslav@327: isRelPath = true;
jaroslav@327: int ind = path.lastIndexOf('/');
jaroslav@327: String seperator = "";
jaroslav@327: if (ind == -1 && authority != null)
jaroslav@327: seperator = "/";
jaroslav@327: path = path.substring(0, ind + 1) + seperator +
jaroslav@327: spec.substring(start, limit);
jaroslav@327:
jaroslav@327: } else {
jaroslav@327: String seperator = (authority != null) ? "/" : "";
jaroslav@327: path = seperator + spec.substring(start, limit);
jaroslav@327: }
jaroslav@327: } else if (queryOnly && path != null) {
jaroslav@327: int ind = path.lastIndexOf('/');
jaroslav@327: if (ind < 0)
jaroslav@327: ind = 0;
jaroslav@327: path = path.substring(0, ind) + "/";
jaroslav@327: }
jaroslav@327: if (path == null)
jaroslav@327: path = "";
jaroslav@327:
jaroslav@327: if (isRelPath) {
jaroslav@327: // Remove embedded /./
jaroslav@327: while ((i = path.indexOf("/./")) >= 0) {
jaroslav@327: path = path.substring(0, i) + path.substring(i + 2);
jaroslav@327: }
jaroslav@327: // Remove embedded /../ if possible
jaroslav@327: i = 0;
jaroslav@327: while ((i = path.indexOf("/../", i)) >= 0) {
jaroslav@327: /*
jaroslav@327: * A "/../" will cancel the previous segment and itself,
jaroslav@327: * unless that segment is a "/../" itself
jaroslav@327: * i.e. "/a/b/../c" becomes "/a/c"
jaroslav@327: * but "/../../a" should stay unchanged
jaroslav@327: */
jaroslav@327: if (i > 0 && (limit = path.lastIndexOf('/', i - 1)) >= 0 &&
jaroslav@327: (path.indexOf("/../", limit) != 0)) {
jaroslav@327: path = path.substring(0, limit) + path.substring(i + 3);
jaroslav@327: i = 0;
jaroslav@327: } else {
jaroslav@327: i = i + 3;
jaroslav@327: }
jaroslav@327: }
jaroslav@327: // Remove trailing .. if possible
jaroslav@327: while (path.endsWith("/..")) {
jaroslav@327: i = path.indexOf("/..");
jaroslav@327: if ((limit = path.lastIndexOf('/', i - 1)) >= 0) {
jaroslav@327: path = path.substring(0, limit+1);
jaroslav@327: } else {
jaroslav@327: break;
jaroslav@327: }
jaroslav@327: }
jaroslav@327: // Remove starting .
jaroslav@327: if (path.startsWith("./") && path.length() > 2)
jaroslav@327: path = path.substring(2);
jaroslav@327:
jaroslav@327: // Remove trailing .
jaroslav@327: if (path.endsWith("/."))
jaroslav@327: path = path.substring(0, path.length() -1);
jaroslav@327: }
jaroslav@327:
jaroslav@327: setURL(u, protocol, host, port, authority, userInfo, path, query, ref);
jaroslav@327: }
jaroslav@327:
jaroslav@327: /**
jaroslav@327: * Returns the default port for a URL parsed by this handler. This method
jaroslav@327: * is meant to be overidden by handlers with default port numbers.
jaroslav@327: * @return the default port for a URL
parsed by this handler.
jaroslav@327: * @since 1.3
jaroslav@327: */
jaroslav@327: protected int getDefaultPort() {
jaroslav@327: return -1;
jaroslav@327: }
jaroslav@327:
jaroslav@327: /**
jaroslav@327: * Provides the default equals calculation. May be overidden by handlers
jaroslav@327: * for other protocols that have different requirements for equals().
jaroslav@327: * This method requires that none of its arguments is null. This is
jaroslav@327: * guaranteed by the fact that it is only called by java.net.URL class.
jaroslav@327: * @param u1 a URL object
jaroslav@327: * @param u2 a URL object
jaroslav@327: * @return true if the two urls are
jaroslav@327: * considered equal, ie. they refer to the same
jaroslav@327: * fragment in the same file.
jaroslav@327: * @since 1.3
jaroslav@327: */
jaroslav@327: protected boolean equals(URL u1, URL u2) {
jaroslav@327: String ref1 = u1.getRef();
jaroslav@327: String ref2 = u2.getRef();
jaroslav@327: return (ref1 == ref2 || (ref1 != null && ref1.equals(ref2))) &&
jaroslav@327: sameFile(u1, u2);
jaroslav@327: }
jaroslav@327:
jaroslav@327: /**
jaroslav@327: * Provides the default hash calculation. May be overidden by handlers for
jaroslav@327: * other protocols that have different requirements for hashCode
jaroslav@327: * calculation.
jaroslav@327: * @param u a URL object
jaroslav@327: * @return an int suitable for hash table indexing
jaroslav@327: * @since 1.3
jaroslav@327: */
jaroslav@327: protected int hashCode(URL u) {
jaroslav@327: int h = 0;
jaroslav@327:
jaroslav@327: // Generate the protocol part.
jaroslav@327: String protocol = u.getProtocol();
jaroslav@327: if (protocol != null)
jaroslav@327: h += protocol.hashCode();
jaroslav@327:
jaroslav@327: // Generate the host part.
jaroslav@339: Object addr = getHostAddress(u);
jaroslav@327: if (addr != null) {
jaroslav@327: h += addr.hashCode();
jaroslav@327: } else {
jaroslav@327: String host = u.getHost();
jaroslav@327: if (host != null)
jaroslav@327: h += host.toLowerCase().hashCode();
jaroslav@327: }
jaroslav@327:
jaroslav@327: // Generate the file part.
jaroslav@327: String file = u.getFile();
jaroslav@327: if (file != null)
jaroslav@327: h += file.hashCode();
jaroslav@327:
jaroslav@327: // Generate the port part.
jaroslav@327: if (u.getPort() == -1)
jaroslav@327: h += getDefaultPort();
jaroslav@327: else
jaroslav@327: h += u.getPort();
jaroslav@327:
jaroslav@327: // Generate the ref part.
jaroslav@327: String ref = u.getRef();
jaroslav@327: if (ref != null)
jaroslav@327: h += ref.hashCode();
jaroslav@327:
jaroslav@327: return h;
jaroslav@327: }
jaroslav@327:
jaroslav@327: /**
jaroslav@327: * Compare two urls to see whether they refer to the same file,
jaroslav@327: * i.e., having the same protocol, host, port, and path.
jaroslav@327: * This method requires that none of its arguments is null. This is
jaroslav@327: * guaranteed by the fact that it is only called indirectly
jaroslav@327: * by java.net.URL class.
jaroslav@327: * @param u1 a URL object
jaroslav@327: * @param u2 a URL object
jaroslav@327: * @return true if u1 and u2 refer to the same file
jaroslav@327: * @since 1.3
jaroslav@327: */
jaroslav@327: protected boolean sameFile(URL u1, URL u2) {
jaroslav@327: // Compare the protocols.
jaroslav@327: if (!((u1.getProtocol() == u2.getProtocol()) ||
jaroslav@327: (u1.getProtocol() != null &&
jaroslav@327: u1.getProtocol().equalsIgnoreCase(u2.getProtocol()))))
jaroslav@327: return false;
jaroslav@327:
jaroslav@327: // Compare the files.
jaroslav@327: if (!(u1.getFile() == u2.getFile() ||
jaroslav@327: (u1.getFile() != null && u1.getFile().equals(u2.getFile()))))
jaroslav@327: return false;
jaroslav@327:
jaroslav@327: // Compare the ports.
jaroslav@327: int port1, port2;
jaroslav@327: port1 = (u1.getPort() != -1) ? u1.getPort() : u1.handler.getDefaultPort();
jaroslav@327: port2 = (u2.getPort() != -1) ? u2.getPort() : u2.handler.getDefaultPort();
jaroslav@327: if (port1 != port2)
jaroslav@327: return false;
jaroslav@327:
jaroslav@327: // Compare the hosts.
jaroslav@327: if (!hostsEqual(u1, u2))
jaroslav@327: return false;
jaroslav@327:
jaroslav@327: return true;
jaroslav@327: }
jaroslav@327:
jaroslav@327: /**
jaroslav@327: * Get the IP address of our host. An empty host field or a DNS failure
jaroslav@327: * will result in a null return.
jaroslav@327: *
jaroslav@327: * @param u a URL object
jaroslav@327: * @return an InetAddress
representing the host
jaroslav@327: * IP address.
jaroslav@327: * @since 1.3
jaroslav@327: */
jaroslav@339: private synchronized Object getHostAddress(URL u) {
jaroslav@327: return u.hostAddress;
jaroslav@327: }
jaroslav@327:
jaroslav@327: /**
jaroslav@327: * Compares the host components of two URLs.
jaroslav@327: * @param u1 the URL of the first host to compare
jaroslav@327: * @param u2 the URL of the second host to compare
jaroslav@327: * @return true if and only if they
jaroslav@327: * are equal, false otherwise.
jaroslav@327: * @since 1.3
jaroslav@327: */
jaroslav@327: protected boolean hostsEqual(URL u1, URL u2) {
jaroslav@339: Object a1 = getHostAddress(u1);
jaroslav@339: Object a2 = getHostAddress(u2);
jaroslav@327: // if we have internet address for both, compare them
jaroslav@327: if (a1 != null && a2 != null) {
jaroslav@327: return a1.equals(a2);
jaroslav@327: // else, if both have host names, compare them
jaroslav@327: } else if (u1.getHost() != null && u2.getHost() != null)
jaroslav@327: return u1.getHost().equalsIgnoreCase(u2.getHost());
jaroslav@327: else
jaroslav@327: return u1.getHost() == null && u2.getHost() == null;
jaroslav@327: }
jaroslav@327:
jaroslav@327: /**
jaroslav@327: * Converts a URL
of a specific protocol to a
jaroslav@327: * String
.
jaroslav@327: *
jaroslav@327: * @param u the URL.
jaroslav@327: * @return a string representation of the URL
argument.
jaroslav@327: */
jaroslav@327: protected String toExternalForm(URL u) {
jaroslav@327:
jaroslav@327: // pre-compute length of StringBuffer
jaroslav@327: int len = u.getProtocol().length() + 1;
jaroslav@327: if (u.getAuthority() != null && u.getAuthority().length() > 0)
jaroslav@327: len += 2 + u.getAuthority().length();
jaroslav@327: if (u.getPath() != null) {
jaroslav@327: len += u.getPath().length();
jaroslav@327: }
jaroslav@327: if (u.getQuery() != null) {
jaroslav@327: len += 1 + u.getQuery().length();
jaroslav@327: }
jaroslav@327: if (u.getRef() != null)
jaroslav@327: len += 1 + u.getRef().length();
jaroslav@327:
jaroslav@327: StringBuffer result = new StringBuffer(len);
jaroslav@327: result.append(u.getProtocol());
jaroslav@327: result.append(":");
jaroslav@327: if (u.getAuthority() != null && u.getAuthority().length() > 0) {
jaroslav@327: result.append("//");
jaroslav@327: result.append(u.getAuthority());
jaroslav@327: }
jaroslav@327: if (u.getPath() != null) {
jaroslav@327: result.append(u.getPath());
jaroslav@327: }
jaroslav@327: if (u.getQuery() != null) {
jaroslav@327: result.append('?');
jaroslav@327: result.append(u.getQuery());
jaroslav@327: }
jaroslav@327: if (u.getRef() != null) {
jaroslav@327: result.append("#");
jaroslav@327: result.append(u.getRef());
jaroslav@327: }
jaroslav@327: return result.toString();
jaroslav@327: }
jaroslav@327:
jaroslav@327: /**
jaroslav@327: * Sets the fields of the URL
argument to the indicated values.
jaroslav@327: * Only classes derived from URLStreamHandler are supposed to be able
jaroslav@327: * to call the set method on a URL.
jaroslav@327: *
jaroslav@327: * @param u the URL to modify.
jaroslav@327: * @param protocol the protocol name.
jaroslav@327: * @param host the remote host value for the URL.
jaroslav@327: * @param port the port on the remote machine.
jaroslav@327: * @param authority the authority part for the URL.
jaroslav@327: * @param userInfo the userInfo part of the URL.
jaroslav@327: * @param path the path component of the URL.
jaroslav@327: * @param query the query part for the URL.
jaroslav@327: * @param ref the reference.
jaroslav@327: * @exception SecurityException if the protocol handler of the URL is
jaroslav@327: * different from this one
jaroslav@327: * @see java.net.URL#set(java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)
jaroslav@327: * @since 1.3
jaroslav@327: */
jaroslav@327: protected void setURL(URL u, String protocol, String host, int port,
jaroslav@327: String authority, String userInfo, String path,
jaroslav@327: String query, String ref) {
jaroslav@327: if (this != u.handler) {
jaroslav@327: throw new SecurityException("handler for url different from " +
jaroslav@327: "this handler");
jaroslav@327: }
jaroslav@327: // ensure that no one can reset the protocol on a given URL.
jaroslav@327: u.set(u.getProtocol(), host, port, authority, userInfo, path, query, ref);
jaroslav@327: }
jaroslav@327:
jaroslav@327: /**
jaroslav@327: * Sets the fields of the URL
argument to the indicated values.
jaroslav@327: * Only classes derived from URLStreamHandler are supposed to be able
jaroslav@327: * to call the set method on a URL.
jaroslav@327: *
jaroslav@327: * @param u the URL to modify.
jaroslav@327: * @param protocol the protocol name. This value is ignored since 1.2.
jaroslav@327: * @param host the remote host value for the URL.
jaroslav@327: * @param port the port on the remote machine.
jaroslav@327: * @param file the file.
jaroslav@327: * @param ref the reference.
jaroslav@327: * @exception SecurityException if the protocol handler of the URL is
jaroslav@327: * different from this one
jaroslav@327: * @deprecated Use setURL(URL, String, String, int, String, String, String,
jaroslav@327: * String);
jaroslav@327: */
jaroslav@327: @Deprecated
jaroslav@327: protected void setURL(URL u, String protocol, String host, int port,
jaroslav@327: String file, String ref) {
jaroslav@327: /*
jaroslav@327: * Only old URL handlers call this, so assume that the host
jaroslav@327: * field might contain "user:passwd@host". Fix as necessary.
jaroslav@327: */
jaroslav@327: String authority = null;
jaroslav@327: String userInfo = null;
jaroslav@327: if (host != null && host.length() != 0) {
jaroslav@327: authority = (port == -1) ? host : host + ":" + port;
jaroslav@327: int at = host.lastIndexOf('@');
jaroslav@327: if (at != -1) {
jaroslav@327: userInfo = host.substring(0, at);
jaroslav@327: host = host.substring(at+1);
jaroslav@327: }
jaroslav@327: }
jaroslav@327:
jaroslav@327: /*
jaroslav@327: * Assume file might contain query part. Fix as necessary.
jaroslav@327: */
jaroslav@327: String path = null;
jaroslav@327: String query = null;
jaroslav@327: if (file != null) {
jaroslav@327: int q = file.lastIndexOf('?');
jaroslav@327: if (q != -1) {
jaroslav@327: query = file.substring(q+1);
jaroslav@327: path = file.substring(0, q);
jaroslav@327: } else
jaroslav@327: path = file;
jaroslav@327: }
jaroslav@327: setURL(u, protocol, host, port, authority, userInfo, path, query, ref);
jaroslav@327: }
jaroslav@327: }