Class UriUtils

java.lang.Object
com.aspectran.web.support.util.UriUtils

public abstract class UriUtils extends Object

This class is a clone of org.springframework.web.util.UriUtils

Utility methods for URI encoding and decoding based on RFC 3986.

There are two types of encode methods:

  • "encodeXyz" -- these encode a specific URI component (e.g. path, query) by percent encoding illegal characters, which includes non-US-ASCII characters, and also characters that are otherwise illegal within the given URI component type, as defined in RFC 3986. The effect of this method, with regards to encoding, is comparable to using the multi-argument constructor of URI.
  • "encode" and "encodeUriVariables" -- these can be used to encode URI variable values by percent encoding all characters that are either illegal, or have any reserved meaning, anywhere within a URI.
See Also:
  • Constructor Details

    • UriUtils

      public UriUtils()
  • Method Details

    • encodeScheme

      public static String encodeScheme(String scheme, String encoding)
      Encode the given URI scheme with the given encoding.
      Parameters:
      scheme - the scheme to be encoded
      encoding - the character encoding to encode to
      Returns:
      the encoded scheme
    • encodeScheme

      public static String encodeScheme(String scheme, Charset charset)
      Encode the given URI scheme with the given encoding.
      Parameters:
      scheme - the scheme to be encoded
      charset - the character encoding to encode to
      Returns:
      the encoded scheme
    • encodeAuthority

      public static String encodeAuthority(String authority, String encoding)
      Encode the given URI authority with the given encoding.
      Parameters:
      authority - the authority to be encoded
      encoding - the character encoding to encode to
      Returns:
      the encoded authority
    • encodeAuthority

      public static String encodeAuthority(String authority, Charset charset)
      Encode the given URI authority with the given encoding.
      Parameters:
      authority - the authority to be encoded
      charset - the character encoding to encode to
      Returns:
      the encoded authority
    • encodeUserInfo

      public static String encodeUserInfo(String userInfo, String encoding)
      Encode the given URI user info with the given encoding.
      Parameters:
      userInfo - the user info to be encoded
      encoding - the character encoding to encode to
      Returns:
      the encoded user info
    • encodeUserInfo

      public static String encodeUserInfo(String userInfo, Charset charset)
      Encode the given URI user info with the given encoding.
      Parameters:
      userInfo - the user info to be encoded
      charset - the character encoding to encode to
      Returns:
      the encoded user info
    • encodeHost

      public static String encodeHost(String host, String encoding)
      Encode the given URI host with the given encoding.
      Parameters:
      host - the host to be encoded
      encoding - the character encoding to encode to
      Returns:
      the encoded host
    • encodeHost

      public static String encodeHost(String host, Charset charset)
      Encode the given URI host with the given encoding.
      Parameters:
      host - the host to be encoded
      charset - the character encoding to encode to
      Returns:
      the encoded host
    • encodePort

      public static String encodePort(String port, String encoding)
      Encode the given URI port with the given encoding.
      Parameters:
      port - the port to be encoded
      encoding - the character encoding to encode to
      Returns:
      the encoded port
    • encodePort

      public static String encodePort(String port, Charset charset)
      Encode the given URI port with the given encoding.
      Parameters:
      port - the port to be encoded
      charset - the character encoding to encode to
      Returns:
      the encoded port
    • encodePath

      public static String encodePath(String path, String encoding)
      Encode the given URI path with the given encoding.
      Parameters:
      path - the path to be encoded
      encoding - the character encoding to encode to
      Returns:
      the encoded path
    • encodePath

      public static String encodePath(String path, Charset charset)
      Encode the given URI path with the given encoding.
      Parameters:
      path - the path to be encoded
      charset - the character encoding to encode to
      Returns:
      the encoded path
    • encodePathSegment

      public static String encodePathSegment(String segment, String encoding)
      Encode the given URI path segment with the given encoding.
      Parameters:
      segment - the segment to be encoded
      encoding - the character encoding to encode to
      Returns:
      the encoded segment
    • encodePathSegment

      public static String encodePathSegment(String segment, Charset charset)
      Encode the given URI path segment with the given encoding.
      Parameters:
      segment - the segment to be encoded
      charset - the character encoding to encode to
      Returns:
      the encoded segment
    • encodeQuery

      public static String encodeQuery(String query, String encoding)
      Encode the given URI query with the given encoding.
      Parameters:
      query - the query to be encoded
      encoding - the character encoding to encode to
      Returns:
      the encoded query
    • encodeQuery

      public static String encodeQuery(String query, Charset charset)
      Encode the given URI query with the given encoding.
      Parameters:
      query - the query to be encoded
      charset - the character encoding to encode to
      Returns:
      the encoded query
    • encodeQueryParam

      public static String encodeQueryParam(String queryParam, String encoding)
      Encode the given URI query parameter with the given encoding.
      Parameters:
      queryParam - the query parameter to be encoded
      encoding - the character encoding to encode to
      Returns:
      the encoded query parameter
    • encodeQueryParam

      public static String encodeQueryParam(String queryParam, Charset charset)
      Encode the given URI query parameter with the given encoding.
      Parameters:
      queryParam - the query parameter to be encoded
      charset - the character encoding to encode to
      Returns:
      the encoded query parameter
    • encodeQueryParams

      @NonNull public static MultiValueMap<String,String> encodeQueryParams(@NonNull MultiValueMap<String,String> params)
      Encode the query parameters from the given MultiValueMap with UTF-8.
      Parameters:
      params - the parameters to encode
      Returns:
      a new MultiValueMap with the encoded names and values
    • encodeFragment

      public static String encodeFragment(String fragment, String encoding)
      Encode the given URI fragment with the given encoding.
      Parameters:
      fragment - the fragment to be encoded
      encoding - the character encoding to encode to
      Returns:
      the encoded fragment
    • encodeFragment

      public static String encodeFragment(String fragment, Charset charset)
      Encode the given URI fragment with the given encoding.
      Parameters:
      fragment - the fragment to be encoded
      charset - the character encoding to encode to
      Returns:
      the encoded fragment
    • encode

      public static String encode(String source, String encoding)
      Variant of encode(String, Charset) with a String charset.
      Parameters:
      source - the String to be encoded
      encoding - the character encoding to encode to
      Returns:
      the encoded String
    • encode

      public static String encode(String source, Charset charset)
      Encode all characters that are either illegal, or have any reserved meaning, anywhere within a URI, as defined in RFC 3986. This is useful to ensure that the given String will be preserved as-is and will not have any impact on the structure or meaning of the URI.
      Parameters:
      source - the String to be encoded
      charset - the character encoding to encode to
      Returns:
      the encoded String
    • encodeUriVariables

      @NonNull public static Map<String,String> encodeUriVariables(@NonNull Map<String,?> uriVariables)
      Convenience method to apply encode(String, Charset) to all given URI variable values.
      Parameters:
      uriVariables - the URI variable values to be encoded
      Returns:
      the encoded String
    • encodeUriVariables

      @NonNull public static Object[] encodeUriVariables(Object... uriVariables)
      Convenience method to apply encode(String, Charset) to all given URI variable values.
      Parameters:
      uriVariables - the URI variable values to be encoded
      Returns:
      the encoded String
    • decode

      public static String decode(String source, String encoding)
      Decode the given encoded URI component.
      Parameters:
      source - the encoded String
      encoding - the character encoding to use
      Returns:
      the decoded value
      Throws:
      IllegalArgumentException - when the given source contains invalid encoded sequences
      See Also:
    • decode

      public static String decode(@NonNull String source, Charset charset)
      Decode the given encoded URI component value. Based on the following rules:
      • Alphanumeric characters "a" through "z", "A" through "Z", and "0" through "9" stay the same.
      • Special characters "-", "_", ".", and "*" stay the same.
      • A sequence "%<i>xy</i>" is interpreted as a hexadecimal representation of the character.
      Parameters:
      source - the encoded String
      charset - the character set
      Returns:
      the decoded value
      Throws:
      IllegalArgumentException - when the given source contains invalid encoded sequences
      See Also:
    • extractFileExtension

      @Nullable public static String extractFileExtension(@NonNull String path)
      Extract the file extension from the given URI path.
      Parameters:
      path - the URI path (e.g. "/products/index.html")
      Returns:
      the extracted file extension (e.g. "html")