Package jodd.net

Class URLCoder

java.lang.Object
jodd.net.URLCoder

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

Here is an example of full URL: https://jodd:ddoj@www.jodd.org: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.
  • Field Details

  • Constructor Details

    • URLCoder

      public URLCoder()
  • Method Details

    • encodeUriComponent

      private static String encodeUriComponent(String source, Charset encoding, URLCoder.URIPart uriPart)
      Encodes single URI component.
    • encodeBytes

      private static byte[] encodeBytes(byte[] source, URLCoder.URIPart uriPart)
      Encodes byte array using allowed characters from URLCoder.URIPart.
    • encode

      public static String encode(String string, Charset encoding)
      Encodes string using default RFCP rules.
    • encode

      public static String encode(String string)
    • encodeScheme

      public static String encodeScheme(String scheme, Charset encoding)
      Encodes the given URI scheme with the given encoding.
    • encodeScheme

      public static String encodeScheme(String scheme)
    • encodeUserInfo

      public static String encodeUserInfo(String userInfo, Charset encoding)
      Encodes the given URI user info with the given encoding.
    • encodeUserInfo

      public static String encodeUserInfo(String userInfo)
    • encodeHost

      public static String encodeHost(String host, Charset encoding)
      Encodes the given URI host with the given encoding.
    • encodeHost

      public static String encodeHost(String host)
    • encodePort

      public static String encodePort(String port, Charset encoding)
      Encodes the given URI port with the given encoding.
    • encodePort

      public static String encodePort(String port)
    • encodePath

      public static String encodePath(String path, Charset encoding)
      Encodes the given URI path with the given encoding.
    • encodePath

      public static String encodePath(String path)
    • encodePathSegment

      public static String encodePathSegment(String segment, Charset encoding)
      Encodes the given URI path segment with the given encoding.
    • encodePathSegment

      public static String encodePathSegment(String segment)
    • encodeQuery

      public static String encodeQuery(String query, Charset encoding)
      Encodes the given URI query with the given encoding.
    • encodeQuery

      public static String encodeQuery(String query)
    • encodeQueryParam

      public static String encodeQueryParam(String queryParam, Charset encoding)
      Encodes the given URI query parameter with the given encoding.
    • encodeQueryParam

      public static String encodeQueryParam(String queryParam)
    • encodeFragment

      public static String encodeFragment(String fragment, Charset encoding)
      Encodes the given URI fragment with the given encoding.
    • encodeFragment

      public static String encodeFragment(String fragment)
    • encodeUri

      public static String encodeUri(String uri)
      See Also:
    • encodeUri

      public static String encodeUri(String uri, 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 String encodeHttpUrl(String httpUrl)
      See Also:
    • encodeHttpUrl

      public static String encodeHttpUrl(String httpUrl, 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.

    • encodeUriComponents

      private static String encodeUriComponents(String scheme, String authority, String userInfo, String host, String port, String path, String query, String fragment, Charset encoding)
    • build

      public static URLCoder.Builder build(String path)
      Creates URL builder for user-friendly way of building URLs. Provided path is parsed and encoded.
      See Also:
    • build

      public static URLCoder.Builder build(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.