jtulach@120: /* jtulach@120: * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. jtulach@120: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jtulach@120: * jtulach@120: * This code is free software; you can redistribute it and/or modify it jtulach@120: * under the terms of the GNU General Public License version 2 only, as jtulach@120: * published by the Free Software Foundation. Oracle designates this jtulach@120: * particular file as subject to the "Classpath" exception as provided jtulach@120: * by Oracle in the LICENSE file that accompanied this code. jtulach@120: * jtulach@120: * This code is distributed in the hope that it will be useful, but WITHOUT jtulach@120: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jtulach@120: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jtulach@120: * version 2 for more details (a copy is included in the LICENSE file that jtulach@120: * accompanied this code). jtulach@120: * jtulach@120: * You should have received a copy of the GNU General Public License version jtulach@120: * 2 along with this work; if not, write to the Free Software Foundation, jtulach@120: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jtulach@120: * jtulach@120: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jtulach@120: * or visit www.oracle.com if you need additional information or have any jtulach@120: * questions. jtulach@120: */ jtulach@120: jtulach@120: package java.net; jtulach@120: jtulach@120: import java.io.IOException; jtulach@120: import java.io.InputStream; jtulach@120: jtulach@120: /** jtulach@120: * Class URL represents a Uniform Resource jtulach@120: * Locator, a pointer to a "resource" on the World jtulach@120: * Wide Web. A resource can be something as simple as a file or a jtulach@120: * directory, or it can be a reference to a more complicated object, jtulach@120: * such as a query to a database or to a search engine. More jtulach@120: * information on the types of URLs and their formats can be found at: jtulach@120: *
jtulach@120: * jtulach@120: * http://www.socs.uts.edu.au/MosaicDocs-old/url-primer.html jtulach@120: *
jtulach@120: *

jtulach@120: * In general, a URL can be broken into several parts. The previous jtulach@120: * example of a URL indicates that the protocol to use is jtulach@120: * http (HyperText Transfer Protocol) and that the jtulach@120: * information resides on a host machine named jtulach@120: * www.socs.uts.edu.au. The information on that host jtulach@120: * machine is named /MosaicDocs-old/url-primer.html. The exact jtulach@120: * meaning of this name on the host machine is both protocol jtulach@120: * dependent and host dependent. The information normally resides in jtulach@120: * a file, but it could be generated on the fly. This component of jtulach@120: * the URL is called the path component. jtulach@120: *

jtulach@120: * A URL can optionally specify a "port", which is the jtulach@120: * port number to which the TCP connection is made on the remote host jtulach@120: * machine. If the port is not specified, the default port for jtulach@120: * the protocol is used instead. For example, the default port for jtulach@120: * http is 80. An alternative port could be jtulach@120: * specified as: jtulach@120: *

jtulach@120:  *     http://www.socs.uts.edu.au:80/MosaicDocs-old/url-primer.html
jtulach@120:  * 
jtulach@120: *

jtulach@120: * The syntax of URL is defined by RFC 2396: Uniform jtulach@120: * Resource Identifiers (URI): Generic Syntax, amended by RFC 2732: Format for jtulach@120: * Literal IPv6 Addresses in URLs. The Literal IPv6 address format jtulach@120: * also supports scope_ids. The syntax and usage of scope_ids is described jtulach@120: * here. jtulach@120: *

jtulach@120: * A URL may have appended to it a "fragment", also known jtulach@120: * as a "ref" or a "reference". The fragment is indicated by the sharp jtulach@120: * sign character "#" followed by more characters. For example, jtulach@120: *

jtulach@120:  *     http://java.sun.com/index.html#chapter1
jtulach@120:  * 
jtulach@120: *

jtulach@120: * This fragment is not technically part of the URL. Rather, it jtulach@120: * indicates that after the specified resource is retrieved, the jtulach@120: * application is specifically interested in that part of the jtulach@120: * document that has the tag chapter1 attached to it. The jtulach@120: * meaning of a tag is resource specific. jtulach@120: *

jtulach@120: * An application can also specify a "relative URL", jtulach@120: * which contains only enough information to reach the resource jtulach@120: * relative to another URL. Relative URLs are frequently used within jtulach@120: * HTML pages. For example, if the contents of the URL: jtulach@120: *

jtulach@120:  *     http://java.sun.com/index.html
jtulach@120:  * 
jtulach@120: * contained within it the relative URL: jtulach@120: *
jtulach@120:  *     FAQ.html
jtulach@120:  * 
jtulach@120: * it would be a shorthand for: jtulach@120: *
jtulach@120:  *     http://java.sun.com/FAQ.html
jtulach@120:  * 
jtulach@120: *

jtulach@120: * The relative URL need not specify all the components of a URL. If jtulach@120: * the protocol, host name, or port number is missing, the value is jtulach@120: * inherited from the fully specified URL. The file component must be jtulach@120: * specified. The optional fragment is not inherited. jtulach@120: *

jtulach@120: * The URL class does not itself encode or decode any URL components jtulach@120: * according to the escaping mechanism defined in RFC2396. It is the jtulach@120: * responsibility of the caller to encode any fields, which need to be jtulach@120: * escaped prior to calling URL, and also to decode any escaped fields, jtulach@120: * that are returned from URL. Furthermore, because URL has no knowledge jtulach@120: * of URL escaping, it does not recognise equivalence between the encoded jtulach@120: * or decoded form of the same URL. For example, the two URLs:
jtulach@120: *

    http://foo.com/hello world/ and http://foo.com/hello%20world
jtulach@120: * would be considered not equal to each other. jtulach@120: *

jtulach@120: * Note, the {@link java.net.URI} class does perform escaping of its jtulach@120: * component fields in certain circumstances. The recommended way jtulach@120: * to manage the encoding and decoding of URLs is to use {@link java.net.URI}, jtulach@120: * and to convert between these two classes using {@link #toURI()} and jtulach@120: * {@link URI#toURL()}. jtulach@120: *

jtulach@120: * The {@link URLEncoder} and {@link URLDecoder} classes can also be jtulach@120: * used, but only for HTML form encoding, which is not the same jtulach@120: * as the encoding scheme defined in RFC2396. jtulach@120: * jtulach@120: * @author James Gosling jtulach@120: * @since JDK1.0 jtulach@120: */ jtulach@120: public final class URL implements java.io.Serializable { jtulach@120: jtulach@120: static final long serialVersionUID = -7627629688361524110L; jtulach@120: jtulach@120: /** jtulach@120: * The property which specifies the package prefix list to be scanned jtulach@120: * for protocol handlers. The value of this property (if any) should jtulach@120: * be a vertical bar delimited list of package names to search through jtulach@120: * for a protocol handler to load. The policy of this class is that jtulach@120: * all protocol handlers will be in a class called .Handler, jtulach@120: * and each package in the list is examined in turn for a matching jtulach@120: * handler. If none are found (or the property is not specified), the jtulach@120: * default package prefix, sun.net.www.protocol, is used. The search jtulach@120: * proceeds from the first package in the list to the last and stops jtulach@120: * when a match is found. jtulach@120: */ jtulach@120: private static final String protocolPathProp = "java.protocol.handler.pkgs"; jtulach@120: jtulach@120: /** jtulach@120: * The protocol to use (ftp, http, nntp, ... etc.) . jtulach@120: * @serial jtulach@120: */ jtulach@120: private String protocol; jtulach@120: jtulach@120: /** jtulach@120: * The host name to connect to. jtulach@120: * @serial jtulach@120: */ jtulach@120: private String host; jtulach@120: jtulach@120: /** jtulach@120: * The protocol port to connect to. jtulach@120: * @serial jtulach@120: */ jtulach@120: private int port = -1; jtulach@120: jtulach@120: /** jtulach@120: * The specified file name on that host. file is jtulach@120: * defined as path[?query] jtulach@120: * @serial jtulach@120: */ jtulach@120: private String file; jtulach@120: jtulach@120: /** jtulach@120: * The query part of this URL. jtulach@120: */ jtulach@120: private transient String query; jtulach@120: jtulach@120: /** jtulach@120: * The authority part of this URL. jtulach@120: * @serial jtulach@120: */ jtulach@120: private String authority; jtulach@120: jtulach@120: /** jtulach@120: * The path part of this URL. jtulach@120: */ jtulach@120: private transient String path; jtulach@120: jtulach@120: /** jtulach@120: * The userinfo part of this URL. jtulach@120: */ jtulach@120: private transient String userInfo; jtulach@120: jtulach@120: /** jtulach@120: * # reference. jtulach@120: * @serial jtulach@120: */ jtulach@120: private String ref; jtulach@120: jaroslav@339: /** jaroslav@339: * The host's IP address, used in equals and hashCode. jaroslav@339: * Computed on demand. An uninitialized or unknown hostAddress is null. jaroslav@339: */ jaroslav@339: transient Object hostAddress; jaroslav@339: jaroslav@339: /** jaroslav@339: * The URLStreamHandler for this URL. jaroslav@339: */ jaroslav@339: transient URLStreamHandler handler; jaroslav@339: jtulach@120: /* Our hash code. jtulach@120: * @serial jtulach@120: */ jtulach@120: private int hashCode = -1; jtulach@120: jtulach@120: /** jtulach@120: * Creates a URL object from the specified jtulach@120: * protocol, host, port jtulach@120: * number, and file.

jtulach@120: * jtulach@120: * host can be expressed as a host name or a literal jtulach@120: * IP address. If IPv6 literal address is used, it should be jtulach@120: * enclosed in square brackets ('[' and ']'), as jtulach@120: * specified by RFC 2732; jtulach@120: * However, the literal IPv6 address format defined in RFC 2373: IP jtulach@120: * Version 6 Addressing Architecture is also accepted.

jtulach@120: * jtulach@120: * Specifying a port number of -1 jtulach@120: * indicates that the URL should use the default port for the jtulach@120: * protocol.

jtulach@120: * jtulach@120: * If this is the first URL object being created with the specified jtulach@120: * protocol, a stream protocol handler object, an instance of jtulach@120: * class URLStreamHandler, is created for that protocol: jtulach@120: *

    jtulach@120: *
  1. If the application has previously set up an instance of jtulach@120: * URLStreamHandlerFactory as the stream handler factory, jtulach@120: * then the createURLStreamHandler method of that instance jtulach@120: * is called with the protocol string as an argument to create the jtulach@120: * stream protocol handler. jtulach@120: *
  2. If no URLStreamHandlerFactory has yet been set up, jtulach@120: * or if the factory's createURLStreamHandler method jtulach@120: * returns null, then the constructor finds the jtulach@120: * value of the system property: jtulach@120: *
    jtulach@120:      *         java.protocol.handler.pkgs
    jtulach@120:      *     
    jtulach@120: * If the value of that system property is not null, jtulach@120: * it is interpreted as a list of packages separated by a vertical jtulach@120: * slash character '|'. The constructor tries to load jtulach@120: * the class named: jtulach@120: *
    jtulach@120:      *         <package>.<protocol>.Handler
    jtulach@120:      *     
    jtulach@120: * where <package> is replaced by the name of the package jtulach@120: * and <protocol> is replaced by the name of the protocol. jtulach@120: * If this class does not exist, or if the class exists but it is not jtulach@120: * a subclass of URLStreamHandler, then the next package jtulach@120: * in the list is tried. jtulach@120: *
  3. If the previous step fails to find a protocol handler, then the jtulach@120: * constructor tries to load from a system default package. jtulach@120: *
    jtulach@120:      *         <system default package>.<protocol>.Handler
    jtulach@120:      *     
    jtulach@120: * If this class does not exist, or if the class exists but it is not a jtulach@120: * subclass of URLStreamHandler, then a jtulach@120: * MalformedURLException is thrown. jtulach@120: *
jtulach@120: * jtulach@120: *

Protocol handlers for the following protocols are guaranteed jtulach@120: * to exist on the search path :- jtulach@120: *

jtulach@120:      *     http, https, ftp, file, and jar
jtulach@120:      * 
jtulach@120: * Protocol handlers for additional protocols may also be jtulach@120: * available. jtulach@120: * jtulach@120: *

No validation of the inputs is performed by this constructor. jtulach@120: * jtulach@120: * @param protocol the name of the protocol to use. jtulach@120: * @param host the name of the host. jtulach@120: * @param port the port number on the host. jtulach@120: * @param file the file on the host jtulach@120: * @exception MalformedURLException if an unknown protocol is specified. jtulach@120: * @see java.lang.System#getProperty(java.lang.String) jtulach@120: * @see java.net.URL#setURLStreamHandlerFactory( jtulach@120: * java.net.URLStreamHandlerFactory) jtulach@120: * @see java.net.URLStreamHandler jtulach@120: * @see java.net.URLStreamHandlerFactory#createURLStreamHandler( jtulach@120: * java.lang.String) jtulach@120: */ jtulach@120: public URL(String protocol, String host, int port, String file) jtulach@120: throws MalformedURLException jtulach@120: { jtulach@120: this(protocol, host, port, file, null); jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Creates a URL from the specified protocol jtulach@120: * name, host name, and file name. The jtulach@120: * default port for the specified protocol is used. jtulach@120: *

jtulach@120: * This method is equivalent to calling the four-argument jtulach@120: * constructor with the arguments being protocol, jtulach@120: * host, -1, and file. jtulach@120: * jtulach@120: * No validation of the inputs is performed by this constructor. jtulach@120: * jtulach@120: * @param protocol the name of the protocol to use. jtulach@120: * @param host the name of the host. jtulach@120: * @param file the file on the host. jtulach@120: * @exception MalformedURLException if an unknown protocol is specified. jtulach@120: * @see java.net.URL#URL(java.lang.String, java.lang.String, jtulach@120: * int, java.lang.String) jtulach@120: */ jtulach@120: public URL(String protocol, String host, String file) jtulach@120: throws MalformedURLException { jtulach@120: this(protocol, host, -1, file); jtulach@120: } jtulach@120: jaroslav@339: /** jaroslav@339: * Creates a URL object from the specified jaroslav@339: * protocol, host, port jaroslav@339: * number, file, and handler. Specifying jaroslav@339: * a port number of -1 indicates that jaroslav@339: * the URL should use the default port for the protocol. Specifying jaroslav@339: * a handler of null indicates that the URL jaroslav@339: * should use a default stream handler for the protocol, as outlined jaroslav@339: * for: jaroslav@339: * java.net.URL#URL(java.lang.String, java.lang.String, int, jaroslav@339: * java.lang.String) jaroslav@339: * jaroslav@339: *

If the handler is not null and there is a security manager, jaroslav@339: * the security manager's checkPermission jaroslav@339: * method is called with a jaroslav@339: * NetPermission("specifyStreamHandler") permission. jaroslav@339: * This may result in a SecurityException. jaroslav@339: * jaroslav@339: * No validation of the inputs is performed by this constructor. jaroslav@339: * jaroslav@339: * @param protocol the name of the protocol to use. jaroslav@339: * @param host the name of the host. jaroslav@339: * @param port the port number on the host. jaroslav@339: * @param file the file on the host jaroslav@339: * @param handler the stream handler for the URL. jaroslav@339: * @exception MalformedURLException if an unknown protocol is specified. jaroslav@339: * @exception SecurityException jaroslav@339: * if a security manager exists and its jaroslav@339: * checkPermission method doesn't allow jaroslav@339: * specifying a stream handler explicitly. jaroslav@339: * @see java.lang.System#getProperty(java.lang.String) jaroslav@339: * @see java.net.URL#setURLStreamHandlerFactory( jaroslav@339: * java.net.URLStreamHandlerFactory) jaroslav@339: * @see java.net.URLStreamHandler jaroslav@339: * @see java.net.URLStreamHandlerFactory#createURLStreamHandler( jaroslav@339: * java.lang.String) jaroslav@339: * @see SecurityManager#checkPermission jaroslav@339: * @see java.net.NetPermission jaroslav@339: */ jaroslav@339: public URL(String protocol, String host, int port, String file, jaroslav@339: URLStreamHandler handler) throws MalformedURLException { jtulach@120: if (handler != null) { jaroslav@122: throw new SecurityException(); jtulach@120: } jtulach@120: jtulach@120: protocol = protocol.toLowerCase(); jtulach@120: this.protocol = protocol; jtulach@120: if (host != null) { jtulach@120: jtulach@120: /** jtulach@120: * if host is a literal IPv6 address, jtulach@120: * we will make it conform to RFC 2732 jtulach@120: */ jtulach@120: if (host.indexOf(':') >= 0 && !host.startsWith("[")) { jtulach@120: host = "["+host+"]"; jtulach@120: } jtulach@120: this.host = host; jtulach@120: jtulach@120: if (port < -1) { jtulach@120: throw new MalformedURLException("Invalid port number :" + jtulach@120: port); jtulach@120: } jtulach@120: this.port = port; jtulach@120: authority = (port == -1) ? host : host + ":" + port; jtulach@120: } jtulach@120: jtulach@120: Parts parts = new Parts(file); jtulach@120: path = parts.getPath(); jtulach@120: query = parts.getQuery(); jtulach@120: jtulach@120: if (query != null) { jtulach@120: this.file = path + "?" + query; jtulach@120: } else { jtulach@120: this.file = path; jtulach@120: } jtulach@120: ref = parts.getRef(); jtulach@120: jtulach@120: // Note: we don't do validation of the URL here. Too risky to change jtulach@120: // right now, but worth considering for future reference. -br jaroslav@339: if (handler == null && jaroslav@339: (handler = getURLStreamHandler(protocol)) == null) { jaroslav@339: throw new MalformedURLException("unknown protocol: " + protocol); jaroslav@339: } jaroslav@339: this.handler = handler; jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Creates a URL object from the String jtulach@120: * representation. jtulach@120: *

jtulach@120: * This constructor is equivalent to a call to the two-argument jtulach@120: * constructor with a null first argument. jtulach@120: * jtulach@120: * @param spec the String to parse as a URL. jtulach@120: * @exception MalformedURLException if no protocol is specified, or an jtulach@120: * unknown protocol is found, or spec is null. jtulach@120: * @see java.net.URL#URL(java.net.URL, java.lang.String) jtulach@120: */ jtulach@120: public URL(String spec) throws MalformedURLException { jtulach@120: this(null, spec); jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Creates a URL by parsing the given spec within a specified context. jtulach@120: * jtulach@120: * The new URL is created from the given context URL and the spec jtulach@120: * argument as described in jtulach@120: * RFC2396 "Uniform Resource Identifiers : Generic * Syntax" : jtulach@120: *

jtulach@120:      *          <scheme>://<authority><path>?<query>#<fragment>
jtulach@120:      * 
jtulach@120: * The reference is parsed into the scheme, authority, path, query and jtulach@120: * fragment parts. If the path component is empty and the scheme, jtulach@120: * authority, and query components are undefined, then the new URL is a jtulach@120: * reference to the current document. Otherwise, the fragment and query jtulach@120: * parts present in the spec are used in the new URL. jtulach@120: *

jtulach@120: * If the scheme component is defined in the given spec and does not match jtulach@120: * the scheme of the context, then the new URL is created as an absolute jtulach@120: * URL based on the spec alone. Otherwise the scheme component is inherited jtulach@120: * from the context URL. jtulach@120: *

jtulach@120: * If the authority component is present in the spec then the spec is jtulach@120: * treated as absolute and the spec authority and path will replace the jtulach@120: * context authority and path. If the authority component is absent in the jtulach@120: * spec then the authority of the new URL will be inherited from the jtulach@120: * context. jtulach@120: *

jtulach@120: * If the spec's path component begins with a slash character jtulach@120: * "/" then the jtulach@120: * path is treated as absolute and the spec path replaces the context path. jtulach@120: *

jtulach@120: * Otherwise, the path is treated as a relative path and is appended to the jtulach@120: * context path, as described in RFC2396. Also, in this case, jtulach@120: * the path is canonicalized through the removal of directory jtulach@120: * changes made by occurences of ".." and ".". jtulach@120: *

jtulach@120: * For a more detailed description of URL parsing, refer to RFC2396. jtulach@120: * jtulach@120: * @param context the context in which to parse the specification. jtulach@120: * @param spec the String to parse as a URL. jtulach@120: * @exception MalformedURLException if no protocol is specified, or an jtulach@120: * unknown protocol is found, or spec is null. jtulach@120: * @see java.net.URL#URL(java.lang.String, java.lang.String, jtulach@120: * int, java.lang.String) jtulach@120: * @see java.net.URLStreamHandler jtulach@120: * @see java.net.URLStreamHandler#parseURL(java.net.URL, jtulach@120: * java.lang.String, int, int) jtulach@120: */ jtulach@120: public URL(URL context, String spec) throws MalformedURLException { jtulach@120: this(context, spec, null); jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Creates a URL by parsing the given spec with the specified handler jtulach@120: * within a specified context. If the handler is null, the parsing jtulach@120: * occurs as with the two argument constructor. jtulach@120: * jtulach@120: * @param context the context in which to parse the specification. jtulach@120: * @param spec the String to parse as a URL. jtulach@120: * @param handler the stream handler for the URL. jtulach@120: * @exception MalformedURLException if no protocol is specified, or an jtulach@120: * unknown protocol is found, or spec is null. jtulach@120: * @exception SecurityException jtulach@120: * if a security manager exists and its jtulach@120: * checkPermission method doesn't allow jtulach@120: * specifying a stream handler. jtulach@120: * @see java.net.URL#URL(java.lang.String, java.lang.String, jtulach@120: * int, java.lang.String) jtulach@120: * @see java.net.URLStreamHandler jtulach@120: * @see java.net.URLStreamHandler#parseURL(java.net.URL, jtulach@120: * java.lang.String, int, int) jtulach@120: */ jaroslav@339: public URL(URL context, String spec, URLStreamHandler handler) jtulach@120: throws MalformedURLException jtulach@120: { jtulach@120: String original = spec; jtulach@120: int i, limit, c; jtulach@120: int start = 0; jtulach@120: String newProtocol = null; jtulach@120: boolean aRef=false; jtulach@120: boolean isRelative = false; jtulach@120: jtulach@120: // Check for permission to specify a handler jtulach@120: if (handler != null) { jaroslav@122: throw new SecurityException(); jtulach@120: } jtulach@120: jtulach@120: try { jtulach@120: limit = spec.length(); jtulach@120: while ((limit > 0) && (spec.charAt(limit - 1) <= ' ')) { jtulach@120: limit--; //eliminate trailing whitespace jtulach@120: } jtulach@120: while ((start < limit) && (spec.charAt(start) <= ' ')) { jtulach@120: start++; // eliminate leading whitespace jtulach@120: } jtulach@120: jtulach@120: if (spec.regionMatches(true, start, "url:", 0, 4)) { jtulach@120: start += 4; jtulach@120: } jtulach@120: if (start < spec.length() && spec.charAt(start) == '#') { jtulach@120: /* we're assuming this is a ref relative to the context URL. jtulach@120: * This means protocols cannot start w/ '#', but we must parse jtulach@120: * ref URL's like: "hello:there" w/ a ':' in them. jtulach@120: */ jtulach@120: aRef=true; jtulach@120: } jtulach@120: for (i = start ; !aRef && (i < limit) && jtulach@120: ((c = spec.charAt(i)) != '/') ; i++) { jtulach@120: if (c == ':') { jtulach@120: jtulach@120: String s = spec.substring(start, i).toLowerCase(); jtulach@120: if (isValidProtocol(s)) { jtulach@120: newProtocol = s; jtulach@120: start = i + 1; jtulach@120: } jtulach@120: break; jtulach@120: } jtulach@120: } jtulach@120: jtulach@120: // Only use our context if the protocols match. jtulach@120: protocol = newProtocol; jtulach@120: if ((context != null) && ((newProtocol == null) || jtulach@120: newProtocol.equalsIgnoreCase(context.protocol))) { jtulach@120: // inherit the protocol handler from the context jtulach@120: // if not specified to the constructor jaroslav@339: if (handler == null) { jaroslav@339: handler = context.handler; jaroslav@339: } jtulach@120: jtulach@120: // If the context is a hierarchical URL scheme and the spec jtulach@120: // contains a matching scheme then maintain backwards jtulach@120: // compatibility and treat it as if the spec didn't contain jtulach@120: // the scheme; see 5.2.3 of RFC2396 jtulach@120: if (context.path != null && context.path.startsWith("/")) jtulach@120: newProtocol = null; jtulach@120: jtulach@120: if (newProtocol == null) { jtulach@120: protocol = context.protocol; jtulach@120: authority = context.authority; jtulach@120: userInfo = context.userInfo; jtulach@120: host = context.host; jtulach@120: port = context.port; jtulach@120: file = context.file; jtulach@120: path = context.path; jtulach@120: isRelative = true; jtulach@120: } jtulach@120: } jtulach@120: jtulach@120: if (protocol == null) { jtulach@120: throw new MalformedURLException("no protocol: "+original); jtulach@120: } jtulach@120: jtulach@120: // Get the protocol handler if not specified or the protocol jtulach@120: // of the context could not be used jaroslav@339: if (handler == null && jaroslav@339: (handler = getURLStreamHandler(protocol)) == null) { jaroslav@339: throw new MalformedURLException("unknown protocol: "+protocol); jaroslav@339: } jaroslav@339: this.handler = handler; jtulach@120: jtulach@120: i = spec.indexOf('#', start); jtulach@120: if (i >= 0) { jaroslav@339: //thrw(protocol + " hnd: " + handler.getClass().getName() + " i: " + i); jtulach@120: ref = spec.substring(i + 1, limit); jtulach@120: limit = i; jtulach@120: } jtulach@120: jtulach@120: /* jtulach@120: * Handle special case inheritance of query and fragment jtulach@120: * implied by RFC2396 section 5.2.2. jtulach@120: */ jtulach@120: if (isRelative && start == limit) { jtulach@120: query = context.query; jtulach@120: if (ref == null) { jtulach@120: ref = context.ref; jtulach@120: } jtulach@120: } jtulach@120: jaroslav@339: handler.parseURL(this, spec, start, limit); jtulach@120: jtulach@120: } catch(MalformedURLException e) { jtulach@120: throw e; jtulach@120: } catch(Exception e) { jtulach@120: MalformedURLException exception = new MalformedURLException(e.getMessage()); jtulach@120: exception.initCause(e); jtulach@120: throw exception; jtulach@120: } jtulach@120: } jaroslav@339: jtulach@120: /* jtulach@120: * Returns true if specified string is a valid protocol name. jtulach@120: */ jtulach@120: private boolean isValidProtocol(String protocol) { jtulach@120: int len = protocol.length(); jtulach@120: if (len < 1) jtulach@120: return false; jtulach@120: char c = protocol.charAt(0); jtulach@120: if (!Character.isLetter(c)) jtulach@120: return false; jtulach@120: for (int i = 1; i < len; i++) { jtulach@120: c = protocol.charAt(i); jtulach@120: if (!Character.isLetterOrDigit(c) && c != '.' && c != '+' && jtulach@120: c != '-') { jtulach@120: return false; jtulach@120: } jtulach@120: } jtulach@120: return true; jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Sets the fields of the URL. This is not a public method so that jtulach@120: * only URLStreamHandlers can modify URL fields. URLs are jtulach@120: * otherwise constant. jtulach@120: * jtulach@120: * @param protocol the name of the protocol to use jtulach@120: * @param host the name of the host jtulach@120: @param port the port number on the host jtulach@120: * @param file the file on the host jtulach@120: * @param ref the internal reference in the URL jtulach@120: */ jtulach@120: protected void set(String protocol, String host, jtulach@120: int port, String file, String ref) { jtulach@120: synchronized (this) { jtulach@120: this.protocol = protocol; jtulach@120: this.host = host; jtulach@120: authority = port == -1 ? host : host + ":" + port; jtulach@120: this.port = port; jtulach@120: this.file = file; jtulach@120: this.ref = ref; jtulach@120: /* This is very important. We must recompute this after the jtulach@120: * URL has been changed. */ jtulach@120: hashCode = -1; jaroslav@339: hostAddress = null; jtulach@120: int q = file.lastIndexOf('?'); jtulach@120: if (q != -1) { jtulach@120: query = file.substring(q+1); jtulach@120: path = file.substring(0, q); jtulach@120: } else jtulach@120: path = file; jtulach@120: } jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Sets the specified 8 fields of the URL. This is not a public method so jtulach@120: * that only URLStreamHandlers can modify URL fields. URLs are otherwise jtulach@120: * constant. jtulach@120: * jtulach@120: * @param protocol the name of the protocol to use jtulach@120: * @param host the name of the host jtulach@120: * @param port the port number on the host jtulach@120: * @param authority the authority part for the url jtulach@120: * @param userInfo the username and password jtulach@120: * @param path the file on the host jtulach@120: * @param ref the internal reference in the URL jtulach@120: * @param query the query part of this URL jtulach@120: * @since 1.3 jtulach@120: */ jtulach@120: protected void set(String protocol, String host, int port, jtulach@120: String authority, String userInfo, String path, jtulach@120: String query, String ref) { jtulach@120: synchronized (this) { jtulach@120: this.protocol = protocol; jtulach@120: this.host = host; jtulach@120: this.port = port; jtulach@120: this.file = query == null ? path : path + "?" + query; jtulach@120: this.userInfo = userInfo; jtulach@120: this.path = path; jtulach@120: this.ref = ref; jtulach@120: /* This is very important. We must recompute this after the jtulach@120: * URL has been changed. */ jtulach@120: hashCode = -1; jaroslav@339: hostAddress = null; jtulach@120: this.query = query; jtulach@120: this.authority = authority; jtulach@120: } jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Gets the query part of this URL. jtulach@120: * jtulach@120: * @return the query part of this URL, jtulach@120: * or null if one does not exist jtulach@120: * @since 1.3 jtulach@120: */ jtulach@120: public String getQuery() { jtulach@120: return query; jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Gets the path part of this URL. jtulach@120: * jtulach@120: * @return the path part of this URL, or an jtulach@120: * empty string if one does not exist jtulach@120: * @since 1.3 jtulach@120: */ jtulach@120: public String getPath() { jtulach@120: return path; jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Gets the userInfo part of this URL. jtulach@120: * jtulach@120: * @return the userInfo part of this URL, or jtulach@120: * null if one does not exist jtulach@120: * @since 1.3 jtulach@120: */ jtulach@120: public String getUserInfo() { jtulach@120: return userInfo; jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Gets the authority part of this URL. jtulach@120: * jtulach@120: * @return the authority part of this URL jtulach@120: * @since 1.3 jtulach@120: */ jtulach@120: public String getAuthority() { jtulach@120: return authority; jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Gets the port number of this URL. jtulach@120: * jtulach@120: * @return the port number, or -1 if the port is not set jtulach@120: */ jtulach@120: public int getPort() { jtulach@120: return port; jtulach@120: } jtulach@120: jtulach@120: /** jaroslav@339: * Gets the default port number of the protocol associated jaroslav@339: * with this URL. If the URL scheme or the URLStreamHandler jaroslav@339: * for the URL do not define a default port number, jaroslav@339: * then -1 is returned. jaroslav@339: * jaroslav@339: * @return the port number jaroslav@339: * @since 1.4 jaroslav@339: */ jaroslav@339: public int getDefaultPort() { jaroslav@339: return handler.getDefaultPort(); jaroslav@339: } jaroslav@339: jaroslav@339: /** jtulach@120: * Gets the protocol name of this URL. jtulach@120: * jtulach@120: * @return the protocol of this URL. jtulach@120: */ jtulach@120: public String getProtocol() { jtulach@120: return protocol; jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Gets the host name of this URL, if applicable. jtulach@120: * The format of the host conforms to RFC 2732, i.e. for a jtulach@120: * literal IPv6 address, this method will return the IPv6 address jtulach@120: * enclosed in square brackets ('[' and ']'). jtulach@120: * jtulach@120: * @return the host name of this URL. jtulach@120: */ jtulach@120: public String getHost() { jtulach@120: return host; jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Gets the file name of this URL. jtulach@120: * The returned file portion will be jtulach@120: * the same as getPath(), plus the concatenation of jtulach@120: * the value of getQuery(), if any. If there is jtulach@120: * no query portion, this method and getPath() will jtulach@120: * return identical results. jtulach@120: * jtulach@120: * @return the file name of this URL, jtulach@120: * or an empty string if one does not exist jtulach@120: */ jtulach@120: public String getFile() { jtulach@120: return file; jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Gets the anchor (also known as the "reference") of this jtulach@120: * URL. jtulach@120: * jtulach@120: * @return the anchor (also known as the "reference") of this jtulach@120: * URL, or null if one does not exist jtulach@120: */ jtulach@120: public String getRef() { jtulach@120: return ref; jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Compares this URL for equality with another object.

jtulach@120: * jtulach@120: * If the given object is not a URL then this method immediately returns jtulach@120: * false.

jtulach@120: * jtulach@120: * Two URL objects are equal if they have the same protocol, reference jtulach@120: * equivalent hosts, have the same port number on the host, and the same jtulach@120: * file and fragment of the file.

jtulach@120: * jtulach@120: * Two hosts are considered equivalent if both host names can be resolved jtulach@120: * into the same IP addresses; else if either host name can't be jtulach@120: * resolved, the host names must be equal without regard to case; or both jtulach@120: * host names equal to null.

jtulach@120: * jtulach@120: * Since hosts comparison requires name resolution, this operation is a jtulach@120: * blocking operation.

jtulach@120: * jtulach@120: * Note: The defined behavior for equals is known to jtulach@120: * be inconsistent with virtual hosting in HTTP. jtulach@120: * jtulach@120: * @param obj the URL to compare against. jtulach@120: * @return true if the objects are the same; jtulach@120: * false otherwise. jtulach@120: */ jtulach@120: public boolean equals(Object obj) { jtulach@120: if (!(obj instanceof URL)) jtulach@120: return false; jtulach@120: URL u2 = (URL)obj; jtulach@120: jaroslav@339: return handler.equals(this, u2); jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Creates an integer suitable for hash table indexing.

jtulach@120: * jtulach@120: * The hash code is based upon all the URL components relevant for URL jtulach@120: * comparison. As such, this operation is a blocking operation.

jtulach@120: * jtulach@120: * @return a hash code for this URL. jtulach@120: */ jtulach@120: public synchronized int hashCode() { jtulach@120: if (hashCode != -1) jtulach@120: return hashCode; jtulach@120: jaroslav@339: hashCode = handler.hashCode(this); jtulach@120: return hashCode; jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Compares two URLs, excluding the fragment component.

jtulach@120: * jtulach@120: * Returns true if this URL and the jtulach@120: * other argument are equal without taking the jtulach@120: * fragment component into consideration. jtulach@120: * jtulach@120: * @param other the URL to compare against. jtulach@120: * @return true if they reference the same remote object; jtulach@120: * false otherwise. jtulach@120: */ jtulach@120: public boolean sameFile(URL other) { jaroslav@339: return handler.sameFile(this, other); jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Constructs a string representation of this URL. The jtulach@120: * string is created by calling the toExternalForm jtulach@120: * method of the stream protocol handler for this object. jtulach@120: * jtulach@120: * @return a string representation of this object. jtulach@120: * @see java.net.URL#URL(java.lang.String, java.lang.String, int, jtulach@120: * java.lang.String) jtulach@120: * @see java.net.URLStreamHandler#toExternalForm(java.net.URL) jtulach@120: */ jtulach@120: public String toString() { jtulach@120: return toExternalForm(); jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Constructs a string representation of this URL. The jtulach@120: * string is created by calling the toExternalForm jtulach@120: * method of the stream protocol handler for this object. jtulach@120: * jtulach@120: * @return a string representation of this object. jtulach@120: * @see java.net.URL#URL(java.lang.String, java.lang.String, jtulach@120: * int, java.lang.String) jtulach@120: * @see java.net.URLStreamHandler#toExternalForm(java.net.URL) jtulach@120: */ jtulach@120: public String toExternalForm() { jaroslav@339: return handler.toExternalForm(this); jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Returns a {@link java.net.URLConnection URLConnection} instance that jtulach@120: * represents a connection to the remote object referred to by the jtulach@120: * {@code URL}. jtulach@120: * jtulach@120: *

A new instance of {@linkplain java.net.URLConnection URLConnection} is jtulach@120: * created every time when invoking the jtulach@120: * {@linkplain java.net.URLStreamHandler#openConnection(URL) jtulach@120: * URLStreamHandler.openConnection(URL)} method of the protocol handler for jtulach@120: * this URL.

jtulach@120: * jtulach@120: *

It should be noted that a URLConnection instance does not establish jtulach@120: * the actual network connection on creation. This will happen only when jtulach@120: * calling {@linkplain java.net.URLConnection#connect() URLConnection.connect()}.

jtulach@120: * jtulach@120: *

If for the URL's protocol (such as HTTP or JAR), there jtulach@120: * exists a public, specialized URLConnection subclass belonging jtulach@120: * to one of the following packages or one of their subpackages: jtulach@120: * java.lang, java.io, java.util, java.net, the connection jtulach@120: * returned will be of that subclass. For example, for HTTP an jtulach@120: * HttpURLConnection will be returned, and for JAR a jtulach@120: * JarURLConnection will be returned.

jtulach@120: * jtulach@120: * @return a {@link java.net.URLConnection URLConnection} linking jtulach@120: * to the URL. jtulach@120: * @exception IOException if an I/O exception occurs. jtulach@120: * @see java.net.URL#URL(java.lang.String, java.lang.String, jtulach@120: * int, java.lang.String) jtulach@120: */ jaroslav@122: // public URLConnection openConnection() throws java.io.IOException { jaroslav@122: // return handler.openConnection(this); jaroslav@122: // } jtulach@120: jtulach@120: jtulach@120: /** jtulach@120: * Opens a connection to this URL and returns an jtulach@120: * InputStream for reading from that connection. This jtulach@120: * method is a shorthand for: jtulach@120: *
jtulach@120:      *     openConnection().getInputStream()
jtulach@120:      * 
jtulach@120: * jtulach@120: * @return an input stream for reading from the URL connection. jtulach@120: * @exception IOException if an I/O exception occurs. jtulach@120: * @see java.net.URL#openConnection() jtulach@120: * @see java.net.URLConnection#getInputStream() jtulach@120: */ jtulach@120: public final InputStream openStream() throws java.io.IOException { jaroslav@122: throw new IOException(); jaroslav@122: // return openConnection().getInputStream(); jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Gets the contents of this URL. This method is a shorthand for: jtulach@120: *
jtulach@120:      *     openConnection().getContent()
jtulach@120:      * 
jtulach@120: * jtulach@120: * @return the contents of this URL. jtulach@120: * @exception IOException if an I/O exception occurs. jtulach@120: * @see java.net.URLConnection#getContent() jtulach@120: */ jtulach@120: public final Object getContent() throws java.io.IOException { jaroslav@122: throw new IOException(); jaroslav@122: // return openConnection().getContent(); jtulach@120: } jtulach@120: jtulach@120: /** jtulach@120: * Gets the contents of this URL. This method is a shorthand for: jtulach@120: *
jtulach@120:      *     openConnection().getContent(Class[])
jtulach@120:      * 
jtulach@120: * jtulach@120: * @param classes an array of Java types jtulach@120: * @return the content object of this URL that is the first match of jtulach@120: * the types specified in the classes array. jtulach@120: * null if none of the requested types are supported. jtulach@120: * @exception IOException if an I/O exception occurs. jtulach@120: * @see java.net.URLConnection#getContent(Class[]) jtulach@120: * @since 1.3 jtulach@120: */ jtulach@120: public final Object getContent(Class[] classes) jtulach@120: throws java.io.IOException { jaroslav@122: throw new IOException(); jaroslav@122: // return openConnection().getContent(classes); jtulach@120: } jtulach@120: jaroslav@339: static URLStreamHandler getURLStreamHandler(String protocol) { jaroslav@339: URLStreamHandler universal = new URLStreamHandler() {}; jaroslav@339: return universal; jaroslav@339: } jtulach@120: jtulach@120: } jtulach@120: jtulach@120: class Parts { jtulach@120: String path, query, ref; jtulach@120: jtulach@120: Parts(String file) { jtulach@120: int ind = file.indexOf('#'); jtulach@120: ref = ind < 0 ? null: file.substring(ind + 1); jtulach@120: file = ind < 0 ? file: file.substring(0, ind); jtulach@120: int q = file.lastIndexOf('?'); jtulach@120: if (q != -1) { jtulach@120: query = file.substring(q+1); jtulach@120: path = file.substring(0, q); jtulach@120: } else { jtulach@120: path = file; jtulach@120: } jtulach@120: } jtulach@120: jtulach@120: String getPath() { jtulach@120: return path; jtulach@120: } jtulach@120: jtulach@120: String getQuery() { jtulach@120: return query; jtulach@120: } jtulach@120: jtulach@120: String getRef() { jtulach@120: return ref; jtulach@120: } jtulach@120: }