Package java.net

Class URL

  • All Implemented Interfaces:
    Serializable

    public final class URL
    extends Object
    implements Serializable
    A stripped-down URL suitable for use in GWT client-side.

    It could do with a lot more trimming, and use of native js encodeURIComponent.

    Any volunteers with some existing unit tests who'd like to improve client side URL performance?

    See Also:
    Serialized Form
    • Constructor Detail

      • URL

        public URL​(URL context,
                   String spec)
            throws MalformedURLException
        Creates a URL by parsing the given spec within a specified context. The new URL is created from the given context URL and the spec argument as described in RFC2396 "Uniform Resource Identifiers : Generic * Syntax" :
                  <scheme>://<authority><path>?<query>#<fragment>
         
        The reference is parsed into the scheme, authority, path, query and fragment parts. If the path component is empty and the scheme, authority, and query components are undefined, then the new URL is a reference to the current document. Otherwise, the fragment and query parts present in the spec are used in the new URL.

        If the scheme component is defined in the given spec and does not match the scheme of the context, then the new URL is created as an absolute URL based on the spec alone. Otherwise the scheme component is inherited from the context URL.

        If the authority component is present in the spec then the spec is treated as absolute and the spec authority and path will replace the context authority and path. If the authority component is absent in the spec then the authority of the new URL will be inherited from the context.

        If the spec's path component begins with a slash character "/" then the path is treated as absolute and the spec path replaces the context path.

        Otherwise, the path is treated as a relative path and is appended to the context path, as described in RFC2396. Also, in this case, the path is canonicalized through the removal of directory changes made by occurences of ".." and ".".

        For a more detailed description of URL parsing, refer to RFC2396.

        Parameters:
        context - the context in which to parse the specification.
        spec - the String to parse as a URL.
        Throws:
        MalformedURLException - if no protocol is specified, or an unknown protocol is found.
        See Also:
        java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String), URLStreamHandler, URLStreamHandler.parseURL(java.net.URL, java.lang.String, int, int)
    • Method Detail

      • parseURL

        protected void parseURL​(URL purl,
                                String pspec,
                                int pstart,
                                int plimit)
        Parses the string representation of a URL into a URL object.

        If there is any inherited context, then it has already been copied into the URL argument.

        The parseURL method of URLStreamHandler parses the string representation as if it were an http specification. Most URL protocol families have a similar parsing. A stream protocol handler for a protocol that has a different syntax must override this routine.

        Parameters:
        purl - the URL to receive the result of parsing the spec.
        pspec - the String representing the URL that must be parsed.
        pstart - the character index at which to begin parsing. This is just past the ' :' (if there is one) that specifies the determination of the protocol name.
        plimit - the character position to stop parsing at. This is the end of the string or the position of the "#" character, if present. All information after the sharp sign indicates an anchor.
      • textToNumericFormatV4

        public static byte[] textToNumericFormatV4​(String src)
        Convert IPv4-Mapped address to IPv4 address. Both input and returned value are in network order binary form.
        Parameters:
        src - a string representing an IPv4-Mapped address in textual format
        Returns:
        a byte array representing the IPv4 numeric address
      • convertFromIPv4MappedAddress

        public static byte[] convertFromIPv4MappedAddress​(byte[] addr)
        Convert IPv4-Mapped address to IPv4 address. Both input and returned value are in network order binary form.
        Parameters:
        addr - a representing an IPv4-Mapped address in textual format
        Returns:
        a byte array representing the IPv4 numeric address
      • set

        protected void set​(String protocol,
                           String host,
                           int port,
                           String file,
                           String ref)
        Sets the fields of the URL. This is not a public method so that only URLStreamHandlers can modify URL fields. URLs are otherwise constant.
        Parameters:
        protocol - the name of the protocol to use
        host - the name of the host
        port - the port number on the host
        file - the file on the host
        ref - the internal reference in the URL
      • set

        protected void set​(String protocol,
                           String host,
                           int port,
                           String authority,
                           String userInfo,
                           String path,
                           String query,
                           String ref)
        Sets the specified 8 fields of the URL. This is not a public method so that only URLStreamHandlers can modify URL fields. URLs are otherwise constant.
        Parameters:
        protocol - the name of the protocol to use
        host - the name of the host
        port - the port number on the host
        authority - the authority part for the url
        userInfo - the username and password
        path - the file on the host
        ref - the internal reference in the URL
        query - the query part of this URL
        Since:
        1.3
      • getQuery

        public String getQuery()
        Gets the query part of this URL.
        Returns:
        the query part of this URL, or null if one does not exist
        Since:
        1.3
      • getPath

        public String getPath()
        Gets the path part of this URL.
        Returns:
        the path part of this URL, or an empty string if one does not exist
        Since:
        1.3
      • getUserInfo

        public String getUserInfo()
        Gets the userInfo part of this URL.
        Returns:
        the userInfo part of this URL, or null if one does not exist
        Since:
        1.3
      • getAuthority

        public String getAuthority()
        Gets the authority part of this URL.
        Returns:
        the authority part of this URL
        Since:
        1.3
      • getPort

        public int getPort()
        Gets the port number of this URL.
        Returns:
        the port number, or -1 if the port is not set
      • getDefaultPort

        public int getDefaultPort()
        Gets the default port number of the protocol associated with this URL. If the URL scheme or the URLStreamHandler for the URL do not define a default port number, then -1 is returned.
        Returns:
        the port number
        Since:
        1.4
      • getProtocol

        public String getProtocol()
        Gets the protocol name of this URL.
        Returns:
        the protocol of this URL.
      • getHost

        public String getHost()
        Gets the host name of this URL, if applicable. The format of the host conforms to RFC 2732, i.e. for a literal IPv6 address, this method will return the IPv6 address enclosed in square brackets ( '[' and ']').
        Returns:
        the host name of this URL.
      • getFile

        public String getFile()
        Gets the file name of this URL. The returned file portion will be the same as getPath(), plus the concatenation of the value of getQuery(), if any. If there is no query portion, this method and getPath() will return identical results.
        Returns:
        the file name of this URL, or an empty string if one does not exist
      • getRef

        public String getRef()
        Gets the anchor (also known as the "reference") of this URL.
        Returns:
        the anchor (also known as the "reference") of this URL, or null if one does not exist
      • equals

        public boolean equals​(Object obj)
        Compares this URL for equality with another object.

        If the given object is not a URL then this method immediately returns false.

        Two URL objects are equal if they have the same protocol, reference equivalent hosts, have the same port number on the host, and the same file and fragment of the file.

        Two hosts are considered equivalent if both host names can be resolved into the same IP addresses; else if either host name can't be resolved, the host names must be equal without regard to case; or both host names equal to null.

        Since hosts comparison requires name resolution, this operation is a blocking operation.

        Note: The defined behavior for equals is known to be inconsistent with virtual hosting in HTTP.

        Overrides:
        equals in class Object
        Parameters:
        obj - the URL to compare against.
        Returns:
        true if the objects are the same; false otherwise.
      • hashCode

        public int hashCode()
        Creates an integer suitable for hash table indexing.

        The hash code is based upon all the URL components relevant for URL comparison. As such, this operation is a blocking operation.

        Overrides:
        hashCode in class Object
        Returns:
        a hash code for this URL.
      • toString

        public String toString()
        Constructs a string representation of this URL. The string is created by calling the toExternalForm method of the stream protocol handler for this object.
        Overrides:
        toString in class Object
        Returns:
        a string representation of this object.
        See Also:
        java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String), URLStreamHandler.toExternalForm(java.net.URL)
      • toExternalForm

        public String toExternalForm()
        Constructs a string representation of this URL. The string is created by calling the toExternalForm method of the stream protocol handler for this object.
        Returns:
        a string representation of this object.
        See Also:
        java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String), URLStreamHandler.toExternalForm(java.net.URL)
      • openConnection

        public URLConnection openConnection()
                                     throws IOException
        Returns a URI equivalent to this URL. This method functions in the same way as new URI (this.toString()).

        Note, any URL instance that complies with RFC 2396 can be converted to a URI. However, some URLs that are not strictly in compliance can not be converted to a URI.

        Returns:
        a URI instance equivalent to this URL.
        Throws:
        URISyntaxException - if this URL is not formatted strictly according to to RFC2396 and cannot be converted to a URI.
        IOException
        Since:
        1.5