Class UrlUtils


  • public class UrlUtils
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      UrlUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static boolean compareNullableStrings​(java.lang.String s1, java.lang.String s2, boolean ignore)
      Compare strings that can be null.
      static java.net.Socket createSocketFromUrl​(java.net.URL url)  
      static java.io.File decodeUrlAsFile​(java.net.URL url)  
      static java.net.URL decodeUrlQuietly​(java.net.URL url)  
      static java.lang.String ensureSlashTail​(java.lang.String s)  
      static java.net.URL ensureSlashTail​(java.net.URL u)  
      static boolean equalUrls​(java.net.URL url1, java.net.URL url2)  
      static java.lang.String getHostAndPort​(java.net.URL url)  
      static int getPort​(java.net.URL url)  
      static int getSanitizedPort​(java.net.URL u)  
      static java.net.URL guessCodeBase​(JNLPFile file)  
      static boolean isLocalFile​(java.net.URL url)  
      static boolean isValidRFC2396Url​(java.net.URL url)  
      static java.lang.String loadUrl​(java.net.URL url)  
      static java.lang.String loadUrl​(java.net.URL url, java.nio.charset.Charset ch)  
      static java.lang.String[] loadUrlWithInvalidHeader​(java.net.URL url)  
      static java.lang.String[] loadUrlWithInvalidHeader​(java.net.URL url, java.nio.charset.Charset ch)  
      static java.net.URL normalizeUrl​(java.net.URL url)  
      static java.net.URL normalizeUrl​(java.net.URL url, boolean encodeFileUrls)  
      static java.net.URL normalizeUrlAndStripParams​(java.net.URL url)  
      static java.net.URL normalizeUrlAndStripParams​(java.net.URL url, boolean encodeFileUrls)  
      static java.net.URL normalizeUrlQuietly​(java.net.URL url)  
      static java.net.URL normalizeUrlQuietly​(java.net.URL url, boolean encodeFileUrls)  
      (package private) static boolean notNullUrlEquals​(java.net.URL u1, java.net.URL u2)  
      static java.net.URL removeFileName​(java.net.URL src)
      This function i striping part behind last path delimiter.
      static java.lang.String sanitizeLastSlash​(java.lang.String in)  
      static java.net.URL sanitizeLastSlash​(java.net.URL in)
      This function is removing all tailing slashes of url and both unix and windows slashes are supported.
      private static java.lang.String setOfUrlsToHtmlList​(java.util.Collection<java.net.URL> remoteUrls, int max)
      Small utility function creating li list from collection of urls
      static java.lang.String setOfUrlsToHtmlList​(java.util.List<java.net.URL> remoteUrls)
      Small utility function creating li list from collection of urls
      static java.lang.String stripFile​(java.net.URL documentbase)  
      private static java.lang.String stripFileImp​(java.net.URL documentbase)  
      static boolean urlEquals​(java.net.URL u1, java.net.URL u2)
      Compares a URL using string compareNullableStrings of its protocol, host, port, path, query, and anchor.
      static boolean urlRelativeTo​(java.net.URL url, java.net.URL codebaseUrl)
      Checks whether url is relative (or equal) to codebaseUrl.
      static void writeRequest​(java.io.OutputStream s, java.net.URL url)  
      • Methods inherited from class java.lang.Object

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

      • UrlUtils

        public UrlUtils()
    • Method Detail

      • normalizeUrlAndStripParams

        public static java.net.URL normalizeUrlAndStripParams​(java.net.URL url,
                                                              boolean encodeFileUrls)
      • normalizeUrlAndStripParams

        public static java.net.URL normalizeUrlAndStripParams​(java.net.URL url)
      • isLocalFile

        public static boolean isLocalFile​(java.net.URL url)
      • decodeUrlQuietly

        public static java.net.URL decodeUrlQuietly​(java.net.URL url)
      • isValidRFC2396Url

        public static boolean isValidRFC2396Url​(java.net.URL url)
      • normalizeUrl

        public static java.net.URL normalizeUrl​(java.net.URL url,
                                                boolean encodeFileUrls)
                                         throws java.net.MalformedURLException,
                                                java.io.UnsupportedEncodingException,
                                                java.net.URISyntaxException
        Throws:
        java.net.MalformedURLException
        java.io.UnsupportedEncodingException
        java.net.URISyntaxException
      • normalizeUrl

        public static java.net.URL normalizeUrl​(java.net.URL url)
                                         throws java.net.MalformedURLException,
                                                java.io.UnsupportedEncodingException,
                                                java.net.URISyntaxException
        Throws:
        java.net.MalformedURLException
        java.io.UnsupportedEncodingException
        java.net.URISyntaxException
      • normalizeUrlQuietly

        public static java.net.URL normalizeUrlQuietly​(java.net.URL url,
                                                       boolean encodeFileUrls)
      • normalizeUrlQuietly

        public static java.net.URL normalizeUrlQuietly​(java.net.URL url)
      • decodeUrlAsFile

        public static java.io.File decodeUrlAsFile​(java.net.URL url)
      • removeFileName

        public static java.net.URL removeFileName​(java.net.URL src)
        This function i striping part behind last path delimiter. Expected is input like protocol://som.url/some/path/file.suff Then output will bee protocol://som.url/some/path Be aware of input like protocol://som.url/some/path/ then input will be just protocol://som.url/some/path You can use sanitizeLastSlash and see also unit tests Both unix and windows slashes are supported
        Parameters:
        src - src to be stripped
        Returns:
        src without file
      • setOfUrlsToHtmlList

        public static java.lang.String setOfUrlsToHtmlList​(java.util.List<java.net.URL> remoteUrls)
        Small utility function creating li list from collection of urls
        Parameters:
        remoteUrls - list of urls
        Returns:
        String containing html item list of those urls
      • setOfUrlsToHtmlList

        private static java.lang.String setOfUrlsToHtmlList​(java.util.Collection<java.net.URL> remoteUrls,
                                                            int max)
        Small utility function creating li list from collection of urls
        Parameters:
        remoteUrls - list of urls
        Returns:
        String containing html item list of those urls
      • sanitizeLastSlash

        public static java.net.URL sanitizeLastSlash​(java.net.URL in)
                                              throws java.net.MalformedURLException
        This function is removing all tailing slashes of url and both unix and windows slashes are supported. See tests for valid and invalid inputs/outputs Shortly protocol://som.url/some/path/ or protocol://som.url/some/path//// (and same for windows protocol://som.url/some\path\\) will become protocol://som.url/some/path Even protocol://som.url/ is reduced to protocol://som.url When input is like
        Parameters:
        in - url t be sanitized
        Returns:
        url without trailing slash (if any)
        Throws:
        java.net.MalformedURLException - if original url was wrong
      • sanitizeLastSlash

        public static java.lang.String sanitizeLastSlash​(java.lang.String in)
      • guessCodeBase

        public static java.net.URL guessCodeBase​(JNLPFile file)
      • urlEquals

        public static boolean urlEquals​(java.net.URL u1,
                                        java.net.URL u2)
        Compares a URL using string compareNullableStrings of its protocol, host, port, path, query, and anchor. This method avoids the host name lookup that URL.equals does for http: protocol URLs. It may not return the same value as the URL.equals method (different hostnames that resolve to the same IP address, ie sourceforge.net and www.sourceforge.net).
        Parameters:
        u1 - first url to compareNullableStrings
        u2 - second url to compareNullableStrings
        Returns:
        whether the u1 and u2 points to same resource or not
      • urlRelativeTo

        public static boolean urlRelativeTo​(java.net.URL url,
                                            java.net.URL codebaseUrl)
        Checks whether url is relative (or equal) to codebaseUrl. both urls are processed by sanitizeLastSlash before actual equals. So protocol://som.url/some/path/ is same as protocol://som.url/some/path. Even protocol://som.url/some/path\ is same as protocol://som.url/some/path/ This method returns false in case url contains parent directory notation "..". See JNLP specification version 9, 3.4: 'A relative URL cannot contain parent directory notations, such as "..". It must denote a file that is stored in a subdirectory of the codebase.'
        Parameters:
        url - the url to check
        codebaseUrl - the url to check against
        Returns:
        true if url is relative to codebaseUrl
      • notNullUrlEquals

        static boolean notNullUrlEquals​(java.net.URL u1,
                                        java.net.URL u2)
      • compareNullableStrings

        static boolean compareNullableStrings​(java.lang.String s1,
                                              java.lang.String s2,
                                              boolean ignore)
        Compare strings that can be null.
        Parameters:
        s1 - first string to compareNullableStrings with s2
        s2 - second string to compareNullableStrings with s1
        ignore - switch to ignore case
      • getSanitizedPort

        public static int getSanitizedPort​(java.net.URL u)
      • getPort

        public static int getPort​(java.net.URL url)
      • getHostAndPort

        public static java.lang.String getHostAndPort​(java.net.URL url)
      • ensureSlashTail

        public static java.net.URL ensureSlashTail​(java.net.URL u)
      • ensureSlashTail

        public static java.lang.String ensureSlashTail​(java.lang.String s)
      • stripFile

        public static java.lang.String stripFile​(java.net.URL documentbase)
      • stripFileImp

        private static java.lang.String stripFileImp​(java.net.URL documentbase)
      • loadUrl

        public static java.lang.String loadUrl​(java.net.URL url)
                                        throws java.io.IOException
        Throws:
        java.io.IOException
      • loadUrl

        public static java.lang.String loadUrl​(java.net.URL url,
                                               java.nio.charset.Charset ch)
                                        throws java.io.IOException
        Throws:
        java.io.IOException
      • loadUrlWithInvalidHeader

        public static java.lang.String[] loadUrlWithInvalidHeader​(java.net.URL url)
                                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • loadUrlWithInvalidHeader

        public static java.lang.String[] loadUrlWithInvalidHeader​(java.net.URL url,
                                                                  java.nio.charset.Charset ch)
                                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • writeRequest

        public static void writeRequest​(java.io.OutputStream s,
                                        java.net.URL url)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • createSocketFromUrl

        public static java.net.Socket createSocketFromUrl​(java.net.URL url)
                                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • equalUrls

        public static boolean equalUrls​(java.net.URL url1,
                                        java.net.URL url2)