Package io.inversion

Class Url


  • public final class Url
    extends java.lang.Object
    Utility class for parsing and working with HTTP(S) URLs.

    Not for use with non HTTP(S) urls.

    A number of different utility methods are provided to make it simple to find or remove different query string keys.

    • Constructor Summary

      Constructors 
      Constructor Description
      Url​(java.lang.String url)
      Parses url into its protocol, host, port, path and query string param parts.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clearParams()  
      java.lang.String clearParams​(java.lang.String... tokens)
      Removes any param that has one of tokens as a whole word case insensitive substring in the key.
      Url copy()  
      static java.lang.String encode​(java.lang.String str)  
      boolean equals​(java.lang.Object url)
      Checks url equality based on type and toString equality
      java.lang.String findKey​(java.lang.String... tokens)
      Finds a key that has any one of tokens as a whole word case insensitive substring
      java.lang.String findKeyValue​(java.lang.String... tokens)
      Finds the value associated with findKey(tokens)
      java.lang.String getDomain()  
      java.lang.String getFile()
      Gets the last url path part if it exists
      java.lang.String getHost()  
      io.inversion.utils.Path getHostAsPath()  
      java.lang.String getOriginal()  
      java.lang.String getParam​(java.lang.String key)
      Gets the param value with key based on a case insensitive match.
      java.util.Map<java.lang.String,​java.lang.String> getParams()  
      io.inversion.utils.Path getPath()  
      int getPort()  
      java.lang.String getProtocol()  
      java.lang.String getQueryString()
      Generates a URL encode query string for params
      static java.lang.String toQueryString​(java.util.Map<java.lang.String,​java.lang.String> params)
      Creates a UTF-8 url encoded query string, not including a leading "?" with key value pairs separated by a '&'
      java.lang.String toString()
      Generates a string string representation of this url with any query string parameters URL encoded and port number included only if it differs from the standard protocol port.
      Url withHost​(java.lang.String host)  
      Url withParam​(io.inversion.rql.Term term)  
      Url withParam​(java.lang.String name, java.lang.String value)
      Adds name/value to params overwriting any preexisting key/value pair on a key case insensitive basis.
      Url withParams​(java.lang.String... nvpairs)  
      Url withParams​(java.util.Map<java.lang.String,​java.lang.String> params)
      Adds all key/value pairs from newParams to params overwriting any exiting keys on a case insensitive basis
      Url withPath​(io.inversion.utils.Path path)  
      Url withPort​(int port)  
      Url withProtocol​(java.lang.String protocol)  
      Url withQueryString​(java.lang.String queryString)
      Parses queryString and replace params
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Url

        public Url​(java.lang.String url)
        Parses url into its protocol, host, port, path and query string param parts.

        If url does not start with "http://" or "https://" then "http://127.0.0.1" will be prepended.

        Parameters:
        url - url string
    • Method Detail

      • copy

        public Url copy()
      • toQueryString

        public static java.lang.String toQueryString​(java.util.Map<java.lang.String,​java.lang.String> params)
        Creates a UTF-8 url encoded query string, not including a leading "?" with key value pairs separated by a '&'
        Parameters:
        params - the key/value pairs to encode
        Returns:
        a UTF-8 url encoded query string
        See Also:
        URLEncoder.encode(String, String)
      • encode

        public static java.lang.String encode​(java.lang.String str)
      • toString

        public java.lang.String toString()
        Generates a string string representation of this url with any query string parameters URL encoded and port number included only if it differs from the standard protocol port.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the string representation of this url
      • equals

        public boolean equals​(java.lang.Object url)
        Checks url equality based on type and toString equality
        Overrides:
        equals in class java.lang.Object
        Returns:
        true if url is a Url with a matching toString
      • getDomain

        public java.lang.String getDomain()
      • getQueryString

        public java.lang.String getQueryString()
        Generates a URL encode query string for params
        Returns:
        a URL encoded query string if params.size() is @gt; 0 else empty string
        See Also:
        toQueryString(Map)
      • getHost

        public java.lang.String getHost()
      • withHost

        public Url withHost​(java.lang.String host)
      • getHostAsPath

        public io.inversion.utils.Path getHostAsPath()
      • getPort

        public int getPort()
      • withPort

        public Url withPort​(int port)
      • getProtocol

        public java.lang.String getProtocol()
      • withProtocol

        public Url withProtocol​(java.lang.String protocol)
      • getPath

        public io.inversion.utils.Path getPath()
      • withPath

        public Url withPath​(io.inversion.utils.Path path)
      • getFile

        public java.lang.String getFile()
        Gets the last url path part if it exists
        Returns:
        path.last() if it exists otherwise null
      • withQueryString

        public Url withQueryString​(java.lang.String queryString)
        Parses queryString and replace params
        Parameters:
        queryString - query prams to add
        Returns:
        this
      • withParams

        public Url withParams​(java.util.Map<java.lang.String,​java.lang.String> params)
        Adds all key/value pairs from newParams to params overwriting any exiting keys on a case insensitive basis
        Parameters:
        params - name/value pairs to add to the query string params map
        Returns:
        this
      • withParam

        public Url withParam​(io.inversion.rql.Term term)
      • withParam

        public Url withParam​(java.lang.String name,
                             java.lang.String value)
        Adds name/value to params overwriting any preexisting key/value pair on a key case insensitive basis.
        Parameters:
        name - the key to add or overwrite, may not be null
        value - the value, may be null
        Returns:
        this
      • withParams

        public Url withParams​(java.lang.String... nvpairs)
      • clearParams

        public java.lang.String clearParams​(java.lang.String... tokens)
        Removes any param that has one of tokens as a whole word case insensitive substring in the key.
        Parameters:
        tokens - string tokens when found in a param key will cause them to be removed
        Returns:
        the first value found that contained any one of tokens
        See Also:
        Utils.containsToken(java.lang.String, java.lang.String)
      • clearParams

        public void clearParams()
      • findKey

        public java.lang.String findKey​(java.lang.String... tokens)
        Finds a key that has any one of tokens as a whole word case insensitive substring
        Parameters:
        tokens - substrings to search params.keySet() for
        Returns:
        the first param key that has anyone of tokens as a whole word case insensitive substring
      • findKeyValue

        public java.lang.String findKeyValue​(java.lang.String... tokens)
        Finds the value associated with findKey(tokens)
        Parameters:
        tokens - substrings to search params.keySet() for
        Returns:
        the value for the first param key that has anyone of tokens as a whole word case insensitive substring
        See Also:
        findKey(String...)
      • getParam

        public java.lang.String getParam​(java.lang.String key)
        Gets the param value with key based on a case insensitive match.
        Parameters:
        key - the key to get
        Returns:
        the param value for key based on a case insensitive match.
      • getParams

        public java.util.Map<java.lang.String,​java.lang.String> getParams()
        Returns:
        a new case insensitive order preserving map copy of params
      • getOriginal

        public java.lang.String getOriginal()
        Returns:
        the url string used in constructing this Url.