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