rt/emul/mini/src/main/java/java/net/URLStreamHandler.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 554 emul/mini/src/main/java/java/net/URLStreamHandler.java@05224402145d
child 1398 9926996eca2d
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
jaroslav@327
     1
/*
jaroslav@327
     2
 * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
jaroslav@327
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@327
     4
 *
jaroslav@327
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@327
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@327
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@327
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@327
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@327
    10
 *
jaroslav@327
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@327
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@327
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@327
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@327
    15
 * accompanied this code).
jaroslav@327
    16
 *
jaroslav@327
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@327
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@327
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@327
    20
 *
jaroslav@327
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@327
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@327
    23
 * questions.
jaroslav@327
    24
 */
jaroslav@327
    25
jaroslav@327
    26
package java.net;
jaroslav@327
    27
jaroslav@327
    28
jaroslav@327
    29
/**
jaroslav@327
    30
 * The abstract class <code>URLStreamHandler</code> is the common
jaroslav@327
    31
 * superclass for all stream protocol handlers. A stream protocol
jaroslav@327
    32
 * handler knows how to make a connection for a particular protocol
jaroslav@327
    33
 * type, such as <code>http</code>, <code>ftp</code>, or
jaroslav@327
    34
 * <code>gopher</code>.
jaroslav@327
    35
 * <p>
jaroslav@327
    36
 * In most cases, an instance of a <code>URLStreamHandler</code>
jaroslav@327
    37
 * subclass is not created directly by an application. Rather, the
jaroslav@327
    38
 * first time a protocol name is encountered when constructing a
jaroslav@327
    39
 * <code>URL</code>, the appropriate stream protocol handler is
jaroslav@327
    40
 * automatically loaded.
jaroslav@327
    41
 *
jaroslav@327
    42
 * @author  James Gosling
jaroslav@327
    43
 * @see     java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
jaroslav@327
    44
 * @since   JDK1.0
jaroslav@327
    45
 */
jaroslav@327
    46
public abstract class URLStreamHandler {
jaroslav@327
    47
    /**
jaroslav@327
    48
     * Opens a connection to the object referenced by the
jaroslav@327
    49
     * <code>URL</code> argument.
jaroslav@327
    50
     * This method should be overridden by a subclass.
jaroslav@327
    51
     *
jaroslav@327
    52
     * <p>If for the handler's protocol (such as HTTP or JAR), there
jaroslav@327
    53
     * exists a public, specialized URLConnection subclass belonging
jaroslav@327
    54
     * to one of the following packages or one of their subpackages:
jaroslav@327
    55
     * java.lang, java.io, java.util, java.net, the connection
jaroslav@327
    56
     * returned will be of that subclass. For example, for HTTP an
jaroslav@327
    57
     * HttpURLConnection will be returned, and for JAR a
jaroslav@327
    58
     * JarURLConnection will be returned.
jaroslav@327
    59
     *
jaroslav@327
    60
     * @param      u   the URL that this connects to.
jaroslav@327
    61
     * @return     a <code>URLConnection</code> object for the <code>URL</code>.
jaroslav@327
    62
     * @exception  IOException  if an I/O error occurs while opening the
jaroslav@327
    63
     *               connection.
jaroslav@327
    64
     */
jaroslav@339
    65
//    abstract protected URLConnection openConnection(URL u) throws IOException;
jaroslav@327
    66
jaroslav@327
    67
    /**
jaroslav@327
    68
     * Same as openConnection(URL), except that the connection will be
jaroslav@327
    69
     * made through the specified proxy; Protocol handlers that do not
jaroslav@327
    70
     * support proxying will ignore the proxy parameter and make a
jaroslav@327
    71
     * normal connection.
jaroslav@327
    72
     *
jaroslav@327
    73
     * Calling this method preempts the system's default ProxySelector
jaroslav@327
    74
     * settings.
jaroslav@327
    75
     *
jaroslav@327
    76
     * @param      u   the URL that this connects to.
jaroslav@327
    77
     * @param      p   the proxy through which the connection will be made.
jaroslav@327
    78
     *                 If direct connection is desired, Proxy.NO_PROXY
jaroslav@327
    79
     *                 should be specified.
jaroslav@327
    80
     * @return     a <code>URLConnection</code> object for the <code>URL</code>.
jaroslav@327
    81
     * @exception  IOException  if an I/O error occurs while opening the
jaroslav@327
    82
     *               connection.
jaroslav@327
    83
     * @exception  IllegalArgumentException if either u or p is null,
jaroslav@327
    84
     *               or p has the wrong type.
jaroslav@327
    85
     * @exception  UnsupportedOperationException if the subclass that
jaroslav@327
    86
     *               implements the protocol doesn't support this method.
jaroslav@327
    87
     * @since      1.5
jaroslav@327
    88
     */
jaroslav@339
    89
//    protected URLConnection openConnection(URL u, Proxy p) throws IOException {
jaroslav@339
    90
//        throw new UnsupportedOperationException("Method not implemented.");
jaroslav@339
    91
//    }
jaroslav@327
    92
jaroslav@327
    93
    /**
jaroslav@327
    94
     * Parses the string representation of a <code>URL</code> into a
jaroslav@327
    95
     * <code>URL</code> object.
jaroslav@327
    96
     * <p>
jaroslav@327
    97
     * If there is any inherited context, then it has already been
jaroslav@327
    98
     * copied into the <code>URL</code> argument.
jaroslav@327
    99
     * <p>
jaroslav@327
   100
     * The <code>parseURL</code> method of <code>URLStreamHandler</code>
jaroslav@327
   101
     * parses the string representation as if it were an
jaroslav@327
   102
     * <code>http</code> specification. Most URL protocol families have a
jaroslav@327
   103
     * similar parsing. A stream protocol handler for a protocol that has
jaroslav@327
   104
     * a different syntax must override this routine.
jaroslav@327
   105
     *
jaroslav@327
   106
     * @param   u       the <code>URL</code> to receive the result of parsing
jaroslav@327
   107
     *                  the spec.
jaroslav@327
   108
     * @param   spec    the <code>String</code> representing the URL that
jaroslav@327
   109
     *                  must be parsed.
jaroslav@327
   110
     * @param   start   the character index at which to begin parsing. This is
jaroslav@327
   111
     *                  just past the '<code>:</code>' (if there is one) that
jaroslav@327
   112
     *                  specifies the determination of the protocol name.
jaroslav@327
   113
     * @param   limit   the character position to stop parsing at. This is the
jaroslav@327
   114
     *                  end of the string or the position of the
jaroslav@327
   115
     *                  "<code>#</code>" character, if present. All information
jaroslav@327
   116
     *                  after the sharp sign indicates an anchor.
jaroslav@327
   117
     */
jaroslav@327
   118
    protected void parseURL(URL u, String spec, int start, int limit) {
jaroslav@327
   119
        // These fields may receive context content if this was relative URL
jaroslav@327
   120
        String protocol = u.getProtocol();
jaroslav@327
   121
        String authority = u.getAuthority();
jaroslav@327
   122
        String userInfo = u.getUserInfo();
jaroslav@327
   123
        String host = u.getHost();
jaroslav@327
   124
        int port = u.getPort();
jaroslav@327
   125
        String path = u.getPath();
jaroslav@327
   126
        String query = u.getQuery();
jaroslav@327
   127
jaroslav@327
   128
        // This field has already been parsed
jaroslav@327
   129
        String ref = u.getRef();
jaroslav@327
   130
jaroslav@327
   131
        boolean isRelPath = false;
jaroslav@327
   132
        boolean queryOnly = false;
jaroslav@327
   133
jaroslav@327
   134
// FIX: should not assume query if opaque
jaroslav@327
   135
        // Strip off the query part
jaroslav@327
   136
        if (start < limit) {
jaroslav@327
   137
            int queryStart = spec.indexOf('?');
jaroslav@327
   138
            queryOnly = queryStart == start;
jaroslav@327
   139
            if ((queryStart != -1) && (queryStart < limit)) {
jaroslav@327
   140
                query = spec.substring(queryStart+1, limit);
jaroslav@327
   141
                if (limit > queryStart)
jaroslav@327
   142
                    limit = queryStart;
jaroslav@327
   143
                spec = spec.substring(0, queryStart);
jaroslav@327
   144
            }
jaroslav@327
   145
        }
jaroslav@327
   146
jaroslav@327
   147
        int i = 0;
jaroslav@327
   148
        // Parse the authority part if any
jaroslav@327
   149
        boolean isUNCName = (start <= limit - 4) &&
jaroslav@327
   150
                        (spec.charAt(start) == '/') &&
jaroslav@327
   151
                        (spec.charAt(start + 1) == '/') &&
jaroslav@327
   152
                        (spec.charAt(start + 2) == '/') &&
jaroslav@327
   153
                        (spec.charAt(start + 3) == '/');
jaroslav@327
   154
        if (!isUNCName && (start <= limit - 2) && (spec.charAt(start) == '/') &&
jaroslav@327
   155
            (spec.charAt(start + 1) == '/')) {
jaroslav@327
   156
            start += 2;
jaroslav@327
   157
            i = spec.indexOf('/', start);
jaroslav@327
   158
            if (i < 0) {
jaroslav@327
   159
                i = spec.indexOf('?', start);
jaroslav@327
   160
                if (i < 0)
jaroslav@327
   161
                    i = limit;
jaroslav@327
   162
            }
jaroslav@327
   163
jaroslav@327
   164
            host = authority = spec.substring(start, i);
jaroslav@327
   165
jaroslav@327
   166
            int ind = authority.indexOf('@');
jaroslav@327
   167
            if (ind != -1) {
jaroslav@327
   168
                userInfo = authority.substring(0, ind);
jaroslav@327
   169
                host = authority.substring(ind+1);
jaroslav@327
   170
            } else {
jaroslav@327
   171
                userInfo = null;
jaroslav@327
   172
            }
jaroslav@327
   173
            if (host != null) {
jaroslav@327
   174
                // If the host is surrounded by [ and ] then its an IPv6
jaroslav@327
   175
                // literal address as specified in RFC2732
jaroslav@327
   176
                if (host.length()>0 && (host.charAt(0) == '[')) {
jaroslav@327
   177
                    if ((ind = host.indexOf(']')) > 2) {
jaroslav@327
   178
jaroslav@327
   179
                        String nhost = host ;
jaroslav@327
   180
                        host = nhost.substring(0,ind+1);
jaroslav@339
   181
//                        if (!IPAddressUtil.
jaroslav@339
   182
//                            isIPv6LiteralAddress(host.substring(1, ind))) {
jaroslav@339
   183
//                            throw new IllegalArgumentException(
jaroslav@339
   184
//                                "Invalid host: "+ host);
jaroslav@339
   185
//                        }
jaroslav@327
   186
jaroslav@327
   187
                        port = -1 ;
jaroslav@327
   188
                        if (nhost.length() > ind+1) {
jaroslav@327
   189
                            if (nhost.charAt(ind+1) == ':') {
jaroslav@327
   190
                                ++ind ;
jaroslav@327
   191
                                // port can be null according to RFC2396
jaroslav@327
   192
                                if (nhost.length() > (ind + 1)) {
jaroslav@327
   193
                                    port = Integer.parseInt(nhost.substring(ind+1));
jaroslav@327
   194
                                }
jaroslav@327
   195
                            } else {
jaroslav@327
   196
                                throw new IllegalArgumentException(
jaroslav@327
   197
                                    "Invalid authority field: " + authority);
jaroslav@327
   198
                            }
jaroslav@327
   199
                        }
jaroslav@327
   200
                    } else {
jaroslav@327
   201
                        throw new IllegalArgumentException(
jaroslav@327
   202
                            "Invalid authority field: " + authority);
jaroslav@327
   203
                    }
jaroslav@327
   204
                } else {
jaroslav@327
   205
                    ind = host.indexOf(':');
jaroslav@327
   206
                    port = -1;
jaroslav@327
   207
                    if (ind >= 0) {
jaroslav@327
   208
                        // port can be null according to RFC2396
jaroslav@327
   209
                        if (host.length() > (ind + 1)) {
jaroslav@327
   210
                            port = Integer.parseInt(host.substring(ind + 1));
jaroslav@327
   211
                        }
jaroslav@327
   212
                        host = host.substring(0, ind);
jaroslav@327
   213
                    }
jaroslav@327
   214
                }
jaroslav@327
   215
            } else {
jaroslav@327
   216
                host = "";
jaroslav@327
   217
            }
jaroslav@327
   218
            if (port < -1)
jaroslav@327
   219
                throw new IllegalArgumentException("Invalid port number :" +
jaroslav@327
   220
                                                   port);
jaroslav@327
   221
            start = i;
jaroslav@327
   222
            // If the authority is defined then the path is defined by the
jaroslav@327
   223
            // spec only; See RFC 2396 Section 5.2.4.
jaroslav@327
   224
            if (authority != null && authority.length() > 0)
jaroslav@327
   225
                path = "";
jaroslav@327
   226
        }
jaroslav@327
   227
jaroslav@327
   228
        if (host == null) {
jaroslav@327
   229
            host = "";
jaroslav@327
   230
        }
jaroslav@327
   231
jaroslav@327
   232
        // Parse the file path if any
jaroslav@327
   233
        if (start < limit) {
jaroslav@327
   234
            if (spec.charAt(start) == '/') {
jaroslav@327
   235
                path = spec.substring(start, limit);
jaroslav@327
   236
            } else if (path != null && path.length() > 0) {
jaroslav@327
   237
                isRelPath = true;
jaroslav@327
   238
                int ind = path.lastIndexOf('/');
jaroslav@327
   239
                String seperator = "";
jaroslav@327
   240
                if (ind == -1 && authority != null)
jaroslav@327
   241
                    seperator = "/";
jaroslav@327
   242
                path = path.substring(0, ind + 1) + seperator +
jaroslav@327
   243
                         spec.substring(start, limit);
jaroslav@327
   244
jaroslav@327
   245
            } else {
jaroslav@327
   246
                String seperator = (authority != null) ? "/" : "";
jaroslav@327
   247
                path = seperator + spec.substring(start, limit);
jaroslav@327
   248
            }
jaroslav@327
   249
        } else if (queryOnly && path != null) {
jaroslav@327
   250
            int ind = path.lastIndexOf('/');
jaroslav@327
   251
            if (ind < 0)
jaroslav@327
   252
                ind = 0;
jaroslav@327
   253
            path = path.substring(0, ind) + "/";
jaroslav@327
   254
        }
jaroslav@327
   255
        if (path == null)
jaroslav@327
   256
            path = "";
jaroslav@327
   257
jaroslav@327
   258
        if (isRelPath) {
jaroslav@327
   259
            // Remove embedded /./
jaroslav@327
   260
            while ((i = path.indexOf("/./")) >= 0) {
jaroslav@327
   261
                path = path.substring(0, i) + path.substring(i + 2);
jaroslav@327
   262
            }
jaroslav@327
   263
            // Remove embedded /../ if possible
jaroslav@327
   264
            i = 0;
jaroslav@327
   265
            while ((i = path.indexOf("/../", i)) >= 0) {
jaroslav@327
   266
                /*
jaroslav@327
   267
                 * A "/../" will cancel the previous segment and itself,
jaroslav@327
   268
                 * unless that segment is a "/../" itself
jaroslav@327
   269
                 * i.e. "/a/b/../c" becomes "/a/c"
jaroslav@327
   270
                 * but "/../../a" should stay unchanged
jaroslav@327
   271
                 */
jaroslav@327
   272
                if (i > 0 && (limit = path.lastIndexOf('/', i - 1)) >= 0 &&
jaroslav@327
   273
                    (path.indexOf("/../", limit) != 0)) {
jaroslav@327
   274
                    path = path.substring(0, limit) + path.substring(i + 3);
jaroslav@327
   275
                    i = 0;
jaroslav@327
   276
                } else {
jaroslav@327
   277
                    i = i + 3;
jaroslav@327
   278
                }
jaroslav@327
   279
            }
jaroslav@327
   280
            // Remove trailing .. if possible
jaroslav@327
   281
            while (path.endsWith("/..")) {
jaroslav@327
   282
                i = path.indexOf("/..");
jaroslav@327
   283
                if ((limit = path.lastIndexOf('/', i - 1)) >= 0) {
jaroslav@327
   284
                    path = path.substring(0, limit+1);
jaroslav@327
   285
                } else {
jaroslav@327
   286
                    break;
jaroslav@327
   287
                }
jaroslav@327
   288
            }
jaroslav@327
   289
            // Remove starting .
jaroslav@327
   290
            if (path.startsWith("./") && path.length() > 2)
jaroslav@327
   291
                path = path.substring(2);
jaroslav@327
   292
jaroslav@327
   293
            // Remove trailing .
jaroslav@327
   294
            if (path.endsWith("/."))
jaroslav@327
   295
                path = path.substring(0, path.length() -1);
jaroslav@327
   296
        }
jaroslav@327
   297
jaroslav@327
   298
        setURL(u, protocol, host, port, authority, userInfo, path, query, ref);
jaroslav@327
   299
    }
jaroslav@327
   300
jaroslav@327
   301
    /**
jaroslav@327
   302
     * Returns the default port for a URL parsed by this handler. This method
jaroslav@327
   303
     * is meant to be overidden by handlers with default port numbers.
jaroslav@327
   304
     * @return the default port for a <code>URL</code> parsed by this handler.
jaroslav@327
   305
     * @since 1.3
jaroslav@327
   306
     */
jaroslav@327
   307
    protected int getDefaultPort() {
jaroslav@327
   308
        return -1;
jaroslav@327
   309
    }
jaroslav@327
   310
jaroslav@327
   311
    /**
jaroslav@327
   312
     * Provides the default equals calculation. May be overidden by handlers
jaroslav@327
   313
     * for other protocols that have different requirements for equals().
jaroslav@327
   314
     * This method requires that none of its arguments is null. This is
jaroslav@327
   315
     * guaranteed by the fact that it is only called by java.net.URL class.
jaroslav@327
   316
     * @param u1 a URL object
jaroslav@327
   317
     * @param u2 a URL object
jaroslav@327
   318
     * @return <tt>true</tt> if the two urls are
jaroslav@327
   319
     * considered equal, ie. they refer to the same
jaroslav@327
   320
     * fragment in the same file.
jaroslav@327
   321
     * @since 1.3
jaroslav@327
   322
     */
jaroslav@327
   323
    protected boolean equals(URL u1, URL u2) {
jaroslav@327
   324
        String ref1 = u1.getRef();
jaroslav@327
   325
        String ref2 = u2.getRef();
jaroslav@327
   326
        return (ref1 == ref2 || (ref1 != null && ref1.equals(ref2))) &&
jaroslav@327
   327
               sameFile(u1, u2);
jaroslav@327
   328
    }
jaroslav@327
   329
jaroslav@327
   330
    /**
jaroslav@327
   331
     * Provides the default hash calculation. May be overidden by handlers for
jaroslav@327
   332
     * other protocols that have different requirements for hashCode
jaroslav@327
   333
     * calculation.
jaroslav@327
   334
     * @param u a URL object
jaroslav@327
   335
     * @return an <tt>int</tt> suitable for hash table indexing
jaroslav@327
   336
     * @since 1.3
jaroslav@327
   337
     */
jaroslav@327
   338
    protected int hashCode(URL u) {
jaroslav@327
   339
        int h = 0;
jaroslav@327
   340
jaroslav@327
   341
        // Generate the protocol part.
jaroslav@327
   342
        String protocol = u.getProtocol();
jaroslav@327
   343
        if (protocol != null)
jaroslav@327
   344
            h += protocol.hashCode();
jaroslav@327
   345
jaroslav@327
   346
        // Generate the host part.
jaroslav@339
   347
        Object addr = getHostAddress(u);
jaroslav@327
   348
        if (addr != null) {
jaroslav@327
   349
            h += addr.hashCode();
jaroslav@327
   350
        } else {
jaroslav@327
   351
            String host = u.getHost();
jaroslav@327
   352
            if (host != null)
jaroslav@327
   353
                h += host.toLowerCase().hashCode();
jaroslav@327
   354
        }
jaroslav@327
   355
jaroslav@327
   356
        // Generate the file part.
jaroslav@327
   357
        String file = u.getFile();
jaroslav@327
   358
        if (file != null)
jaroslav@327
   359
            h += file.hashCode();
jaroslav@327
   360
jaroslav@327
   361
        // Generate the port part.
jaroslav@327
   362
        if (u.getPort() == -1)
jaroslav@327
   363
            h += getDefaultPort();
jaroslav@327
   364
        else
jaroslav@327
   365
            h += u.getPort();
jaroslav@327
   366
jaroslav@327
   367
        // Generate the ref part.
jaroslav@327
   368
        String ref = u.getRef();
jaroslav@327
   369
        if (ref != null)
jaroslav@327
   370
            h += ref.hashCode();
jaroslav@327
   371
jaroslav@327
   372
        return h;
jaroslav@327
   373
    }
jaroslav@327
   374
jaroslav@327
   375
    /**
jaroslav@327
   376
     * Compare two urls to see whether they refer to the same file,
jaroslav@327
   377
     * i.e., having the same protocol, host, port, and path.
jaroslav@327
   378
     * This method requires that none of its arguments is null. This is
jaroslav@327
   379
     * guaranteed by the fact that it is only called indirectly
jaroslav@327
   380
     * by java.net.URL class.
jaroslav@327
   381
     * @param u1 a URL object
jaroslav@327
   382
     * @param u2 a URL object
jaroslav@327
   383
     * @return true if u1 and u2 refer to the same file
jaroslav@327
   384
     * @since 1.3
jaroslav@327
   385
     */
jaroslav@327
   386
    protected boolean sameFile(URL u1, URL u2) {
jaroslav@327
   387
        // Compare the protocols.
jaroslav@327
   388
        if (!((u1.getProtocol() == u2.getProtocol()) ||
jaroslav@327
   389
              (u1.getProtocol() != null &&
jaroslav@327
   390
               u1.getProtocol().equalsIgnoreCase(u2.getProtocol()))))
jaroslav@327
   391
            return false;
jaroslav@327
   392
jaroslav@327
   393
        // Compare the files.
jaroslav@327
   394
        if (!(u1.getFile() == u2.getFile() ||
jaroslav@327
   395
              (u1.getFile() != null && u1.getFile().equals(u2.getFile()))))
jaroslav@327
   396
            return false;
jaroslav@327
   397
jaroslav@327
   398
        // Compare the ports.
jaroslav@327
   399
        int port1, port2;
jaroslav@327
   400
        port1 = (u1.getPort() != -1) ? u1.getPort() : u1.handler.getDefaultPort();
jaroslav@327
   401
        port2 = (u2.getPort() != -1) ? u2.getPort() : u2.handler.getDefaultPort();
jaroslav@327
   402
        if (port1 != port2)
jaroslav@327
   403
            return false;
jaroslav@327
   404
jaroslav@327
   405
        // Compare the hosts.
jaroslav@327
   406
        if (!hostsEqual(u1, u2))
jaroslav@327
   407
            return false;
jaroslav@327
   408
jaroslav@327
   409
        return true;
jaroslav@327
   410
    }
jaroslav@327
   411
jaroslav@327
   412
    /**
jaroslav@327
   413
     * Get the IP address of our host. An empty host field or a DNS failure
jaroslav@327
   414
     * will result in a null return.
jaroslav@327
   415
     *
jaroslav@327
   416
     * @param u a URL object
jaroslav@327
   417
     * @return an <code>InetAddress</code> representing the host
jaroslav@327
   418
     * IP address.
jaroslav@327
   419
     * @since 1.3
jaroslav@327
   420
     */
jaroslav@339
   421
    private synchronized Object getHostAddress(URL u) {
jaroslav@327
   422
        return u.hostAddress;
jaroslav@327
   423
    }
jaroslav@327
   424
jaroslav@327
   425
    /**
jaroslav@327
   426
     * Compares the host components of two URLs.
jaroslav@327
   427
     * @param u1 the URL of the first host to compare
jaroslav@327
   428
     * @param u2 the URL of the second host to compare
jaroslav@327
   429
     * @return  <tt>true</tt> if and only if they
jaroslav@327
   430
     * are equal, <tt>false</tt> otherwise.
jaroslav@327
   431
     * @since 1.3
jaroslav@327
   432
     */
jaroslav@327
   433
    protected boolean hostsEqual(URL u1, URL u2) {
jaroslav@339
   434
        Object a1 = getHostAddress(u1);
jaroslav@339
   435
        Object a2 = getHostAddress(u2);
jaroslav@327
   436
        // if we have internet address for both, compare them
jaroslav@327
   437
        if (a1 != null && a2 != null) {
jaroslav@327
   438
            return a1.equals(a2);
jaroslav@327
   439
        // else, if both have host names, compare them
jaroslav@327
   440
        } else if (u1.getHost() != null && u2.getHost() != null)
jaroslav@327
   441
            return u1.getHost().equalsIgnoreCase(u2.getHost());
jaroslav@327
   442
         else
jaroslav@327
   443
            return u1.getHost() == null && u2.getHost() == null;
jaroslav@327
   444
    }
jaroslav@327
   445
jaroslav@327
   446
    /**
jaroslav@327
   447
     * Converts a <code>URL</code> of a specific protocol to a
jaroslav@327
   448
     * <code>String</code>.
jaroslav@327
   449
     *
jaroslav@327
   450
     * @param   u   the URL.
jaroslav@327
   451
     * @return  a string representation of the <code>URL</code> argument.
jaroslav@327
   452
     */
jaroslav@327
   453
    protected String toExternalForm(URL u) {
jaroslav@327
   454
jaroslav@327
   455
        // pre-compute length of StringBuffer
jaroslav@327
   456
        int len = u.getProtocol().length() + 1;
jaroslav@327
   457
        if (u.getAuthority() != null && u.getAuthority().length() > 0)
jaroslav@327
   458
            len += 2 + u.getAuthority().length();
jaroslav@327
   459
        if (u.getPath() != null) {
jaroslav@327
   460
            len += u.getPath().length();
jaroslav@327
   461
        }
jaroslav@327
   462
        if (u.getQuery() != null) {
jaroslav@327
   463
            len += 1 + u.getQuery().length();
jaroslav@327
   464
        }
jaroslav@327
   465
        if (u.getRef() != null)
jaroslav@327
   466
            len += 1 + u.getRef().length();
jaroslav@327
   467
jaroslav@327
   468
        StringBuffer result = new StringBuffer(len);
jaroslav@327
   469
        result.append(u.getProtocol());
jaroslav@327
   470
        result.append(":");
jaroslav@327
   471
        if (u.getAuthority() != null && u.getAuthority().length() > 0) {
jaroslav@327
   472
            result.append("//");
jaroslav@327
   473
            result.append(u.getAuthority());
jaroslav@327
   474
        }
jaroslav@327
   475
        if (u.getPath() != null) {
jaroslav@327
   476
            result.append(u.getPath());
jaroslav@327
   477
        }
jaroslav@327
   478
        if (u.getQuery() != null) {
jaroslav@327
   479
            result.append('?');
jaroslav@327
   480
            result.append(u.getQuery());
jaroslav@327
   481
        }
jaroslav@327
   482
        if (u.getRef() != null) {
jaroslav@327
   483
            result.append("#");
jaroslav@327
   484
            result.append(u.getRef());
jaroslav@327
   485
        }
jaroslav@327
   486
        return result.toString();
jaroslav@327
   487
    }
jaroslav@327
   488
jaroslav@327
   489
    /**
jaroslav@327
   490
     * Sets the fields of the <code>URL</code> argument to the indicated values.
jaroslav@327
   491
     * Only classes derived from URLStreamHandler are supposed to be able
jaroslav@327
   492
     * to call the set method on a URL.
jaroslav@327
   493
     *
jaroslav@327
   494
     * @param   u         the URL to modify.
jaroslav@327
   495
     * @param   protocol  the protocol name.
jaroslav@327
   496
     * @param   host      the remote host value for the URL.
jaroslav@327
   497
     * @param   port      the port on the remote machine.
jaroslav@327
   498
     * @param   authority the authority part for the URL.
jaroslav@327
   499
     * @param   userInfo the userInfo part of the URL.
jaroslav@327
   500
     * @param   path      the path component of the URL.
jaroslav@327
   501
     * @param   query     the query part for the URL.
jaroslav@327
   502
     * @param   ref       the reference.
jaroslav@327
   503
     * @exception       SecurityException       if the protocol handler of the URL is
jaroslav@327
   504
     *                                  different from this one
jaroslav@327
   505
     * @see     java.net.URL#set(java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)
jaroslav@327
   506
     * @since 1.3
jaroslav@327
   507
     */
jaroslav@327
   508
       protected void setURL(URL u, String protocol, String host, int port,
jaroslav@327
   509
                             String authority, String userInfo, String path,
jaroslav@327
   510
                             String query, String ref) {
jaroslav@327
   511
        if (this != u.handler) {
jaroslav@327
   512
            throw new SecurityException("handler for url different from " +
jaroslav@327
   513
                                        "this handler");
jaroslav@327
   514
        }
jaroslav@327
   515
        // ensure that no one can reset the protocol on a given URL.
jaroslav@327
   516
        u.set(u.getProtocol(), host, port, authority, userInfo, path, query, ref);
jaroslav@327
   517
    }
jaroslav@327
   518
jaroslav@327
   519
    /**
jaroslav@327
   520
     * Sets the fields of the <code>URL</code> argument to the indicated values.
jaroslav@327
   521
     * Only classes derived from URLStreamHandler are supposed to be able
jaroslav@327
   522
     * to call the set method on a URL.
jaroslav@327
   523
     *
jaroslav@327
   524
     * @param   u         the URL to modify.
jaroslav@327
   525
     * @param   protocol  the protocol name. This value is ignored since 1.2.
jaroslav@327
   526
     * @param   host      the remote host value for the URL.
jaroslav@327
   527
     * @param   port      the port on the remote machine.
jaroslav@327
   528
     * @param   file      the file.
jaroslav@327
   529
     * @param   ref       the reference.
jaroslav@327
   530
     * @exception       SecurityException       if the protocol handler of the URL is
jaroslav@327
   531
     *                                  different from this one
jaroslav@327
   532
     * @deprecated Use setURL(URL, String, String, int, String, String, String,
jaroslav@327
   533
     *             String);
jaroslav@327
   534
     */
jaroslav@327
   535
    @Deprecated
jaroslav@327
   536
    protected void setURL(URL u, String protocol, String host, int port,
jaroslav@327
   537
                          String file, String ref) {
jaroslav@327
   538
        /*
jaroslav@327
   539
         * Only old URL handlers call this, so assume that the host
jaroslav@327
   540
         * field might contain "user:passwd@host". Fix as necessary.
jaroslav@327
   541
         */
jaroslav@327
   542
        String authority = null;
jaroslav@327
   543
        String userInfo = null;
jaroslav@327
   544
        if (host != null && host.length() != 0) {
jaroslav@327
   545
            authority = (port == -1) ? host : host + ":" + port;
jaroslav@327
   546
            int at = host.lastIndexOf('@');
jaroslav@327
   547
            if (at != -1) {
jaroslav@327
   548
                userInfo = host.substring(0, at);
jaroslav@327
   549
                host = host.substring(at+1);
jaroslav@327
   550
            }
jaroslav@327
   551
        }
jaroslav@327
   552
jaroslav@327
   553
        /*
jaroslav@327
   554
         * Assume file might contain query part. Fix as necessary.
jaroslav@327
   555
         */
jaroslav@327
   556
        String path = null;
jaroslav@327
   557
        String query = null;
jaroslav@327
   558
        if (file != null) {
jaroslav@327
   559
            int q = file.lastIndexOf('?');
jaroslav@327
   560
            if (q != -1) {
jaroslav@327
   561
                query = file.substring(q+1);
jaroslav@327
   562
                path = file.substring(0, q);
jaroslav@327
   563
            } else
jaroslav@327
   564
                path = file;
jaroslav@327
   565
        }
jaroslav@327
   566
        setURL(u, protocol, host, port, authority, userInfo, path, query, ref);
jaroslav@327
   567
    }
jaroslav@327
   568
}