1 | /* URL.java -- Uniform Resource Locator Class
|
---|
2 | Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
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)
|
---|
9 | any later version.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
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
|
---|
24 | combination.
|
---|
25 |
|
---|
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. */
|
---|
37 |
|
---|
38 |
|
---|
39 | package java.net;
|
---|
40 |
|
---|
41 | import java.io.InputStream;
|
---|
42 | import java.io.IOException;
|
---|
43 | import java.io.Serializable;
|
---|
44 | import java.io.ObjectInputStream;
|
---|
45 | import java.io.ObjectOutputStream;
|
---|
46 | import java.util.Hashtable;
|
---|
47 | import java.util.StringTokenizer;
|
---|
48 |
|
---|
49 |
|
---|
50 | /*
|
---|
51 | * Written using on-line Java Platform 1.2 API Specification, as well
|
---|
52 | * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
|
---|
53 | * Status: Believed complete and correct.
|
---|
54 | */
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * This final class represents an Internet Uniform Resource Locator (URL).
|
---|
58 | * For details on the syntax of URL's and what they can be used for,
|
---|
59 | * refer to RFC 1738, available from <a
|
---|
60 | * href="http://ds.internic.net/rfcs/rfc1738.txt">http://ds.internic.net/rfcs/rfc1738.txt</a>
|
---|
61 | * <p>
|
---|
62 | * There are a great many protocols supported by URL's such as "http",
|
---|
63 | * "ftp", and "file". This object can handle any arbitrary URL for which
|
---|
64 | * a URLStreamHandler object can be written. Default protocol handlers
|
---|
65 | * are provided for the "http" and "ftp" protocols. Additional protocols
|
---|
66 | * handler implementations may be provided in the future. In any case,
|
---|
67 | * an application or applet can install its own protocol handlers that
|
---|
68 | * can be "chained" with other protocol hanlders in the system to extend
|
---|
69 | * the base functionality provided with this class. (Note, however, that
|
---|
70 | * unsigned applets cannot access properties by default or install their
|
---|
71 | * own protocol handlers).
|
---|
72 | * <p>
|
---|
73 | * This chaining is done via the system property java.protocol.handler.pkgs
|
---|
74 | * If this property is set, it is assumed to be a "|" separated list of
|
---|
75 | * package names in which to attempt locating protocol handlers. The
|
---|
76 | * protocol handler is searched for by appending the string
|
---|
77 | * ".<protocol>.Handler" to each packed in the list until a hander is found.
|
---|
78 | * If a protocol handler is not found in this list of packages, or if the
|
---|
79 | * property does not exist, then the default protocol handler of
|
---|
80 | * "gnu.java.net.<protocol>.Handler" is tried. If this is
|
---|
81 | * unsuccessful, a MalformedURLException is thrown.
|
---|
82 | * <p>
|
---|
83 | * All of the constructor methods of URL attempt to load a protocol
|
---|
84 | * handler and so any needed protocol handlers must be installed when
|
---|
85 | * the URL is constructed.
|
---|
86 | * <p>
|
---|
87 | * Here is an example of how URL searches for protocol handlers. Assume
|
---|
88 | * the value of java.protocol.handler.pkgs is "com.foo|com.bar" and the
|
---|
89 | * URL is "news://comp.lang.java.programmer". URL would looking the
|
---|
90 | * following places for protocol handlers:
|
---|
91 | * <p><pre>
|
---|
92 | * com.foo.news.Handler
|
---|
93 | * com.bar.news.Handler
|
---|
94 | * gnu.java.net.news.Handler
|
---|
95 | * </pre><p>
|
---|
96 | * If the protocol handler is not found in any of those locations, a
|
---|
97 | * MalformedURLException would be thrown.
|
---|
98 | * <p>
|
---|
99 | * Please note that a protocol handler must be a subclass of
|
---|
100 | * URLStreamHandler.
|
---|
101 | *
|
---|
102 | * @author Aaron M. Renn (arenn@urbanophile.com)
|
---|
103 | * @author Warren Levy <warrenl@cygnus.com>
|
---|
104 | *
|
---|
105 | * @see URLStreamHandler
|
---|
106 | */
|
---|
107 | public final class URL implements Serializable
|
---|
108 | {
|
---|
109 | /**
|
---|
110 | * The name of the protocol for this URL.
|
---|
111 | * The protocol is always stored in lower case.
|
---|
112 | */
|
---|
113 | private String protocol;
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * The "authority" portion of the URL.
|
---|
117 | */
|
---|
118 | private String authority;
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * The hostname or IP address of this protocol.
|
---|
122 | * This includes a possible user. For example <code>joe@some.host.net</code>.
|
---|
123 | */
|
---|
124 | private String host;
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * The port number of this protocol or -1 if the port number used is
|
---|
128 | * the default for this protocol.
|
---|
129 | */
|
---|
130 | private int port = -1; // Initialize for constructor using context.
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * The "file" portion of the URL. It is defined as <code>path[?query]</code>.
|
---|
134 | */
|
---|
135 | private String file;
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * The anchor portion of the URL.
|
---|
139 | */
|
---|
140 | private String ref;
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * This is the hashCode for this URL
|
---|
144 | */
|
---|
145 | private int hashCode = 0;
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * The protocol handler in use for this URL
|
---|
149 | */
|
---|
150 | transient private URLStreamHandler handler;
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * This a table where we cache protocol handlers to avoid the overhead
|
---|
154 | * of looking them up each time.
|
---|
155 | */
|
---|
156 | private static Hashtable handlers = new Hashtable();
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * If an application installs its own protocol handler factory, this is
|
---|
160 | * where we keep track of it.
|
---|
161 | */
|
---|
162 | private static URLStreamHandlerFactory factory;
|
---|
163 |
|
---|
164 | private static final long serialVersionUID = -7627629688361524110L;
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Constructs a URL and loads a protocol handler for the values passed as
|
---|
168 | * arguments.
|
---|
169 | *
|
---|
170 | * @param protocol The protocol for this URL ("http", "ftp", etc)
|
---|
171 | * @param host The hostname or IP address to connect to
|
---|
172 | * @param port The port number to use, or -1 to use the protocol's
|
---|
173 | * default port
|
---|
174 | * @param file The "file" portion of the URL.
|
---|
175 | *
|
---|
176 | * @exception MalformedURLException If a protocol handler cannot be loaded or
|
---|
177 | * a parse error occurs.
|
---|
178 | */
|
---|
179 | public URL(String protocol, String host, int port, String file)
|
---|
180 | throws MalformedURLException
|
---|
181 | {
|
---|
182 | this(protocol, host, port, file, null);
|
---|
183 | }
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Constructs a URL and loads a protocol handler for the values passed in
|
---|
187 | * as arugments. Uses the default port for the protocol.
|
---|
188 | *
|
---|
189 | * @param protocol The protocol for this URL ("http", "ftp", etc)
|
---|
190 | * @param host The hostname or IP address for this URL
|
---|
191 | * @param file The "file" portion of this URL.
|
---|
192 | *
|
---|
193 | * @exception MalformedURLException If a protocol handler cannot be loaded or
|
---|
194 | * a parse error occurs.
|
---|
195 | */
|
---|
196 | public URL(String protocol, String host, String file)
|
---|
197 | throws MalformedURLException
|
---|
198 | {
|
---|
199 | this(protocol, host, -1, file, null);
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * This method initializes a new instance of <code>URL</code> with the
|
---|
205 | * specified protocol, host, port, and file. Additionally, this method
|
---|
206 | * allows the caller to specify a protocol handler to use instead of
|
---|
207 | * the default. If this handler is specified, the caller must have
|
---|
208 | * the "specifyStreamHandler" permission (see <code>NetPermission</code>)
|
---|
209 | * or a <code>SecurityException</code> will be thrown.
|
---|
210 | *
|
---|
211 | * @param protocol The protocol for this URL ("http", "ftp", etc)
|
---|
212 | * @param host The hostname or IP address to connect to
|
---|
213 | * @param port The port number to use, or -1 to use the protocol's default
|
---|
214 | * port
|
---|
215 | * @param file The "file" portion of the URL.
|
---|
216 | * @param handler The protocol handler to use with this URL.
|
---|
217 | *
|
---|
218 | * @exception MalformedURLException If no protocol handler can be loaded
|
---|
219 | * for the specified protocol.
|
---|
220 | * @exception SecurityException If the <code>SecurityManager</code> exists
|
---|
221 | * and does not allow the caller to specify its own protocol handler.
|
---|
222 | *
|
---|
223 | * @since 1.2
|
---|
224 | */
|
---|
225 | public URL(String protocol, String host, int port, String file,
|
---|
226 | URLStreamHandler handler) throws MalformedURLException
|
---|
227 | {
|
---|
228 | if (protocol == null)
|
---|
229 | throw new MalformedURLException("null protocol");
|
---|
230 | this.protocol = protocol.toLowerCase();
|
---|
231 |
|
---|
232 | if (handler != null)
|
---|
233 | {
|
---|
234 | SecurityManager s = System.getSecurityManager();
|
---|
235 | if (s != null)
|
---|
236 | s.checkPermission (new NetPermission ("specifyStreamHandler"));
|
---|
237 |
|
---|
238 | this.handler = handler;
|
---|
239 | }
|
---|
240 | else
|
---|
241 | this.handler = getURLStreamHandler(protocol);
|
---|
242 |
|
---|
243 | if (this.handler == null)
|
---|
244 | throw new MalformedURLException (
|
---|
245 | "Protocol handler not found: " + protocol);
|
---|
246 |
|
---|
247 | this.host = host;
|
---|
248 | this.port = port;
|
---|
249 | this.authority = null;
|
---|
250 |
|
---|
251 | int hashAt = file.indexOf('#');
|
---|
252 | if (hashAt < 0)
|
---|
253 | {
|
---|
254 | this.file = file;
|
---|
255 | this.ref = null;
|
---|
256 | }
|
---|
257 | else
|
---|
258 | {
|
---|
259 | this.file = file.substring(0, hashAt);
|
---|
260 | this.ref = file.substring(hashAt + 1);
|
---|
261 | }
|
---|
262 | hashCode = hashCode(); // Used for serialization.
|
---|
263 | }
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Initializes a URL from a complete string specification such as
|
---|
267 | * "http://www.urbanophile.com/arenn/". First the protocol name is parsed
|
---|
268 | * out of the string. Then a handler is located for that protocol and
|
---|
269 | * the parseURL() method of that protocol handler is used to parse the
|
---|
270 | * remaining fields.
|
---|
271 | *
|
---|
272 | * @param spec The complete String representation of a URL
|
---|
273 | *
|
---|
274 | * @exception MalformedURLException If a protocol handler cannot be found
|
---|
275 | * or the URL cannot be parsed
|
---|
276 | */
|
---|
277 | public URL(String spec) throws MalformedURLException
|
---|
278 | {
|
---|
279 | this((URL) null, spec, (URLStreamHandler) null);
|
---|
280 | }
|
---|
281 |
|
---|
282 | /*
|
---|
283 | * This method parses a String representation of a URL within the
|
---|
284 | * context of an existing URL. Principally this means that any
|
---|
285 | * fields not present the URL are inheritied from the context URL.
|
---|
286 | * This allows relative URL's to be easily constructed. If the
|
---|
287 | * context argument is null, then a complete URL must be specified
|
---|
288 | * in the URL string. If the protocol parsed out of the URL is
|
---|
289 | * different from the context URL's protocol, then then URL String
|
---|
290 | * is also expected to be a complete URL.
|
---|
291 | *
|
---|
292 | * @param context The context on which to parse the specification
|
---|
293 | * @param spec The string to parse an URL
|
---|
294 | *
|
---|
295 | * @exception MalformedURLException If a protocol handler cannot be found
|
---|
296 | * for the URL cannot be parsed
|
---|
297 | */
|
---|
298 | public URL(URL context, String spec) throws MalformedURLException
|
---|
299 | {
|
---|
300 | this(context, spec, (URLStreamHandler) null);
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Creates an URL from given arguments
|
---|
305 | * This method parses a String representation of a URL within the
|
---|
306 | * context of an existing URL. Principally this means that any fields
|
---|
307 | * not present the URL are inheritied from the context URL. This allows
|
---|
308 | * relative URL's to be easily constructed. If the context argument is
|
---|
309 | * null, then a complete URL must be specified in the URL string.
|
---|
310 | * If the protocol parsed out of the URL is different
|
---|
311 | * from the context URL's protocol, then then URL String is also
|
---|
312 | * expected to be a complete URL.
|
---|
313 | * <p>
|
---|
314 | * Additionally, this method allows the caller to specify a protocol handler
|
---|
315 | * to use instead of the default. If this handler is specified, the caller
|
---|
316 | * must have the "specifyStreamHandler" permission
|
---|
317 | * (see <code>NetPermission</code>) or a <code>SecurityException</code>
|
---|
318 | * will be thrown.
|
---|
319 | *
|
---|
320 | * @param context The context in which to parse the specification
|
---|
321 | * @param spec The string to parse as an URL
|
---|
322 | * @param handler The stream handler for the URL
|
---|
323 | *
|
---|
324 | * @exception MalformedURLException If a protocol handler cannot be found
|
---|
325 | * or the URL cannot be parsed
|
---|
326 | * @exception SecurityException If the <code>SecurityManager</code> exists
|
---|
327 | * and does not allow the caller to specify its own protocol handler.
|
---|
328 | *
|
---|
329 | * @since 1.2
|
---|
330 | */
|
---|
331 | public URL(URL context, String spec, URLStreamHandler handler)
|
---|
332 | throws MalformedURLException
|
---|
333 | {
|
---|
334 | /* A protocol is defined by the doc as the substring before a ':'
|
---|
335 | * as long as the ':' occurs before any '/'.
|
---|
336 | *
|
---|
337 | * If context is null, then spec must be an absolute URL.
|
---|
338 | *
|
---|
339 | * The relative URL need not specify all the components of a URL.
|
---|
340 | * If the protocol, host name, or port number is missing, the value
|
---|
341 | * is inherited from the context. A bare file component is appended
|
---|
342 | * to the context's file. The optional anchor is not inherited.
|
---|
343 | */
|
---|
344 |
|
---|
345 | // If this is an absolute URL, then ignore context completely.
|
---|
346 | // An absolute URL must have chars prior to "://" but cannot have a colon
|
---|
347 | // right after the "://". The second colon is for an optional port value
|
---|
348 | // and implies that the host from the context is used if available.
|
---|
349 | int colon;
|
---|
350 | if ((colon = spec.indexOf("://", 1)) > 0 &&
|
---|
351 | ! spec.regionMatches(colon, "://:", 0, 4))
|
---|
352 | context = null;
|
---|
353 |
|
---|
354 | int slash;
|
---|
355 | if ((colon = spec.indexOf(':')) > 0 &&
|
---|
356 | (colon < (slash = spec.indexOf('/')) || slash < 0))
|
---|
357 | {
|
---|
358 | // Protocol specified in spec string.
|
---|
359 | protocol = spec.substring(0, colon).toLowerCase();
|
---|
360 | if (context != null && context.protocol.equals(protocol))
|
---|
361 | {
|
---|
362 | // The 1.2 doc specifically says these are copied to the new URL.
|
---|
363 | host = context.host;
|
---|
364 | port = context.port;
|
---|
365 | file = context.file;
|
---|
366 | authority = context.authority;
|
---|
367 | }
|
---|
368 | }
|
---|
369 | else if (context != null)
|
---|
370 | {
|
---|
371 | // Protocol NOT specified in spec string.
|
---|
372 | // Use context fields (except ref) as a foundation for relative URLs.
|
---|
373 | colon = -1;
|
---|
374 | protocol = context.protocol;
|
---|
375 | host = context.host;
|
---|
376 | port = context.port;
|
---|
377 | file = context.file;
|
---|
378 | authority = context.authority;
|
---|
379 | }
|
---|
380 | else // Protocol NOT specified in spec. and no context available.
|
---|
381 | throw new
|
---|
382 | MalformedURLException("Absolute URL required with null context");
|
---|
383 |
|
---|
384 | if (handler != null)
|
---|
385 | {
|
---|
386 | SecurityManager s = System.getSecurityManager ();
|
---|
387 | if (s != null)
|
---|
388 | s.checkPermission (new NetPermission ("specifyStreamHandler"));
|
---|
389 |
|
---|
390 | this.handler = handler;
|
---|
391 | }
|
---|
392 | else
|
---|
393 | this.handler = getURLStreamHandler(protocol);
|
---|
394 |
|
---|
395 | if (this.handler == null)
|
---|
396 | throw new MalformedURLException("Protocol handler not found: "
|
---|
397 | + protocol);
|
---|
398 |
|
---|
399 | // JDK 1.2 doc for parseURL specifically states that any '#' ref
|
---|
400 | // is to be excluded by passing the 'limit' as the indexOf the '#'
|
---|
401 | // if one exists, otherwise pass the end of the string.
|
---|
402 | int hashAt = spec.indexOf('#', colon + 1);
|
---|
403 | this.handler.parseURL(this, spec, colon + 1,
|
---|
404 | hashAt < 0 ? spec.length() : hashAt);
|
---|
405 | if (hashAt >= 0)
|
---|
406 | ref = spec.substring(hashAt + 1);
|
---|
407 |
|
---|
408 | hashCode = hashCode(); // Used for serialization.
|
---|
409 | }
|
---|
410 |
|
---|
411 | /**
|
---|
412 | * Test another URL for equality with this one. This will be true only if
|
---|
413 | * the argument is non-null and all of the fields in the URL's match
|
---|
414 | * exactly (ie, protocol, host, port, file, and ref). Overrides
|
---|
415 | * Object.equals(), implemented by calling the equals method of the handler.
|
---|
416 | *
|
---|
417 | * @param url The URL to compare with
|
---|
418 | *
|
---|
419 | * @return true if the URL is equal, false otherwise
|
---|
420 | */
|
---|
421 | public boolean equals(Object obj)
|
---|
422 | {
|
---|
423 | if (obj == null || ! (obj instanceof URL))
|
---|
424 | return false;
|
---|
425 |
|
---|
426 | URL uObj = (URL) obj;
|
---|
427 |
|
---|
428 | return handler.equals (this, uObj);
|
---|
429 | }
|
---|
430 |
|
---|
431 | /**
|
---|
432 | * Returns the contents of this URL as an object by first opening a
|
---|
433 | * connection, then calling the getContent() method against the connection
|
---|
434 | *
|
---|
435 | * @return A content object for this URL
|
---|
436 | * @exception IOException If opening the connection or getting the
|
---|
437 | * content fails.
|
---|
438 | *
|
---|
439 | * @since 1.3
|
---|
440 | */
|
---|
441 | public final Object getContent() throws IOException
|
---|
442 | {
|
---|
443 | return openConnection().getContent();
|
---|
444 | }
|
---|
445 |
|
---|
446 | /**
|
---|
447 | * Gets the contents of this URL
|
---|
448 | *
|
---|
449 | * @exception IOException If an error occurs
|
---|
450 | */
|
---|
451 | public final Object getContent (Class[] classes) throws IOException
|
---|
452 | {
|
---|
453 | // FIXME: implement this
|
---|
454 | return getContent();
|
---|
455 | }
|
---|
456 |
|
---|
457 | /**
|
---|
458 | * Returns the file portion of the URL.
|
---|
459 | * Defined as <code>path[?query]</code>.
|
---|
460 | * Returns the empty string if there is no file portion.
|
---|
461 | */
|
---|
462 | public String getFile()
|
---|
463 | {
|
---|
464 | return file == null ? "" : file;
|
---|
465 | }
|
---|
466 |
|
---|
467 | /**
|
---|
468 | * Returns the path of the URL. This is the part of the file before any '?'
|
---|
469 | * character.
|
---|
470 | *
|
---|
471 | * @since 1.3
|
---|
472 | */
|
---|
473 | public String getPath()
|
---|
474 | {
|
---|
475 | int quest = file.indexOf('?');
|
---|
476 | return quest < 0 ? file : file.substring(0, quest);
|
---|
477 | }
|
---|
478 |
|
---|
479 | /**
|
---|
480 | * Returns the authority of the URL
|
---|
481 | *
|
---|
482 | * @since 1.3
|
---|
483 | */
|
---|
484 | public String getAuthority()
|
---|
485 | {
|
---|
486 | return authority;
|
---|
487 | }
|
---|
488 |
|
---|
489 | /**
|
---|
490 | * Returns the host of the URL
|
---|
491 | */
|
---|
492 | public String getHost()
|
---|
493 | {
|
---|
494 | int at = (host == null) ? -1 : host.indexOf('@');
|
---|
495 | return at < 0 ? host : host.substring(at + 1, host.length());
|
---|
496 | }
|
---|
497 |
|
---|
498 | /**
|
---|
499 | * Returns the port number of this URL or -1 if the default port number is
|
---|
500 | * being used.
|
---|
501 | *
|
---|
502 | * @return The port number
|
---|
503 | *
|
---|
504 | * @see #getDefaultPort()
|
---|
505 | */
|
---|
506 | public int getPort()
|
---|
507 | {
|
---|
508 | return port;
|
---|
509 | }
|
---|
510 |
|
---|
511 | /**
|
---|
512 | * Returns the default port of the URL. If the StreamHandler for the URL
|
---|
513 | * protocol does not define a default port it returns -1.
|
---|
514 | */
|
---|
515 | public int getDefaultPort()
|
---|
516 | {
|
---|
517 | return handler.getDefaultPort();
|
---|
518 | }
|
---|
519 |
|
---|
520 | /**
|
---|
521 | * Returns the protocol of the URL
|
---|
522 | */
|
---|
523 | public String getProtocol()
|
---|
524 | {
|
---|
525 | return protocol;
|
---|
526 | }
|
---|
527 |
|
---|
528 | /**
|
---|
529 | * Returns the ref (sometimes called the "# reference" or "anchor") portion
|
---|
530 | * of the URL.
|
---|
531 | *
|
---|
532 | * @return The ref
|
---|
533 | */
|
---|
534 | public String getRef()
|
---|
535 | {
|
---|
536 | return ref;
|
---|
537 | }
|
---|
538 |
|
---|
539 | /**
|
---|
540 | * Returns the user information of the URL. This is the part of the host
|
---|
541 | * name before the '@'.
|
---|
542 | *
|
---|
543 | * @return the user at a particular host or null when no user defined.
|
---|
544 | */
|
---|
545 | public String getUserInfo ()
|
---|
546 | {
|
---|
547 | int at = host.indexOf('@');
|
---|
548 | return at < 0 ? null : host.substring(0, at);
|
---|
549 | }
|
---|
550 |
|
---|
551 | /**
|
---|
552 | * Returns the query of the URL. This is the part of the file before the
|
---|
553 | * '?'.
|
---|
554 | *
|
---|
555 | * @ return the query part of the file, or null when there is no query part.
|
---|
556 | */
|
---|
557 | public String getQuery ()
|
---|
558 | {
|
---|
559 | int quest = file.indexOf('?');
|
---|
560 | return quest < 0 ? null : file.substring(quest + 1, file.length());
|
---|
561 | }
|
---|
562 |
|
---|
563 | /**
|
---|
564 | * Returns a hashcode computed by the URLStreamHandler of this URL
|
---|
565 | */
|
---|
566 | public int hashCode()
|
---|
567 | {
|
---|
568 | if (hashCode != 0)
|
---|
569 | return hashCode; // Use cached value if available.
|
---|
570 | else
|
---|
571 | return handler.hashCode (this);
|
---|
572 | }
|
---|
573 |
|
---|
574 | /**
|
---|
575 | * Returns a URLConnection object that represents a connection to the remote
|
---|
576 | * object referred to by the URL. The URLConnection is created by calling the
|
---|
577 | * openConnection() method of the protocol handler
|
---|
578 | *
|
---|
579 | * @return A URLConnection for this URL
|
---|
580 | * @exception IOException If an error occurs
|
---|
581 | */
|
---|
582 | public URLConnection openConnection() throws IOException
|
---|
583 | {
|
---|
584 | return handler.openConnection(this);
|
---|
585 | }
|
---|
586 |
|
---|
587 | /**
|
---|
588 | * Opens a connection to this URL and returns an InputStream for reading
|
---|
589 | * from that connection
|
---|
590 | *
|
---|
591 | * @exception IOException If an error occurs
|
---|
592 | */
|
---|
593 | public final InputStream openStream() throws IOException
|
---|
594 | {
|
---|
595 | return openConnection().getInputStream();
|
---|
596 | }
|
---|
597 |
|
---|
598 | /**
|
---|
599 | * Tests whether or not another URL refers to the same "file" as this one.
|
---|
600 | * This will be true if and only if the passed object is not null, is a
|
---|
601 | * URL, and matches all fields but the ref (ie, protocol, host, port,
|
---|
602 | * and file);
|
---|
603 | *
|
---|
604 | * @param url The URL object to test with
|
---|
605 | *
|
---|
606 | * @return true if URL matches this URL's file, false otherwise
|
---|
607 | */
|
---|
608 | public boolean sameFile(URL other)
|
---|
609 | {
|
---|
610 | return handler.sameFile(this, other);
|
---|
611 | }
|
---|
612 |
|
---|
613 | /**
|
---|
614 | * Sets the specified fields of the URL. This is not a public method so
|
---|
615 | * that only URLStreamHandlers can modify URL fields. This might be called
|
---|
616 | * by the <code>parseURL()</code> method in that class. URLs are otherwise
|
---|
617 | * constant.
|
---|
618 | *
|
---|
619 | * @param protocol The protocol name for this URL
|
---|
620 | * @param host The hostname or IP address for this URL
|
---|
621 | * @param port The port number of this URL
|
---|
622 | * @param file The "file" portion of this URL.
|
---|
623 | * @param ref The anchor portion of this URL.
|
---|
624 | */
|
---|
625 | protected void set(String protocol, String host, int port, String file,
|
---|
626 | String ref)
|
---|
627 | {
|
---|
628 | // TBD: Theoretically, a poorly written StreamHandler could pass an
|
---|
629 | // invalid protocol. It will cause the handler to be set to null
|
---|
630 | // thus overriding a valid handler. Callers of this method should
|
---|
631 | // be aware of this.
|
---|
632 | this.handler = getURLStreamHandler(protocol);
|
---|
633 | this.protocol = protocol.toLowerCase();
|
---|
634 | this.authority = null;
|
---|
635 | this.port = port;
|
---|
636 | this.host = host;
|
---|
637 | this.file = file;
|
---|
638 | this.ref = ref;
|
---|
639 | hashCode = hashCode(); // Used for serialization.
|
---|
640 | }
|
---|
641 |
|
---|
642 | /**
|
---|
643 | * Sets the specified fields of the URL. This is not a public method so
|
---|
644 | * that only URLStreamHandlers can modify URL fields. URLs are otherwise
|
---|
645 | * constant.
|
---|
646 | *
|
---|
647 | * @since 1.3
|
---|
648 | */
|
---|
649 | protected void set(String protocol, String host, int port,
|
---|
650 | String authority, String userInfo,
|
---|
651 | String path, String query, String ref)
|
---|
652 | {
|
---|
653 | // TBD: Theoretically, a poorly written StreamHandler could pass an
|
---|
654 | // invalid protocol. It will cause the handler to be set to null
|
---|
655 | // thus overriding a valid handler. Callers of this method should
|
---|
656 | // be aware of this.
|
---|
657 | this.handler = getURLStreamHandler(protocol);
|
---|
658 | this.protocol = protocol.toLowerCase();
|
---|
659 | if (userInfo == null)
|
---|
660 | this.host = host;
|
---|
661 | else
|
---|
662 | this.host = userInfo + "@" + host;
|
---|
663 | this.port = port;
|
---|
664 | if (query == null)
|
---|
665 | this.file = path;
|
---|
666 | else
|
---|
667 | this.file = path + "?" + query;
|
---|
668 | this.ref = ref;
|
---|
669 | hashCode = hashCode(); // Used for serialization.
|
---|
670 | }
|
---|
671 |
|
---|
672 | /**
|
---|
673 | * Sets the URLStreamHandlerFactory for this class. This factory is
|
---|
674 | * responsible for returning the appropriate protocol handler for
|
---|
675 | * a given URL.
|
---|
676 | *
|
---|
677 | * @param fac The URLStreamHandlerFactory class to use
|
---|
678 | *
|
---|
679 | * @exception Error If the factory is alread set.
|
---|
680 | * @exception SecurityException If a security manager exists and its
|
---|
681 | * checkSetFactory method doesn't allow the operation
|
---|
682 | */
|
---|
683 | public static synchronized void
|
---|
684 | setURLStreamHandlerFactory(URLStreamHandlerFactory fac)
|
---|
685 | {
|
---|
686 | if (factory != null)
|
---|
687 | throw new Error("URLStreamHandlerFactory already set");
|
---|
688 |
|
---|
689 | // Throw an exception if an extant security mgr precludes
|
---|
690 | // setting the factory.
|
---|
691 | SecurityManager s = System.getSecurityManager();
|
---|
692 | if (s != null)
|
---|
693 | s.checkSetFactory();
|
---|
694 | factory = fac;
|
---|
695 | }
|
---|
696 |
|
---|
697 | /**
|
---|
698 | * Returns a String representing this URL. The String returned is
|
---|
699 | * created by calling the protocol handler's toExternalForm() method.
|
---|
700 | *
|
---|
701 | * @return A string for this URL
|
---|
702 | */
|
---|
703 | public String toExternalForm()
|
---|
704 | {
|
---|
705 | // Identical to toString().
|
---|
706 | return handler.toExternalForm(this);
|
---|
707 | }
|
---|
708 |
|
---|
709 | /**
|
---|
710 | * Returns a String representing this URL. Identical to toExternalForm().
|
---|
711 | * The value returned is created by the protocol handler's
|
---|
712 | * toExternalForm method. Overrides Object.toString()
|
---|
713 | *
|
---|
714 | * @return A string for this URL
|
---|
715 | */
|
---|
716 | public String toString()
|
---|
717 | {
|
---|
718 | // Identical to toExternalForm().
|
---|
719 | return handler.toExternalForm(this);
|
---|
720 | }
|
---|
721 |
|
---|
722 | private static synchronized URLStreamHandler
|
---|
723 | getURLStreamHandler(String protocol)
|
---|
724 | {
|
---|
725 | URLStreamHandler handler;
|
---|
726 |
|
---|
727 | // See if a handler has been cached for this protocol.
|
---|
728 | if ((handler = (URLStreamHandler) handlers.get(protocol)) != null)
|
---|
729 | return handler;
|
---|
730 |
|
---|
731 | // If a non-default factory has been set, use it to find the protocol.
|
---|
732 | if (factory != null)
|
---|
733 | handler = factory.createURLStreamHandler(protocol);
|
---|
734 | else if (protocol.equals ("core"))
|
---|
735 | {
|
---|
736 | handler = new gnu.gcj.protocol.core.Handler ();
|
---|
737 | }
|
---|
738 | else if (protocol.equals ("file"))
|
---|
739 | {
|
---|
740 | // This is an interesting case. It's tempting to think that we
|
---|
741 | // could call Class.forName ("gnu.gcj.protocol.file.Handler") to
|
---|
742 | // get the appropriate class. Unfortunately, if we do that the
|
---|
743 | // program will never terminate, because getURLStreamHandler is
|
---|
744 | // eventually called by Class.forName.
|
---|
745 | //
|
---|
746 | // Treating "file" as a special case is the minimum that will
|
---|
747 | // fix this problem. If other protocols are required in a
|
---|
748 | // statically linked application they will need to be handled in
|
---|
749 | // the same way as "file".
|
---|
750 | handler = new gnu.gcj.protocol.file.Handler ();
|
---|
751 | }
|
---|
752 |
|
---|
753 | // Non-default factory may have returned null or a factory wasn't set.
|
---|
754 | // Use the default search algorithm to find a handler for this protocol.
|
---|
755 | if (handler == null)
|
---|
756 | {
|
---|
757 | // Get the list of packages to check and append our default handler
|
---|
758 | // to it, along with the JDK specified default as a last resort.
|
---|
759 | // Except in very unusual environments the JDK specified one shouldn't
|
---|
760 | // ever be needed (or available).
|
---|
761 | String propVal = System.getProperty("java.protocol.handler.pkgs");
|
---|
762 | propVal = (propVal == null) ? "" : (propVal + "|");
|
---|
763 | propVal = propVal + "gnu.gcj.protocol|sun.net.www.protocol";
|
---|
764 |
|
---|
765 | StringTokenizer pkgPrefix = new StringTokenizer(propVal, "|");
|
---|
766 | do
|
---|
767 | {
|
---|
768 | String facName = pkgPrefix.nextToken() + "." + protocol +
|
---|
769 | ".Handler";
|
---|
770 | try
|
---|
771 | {
|
---|
772 | handler =
|
---|
773 | (URLStreamHandler) Class.forName(facName).newInstance();
|
---|
774 | }
|
---|
775 | catch (Exception e)
|
---|
776 | {
|
---|
777 | // Can't instantiate; handler still null, go on to next element.
|
---|
778 | }
|
---|
779 | } while ((handler == null ||
|
---|
780 | ! (handler instanceof URLStreamHandler)) &&
|
---|
781 | pkgPrefix.hasMoreTokens());
|
---|
782 | }
|
---|
783 |
|
---|
784 | // Update the hashtable with the new protocol handler.
|
---|
785 | if (handler != null)
|
---|
786 | if (handler instanceof URLStreamHandler)
|
---|
787 | handlers.put(protocol, handler);
|
---|
788 | else
|
---|
789 | handler = null;
|
---|
790 |
|
---|
791 | return handler;
|
---|
792 | }
|
---|
793 |
|
---|
794 | private void readObject(ObjectInputStream ois)
|
---|
795 | throws IOException, ClassNotFoundException
|
---|
796 | {
|
---|
797 | ois.defaultReadObject();
|
---|
798 | this.handler = getURLStreamHandler(protocol);
|
---|
799 | if (this.handler == null)
|
---|
800 | throw new IOException("Handler for protocol " + protocol + " not found");
|
---|
801 | }
|
---|
802 |
|
---|
803 | private void writeObject(ObjectOutputStream oos) throws IOException
|
---|
804 | {
|
---|
805 | oos.defaultWriteObject();
|
---|
806 | }
|
---|
807 | }
|
---|