2 * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
30 * The abstract class <code>URLStreamHandler</code> is the common
31 * superclass for all stream protocol handlers. A stream protocol
32 * handler knows how to make a connection for a particular protocol
33 * type, such as <code>http</code>, <code>ftp</code>, or
34 * <code>gopher</code>.
36 * In most cases, an instance of a <code>URLStreamHandler</code>
37 * subclass is not created directly by an application. Rather, the
38 * first time a protocol name is encountered when constructing a
39 * <code>URL</code>, the appropriate stream protocol handler is
40 * automatically loaded.
42 * @author James Gosling
43 * @see java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
46 public abstract class URLStreamHandler {
48 * Opens a connection to the object referenced by the
49 * <code>URL</code> argument.
50 * This method should be overridden by a subclass.
52 * <p>If for the handler's protocol (such as HTTP or JAR), there
53 * exists a public, specialized URLConnection subclass belonging
54 * to one of the following packages or one of their subpackages:
55 * java.lang, java.io, java.util, java.net, the connection
56 * returned will be of that subclass. For example, for HTTP an
57 * HttpURLConnection will be returned, and for JAR a
58 * JarURLConnection will be returned.
60 * @param u the URL that this connects to.
61 * @return a <code>URLConnection</code> object for the <code>URL</code>.
62 * @exception IOException if an I/O error occurs while opening the
65 // abstract protected URLConnection openConnection(URL u) throws IOException;
68 * Same as openConnection(URL), except that the connection will be
69 * made through the specified proxy; Protocol handlers that do not
70 * support proxying will ignore the proxy parameter and make a
73 * Calling this method preempts the system's default ProxySelector
76 * @param u the URL that this connects to.
77 * @param p the proxy through which the connection will be made.
78 * If direct connection is desired, Proxy.NO_PROXY
79 * should be specified.
80 * @return a <code>URLConnection</code> object for the <code>URL</code>.
81 * @exception IOException if an I/O error occurs while opening the
83 * @exception IllegalArgumentException if either u or p is null,
84 * or p has the wrong type.
85 * @exception UnsupportedOperationException if the subclass that
86 * implements the protocol doesn't support this method.
89 // protected URLConnection openConnection(URL u, Proxy p) throws IOException {
90 // throw new UnsupportedOperationException("Method not implemented.");
94 * Parses the string representation of a <code>URL</code> into a
95 * <code>URL</code> object.
97 * If there is any inherited context, then it has already been
98 * copied into the <code>URL</code> argument.
100 * The <code>parseURL</code> method of <code>URLStreamHandler</code>
101 * parses the string representation as if it were an
102 * <code>http</code> specification. Most URL protocol families have a
103 * similar parsing. A stream protocol handler for a protocol that has
104 * a different syntax must override this routine.
106 * @param u the <code>URL</code> to receive the result of parsing
108 * @param spec the <code>String</code> representing the URL that
110 * @param start the character index at which to begin parsing. This is
111 * just past the '<code>:</code>' (if there is one) that
112 * specifies the determination of the protocol name.
113 * @param limit the character position to stop parsing at. This is the
114 * end of the string or the position of the
115 * "<code>#</code>" character, if present. All information
116 * after the sharp sign indicates an anchor.
118 protected void parseURL(URL u, String spec, int start, int limit) {
119 // These fields may receive context content if this was relative URL
120 String protocol = u.getProtocol();
121 String authority = u.getAuthority();
122 String userInfo = u.getUserInfo();
123 String host = u.getHost();
124 int port = u.getPort();
125 String path = u.getPath();
126 String query = u.getQuery();
128 // This field has already been parsed
129 String ref = u.getRef();
131 boolean isRelPath = false;
132 boolean queryOnly = false;
134 // FIX: should not assume query if opaque
135 // Strip off the query part
137 int queryStart = spec.indexOf('?');
138 queryOnly = queryStart == start;
139 if ((queryStart != -1) && (queryStart < limit)) {
140 query = spec.substring(queryStart+1, limit);
141 if (limit > queryStart)
143 spec = spec.substring(0, queryStart);
148 // Parse the authority part if any
149 boolean isUNCName = (start <= limit - 4) &&
150 (spec.charAt(start) == '/') &&
151 (spec.charAt(start + 1) == '/') &&
152 (spec.charAt(start + 2) == '/') &&
153 (spec.charAt(start + 3) == '/');
154 if (!isUNCName && (start <= limit - 2) && (spec.charAt(start) == '/') &&
155 (spec.charAt(start + 1) == '/')) {
157 i = spec.indexOf('/', start);
159 i = spec.indexOf('?', start);
164 host = authority = spec.substring(start, i);
166 int ind = authority.indexOf('@');
168 userInfo = authority.substring(0, ind);
169 host = authority.substring(ind+1);
174 // If the host is surrounded by [ and ] then its an IPv6
175 // literal address as specified in RFC2732
176 if (host.length()>0 && (host.charAt(0) == '[')) {
177 if ((ind = host.indexOf(']')) > 2) {
179 String nhost = host ;
180 host = nhost.substring(0,ind+1);
181 // if (!IPAddressUtil.
182 // isIPv6LiteralAddress(host.substring(1, ind))) {
183 // throw new IllegalArgumentException(
184 // "Invalid host: "+ host);
188 if (nhost.length() > ind+1) {
189 if (nhost.charAt(ind+1) == ':') {
191 // port can be null according to RFC2396
192 if (nhost.length() > (ind + 1)) {
193 port = Integer.parseInt(nhost.substring(ind+1));
196 throw new IllegalArgumentException(
197 "Invalid authority field: " + authority);
201 throw new IllegalArgumentException(
202 "Invalid authority field: " + authority);
205 ind = host.indexOf(':');
208 // port can be null according to RFC2396
209 if (host.length() > (ind + 1)) {
210 port = Integer.parseInt(host.substring(ind + 1));
212 host = host.substring(0, ind);
219 throw new IllegalArgumentException("Invalid port number :" +
222 // If the authority is defined then the path is defined by the
223 // spec only; See RFC 2396 Section 5.2.4.
224 if (authority != null && authority.length() > 0)
232 // Parse the file path if any
234 if (spec.charAt(start) == '/') {
235 path = spec.substring(start, limit);
236 } else if (path != null && path.length() > 0) {
238 int ind = path.lastIndexOf('/');
239 String seperator = "";
240 if (ind == -1 && authority != null)
242 path = path.substring(0, ind + 1) + seperator +
243 spec.substring(start, limit);
246 String seperator = (authority != null) ? "/" : "";
247 path = seperator + spec.substring(start, limit);
249 } else if (queryOnly && path != null) {
250 int ind = path.lastIndexOf('/');
253 path = path.substring(0, ind) + "/";
259 // Remove embedded /./
260 while ((i = path.indexOf("/./")) >= 0) {
261 path = path.substring(0, i) + path.substring(i + 2);
263 // Remove embedded /../ if possible
265 while ((i = path.indexOf("/../", i)) >= 0) {
267 * A "/../" will cancel the previous segment and itself,
268 * unless that segment is a "/../" itself
269 * i.e. "/a/b/../c" becomes "/a/c"
270 * but "/../../a" should stay unchanged
272 if (i > 0 && (limit = path.lastIndexOf('/', i - 1)) >= 0 &&
273 (path.indexOf("/../", limit) != 0)) {
274 path = path.substring(0, limit) + path.substring(i + 3);
280 // Remove trailing .. if possible
281 while (path.endsWith("/..")) {
282 i = path.indexOf("/..");
283 if ((limit = path.lastIndexOf('/', i - 1)) >= 0) {
284 path = path.substring(0, limit+1);
290 if (path.startsWith("./") && path.length() > 2)
291 path = path.substring(2);
294 if (path.endsWith("/."))
295 path = path.substring(0, path.length() -1);
298 setURL(u, protocol, host, port, authority, userInfo, path, query, ref);
302 * Returns the default port for a URL parsed by this handler. This method
303 * is meant to be overidden by handlers with default port numbers.
304 * @return the default port for a <code>URL</code> parsed by this handler.
307 protected int getDefaultPort() {
312 * Provides the default equals calculation. May be overidden by handlers
313 * for other protocols that have different requirements for equals().
314 * This method requires that none of its arguments is null. This is
315 * guaranteed by the fact that it is only called by java.net.URL class.
316 * @param u1 a URL object
317 * @param u2 a URL object
318 * @return <tt>true</tt> if the two urls are
319 * considered equal, ie. they refer to the same
320 * fragment in the same file.
323 protected boolean equals(URL u1, URL u2) {
324 String ref1 = u1.getRef();
325 String ref2 = u2.getRef();
326 return (ref1 == ref2 || (ref1 != null && ref1.equals(ref2))) &&
331 * Provides the default hash calculation. May be overidden by handlers for
332 * other protocols that have different requirements for hashCode
334 * @param u a URL object
335 * @return an <tt>int</tt> suitable for hash table indexing
338 protected int hashCode(URL u) {
341 // Generate the protocol part.
342 String protocol = u.getProtocol();
343 if (protocol != null)
344 h += protocol.hashCode();
346 // Generate the host part.
347 Object addr = getHostAddress(u);
349 h += addr.hashCode();
351 String host = u.getHost();
353 h += host.toLowerCase().hashCode();
356 // Generate the file part.
357 String file = u.getFile();
359 h += file.hashCode();
361 // Generate the port part.
362 if (u.getPort() == -1)
363 h += getDefaultPort();
367 // Generate the ref part.
368 String ref = u.getRef();
376 * Compare two urls to see whether they refer to the same file,
377 * i.e., having the same protocol, host, port, and path.
378 * This method requires that none of its arguments is null. This is
379 * guaranteed by the fact that it is only called indirectly
380 * by java.net.URL class.
381 * @param u1 a URL object
382 * @param u2 a URL object
383 * @return true if u1 and u2 refer to the same file
386 protected boolean sameFile(URL u1, URL u2) {
387 // Compare the protocols.
388 if (!((u1.getProtocol() == u2.getProtocol()) ||
389 (u1.getProtocol() != null &&
390 u1.getProtocol().equalsIgnoreCase(u2.getProtocol()))))
393 // Compare the files.
394 if (!(u1.getFile() == u2.getFile() ||
395 (u1.getFile() != null && u1.getFile().equals(u2.getFile()))))
398 // Compare the ports.
400 port1 = (u1.getPort() != -1) ? u1.getPort() : u1.handler.getDefaultPort();
401 port2 = (u2.getPort() != -1) ? u2.getPort() : u2.handler.getDefaultPort();
405 // Compare the hosts.
406 if (!hostsEqual(u1, u2))
413 * Get the IP address of our host. An empty host field or a DNS failure
414 * will result in a null return.
416 * @param u a URL object
417 * @return an <code>InetAddress</code> representing the host
421 private synchronized Object getHostAddress(URL u) {
422 return u.hostAddress;
426 * Compares the host components of two URLs.
427 * @param u1 the URL of the first host to compare
428 * @param u2 the URL of the second host to compare
429 * @return <tt>true</tt> if and only if they
430 * are equal, <tt>false</tt> otherwise.
433 protected boolean hostsEqual(URL u1, URL u2) {
434 Object a1 = getHostAddress(u1);
435 Object a2 = getHostAddress(u2);
436 // if we have internet address for both, compare them
437 if (a1 != null && a2 != null) {
438 return a1.equals(a2);
439 // else, if both have host names, compare them
440 } else if (u1.getHost() != null && u2.getHost() != null)
441 return u1.getHost().equalsIgnoreCase(u2.getHost());
443 return u1.getHost() == null && u2.getHost() == null;
447 * Converts a <code>URL</code> of a specific protocol to a
448 * <code>String</code>.
451 * @return a string representation of the <code>URL</code> argument.
453 protected String toExternalForm(URL u) {
455 // pre-compute length of StringBuffer
456 int len = u.getProtocol().length() + 1;
457 if (u.getAuthority() != null && u.getAuthority().length() > 0)
458 len += 2 + u.getAuthority().length();
459 if (u.getPath() != null) {
460 len += u.getPath().length();
462 if (u.getQuery() != null) {
463 len += 1 + u.getQuery().length();
465 if (u.getRef() != null)
466 len += 1 + u.getRef().length();
468 StringBuffer result = new StringBuffer(len);
469 result.append(u.getProtocol());
471 if (u.getAuthority() != null && u.getAuthority().length() > 0) {
473 result.append(u.getAuthority());
475 if (u.getPath() != null) {
476 result.append(u.getPath());
478 if (u.getQuery() != null) {
480 result.append(u.getQuery());
482 if (u.getRef() != null) {
484 result.append(u.getRef());
486 return result.toString();
490 * Sets the fields of the <code>URL</code> argument to the indicated values.
491 * Only classes derived from URLStreamHandler are supposed to be able
492 * to call the set method on a URL.
494 * @param u the URL to modify.
495 * @param protocol the protocol name.
496 * @param host the remote host value for the URL.
497 * @param port the port on the remote machine.
498 * @param authority the authority part for the URL.
499 * @param userInfo the userInfo part of the URL.
500 * @param path the path component of the URL.
501 * @param query the query part for the URL.
502 * @param ref the reference.
503 * @exception SecurityException if the protocol handler of the URL is
504 * different from this one
505 * @see java.net.URL#set(java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)
508 protected void setURL(URL u, String protocol, String host, int port,
509 String authority, String userInfo, String path,
510 String query, String ref) {
511 if (this != u.handler) {
512 throw new SecurityException("handler for url different from " +
515 // ensure that no one can reset the protocol on a given URL.
516 u.set(u.getProtocol(), host, port, authority, userInfo, path, query, ref);
520 * Sets the fields of the <code>URL</code> argument to the indicated values.
521 * Only classes derived from URLStreamHandler are supposed to be able
522 * to call the set method on a URL.
524 * @param u the URL to modify.
525 * @param protocol the protocol name. This value is ignored since 1.2.
526 * @param host the remote host value for the URL.
527 * @param port the port on the remote machine.
528 * @param file the file.
529 * @param ref the reference.
530 * @exception SecurityException if the protocol handler of the URL is
531 * different from this one
532 * @deprecated Use setURL(URL, String, String, int, String, String, String,
536 protected void setURL(URL u, String protocol, String host, int port,
537 String file, String ref) {
539 * Only old URL handlers call this, so assume that the host
540 * field might contain "user:passwd@host". Fix as necessary.
542 String authority = null;
543 String userInfo = null;
544 if (host != null && host.length() != 0) {
545 authority = (port == -1) ? host : host + ":" + port;
546 int at = host.lastIndexOf('@');
548 userInfo = host.substring(0, at);
549 host = host.substring(at+1);
554 * Assume file might contain query part. Fix as necessary.
559 int q = file.lastIndexOf('?');
561 query = file.substring(q+1);
562 path = file.substring(0, q);
566 setURL(u, protocol, host, port, authority, userInfo, path, query, ref);