0001: /*
0002: * @(#)URL.java 1.108 06/10/10
0003: *
0004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
0005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
0006: *
0007: * This program is free software; you can redistribute it and/or
0008: * modify it under the terms of the GNU General Public License version
0009: * 2 only, as published by the Free Software Foundation.
0010: *
0011: * This program is distributed in the hope that it will be useful, but
0012: * WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014: * General Public License version 2 for more details (a copy is
0015: * included at /legal/license.txt).
0016: *
0017: * You should have received a copy of the GNU General Public License
0018: * version 2 along with this work; if not, write to the Free Software
0019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0020: * 02110-1301 USA
0021: *
0022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
0023: * Clara, CA 95054 or visit www.sun.com if you need additional
0024: * information or have any questions.
0025: *
0026: */
0027:
0028: package java.net;
0029:
0030: import java.io.IOException;
0031: import java.io.InputStream;
0032: import java.io.OutputStream;
0033: import java.util.Hashtable;
0034: import java.util.StringTokenizer;
0035: import sun.security.util.SecurityConstants;
0036:
0037: /**
0038: * Class <code>URL</code> represents a Uniform Resource
0039: * Locator, a pointer to a "resource" on the World
0040: * Wide Web. A resource can be something as simple as a file or a
0041: * directory, or it can be a reference to a more complicated object,
0042: * such as a query to a database or to a search engine. More
0043: * information on the types of URLs and their formats can be found at:
0044: * <blockquote>
0045: * <a href="http://archive.ncsa.uiuc.edu/SDG/Software/Mosaic/Demo/url-primer.html">
0046: * <i>http://archive.ncsa.uiuc.edu/SDG/Software/Mosaic/Demo/url-primer.html</i></a>
0047: * </blockquote>
0048: * <p>
0049: * In general, a URL can be broken into several parts. The previous
0050: * example of a URL indicates that the protocol to use is
0051: * <code>http</code> (HyperText Transfer Protocol) and that the
0052: * information resides on a host machine named
0053: * <code>www.ncsa.uiuc.edu</code>. The information on that host
0054: * machine is named <code>/SDG/Software/Mosaic/Demo/url-primer.html</code>. The exact
0055: * meaning of this name on the host machine is both protocol
0056: * dependent and host dependent. The information normally resides in
0057: * a file, but it could be generated on the fly. This component of
0058: * the URL is called the <i>path</i> component.
0059: * <p>
0060: * A URL can optionally specify a "port", which is the
0061: * port number to which the TCP connection is made on the remote host
0062: * machine. If the port is not specified, the default port for
0063: * the protocol is used instead. For example, the default port for
0064: * <code>http</code> is <code>80</code>. An alternative port could be
0065: * specified as:
0066: * <blockquote><pre>
0067: * http://archive.ncsa.uiuc.edu:80/SDG/Software/Mosaic/Demo/url-primer.html
0068: * </pre></blockquote>
0069: * <p>
0070: * The syntax of <code>URL</code> is defined by <a
0071: * href="http://www.ietf.org/rfc/rfc2396.txt""><i>RFC 2396: Uniform
0072: * Resource Identifiers (URI): Generic Syntax</i></a>, amended by <a
0073: * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC 2732: Format for
0074: * Literal IPv6 Addresses in URLs</i></a>.
0075: * <p>
0076: * A URL may have appended to it a "fragment", also known
0077: * as a "ref" or a "reference". The fragment is indicated by the sharp
0078: * sign character "#" followed by more characters. For example,
0079: * <blockquote><pre>
0080: * http://java.sun.com/index.html#chapter1
0081: * </pre></blockquote>
0082: * <p>
0083: * This fragment is not technically part of the URL. Rather, it
0084: * indicates that after the specified resource is retrieved, the
0085: * application is specifically interested in that part of the
0086: * document that has the tag <code>chapter1</code> attached to it. The
0087: * meaning of a tag is resource specific.
0088: * <p>
0089: * An application can also specify a "relative URL",
0090: * which contains only enough information to reach the resource
0091: * relative to another URL. Relative URLs are frequently used within
0092: * HTML pages. For example, if the contents of the URL:
0093: * <blockquote><pre>
0094: * http://java.sun.com/index.html
0095: * </pre></blockquote>
0096: * contained within it the relative URL:
0097: * <blockquote><pre>
0098: * FAQ.html
0099: * </pre></blockquote>
0100: * it would be a shorthand for:
0101: * <blockquote><pre>
0102: * http://java.sun.com/FAQ.html
0103: * </pre></blockquote>
0104: * <p>
0105: * The relative URL need not specify all the components of a URL. If
0106: * the protocol, host name, or port number is missing, the value is
0107: * inherited from the fully specified URL. The file component must be
0108: * specified. The optional fragment is not inherited.
0109: *
0110: * @author James Gosling
0111: * @version 1.108, 10/10/06
0112: * @since JDK1.0
0113: */
0114: public final class URL implements java.io.Serializable {
0115:
0116: static final long serialVersionUID = -7627629688361524110L;
0117:
0118: /**
0119: * The property which specifies the package prefix list to be scanned
0120: * for protocol handlers. The value of this property (if any) should
0121: * be a vertical bar delimited list of package names to search through
0122: * for a protocol handler to load. The policy of this class is that
0123: * all protocol handlers will be in a class called <protocolname>.Handler,
0124: * and each package in the list is examined in turn for a matching
0125: * handler. If none are found (or the property is not specified), the
0126: * default package prefix, sun.net.www.protocol, is used. The search
0127: * proceeds from the first package in the list to the last and stops
0128: * when a match is found.
0129: */
0130: private static final String protocolPathProp = "java.protocol.handler.pkgs";
0131:
0132: /**
0133: * The protocol to use (ftp, http, nntp, ... etc.) .
0134: * @serial
0135: */
0136: private String protocol;
0137:
0138: /**
0139: * The host name to connect to.
0140: * @serial
0141: */
0142: private String host;
0143:
0144: /**
0145: * The protocol port to connect to.
0146: * @serial
0147: */
0148: private int port = -1;
0149:
0150: /**
0151: * The specified file name on that host. <code>file</code> is
0152: * defined as <code>path[?query]</code>
0153: * @serial
0154: */
0155: private String file;
0156:
0157: /**
0158: * The query part of this URL.
0159: */
0160: private transient String query;
0161:
0162: /**
0163: * The authority part of this URL.
0164: * @serial
0165: */
0166: private String authority;
0167:
0168: /**
0169: * The path part of this URL.
0170: */
0171: private transient String path;
0172:
0173: /**
0174: * The userinfo part of this URL.
0175: */
0176: private transient String userInfo;
0177:
0178: /**
0179: * # reference.
0180: * @serial
0181: */
0182: private String ref;
0183:
0184: /**
0185: * The host's IP address, used in equals and hashCode.
0186: * Computed on demand. An uninitialized or unknown hostAddress is null.
0187: */
0188: transient InetAddress hostAddress;
0189:
0190: /**
0191: * The URLStreamHandler for this URL.
0192: */
0193: transient URLStreamHandler handler;
0194:
0195: /* Our hash code.
0196: * @serial
0197: */
0198: private int hashCode = -1;
0199:
0200: /**
0201: * Creates a <code>URL</code> object from the specified
0202: * <code>protocol</code>, <code>host</code>, <code>port</code>
0203: * number, and <code>file</code>.<p>
0204: *
0205: * <code>host</code> can be expressed as a host name or a literal
0206: * IP address. If IPv6 literal address is used, it should be
0207: * enclosed in square brackets (<tt>'['</tt> and <tt>']'</tt>), as
0208: * specified by <a
0209: * href="http://www.ietf.org/rfc/rfc2732.txt">RFC 2732</a>;
0210: * However, the literal IPv6 address format defined in <a
0211: * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC 2373: IP
0212: * Version 6 Addressing Architecture</i></a> is also accepted.<p>
0213: *
0214: * Specifying a <code>port</code> number of <code>-1</code>
0215: * indicates that the URL should use the default port for the
0216: * protocol.<p>
0217: *
0218: * If this is the first URL object being created with the specified
0219: * protocol, a <i>stream protocol handler</i> object, an instance of
0220: * class <code>URLStreamHandler</code>, is created for that protocol:
0221: * <ol>
0222: * <li>If the application has previously set up an instance of
0223: * <code>URLStreamHandlerFactory</code> as the stream handler factory,
0224: * then the <code>createURLStreamHandler</code> method of that instance
0225: * is called with the protocol string as an argument to create the
0226: * stream protocol handler.
0227: * <li>If no <code>URLStreamHandlerFactory</code> has yet been set up,
0228: * or if the factory's <code>createURLStreamHandler</code> method
0229: * returns <code>null</code>, then the constructor finds the
0230: * value of the system property:
0231: * <blockquote><pre>
0232: * java.protocol.handler.pkgs
0233: * </pre></blockquote>
0234: * If the value of that system property is not <code>null</code>,
0235: * it is interpreted as a list of packages separated by a vertical
0236: * slash character '<code>|</code>'. The constructor tries to load
0237: * the class named:
0238: * <blockquote><pre>
0239: * <<i>package</i>>.<<i>protocol</i>>.Handler
0240: * </pre></blockquote>
0241: * where <<i>package</i>> is replaced by the name of the package
0242: * and <<i>protocol</i>> is replaced by the name of the protocol.
0243: * If this class does not exist, or if the class exists but it is not
0244: * a subclass of <code>URLStreamHandler</code>, then the next package
0245: * in the list is tried.
0246: * <li>If the previous step fails to find a protocol handler, then the
0247: * constructor tries to load from a system default package.
0248: * <blockquote><pre>
0249: * <<i>system default package</i>>.<<i>protocol</i>>.Handler
0250: * </pre></blockquote>
0251: * If this class does not exist, or if the class exists but it is not a
0252: * subclass of <code>URLStreamHandler</code>, then a
0253: * <code>MalformedURLException</code> is thrown.
0254: * </ol>
0255: *
0256: * <p>No validation of the inputs is performed by this constructor.
0257: *
0258: * @param protocol the name of the protocol to use.
0259: * @param host the name of the host.
0260: * @param port the port number on the host.
0261: * @param file the file on the host
0262: * @exception MalformedURLException if an unknown protocol is specified.
0263: * @see java.lang.System#getProperty(java.lang.String)
0264: * @see java.net.URL#setURLStreamHandlerFactory(
0265: * java.net.URLStreamHandlerFactory)
0266: * @see java.net.URLStreamHandler
0267: * @see java.net.URLStreamHandlerFactory#createURLStreamHandler(
0268: * java.lang.String)
0269: */
0270: public URL(String protocol, String host, int port, String file)
0271: throws MalformedURLException {
0272: this (protocol, host, port, file, null);
0273: }
0274:
0275: /**
0276: * Creates a URL from the specified <code>protocol</code>
0277: * name, <code>host</code> name, and <code>file</code> name. The
0278: * default port for the specified protocol is used.
0279: * <p>
0280: * This method is equivalent to calling the four-argument
0281: * constructor with the arguments being <code>protocol</code>,
0282: * <code>host</code>, <code>-1</code>, and <code>file</code>.
0283: *
0284: * No validation of the inputs is performed by this constructor.
0285: *
0286: * @param protocol the name of the protocol to use.
0287: * @param host the name of the host.
0288: * @param file the file on the host.
0289: * @exception MalformedURLException if an unknown protocol is specified.
0290: * @see java.net.URL#URL(java.lang.String, java.lang.String,
0291: * int, java.lang.String)
0292: */
0293: public URL(String protocol, String host, String file)
0294: throws MalformedURLException {
0295: this (protocol, host, -1, file);
0296: }
0297:
0298: /**
0299: * Creates a <code>URL</code> object from the specified
0300: * <code>protocol</code>, <code>host</code>, <code>port</code>
0301: * number, <code>file</code>, and <code>handler</code>. Specifying
0302: * a <code>port</code> number of <code>-1</code> indicates that
0303: * the URL should use the default port for the protocol. Specifying
0304: * a <code>handler</code> of <code>null</code> indicates that the URL
0305: * should use a default stream handler for the protocol, as outlined
0306: * for:
0307: * java.net.URL#URL(java.lang.String, java.lang.String, int,
0308: * java.lang.String)
0309: *
0310: * <p>If the handler is not null and there is a security manager,
0311: * the security manager's <code>checkPermission</code>
0312: * method is called with a
0313: * <code>NetPermission("specifyStreamHandler")</code> permission.
0314: * This may result in a SecurityException.
0315: *
0316: * No validation of the inputs is performed by this constructor.
0317: *
0318: * @param protocol the name of the protocol to use.
0319: * @param host the name of the host.
0320: * @param port the port number on the host.
0321: * @param file the file on the host
0322: * @param handler the stream handler for the URL.
0323: * @exception MalformedURLException if an unknown protocol is specified.
0324: * @exception SecurityException
0325: * if a security manager exists and its
0326: * <code>checkPermission</code> method doesn't allow
0327: * specifying a stream handler explicitly.
0328: * @see java.lang.System#getProperty(java.lang.String)
0329: * @see java.net.URL#setURLStreamHandlerFactory(
0330: * java.net.URLStreamHandlerFactory)
0331: * @see java.net.URLStreamHandler
0332: * @see java.net.URLStreamHandlerFactory#createURLStreamHandler(
0333: * java.lang.String)
0334: * @see SecurityManager#checkPermission
0335: * @see java.net.NetPermission
0336: */
0337: public URL(String protocol, String host, int port, String file,
0338: URLStreamHandler handler) throws MalformedURLException {
0339: if (handler != null) {
0340: SecurityManager sm = System.getSecurityManager();
0341: if (sm != null) {
0342: // check for permission to specify a handler
0343: checkSpecifyHandler(sm);
0344: }
0345: }
0346:
0347: protocol = protocol.toLowerCase();
0348: this .protocol = protocol;
0349: if (host != null) {
0350:
0351: /**
0352: * if host is a literal IPv6 address,
0353: * we will make it conform to RFC 2732
0354: */
0355: if (host != null && host.indexOf(':') >= 0
0356: && !host.startsWith("[")) {
0357: host = "[" + host + "]";
0358: }
0359: this .host = host;
0360:
0361: if (port < -1) {
0362: throw new MalformedURLException("Invalid port number :"
0363: + port);
0364: }
0365: this .port = port;
0366: authority = (port == -1) ? host : host + ":" + port;
0367: }
0368:
0369: Parts parts = new Parts(file);
0370: path = parts.getPath();
0371: query = parts.getQuery();
0372:
0373: if (query != null) {
0374: this .file = path + "?" + query;
0375: } else {
0376: this .file = path;
0377: }
0378: ref = parts.getRef();
0379:
0380: // Note: we don't do validation of the URL here. Too risky to change
0381: // right now, but worth considering for future reference. -br
0382: if (handler == null
0383: && (handler = getURLStreamHandler(protocol)) == null) {
0384: throw new MalformedURLException("unknown protocol: "
0385: + protocol);
0386: }
0387: this .handler = handler;
0388: }
0389:
0390: /**
0391: * Creates a <code>URL</code> object from the <code>String</code>
0392: * representation.
0393: * <p>
0394: * This constructor is equivalent to a call to the two-argument
0395: * constructor with a <code>null</code> first argument.
0396: *
0397: * @param spec the <code>String</code> to parse as a URL.
0398: * @exception MalformedURLException If the string specifies an
0399: * unknown protocol.
0400: * @see java.net.URL#URL(java.net.URL, java.lang.String)
0401: */
0402: public URL(String spec) throws MalformedURLException {
0403: this (null, spec);
0404: }
0405:
0406: /**
0407: * Creates a URL by parsing the given spec within a specified context.
0408: *
0409: * The new URL is created from the given context URL and the spec
0410: * argument as described in
0411: * RFC2396 "Uniform Resource Identifiers : Generic * Syntax" :
0412: * <blockquote><pre>
0413: * <scheme>://<authority><path>?<query>#<fragment>
0414: * </pre></blockquote>
0415: * The reference is parsed into the scheme, authority, path, query and
0416: * fragment parts. If the path component is empty and the scheme,
0417: * authority, and query components are undefined, then the new URL is a
0418: * reference to the current document. Otherwise, the fragment and query
0419: * parts present in the spec are used in the new URL.
0420: * <p>
0421: * If the scheme component is defined in the given spec and does not match
0422: * the scheme of the context, then the new URL is created as an absolute
0423: * URL based on the spec alone. Otherwise the scheme component is inherited
0424: * from the context URL.
0425: * <p>
0426: * If the authority component is present in the spec then the spec is
0427: * treated as absolute and the spec authority and path will replace the
0428: * context authority and path. If the authority component is absent in the
0429: * spec then the authority of the new URL will be inherited from the
0430: * context.
0431: * <p>
0432: * If the spec's path component begins with a slash character
0433: * "/" then the
0434: * path is treated as absolute and the spec path replaces the context path.
0435: * <p>
0436: * Otherwise, the path is treated as a relative path and is appended to the
0437: * context path, as described in RFC2396. Also, in this case,
0438: * the path is canonicalized through the removal of directory
0439: * changes made by occurences of ".." and ".".
0440: * <p>
0441: * For a more detailed description of URL parsing, refer to RFC2396.
0442: *
0443: * @param context the context in which to parse the specification.
0444: * @param spec the <code>String</code> to parse as a URL.
0445: * @exception MalformedURLException if no protocol is specified, or an
0446: * unknown protocol is found.
0447: * @see java.net.URL#URL(java.lang.String, java.lang.String,
0448: * int, java.lang.String)
0449: * @see java.net.URLStreamHandler
0450: * @see java.net.URLStreamHandler#parseURL(java.net.URL,
0451: * java.lang.String, int, int)
0452: */
0453: public URL(URL context, String spec) throws MalformedURLException {
0454: this (context, spec, null);
0455: }
0456:
0457: /**
0458: * Creates a URL by parsing the given spec with the specified handler
0459: * within a specified context. If the handler is null, the parsing
0460: * occurs as with the two argument constructor.
0461: *
0462: * @param context the context in which to parse the specification.
0463: * @param spec the <code>String</code> to parse as a URL.
0464: * @param handler the stream handler for the URL.
0465: * @exception MalformedURLException if no protocol is specified, or an
0466: * unknown protocol is found.
0467: * @exception SecurityException
0468: * if a security manager exists and its
0469: * <code>checkPermission</code> method doesn't allow
0470: * specifying a stream handler.
0471: * @see java.net.URL#URL(java.lang.String, java.lang.String,
0472: * int, java.lang.String)
0473: * @see java.net.URLStreamHandler
0474: * @see java.net.URLStreamHandler#parseURL(java.net.URL,
0475: * java.lang.String, int, int)
0476: */
0477: public URL(URL context, String spec, URLStreamHandler handler)
0478: throws MalformedURLException {
0479: String original = spec;
0480: int i, limit, c;
0481: int start = 0;
0482: String newProtocol = null;
0483: boolean aRef = false;
0484: boolean isRelative = false;
0485:
0486: // Check for permission to specify a handler
0487: if (handler != null) {
0488: SecurityManager sm = System.getSecurityManager();
0489: if (sm != null) {
0490: checkSpecifyHandler(sm);
0491: }
0492: }
0493:
0494: try {
0495: limit = spec.length();
0496: while ((limit > 0) && (spec.charAt(limit - 1) <= ' ')) {
0497: limit--; //eliminate trailing whitespace
0498: }
0499: while ((start < limit) && (spec.charAt(start) <= ' ')) {
0500: start++; // eliminate leading whitespace
0501: }
0502:
0503: if (spec.regionMatches(true, start, "url:", 0, 4)) {
0504: start += 4;
0505: }
0506: if (start < spec.length() && spec.charAt(start) == '#') {
0507: /* we're assuming this is a ref relative to the context URL.
0508: * This means protocols cannot start w/ '#', but we must parse
0509: * ref URL's like: "hello:there" w/ a ':' in them.
0510: */
0511: aRef = true;
0512: }
0513: for (i = start; !aRef && (i < limit)
0514: && ((c = spec.charAt(i)) != '/'); i++) {
0515: if (c == ':') {
0516:
0517: String s = spec.substring(start, i).toLowerCase();
0518: if (isValidProtocol(s)) {
0519: newProtocol = s;
0520: start = i + 1;
0521: }
0522: break;
0523: }
0524: }
0525:
0526: // Only use our context if the protocols match.
0527: protocol = newProtocol;
0528: if ((context != null)
0529: && ((newProtocol == null) || newProtocol
0530: .equalsIgnoreCase(context.protocol))) {
0531: // inherit the protocol handler from the context
0532: // if not specified to the contructor
0533: if (handler == null) {
0534: handler = context.handler;
0535: }
0536:
0537: // If the context is a hierarchical URL scheme and the spec
0538: // contains a matching scheme then maintain backwards
0539: // compatibility and treat it as if the spec didn't contain
0540: // the scheme; see 5.2.3 of RFC2396
0541: if (context.path != null
0542: && context.path.startsWith("/"))
0543: newProtocol = null;
0544:
0545: if (newProtocol == null) {
0546: protocol = context.protocol;
0547: authority = context.authority;
0548: userInfo = context.userInfo;
0549: host = context.host;
0550: port = context.port;
0551: file = context.file;
0552: path = context.path;
0553: isRelative = true;
0554: }
0555: }
0556:
0557: if (protocol == null) {
0558: throw new MalformedURLException("no protocol: "
0559: + original);
0560: }
0561:
0562: // Get the protocol handler if not specified or the protocol
0563: // of the context could not be used
0564: if (handler == null
0565: && (handler = getURLStreamHandler(protocol)) == null) {
0566: throw new MalformedURLException("unknown protocol: "
0567: + protocol);
0568: }
0569:
0570: this .handler = handler;
0571:
0572: i = spec.indexOf('#', start);
0573: if (i >= 0) {
0574: ref = spec.substring(i + 1, limit);
0575: limit = i;
0576: }
0577:
0578: /*
0579: * Handle special case inheritance of query and fragment
0580: * implied by RFC2396 section 5.2.2.
0581: */
0582: if (isRelative && start == limit) {
0583: query = context.query;
0584: if (ref == null) {
0585: ref = context.ref;
0586: }
0587: }
0588:
0589: handler.parseURL(this , spec, start, limit);
0590:
0591: } catch (MalformedURLException e) {
0592: throw e;
0593: } catch (Exception e) {
0594: throw new MalformedURLException(e.getMessage());
0595: }
0596: }
0597:
0598: /*
0599: * Returns true if specified string is a valid protocol name.
0600: */
0601: private boolean isValidProtocol(String protocol) {
0602: int len = protocol.length();
0603: if (len < 1)
0604: return false;
0605: char c = protocol.charAt(0);
0606: if (!Character.isLetter(c))
0607: return false;
0608: for (int i = 1; i < len; i++) {
0609: c = protocol.charAt(i);
0610: if (!Character.isLetterOrDigit(c) && c != '.' && c != '+'
0611: && c != '-') {
0612: return false;
0613: }
0614: }
0615: return true;
0616: }
0617:
0618: /*
0619: * Checks for permission to specify a stream handler.
0620: */
0621: private void checkSpecifyHandler(SecurityManager sm) {
0622: sm
0623: .checkPermission(SecurityConstants.SPECIFY_HANDLER_PERMISSION);
0624: }
0625:
0626: /**
0627: * Sets the fields of the URL. This is not a public method so that
0628: * only URLStreamHandlers can modify URL fields. URLs are
0629: * otherwise constant.
0630: *
0631: * @param protocol the name of the protocol to use
0632: * @param host the name of the host
0633: @param port the port number on the host
0634: * @param file the file on the host
0635: * @param ref the internal reference in the URL
0636: */
0637: protected void set(String protocol, String host, int port,
0638: String file, String ref) {
0639: synchronized (this ) {
0640: this .protocol = protocol;
0641: this .host = host;
0642: authority = port == -1 ? host : host + ":" + port;
0643: this .port = port;
0644: this .file = file;
0645: this .ref = ref;
0646: /* This is very important. We must recompute this after the
0647: * URL has been changed. */
0648: hashCode = -1;
0649: hostAddress = null;
0650: int q = file.lastIndexOf('?');
0651: if (q != -1) {
0652: query = file.substring(q + 1);
0653: path = file.substring(0, q);
0654: } else
0655: path = file;
0656: }
0657: }
0658:
0659: /**
0660: * Sets the specified 8 fields of the URL. This is not a public method so
0661: * that only URLStreamHandlers can modify URL fields. URLs are otherwise
0662: * constant.
0663: *
0664: * @param protocol the name of the protocol to use
0665: * @param host the name of the host
0666: * @param port the port number on the host
0667: * @param authority the authority part for the url
0668: * @param userInfo the username and password
0669: * @param path the file on the host
0670: * @param ref the internal reference in the URL
0671: * @param query the query part of this URL
0672: * @since 1.3
0673: */
0674: protected void set(String protocol, String host, int port,
0675: String authority, String userInfo, String path,
0676: String query, String ref) {
0677: synchronized (this ) {
0678: this .protocol = protocol;
0679: this .host = host;
0680: this .port = port;
0681: this .file = query == null ? path : path + "?" + query;
0682: this .userInfo = userInfo;
0683: this .path = path;
0684: this .ref = ref;
0685: /* This is very important. We must recompute this after the
0686: * URL has been changed. */
0687: hashCode = -1;
0688: hostAddress = null;
0689: this .query = query;
0690: this .authority = authority;
0691: }
0692: }
0693:
0694: /**
0695: * Gets the query part of this <code>URL</code>.
0696: *
0697: * @return the query part of this <code>URL</code>,
0698: * or <CODE>null</CODE> if one does not exist
0699: * @since 1.3
0700: */
0701: public String getQuery() {
0702: return query;
0703: }
0704:
0705: /**
0706: * Gets the path part of this <code>URL</code>.
0707: *
0708: * @return the path part of this <code>URL</code>, or an
0709: * empty string if one does not exist
0710: * @since 1.3
0711: */
0712: public String getPath() {
0713: return path;
0714: }
0715:
0716: /**
0717: * Gets the userInfo part of this <code>URL</code>.
0718: *
0719: * @return the userInfo part of this <code>URL</code>, or
0720: * <CODE>null</CODE> if one does not exist
0721: */
0722: public String getUserInfo() {
0723: return userInfo;
0724: }
0725:
0726: /**
0727: * Gets the authority part of this <code>URL</code>.
0728: *
0729: * @return the authority part of this <code>URL</code>
0730: * @since 1.3
0731: */
0732: public String getAuthority() {
0733: return authority;
0734: }
0735:
0736: /**
0737: * Gets the port number of this <code>URL</code>.
0738: *
0739: * @return the port number, or -1 if the port is not set
0740: */
0741: public int getPort() {
0742: return port;
0743: }
0744:
0745: /**
0746: * Gets the default port number of the protocol associated
0747: * with this <code>URL</code>. If the URL scheme or the URLStreamHandler
0748: * for the URL do not define a default port number,
0749: * then -1 is returned.
0750: *
0751: * @return the port number
0752: */
0753: public int getDefaultPort() {
0754: return handler.getDefaultPort();
0755: }
0756:
0757: /**
0758: * Gets the protocol name of this <code>URL</code>.
0759: *
0760: * @return the protocol of this <code>URL</code>.
0761: */
0762: public String getProtocol() {
0763: return protocol;
0764: }
0765:
0766: /**
0767: * Gets the host name of this <code>URL</code>, if applicable.
0768: * The format of the host conforms to RFC 2732, i.e. for a
0769: * literal IPv6 address, this method will return the IPv6 address
0770: * enclosed in square brackets (<tt>'['</tt> and <tt>']'</tt>).
0771: *
0772: * @return the host name of this <code>URL</code>.
0773: */
0774: public String getHost() {
0775: return host;
0776: }
0777:
0778: /**
0779: * Gets the file name of this <code>URL</code>.
0780: * The returned file portion will be
0781: * the same as <CODE>getPath()</CODE>, plus the concatenation of
0782: * the value of <CODE>getQuery()</CODE>, if any. If there is
0783: * no query portion, this method and <CODE>getPath()</CODE> will
0784: * return identical results.
0785: *
0786: * @return the file name of this <code>URL</code>,
0787: * or an empty string if one does not exist
0788: */
0789: public String getFile() {
0790: return file;
0791: }
0792:
0793: /**
0794: * Gets the anchor (also known as the "reference") of this
0795: * <code>URL</code>.
0796: *
0797: * @return the anchor (also known as the "reference") of this
0798: * <code>URL</code>, or <CODE>null</CODE> if one does not exist
0799: */
0800: public String getRef() {
0801: return ref;
0802: }
0803:
0804: /**
0805: * Compares this URL for equality with another object.<p>
0806: *
0807: * If the given object is not a URL then this method immediately returns
0808: * <code>false</code>.<p>
0809: *
0810: * Two URL objects are equal if they have the same protocol, reference
0811: * equivalent hosts, have the same port number on the host, and the same
0812: * file and fragment of the file.<p>
0813: *
0814: * Two hosts are considered equivalent if both host names can be resolved
0815: * into the same IP addresses; else if either host name can't be
0816: * resolved, the host names must be equal without regard to case; or both
0817: * host names equal to null.<p>
0818: *
0819: * Since hosts comparison requires name resolution, this operation is a
0820: * blocking operation. <p>
0821: *
0822: * Note: The defined behavior for <code>equals</code> is known to
0823: * be inconsistent with virtual hosting in HTTP.
0824: *
0825: * @param obj the URL to compare against.
0826: * @return <code>true</code> if the objects are the same;
0827: * <code>false</code> otherwise.
0828: */
0829: public boolean equals(Object obj) {
0830: if (!(obj instanceof URL))
0831: return false;
0832: URL u2 = (URL) obj;
0833:
0834: return handler.equals(this , u2);
0835: }
0836:
0837: /**
0838: * Creates an integer suitable for hash table indexing.<p>
0839: *
0840: * The hash code is based upon all the URL components relevant for URL
0841: * comparison. As such, this operation is a blocking operation.<p>
0842: *
0843: * @return a hash code for this <code>URL</code>.
0844: */
0845: public synchronized int hashCode() {
0846: if (hashCode != -1)
0847: return hashCode;
0848:
0849: hashCode = handler.hashCode(this );
0850: return hashCode;
0851: }
0852:
0853: /**
0854: * Compares two URLs, excluding the fragment component.<p>
0855: *
0856: * Returns <code>true</code> if this <code>URL</code> and the
0857: * <code>other</code> argument are equal without taking the
0858: * fragment component into consideration.
0859: *
0860: * @param other the <code>URL</code> to compare against.
0861: * @return <code>true</code> if they reference the same remote object;
0862: * <code>false</code> otherwise.
0863: */
0864: public boolean sameFile(URL other) {
0865: return handler.sameFile(this , other);
0866: }
0867:
0868: /**
0869: * Constructs a string representation of this <code>URL</code>. The
0870: * string is created by calling the <code>toExternalForm</code>
0871: * method of the stream protocol handler for this object.
0872: *
0873: * @return a string representation of this object.
0874: * @see java.net.URL#URL(java.lang.String, java.lang.String, int,
0875: * java.lang.String)
0876: * @see java.net.URLStreamHandler#toExternalForm(java.net.URL)
0877: */
0878: public String toString() {
0879: return toExternalForm();
0880: }
0881:
0882: /**
0883: * Constructs a string representation of this <code>URL</code>. The
0884: * string is created by calling the <code>toExternalForm</code>
0885: * method of the stream protocol handler for this object.
0886: *
0887: * @return a string representation of this object.
0888: * @see java.net.URL#URL(java.lang.String, java.lang.String,
0889: * int, java.lang.String)
0890: * @see java.net.URLStreamHandler#toExternalForm(java.net.URL)
0891: */
0892: public String toExternalForm() {
0893: return handler.toExternalForm(this );
0894: }
0895:
0896: /**
0897: * Returns a <code>URLConnection</code> object that represents a
0898: * connection to the remote object referred to by the <code>URL</code>.
0899: *
0900: * <p>A new connection is opened every time by calling the
0901: * <code>openConnection</code> method of the protocol handler for
0902: * this URL.
0903: *
0904: * <p>If for the URL's protocol (such as HTTP or JAR), there
0905: * exists a public, specialized URLConnection subclass belonging
0906: * to one of the following packages or one of their subpackages:
0907: * java.lang, java.io, java.util, java.net, the connection
0908: * returned will be of that subclass. For example, for HTTP an
0909: * HttpURLConnection will be returned, and for JAR a
0910: * JarURLConnection will be returned.
0911: * NOTE: <B>java.net.HttpURLConnection</B> is found in J2ME CDC profiles
0912: * such as J2ME Foundation Profile.
0913: * Subclasses of these should
0914: *
0915: * @return a <code>URLConnection</code> to the URL.
0916: * @exception IOException if an I/O exception occurs.
0917: * @see java.net.URL#URL(java.lang.String, java.lang.String,
0918: * int, java.lang.String)
0919: * @see java.net.URLConnection
0920: * @see java.net.URLStreamHandler#openConnection(java.net.URL)
0921: */
0922: public URLConnection openConnection() throws java.io.IOException {
0923: return handler.openConnection(this );
0924: }
0925:
0926: /**
0927: * Opens a connection to this <code>URL</code> and returns an
0928: * <code>InputStream</code> for reading from that connection. This
0929: * method is a shorthand for:
0930: * <blockquote><pre>
0931: * openConnection().getInputStream()
0932: * </pre></blockquote>
0933: *
0934: * @return an input stream for reading from the URL connection.
0935: * @exception IOException if an I/O exception occurs.
0936: * @see java.net.URL#openConnection()
0937: * @see java.net.URLConnection#getInputStream()
0938: */
0939: public final InputStream openStream() throws java.io.IOException {
0940: return openConnection().getInputStream();
0941: }
0942:
0943: /**
0944: * Gets the contents of this URL. This method is a shorthand for:
0945: * <blockquote><pre>
0946: * openConnection().getContent()
0947: * </pre></blockquote>
0948: *
0949: * @return the contents of this URL.
0950: * @exception IOException if an I/O exception occurs.
0951: * @see java.net.URLConnection#getContent()
0952: */
0953: public final Object getContent() throws java.io.IOException {
0954: return openConnection().getContent();
0955: }
0956:
0957: /**
0958: * Gets the contents of this URL. This method is a shorthand for:
0959: * <blockquote><pre>
0960: * openConnection().getContent(Class[])
0961: * </pre></blockquote>
0962: *
0963: * @param classes an array of Java types
0964: * @return the content object of this URL that is the first match of
0965: * the types specified in the classes array.
0966: * null if none of the requested types are supported.
0967: * @exception IOException if an I/O exception occurs.
0968: * @see java.net.URLConnection#getContent(Class[])
0969: * @since 1.3
0970: */
0971: public final Object getContent(Class[] classes)
0972: throws java.io.IOException {
0973: return openConnection().getContent(classes);
0974: }
0975:
0976: /**
0977: * The URLStreamHandler factory.
0978: */
0979: static URLStreamHandlerFactory factory;
0980:
0981: /**
0982: * Sets an application's <code>URLStreamHandlerFactory</code>.
0983: * This method can be called at most once in a given Java Virtual
0984: * Machine.
0985: *
0986: *<p> The <code>URLStreamHandlerFactory</code> instance is used to
0987: *construct a stream protocol handler from a protocol name.
0988: *
0989: * <p> If there is a security manager, this method first calls
0990: * the security manager's <code>checkSetFactory</code> method
0991: * to ensure the operation is allowed.
0992: * This could result in a SecurityException.
0993: *
0994: * @param fac the desired factory.
0995: * @exception Error if the application has already set a factory.
0996: * @exception SecurityException if a security manager exists and its
0997: * <code>checkSetFactory</code> method doesn't allow
0998: * the operation.
0999: * @see java.net.URL#URL(java.lang.String, java.lang.String,
1000: * int, java.lang.String)
1001: * @see java.net.URLStreamHandlerFactory
1002: * @see SecurityManager#checkSetFactory
1003: */
1004: public static void setURLStreamHandlerFactory(
1005: URLStreamHandlerFactory fac) {
1006: synchronized (streamHandlerLock) {
1007: if (factory != null) {
1008: throw new Error("factory already defined");
1009: }
1010: SecurityManager security = System.getSecurityManager();
1011: if (security != null) {
1012: security.checkSetFactory();
1013: }
1014: handlers.clear();
1015: factory = fac;
1016: }
1017: }
1018:
1019: /**
1020: * A table of protocol handlers.
1021: */
1022: static Hashtable handlers = new Hashtable();
1023: private static Object streamHandlerLock = new Object();
1024:
1025: /**
1026: * Returns the Stream Handler.
1027: * @param protocol the protocol to use
1028: */
1029: static URLStreamHandler getURLStreamHandler(String protocol) {
1030:
1031: URLStreamHandler handler = (URLStreamHandler) handlers
1032: .get(protocol);
1033: if (handler == null) {
1034:
1035: boolean checkedWithFactory = false;
1036:
1037: // Use the factory (if any)
1038: if (factory != null) {
1039: handler = factory.createURLStreamHandler(protocol);
1040: checkedWithFactory = true;
1041: }
1042:
1043: // Try java protocol handler
1044: if (handler == null) {
1045: String packagePrefixList = null;
1046:
1047: packagePrefixList = (String) java.security.AccessController
1048: .doPrivileged(new sun.security.action.GetPropertyAction(
1049: protocolPathProp, ""));
1050: if (packagePrefixList != "") {
1051: packagePrefixList += "|";
1052: }
1053:
1054: packagePrefixList += "sun.net.www.protocol";
1055:
1056: StringTokenizer packagePrefixIter = new StringTokenizer(
1057: packagePrefixList, "|");
1058:
1059: while (handler == null
1060: && packagePrefixIter.hasMoreTokens()) {
1061:
1062: String packagePrefix = packagePrefixIter
1063: .nextToken().trim();
1064: try {
1065: String clsName = packagePrefix + "." + protocol
1066: + ".Handler";
1067: Class cls = null;
1068: try {
1069: cls = Class.forName(clsName);
1070: } catch (ClassNotFoundException e) {
1071: ClassLoader cl = ClassLoader
1072: .getSystemClassLoader();
1073: if (cl != null) {
1074: cls = cl.loadClass(clsName);
1075: }
1076: }
1077: if (cls != null) {
1078: handler = (URLStreamHandler) cls
1079: .newInstance();
1080: }
1081: } catch (Exception e) {
1082: // any number of exceptions can get thrown here
1083: }
1084: }
1085: }
1086:
1087: synchronized (streamHandlerLock) {
1088:
1089: URLStreamHandler handler2 = null;
1090:
1091: // Check again with hashtable just in case another
1092: // thread created a handler since we last checked
1093: handler2 = (URLStreamHandler) handlers.get(protocol);
1094:
1095: if (handler2 != null) {
1096: return handler2;
1097: }
1098:
1099: // Check with factory if another thread set a
1100: // factory since our last check
1101: if (!checkedWithFactory && factory != null) {
1102: handler2 = factory.createURLStreamHandler(protocol);
1103: }
1104:
1105: if (handler2 != null) {
1106: // The handler from the factory must be given more
1107: // importance. Discard the default handler that
1108: // this thread created.
1109: handler = handler2;
1110: }
1111:
1112: // Insert this handler into the hashtable
1113: if (handler != null) {
1114: handlers.put(protocol, handler);
1115: }
1116:
1117: }
1118: }
1119:
1120: return handler;
1121:
1122: }
1123:
1124: /**
1125: * WriteObject is called to save the state of the URL to an
1126: * ObjectOutputStream. The handler is not saved since it is
1127: * specific to this system.
1128: *
1129: * @serialData the default write object value. When read back in,
1130: * the reader must ensure that calling getURLStreamHandler with
1131: * the protocol variable returns a valid URLStreamHandler and
1132: * throw an IOException if it does not.
1133: */
1134: private synchronized void writeObject(java.io.ObjectOutputStream s)
1135: throws IOException {
1136: s.defaultWriteObject(); // write the fields
1137: }
1138:
1139: /**
1140: * readObject is called to restore the state of the URL from the
1141: * stream. It reads the components of the URL and finds the local
1142: * stream handler.
1143: */
1144: private synchronized void readObject(java.io.ObjectInputStream s)
1145: throws IOException, ClassNotFoundException {
1146: s.defaultReadObject(); // read the fields
1147: if ((handler = getURLStreamHandler(protocol)) == null) {
1148: throw new IOException("unknown protocol: " + protocol);
1149: }
1150:
1151: // Construct authority part
1152: if (authority == null
1153: && ((host != null && host.length() > 0) || port != -1)) {
1154: if (host == null)
1155: host = "";
1156: authority = (port == -1) ? host : host + ":" + port;
1157:
1158: // Handle hosts with userInfo in them
1159: int at = host.lastIndexOf('@');
1160: if (at != -1) {
1161: userInfo = host.substring(0, at);
1162: host = host.substring(at + 1);
1163: }
1164: } else if (authority != null) {
1165: // Construct user info part
1166: int ind = authority.indexOf('@');
1167: if (ind != -1)
1168: userInfo = authority.substring(0, ind);
1169: }
1170:
1171: // Construct path and query part
1172: path = null;
1173: query = null;
1174: if (file != null) {
1175: // Fix: only do this if hierarchical?
1176: int q = file.lastIndexOf('?');
1177: if (q != -1) {
1178: query = file.substring(q + 1);
1179: path = file.substring(0, q);
1180: } else
1181: path = file;
1182: }
1183: }
1184: }
1185:
1186: class Parts {
1187: String path, query, ref;
1188:
1189: Parts(String file) {
1190: int ind = file.indexOf('#');
1191: ref = ind < 0 ? null : file.substring(ind + 1);
1192: file = ind < 0 ? file : file.substring(0, ind);
1193: int q = file.lastIndexOf('?');
1194: if (q != -1) {
1195: query = file.substring(q + 1);
1196: path = file.substring(0, q);
1197: } else {
1198: path = file;
1199: }
1200: }
1201:
1202: String getPath() {
1203: return path;
1204: }
1205:
1206: String getQuery() {
1207: return query;
1208: }
1209:
1210: String getRef() {
1211: return ref;
1212: }
1213: }
|