Package jodd.net

Class URLCoder


  • public class URLCoder
    extends java.lang.Object
    Encodes URLs correctly, significantly faster and more convenient.

    Here is an example of full URL: https://jodd:[email protected]:8080/file;p=1?q=2#third. It consist of:

    • scheme (https)
    • user (jodd)
    • password (ddoj)
    • host (www.jodd.org)
    • port (8080)
    • path (file)
    • path parameter (p=1)
    • query parameter (q=2)
    • fragment (third)
    Each URL part has its own encoding rules. The only correct way of encoding URLs is to encode each part separately, and then to concatenate results. For easier query building you can use builder. It provides fluent interface for defining query parameters.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  URLCoder.Builder  
    • Constructor Summary

      Constructors 
      Constructor Description
      URLCoder()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static URLCoder.Builder build​(java.lang.String path)
      Creates URL builder for user-friendly way of building URLs.
      static URLCoder.Builder build​(java.lang.String path, boolean encodePath)
      Creates URL builder with given path that can be optionally encoded.
      static java.lang.String encode​(java.lang.String string)  
      static java.lang.String encode​(java.lang.String string, java.nio.charset.Charset encoding)
      Encodes string using default RFCP rules.
      static java.lang.String encodeFragment​(java.lang.String fragment)  
      static java.lang.String encodeFragment​(java.lang.String fragment, java.nio.charset.Charset encoding)
      Encodes the given URI fragment with the given encoding.
      static java.lang.String encodeHost​(java.lang.String host)  
      static java.lang.String encodeHost​(java.lang.String host, java.nio.charset.Charset encoding)
      Encodes the given URI host with the given encoding.
      static java.lang.String encodeHttpUrl​(java.lang.String httpUrl)  
      static java.lang.String encodeHttpUrl​(java.lang.String httpUrl, java.nio.charset.Charset encoding)
      Encodes the given HTTP URI into an encoded String.
      static java.lang.String encodePath​(java.lang.String path)  
      static java.lang.String encodePath​(java.lang.String path, java.nio.charset.Charset encoding)
      Encodes the given URI path with the given encoding.
      static java.lang.String encodePathSegment​(java.lang.String segment)  
      static java.lang.String encodePathSegment​(java.lang.String segment, java.nio.charset.Charset encoding)
      Encodes the given URI path segment with the given encoding.
      static java.lang.String encodePort​(java.lang.String port)  
      static java.lang.String encodePort​(java.lang.String port, java.nio.charset.Charset encoding)
      Encodes the given URI port with the given encoding.
      static java.lang.String encodeQuery​(java.lang.String query)  
      static java.lang.String encodeQuery​(java.lang.String query, java.nio.charset.Charset encoding)
      Encodes the given URI query with the given encoding.
      static java.lang.String encodeQueryParam​(java.lang.String queryParam)  
      static java.lang.String encodeQueryParam​(java.lang.String queryParam, java.nio.charset.Charset encoding)
      Encodes the given URI query parameter with the given encoding.
      static java.lang.String encodeScheme​(java.lang.String scheme)  
      static java.lang.String encodeScheme​(java.lang.String scheme, java.nio.charset.Charset encoding)
      Encodes the given URI scheme with the given encoding.
      static java.lang.String encodeUri​(java.lang.String uri)  
      static java.lang.String encodeUri​(java.lang.String uri, java.nio.charset.Charset encoding)
      Encodes the given source URI into an encoded String.
      static java.lang.String encodeUserInfo​(java.lang.String userInfo)  
      static java.lang.String encodeUserInfo​(java.lang.String userInfo, java.nio.charset.Charset encoding)
      Encodes the given URI user info with the given encoding.
      • Methods inherited from class java.lang.Object

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

      • URLCoder

        public URLCoder()
    • Method Detail

      • encode

        public static java.lang.String encode​(java.lang.String string,
                                              java.nio.charset.Charset encoding)
        Encodes string using default RFCP rules.
      • encode

        public static java.lang.String encode​(java.lang.String string)
      • encodeScheme

        public static java.lang.String encodeScheme​(java.lang.String scheme,
                                                    java.nio.charset.Charset encoding)
        Encodes the given URI scheme with the given encoding.
      • encodeScheme

        public static java.lang.String encodeScheme​(java.lang.String scheme)
      • encodeUserInfo

        public static java.lang.String encodeUserInfo​(java.lang.String userInfo,
                                                      java.nio.charset.Charset encoding)
        Encodes the given URI user info with the given encoding.
      • encodeUserInfo

        public static java.lang.String encodeUserInfo​(java.lang.String userInfo)
      • encodeHost

        public static java.lang.String encodeHost​(java.lang.String host,
                                                  java.nio.charset.Charset encoding)
        Encodes the given URI host with the given encoding.
      • encodeHost

        public static java.lang.String encodeHost​(java.lang.String host)
      • encodePort

        public static java.lang.String encodePort​(java.lang.String port,
                                                  java.nio.charset.Charset encoding)
        Encodes the given URI port with the given encoding.
      • encodePort

        public static java.lang.String encodePort​(java.lang.String port)
      • encodePath

        public static java.lang.String encodePath​(java.lang.String path,
                                                  java.nio.charset.Charset encoding)
        Encodes the given URI path with the given encoding.
      • encodePath

        public static java.lang.String encodePath​(java.lang.String path)
      • encodePathSegment

        public static java.lang.String encodePathSegment​(java.lang.String segment,
                                                         java.nio.charset.Charset encoding)
        Encodes the given URI path segment with the given encoding.
      • encodePathSegment

        public static java.lang.String encodePathSegment​(java.lang.String segment)
      • encodeQuery

        public static java.lang.String encodeQuery​(java.lang.String query,
                                                   java.nio.charset.Charset encoding)
        Encodes the given URI query with the given encoding.
      • encodeQuery

        public static java.lang.String encodeQuery​(java.lang.String query)
      • encodeQueryParam

        public static java.lang.String encodeQueryParam​(java.lang.String queryParam,
                                                        java.nio.charset.Charset encoding)
        Encodes the given URI query parameter with the given encoding.
      • encodeQueryParam

        public static java.lang.String encodeQueryParam​(java.lang.String queryParam)
      • encodeFragment

        public static java.lang.String encodeFragment​(java.lang.String fragment,
                                                      java.nio.charset.Charset encoding)
        Encodes the given URI fragment with the given encoding.
      • encodeFragment

        public static java.lang.String encodeFragment​(java.lang.String fragment)
      • encodeUri

        public static java.lang.String encodeUri​(java.lang.String uri,
                                                 java.nio.charset.Charset encoding)
        Encodes the given source URI into an encoded String. All various URI components are encoded according to their respective valid character sets.

        This method does not attempt to encode "=" and "&" characters in query parameter names and query parameter values because they cannot be parsed in a reliable way.

      • encodeHttpUrl

        public static java.lang.String encodeHttpUrl​(java.lang.String httpUrl,
                                                     java.nio.charset.Charset encoding)
        Encodes the given HTTP URI into an encoded String. All various URI components are encoded according to their respective valid character sets.

        This method does not support fragments (#), as these are not supposed to be sent to the server, but retained by the client.

        This method does not attempt to encode "=" and "&" characters in query parameter names and query parameter values because they cannot be parsed in a reliable way.

      • build

        public static URLCoder.Builder build​(java.lang.String path,
                                             boolean encodePath)
        Creates URL builder with given path that can be optionally encoded. Since most of the time path is valid and does not require to be encoded, use this method to gain some performance. When encoding flag is turned off, provided path is used without processing.

        The purpose of builder is to help with query parameters. All other URI parts should be set previously or after the URL is built.