public abstract class

UriUtils

extends Object
java.lang.Object
   ↳ org.springframework.web.util.UriUtils

Class Overview

Utility class for URI encoding and decoding based on RFC 3986. Offers encoding methods for the various URI components.

All encode*(String, String methods in this class operate in a similar way:

  • Valid characters for the specific URI component as defined in RFC 3986 stay the same.
  • All other characters are converted into one or more bytes in the given encoding scheme. Each of the resulting bytes is written as a hexadecimal string in the "%xy" format.

See Also

Summary

Public Constructors
UriUtils()
Public Methods
static String decode(String source, String encoding)
Decodes the given encoded source String into an URI.
static String encodeFragment(String fragment, String encoding)
Encodes the given URI fragment.
static String encodeHost(String host, String encoding)
Encodes the given URI host.
static String encodeHttpUrl(String httpUrl, String encoding)
Encodes the given HTTP URI into an encoded String.
static String encodePath(String path, String encoding)
Encodes the given URI path.
static String encodePathSegment(String segment, String encoding)
Encodes the given URI path segment.
static String encodePort(String port, String encoding)
Encodes the given URI port.
static String encodeQuery(String query, String encoding)
Encodes the given URI query.
static String encodeQueryParam(String queryParam, String encoding)
Encodes the given URI query parameter.
static String encodeScheme(String scheme, String encoding)
Encodes the given URI scheme.
static String encodeUri(String uri, String encoding)
Encodes the given source URI into an encoded String.
static String encodeUriComponents(String scheme, String authority, String userinfo, String host, String port, String path, String query, String fragment, String encoding)
Encodes the given source URI components into an encoded String.
static String encodeUserInfo(String userInfo, String encoding)
Encodes the given URI user info.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public UriUtils ()

Public Methods

public static String decode (String source, String encoding)

Decodes the given encoded source String into an URI. 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.
  • All other characters are converted into one or more bytes using the given encoding scheme. Each of the resulting bytes is written as a hexadecimal string in the %xy format.
  • A sequence "%xy" is interpreted as a hexadecimal representation of the character.

Parameters
source the source string
encoding the encoding
Returns
  • the decoded URI
Throws
UnsupportedEncodingException when the given encoding parameter is not supported

public static String encodeFragment (String fragment, String encoding)

Encodes the given URI fragment.

Parameters
fragment the fragment to be encoded
encoding the character encoding to encode to
Returns
  • the encoded fragment
Throws
UnsupportedEncodingException when the given encoding parameter is not supported

public static String encodeHost (String host, String encoding)

Encodes the given URI host.

Parameters
host the host to be encoded
encoding the character encoding to encode to
Returns
  • the encoded host
Throws
UnsupportedEncodingException when the given encoding parameter is not supported

public static String encodeHttpUrl (String httpUrl, String encoding)

Encodes the given HTTP URI into an encoded String. All various URI components are encoded according to their respective valid character sets.

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

Parameters
httpUrl the HTTP URL to be encoded
encoding the character encoding to encode to
Returns
  • the encoded URL
Throws
IllegalArgumentException when the given uri parameter is not a valid URI
UnsupportedEncodingException when the given encoding parameter is not supported

public static String encodePath (String path, String encoding)

Encodes the given URI path.

Parameters
path the path to be encoded
encoding the character encoding to encode to
Returns
  • the encoded path
Throws
UnsupportedEncodingException when the given encoding parameter is not supported

public static String encodePathSegment (String segment, String encoding)

Encodes the given URI path segment.

Parameters
segment the segment to be encoded
encoding the character encoding to encode to
Returns
  • the encoded segment
Throws
UnsupportedEncodingException when the given encoding parameter is not supported

public static String encodePort (String port, String encoding)

Encodes the given URI port.

Parameters
port the port to be encoded
encoding the character encoding to encode to
Returns
  • the encoded port
Throws
UnsupportedEncodingException when the given encoding parameter is not supported

public static String encodeQuery (String query, String encoding)

Encodes the given URI query.

Parameters
query the query to be encoded
encoding the character encoding to encode to
Returns
  • the encoded query
Throws
UnsupportedEncodingException when the given encoding parameter is not supported

public static String encodeQueryParam (String queryParam, String encoding)

Encodes the given URI query parameter.

Parameters
queryParam the query parameter to be encoded
encoding the character encoding to encode to
Returns
  • the encoded query parameter
Throws
UnsupportedEncodingException when the given encoding parameter is not supported

public static String encodeScheme (String scheme, String encoding)

Encodes the given URI scheme.

Parameters
scheme the scheme to be encoded
encoding the character encoding to encode to
Returns
  • the encoded scheme
Throws
UnsupportedEncodingException when the given encoding parameter is not supported

public static String encodeUri (String uri, String encoding)

Encodes the given source URI into an encoded String. All various URI components are encoded according to their respective valid character sets.

Parameters
uri the URI to be encoded
encoding the character encoding to encode to
Returns
  • the encoded URI
Throws
IllegalArgumentException when the given uri parameter is not a valid URI
UnsupportedEncodingException when the given encoding parameter is not supported

public static String encodeUriComponents (String scheme, String authority, String userinfo, String host, String port, String path, String query, String fragment, String encoding)

Encodes the given source URI components into an encoded String. All various URI components are optional, but encoded according to their respective valid character sets.

Parameters
scheme the scheme
authority the authority
userinfo the user info
host the host
port the port
path the path
query the query
fragment the fragment
encoding the character encoding to encode to
Returns
  • the encoded URI
Throws
IllegalArgumentException when the given uri parameter is not a valid URI
UnsupportedEncodingException when the given encoding parameter is not supported

public static String encodeUserInfo (String userInfo, String encoding)

Encodes the given URI user info.

Parameters
userInfo the user info to be encoded
encoding the character encoding to encode to
Returns
  • the encoded user info
Throws
UnsupportedEncodingException when the given encoding parameter is not supported