Class URIUtil

  • All Implemented Interfaces:
    java.lang.Cloneable

    public class URIUtil
    extends java.lang.Object
    implements java.lang.Cloneable
    URI Utility methods.

    This class assists with the decoding and encoding or HTTP URI's. It differs from the java.net.URL class as it does not provide communications ability, but it does assist with query string formatting.

    See Also:
    UrlEncoded
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.nio.charset.Charset __CHARSET  
      static java.lang.String HTTP  
      static java.lang.String HTTPS  
      static java.lang.String SLASH  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.lang.String addEncodedPaths​(java.lang.String p1, java.lang.String p2)
      Add two encoded URI path segments.
      static java.net.URI addPath​(java.net.URI uri, java.lang.String path)  
      static java.lang.String addPathQuery​(java.lang.String path, java.lang.String query)
      Add a path and a query string
      static java.lang.String addPaths​(java.lang.String p1, java.lang.String p2)
      Add two Decoded URI path segments.
      static java.lang.String addQueries​(java.lang.String query1, java.lang.String query2)
      Combine two query strings into one.
      static void appendSchemeHostPort​(java.lang.StringBuffer url, java.lang.String scheme, java.lang.String server, int port)
      Append scheme, host and port URI prefix, handling IPv6 address encoding and default ports
      static void appendSchemeHostPort​(java.lang.StringBuilder url, java.lang.String scheme, java.lang.String server, int port)
      Append scheme, host and port URI prefix, handling IPv6 address encoding and default ports
      static java.lang.String canonicalEncodedPath​(java.lang.String path)
      Deprecated.
      static java.lang.String canonicalPath​(java.lang.String path)
      Convert a decoded URI path to a canonical form.
      static java.lang.String canonicalURI​(java.lang.String uri)
      Convert a partial URI to a canonical form.
      static java.lang.String compactPath​(java.lang.String path)
      Convert a path to a compact form.
      static java.lang.String decodePath​(java.lang.String path)  
      static java.lang.String decodePath​(java.lang.String path, int offset, int length)  
      static java.lang.String decodeSpecific​(java.lang.String str, java.lang.String charsToDecode)
      Decode a raw String and convert any specific URI encoded sequences into characters.
      static java.lang.String encodePath​(java.lang.String path)
      Encode a URI path.
      static java.lang.StringBuilder encodePath​(java.lang.StringBuilder buf, java.lang.String path)
      Encode a URI path.
      static java.lang.String encodeSpaces​(java.lang.String str)
      Encode a raw URI String and convert any raw spaces to their "%20" equivalent.
      static java.lang.String encodeSpecific​(java.lang.String str, java.lang.String charsToEncode)
      Encode a raw String and convert any specific characters to their URI encoded equivalent.
      static java.lang.StringBuilder encodeString​(java.lang.StringBuilder buf, java.lang.String path, java.lang.String encode)
      Encode a URI path.
      static boolean equalsIgnoreEncodings​(java.lang.String uriA, java.lang.String uriB)  
      static boolean equalsIgnoreEncodings​(java.net.URI uriA, java.net.URI uriB)  
      static java.lang.String getJarSource​(java.lang.String uri)  
      static java.net.URI getJarSource​(java.net.URI uri)  
      static java.lang.String getUriLastPathSegment​(java.net.URI uri)
      Given a URI, attempt to get the last segment.
      static boolean hasScheme​(java.lang.String uri)  
      static java.lang.String newURI​(java.lang.String scheme, java.lang.String server, int port, java.lang.String path, java.lang.String query)
      Create a new URI from the arguments, handling IPv6 host encoding and default ports
      static java.lang.StringBuilder newURIBuilder​(java.lang.String scheme, java.lang.String server, int port)
      Create a new URI StringBuilder from the arguments, handling IPv6 host encoding and default ports
      static java.lang.String parentPath​(java.lang.String p)
      Return the parent Path.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • encodePath

        public static java.lang.String encodePath​(java.lang.String path)
        Encode a URI path. This is the same encoding offered by URLEncoder, except that the '/' character is not encoded.
        Parameters:
        path - The path the encode
        Returns:
        The encoded path
      • encodePath

        public static java.lang.StringBuilder encodePath​(java.lang.StringBuilder buf,
                                                         java.lang.String path)
        Encode a URI path.
        Parameters:
        path - The path the encode
        buf - StringBuilder to encode path into (or null)
        Returns:
        The StringBuilder or null if no substitutions required.
      • encodeSpaces

        public static java.lang.String encodeSpaces​(java.lang.String str)
        Encode a raw URI String and convert any raw spaces to their "%20" equivalent.
        Parameters:
        str - input raw string
        Returns:
        output with spaces converted to "%20"
      • encodeSpecific

        public static java.lang.String encodeSpecific​(java.lang.String str,
                                                      java.lang.String charsToEncode)
        Encode a raw String and convert any specific characters to their URI encoded equivalent.
        Parameters:
        str - input raw string
        charsToEncode - the list of raw characters that need to be encoded (if encountered)
        Returns:
        output with specified characters encoded.
      • decodeSpecific

        public static java.lang.String decodeSpecific​(java.lang.String str,
                                                      java.lang.String charsToDecode)
        Decode a raw String and convert any specific URI encoded sequences into characters.
        Parameters:
        str - input raw string
        charsToDecode - the list of raw characters that need to be decoded (if encountered), leaving all other encoded sequences alone.
        Returns:
        output with specified characters decoded.
      • encodeString

        public static java.lang.StringBuilder encodeString​(java.lang.StringBuilder buf,
                                                           java.lang.String path,
                                                           java.lang.String encode)
        Encode a URI path.
        Parameters:
        path - The path the encode
        buf - StringBuilder to encode path into (or null)
        encode - String of characters to encode. % is always encoded.
        Returns:
        The StringBuilder or null if no substitutions required.
      • decodePath

        public static java.lang.String decodePath​(java.lang.String path)
      • decodePath

        public static java.lang.String decodePath​(java.lang.String path,
                                                  int offset,
                                                  int length)
      • addEncodedPaths

        public static java.lang.String addEncodedPaths​(java.lang.String p1,
                                                       java.lang.String p2)
        Add two encoded URI path segments. Handles null and empty paths, path and query params (eg ?a=b or ;JSESSIONID=xxx) and avoids duplicate '/'
        Parameters:
        p1 - URI path segment (should be encoded)
        p2 - URI path segment (should be encoded)
        Returns:
        Legally combined path segments.
      • addPaths

        public static java.lang.String addPaths​(java.lang.String p1,
                                                java.lang.String p2)
        Add two Decoded URI path segments. Handles null and empty paths. Path and query params (eg ?a=b or ;JSESSIONID=xxx) are not handled
        Parameters:
        p1 - URI path segment (should be decoded)
        p2 - URI path segment (should be decoded)
        Returns:
        Legally combined path segments.
      • addPathQuery

        public static java.lang.String addPathQuery​(java.lang.String path,
                                                    java.lang.String query)
        Add a path and a query string
        Parameters:
        path - The path which may already contain contain a query
        query - The query string or null if no query to be added
        Returns:
        The path with any non null query added after a '?' or '&' as appropriate.
      • getUriLastPathSegment

        public static java.lang.String getUriLastPathSegment​(java.net.URI uri)
        Given a URI, attempt to get the last segment.

        If this is a jar:file:// style URI, then the JAR filename is returned (not the deep !/path location)

        Parameters:
        uri - the URI to look in
        Returns:
        the last segment.
      • parentPath

        public static java.lang.String parentPath​(java.lang.String p)
        Return the parent Path. Treat a URI like a directory path and return the parent directory.
        Parameters:
        p - the path to return a parent reference to
        Returns:
        the parent path of the URI
      • canonicalURI

        public static java.lang.String canonicalURI​(java.lang.String uri)
        Convert a partial URI to a canonical form.

        All segments of "." and ".." are factored out. Null is returned if the path tries to .. above its root.

        Parameters:
        uri - the encoded URI from the path onwards, which may contain query strings and/or fragments
        Returns:
        the canonical path, or null if path traversal above root.
        See Also:
        canonicalPath(String)
      • canonicalEncodedPath

        @Deprecated
        public static java.lang.String canonicalEncodedPath​(java.lang.String path)
        Deprecated.
        Parameters:
        path - the encoded URI from the path onwards, which may contain query strings and/or fragments
        Returns:
        the canonical path, or null if path traversal above root.
      • canonicalPath

        public static java.lang.String canonicalPath​(java.lang.String path)
        Convert a decoded URI path to a canonical form.

        All segments of "." and ".." are factored out. Null is returned if the path tries to .. above its root.

        Parameters:
        path - the decoded URI path to convert. Any special characters (e.g. '?', "#") are assumed to be part of the path segments.
        Returns:
        the canonical path, or null if path traversal above root.
        See Also:
        canonicalURI(String)
      • compactPath

        public static java.lang.String compactPath​(java.lang.String path)
        Convert a path to a compact form. All instances of "//" and "///" etc. are factored out to single "/"
        Parameters:
        path - the path to compact
        Returns:
        the compacted path
      • hasScheme

        public static boolean hasScheme​(java.lang.String uri)
        Parameters:
        uri - URI
        Returns:
        True if the uri has a scheme
      • newURI

        public static java.lang.String newURI​(java.lang.String scheme,
                                              java.lang.String server,
                                              int port,
                                              java.lang.String path,
                                              java.lang.String query)
        Create a new URI from the arguments, handling IPv6 host encoding and default ports
        Parameters:
        scheme - the URI scheme
        server - the URI server
        port - the URI port
        path - the URI path
        query - the URI query
        Returns:
        A String URI
      • newURIBuilder

        public static java.lang.StringBuilder newURIBuilder​(java.lang.String scheme,
                                                            java.lang.String server,
                                                            int port)
        Create a new URI StringBuilder from the arguments, handling IPv6 host encoding and default ports
        Parameters:
        scheme - the URI scheme
        server - the URI server
        port - the URI port
        Returns:
        a StringBuilder containing URI prefix
      • appendSchemeHostPort

        public static void appendSchemeHostPort​(java.lang.StringBuilder url,
                                                java.lang.String scheme,
                                                java.lang.String server,
                                                int port)
        Append scheme, host and port URI prefix, handling IPv6 address encoding and default ports
        Parameters:
        url - StringBuilder to append to
        scheme - the URI scheme
        server - the URI server
        port - the URI port
      • appendSchemeHostPort

        public static void appendSchemeHostPort​(java.lang.StringBuffer url,
                                                java.lang.String scheme,
                                                java.lang.String server,
                                                int port)
        Append scheme, host and port URI prefix, handling IPv6 address encoding and default ports
        Parameters:
        url - StringBuffer to append to
        scheme - the URI scheme
        server - the URI server
        port - the URI port
      • equalsIgnoreEncodings

        public static boolean equalsIgnoreEncodings​(java.lang.String uriA,
                                                    java.lang.String uriB)
      • equalsIgnoreEncodings

        public static boolean equalsIgnoreEncodings​(java.net.URI uriA,
                                                    java.net.URI uriB)
      • addPath

        public static java.net.URI addPath​(java.net.URI uri,
                                           java.lang.String path)
        Parameters:
        uri - A URI to add the path to
        path - A decoded path element
        Returns:
        URI with path added.
      • addQueries

        public static java.lang.String addQueries​(java.lang.String query1,
                                                  java.lang.String query2)
        Combine two query strings into one. Each query string should not contain the beginning '?' character, but may contain multiple parameters separated by the '&' character.
        Parameters:
        query1 - the first query string.
        query2 - the second query string.
        Returns:
        the combination of the two query strings.
      • getJarSource

        public static java.net.URI getJarSource​(java.net.URI uri)
      • getJarSource

        public static java.lang.String getJarSource​(java.lang.String uri)