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