1 /* URLStreamHandler.java -- Abstract superclass for all protocol handlers
2 Copyright (C) 1998, 1999, 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.io.IOException;
45 * Written using on-line Java Platform 1.2 API Specification, as well
46 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
47 * Status: Believed complete and correct.
51 * This class is the superclass of all URL protocol handlers. The URL
52 * class loads the appropriate protocol handler to establish a connection
53 * to a (possibly) remote service (eg, "http", "ftp") and to do protocol
54 * specific parsing of URL's. Refer to the URL class documentation for
55 * details on how that class locates and loads protocol handlers.
57 * A protocol handler implementation should override the openConnection()
58 * method, and optionally override the parseURL() and toExternalForm()
59 * methods if necessary. (The default implementations will parse/write all
60 * URL's in the same form as http URL's). A protocol specific subclass
61 * of URLConnection will most likely need to be created as well.
63 * Note that the instance methods in this class are called as if they
64 * were static methods. That is, a URL object to act on is passed with
65 * every call rather than the caller assuming the URL is stored in an
66 * instance variable of the "this" object.
68 * The methods in this class are protected and accessible only to subclasses.
69 * URLStreamConnection objects are intended for use by the URL class only,
70 * not by other classes (unless those classes are implementing protocols).
72 * @author Aaron M. Renn (arenn@urbanophile.com)
73 * @author Warren Levy (warrenl@cygnus.com)
77 public abstract class URLStreamHandler
80 * Creates a URLStreamHander
82 public URLStreamHandler ()
87 * Returns a URLConnection for the passed in URL. Note that this should
88 * not actually create the connection to the (possibly) remote host, but
89 * rather simply return a URLConnection object. The connect() method of
90 * URL connection is used to establish the actual connection, possibly
91 * after the caller sets up various connection options.
93 * @param url The URL to get a connection object for
95 * @return A URLConnection object for the given URL
97 * @exception IOException If an error occurs
99 protected abstract URLConnection openConnection(URL u)
103 * This method parses the string passed in as a URL and set's the
104 * instance data fields in the URL object passed in to the various values
105 * parsed out of the string. The start parameter is the position to start
106 * scanning the string. This is usually the position after the ":" which
107 * terminates the protocol name. The end parameter is the position to
108 * stop scanning. This will be either the end of the String, or the
109 * position of the "#" character, which separates the "file" portion of
110 * the URL from the "anchor" portion.
112 * This method assumes URL's are formatted like http protocol URL's, so
113 * subclasses that implement protocols with URL's the follow a different
114 * syntax should override this method. The lone exception is that if
115 * the protocol name set in the URL is "file", this method will accept
116 * an empty hostname (i.e., "file:///"), which is legal for that protocol
118 * @param url The URL object in which to store the results
119 * @param spec The String-ized URL to parse
120 * @param start The position in the string to start scanning from
121 * @param end The position in the string to stop scanning
123 protected void parseURL(URL url, String spec, int start, int end)
125 String host = url.getHost();
126 int port = url.getPort();
127 String file = url.getFile();
128 String ref = url.getRef();
130 if (spec.regionMatches (start, "//", 0, 2))
136 int slash = spec.indexOf('/', start);
142 host = spec.substring (start, hostEnd);
144 // Look for optional port number. It is valid for the non-port
145 // part of the host name to be null (e.g. a URL "http://:80").
146 // TBD: JDK 1.2 in this case sets host to null rather than "";
147 // this is undocumented and likely an unintended side effect in 1.2
148 // so we'll be simple here and stick with "". Note that
149 // "http://" or "http:///" produce a "" host in JDK 1.2.
150 if ((colon = host.indexOf(':')) >= 0)
154 port = Integer.parseInt(host.substring(colon + 1));
156 catch (NumberFormatException e)
158 ; // Ignore invalid port values; port is already set to u's
161 host = host.substring(0, colon);
166 else if (host == null)
169 if (file == null || file.length() == 0
170 || (start < end && spec.charAt(start) == '/'))
172 // No file context available; just spec for file.
173 // Or this is an absolute path name; ignore any file context.
174 file = spec.substring(start, end);
177 else if (start < end)
179 // Context is available, but only override it if there is a new file.
181 int lastSlash = file.lastIndexOf (sepChar);
182 if (lastSlash < 0 && File.separatorChar != sepChar
183 && url.getProtocol ().equals ("file"))
185 // On Windows, even '\' is allowed in a "file" URL.
186 sepChar = File.separatorChar;
187 lastSlash = file.lastIndexOf (sepChar);
190 file = file.substring(0, lastSlash)
191 + sepChar + spec.substring (start, end);
193 if (url.getProtocol ().equals ("file"))
195 // For "file" URLs constructed relative to a context, we
196 // need to canonicalise the file path.
199 boolean endsWithSlash = file.charAt(file.length() - 1) == '/';
200 file = new File (file).getCanonicalPath ();
202 && file.charAt(file.length() - 1) != '/')
205 catch (IOException e)
215 // Normally there should be no '#' in the file part,
217 int hash = file.indexOf('#');
220 ref = file.substring(hash + 1, file.length());
221 file = file.substring(0, hash);
225 // XXX - Classpath used to call PlatformHelper.toCanonicalForm() on
226 // the file part. It seems like overhead, but supposedly there is some
227 // benefit in windows based systems (it also lowercased the string).
229 setURL(url, url.getProtocol(), host, port, file, ref);
232 private static String canonicalizeFilename(String file)
234 // XXX - GNU Classpath has an implementation that might be more appropriate
235 // for Windows based systems (gnu.java.io.PlatformHelper.toCanonicalForm)
239 // Replace "/./" with "/". This probably isn't very efficient in
240 // the general case, but it's probably not bad most of the time.
241 while ((index = file.indexOf("/./")) >= 0)
242 file = file.substring(0, index) + file.substring(index + 2);
244 // Process "/../" correctly. This probably isn't very efficient in
245 // the general case, but it's probably not bad most of the time.
246 while ((index = file.indexOf("/../")) >= 0)
248 // Strip of the previous directory - if it exists.
249 int previous = file.lastIndexOf('/', index - 1);
251 file = file.substring(0, previous) + file.substring(index + 3);
259 * Compares two URLs, excluding the fragment component
261 * @param url1 The first url
262 * @param url2 The second url to compare with the first
264 * @specnote Now protected
266 protected boolean sameFile(URL url1, URL url2)
270 // This comparison is very conservative. It assumes that any
271 // field can be null.
272 if (url1 == null || url2 == null)
274 int p1 = url1.getPort ();
276 p1 = url1.ph.getDefaultPort ();
277 int p2 = url2.getPort ();
279 p2 = url2.ph.getDefaultPort ();
283 s1 = url1.getProtocol();
284 s2 = url2.getProtocol();
285 if (s1 != s2 && (s1 == null || ! s1.equals(s2)))
289 if (s1 != s2 && (s1 == null || ! s1.equals(s2)))
291 s1 = canonicalizeFilename(url1.getFile());
292 s2 = canonicalizeFilename(url2.getFile());
293 if (s1 != s2 && (s1 == null || ! s1.equals(s2)))
299 * This methods sets the instance variables representing the various fields
300 * of the URL to the values passed in.
302 * @param u The URL to modify
303 * @param protocol The protocol to set
304 * @param host The host name to et
305 * @param port The port number to set
306 * @param file The filename to set
307 * @param ref The reference
309 * @exception SecurityException If the protocol handler of the URL is
310 * different from this one
312 * @deprecated 1.2 Please use
313 * #setURL(URL,String,String,int,String,String,String,String);
315 protected void setURL(URL u, String protocol, String host, int port,
316 String file, String ref)
318 u.set(protocol, host, port, file, ref);
322 * Sets the fields of the URL argument to the indicated values
324 * @param u The URL to modify
325 * @param protocol The protocol to set
326 * @param host The host name to set
327 * @param port The port number to set
328 * @param authority The authority to set
329 * @param userInfo The user information to set
330 * @param path The path/filename to set
331 * @param query The query part to set
332 * @param ref The reference
334 * @exception SecurityException If the protocol handler of the URL is
335 * different from this one
337 protected void setURL(URL u, String protocol, String host, int port,
338 String authority, String userInfo, String path,
339 String query, String ref)
341 u.set(protocol, host, port, authority, userInfo, path, query, ref);
345 * Provides the default equals calculation. May be overidden by handlers for
346 * other protocols that have different requirements for equals(). This method
347 * requires that none of its arguments is null. This is guaranteed by the
348 * fact that it is only called by java.net.URL class.
350 * @param url1 An URL object
351 * @param url2 An URL object
353 protected boolean equals (URL url1, URL url2)
355 // This comparison is very conservative. It assumes that any
356 // field can be null.
357 return (url1.getPort () == url2.getPort ()
358 && ((url1.getProtocol () == null && url2.getProtocol () == null)
359 || (url1.getProtocol () != null
360 && url1.getProtocol ().equals (url2.getProtocol ())))
361 && ((url1.getUserInfo () == null && url2.getUserInfo () == null)
362 || (url1.getUserInfo () != null
363 && url1.getUserInfo ().equals(url2.getUserInfo ())))
364 && ((url1.getAuthority () == null && url2.getAuthority () == null)
365 || (url1.getAuthority () != null
366 && url1.getAuthority ().equals(url2.getAuthority ())))
367 && ((url1.getHost () == null && url2.getHost () == null)
368 || (url1.getHost () != null
369 && url1.getHost ().equals(url2.getHost ())))
370 && ((url1.getPath () == null && url2.getPath () == null)
371 || (url1.getPath () != null
372 && url1.getPath ().equals (url2.getPath ())))
373 && ((url1.getQuery () == null && url2.getQuery () == null)
374 || (url1.getQuery () != null
375 && url1.getQuery ().equals(url2.getQuery ())))
376 && ((url1.getRef () == null && url2.getRef () == null)
377 || (url1.getRef () != null
378 && url1.getRef ().equals(url2.getRef ()))));
382 * Compares the host components of two URLs.
384 * @exception UnknownHostException If an unknown host is found
386 protected boolean hostsEqual (URL url1, URL url2)
388 InetAddress addr1 = getHostAddress (url1);
389 InetAddress addr2 = getHostAddress (url2);
391 if (addr1 != null || addr2 != null)
392 return addr1.equals (addr2);
394 String host1 = url1.getHost();
395 String host2 = url2.getHost();
397 if (host1 != null && host2 != null)
398 return host1.equalsIgnoreCase (host2);
400 return host1 == null && host2 == null;
404 * Get the IP address of our host. An empty host field or a DNS failure will
405 * result in a null return.
407 protected InetAddress getHostAddress (URL url)
409 String hostname = url.getHost ();
416 return InetAddress.getByName (hostname);
418 catch (UnknownHostException e)
425 * Returns the default port for a URL parsed by this handler. This method is
426 * meant to be overidden by handlers with default port numbers.
428 protected int getDefaultPort ()
434 * Provides the default hash calculation. May be overidden by handlers for
435 * other protocols that have different requirements for hashCode calculation.
437 protected int hashCode (URL url)
439 return url.getProtocol ().hashCode () +
440 ((url.getHost () == null) ? 0 : url.getHost ().hashCode ()) +
441 url.getFile ().hashCode() +
446 * This method converts a URL object into a String. This method creates
447 * Strings in the mold of http URL's, so protocol handlers which use URL's
448 * that have a different syntax should override this method
450 * @param url The URL object to convert
452 protected String toExternalForm(URL u)
454 String protocol, host, file, ref;
457 protocol = u.getProtocol();
459 // JDK 1.2 online doc infers that host could be null because it
460 // explicitly states that file cannot be null, but is silent on host.
469 // Guess a reasonable size for the string buffer so we have to resize
471 int size = protocol.length() + host.length() + file.length() + 24;
472 StringBuffer sb = new StringBuffer(size);
474 if (protocol != null && protocol.length() > 0)
480 if (host.length() != 0)
481 sb.append("//").append(host);
483 // Append port if port was in URL spec.
485 sb.append(':').append(port);
490 sb.append('#').append(ref);
492 return sb.toString();