Package org.springframework.web.util
Class UriUtils
java.lang.Object
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 (for example, 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.
- Since:
- 3.0
- Author:
- Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev
- See Also:
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic StringDecode the given encoded URI component.static StringDecode the given encoded URI component value by replacing "%xy" sequences by an hexadecimal representation of the character in the specified charset, letting other characters unchanged.static StringVariant ofencode(String, Charset)with a String charset.static StringEncode all characters that are either illegal, or have any reserved meaning, anywhere within a URI, as defined in RFC 3986.static StringencodeAuthority(String authority, String encoding) Encode the given URI authority with the given encoding.static StringencodeAuthority(String authority, Charset charset) Encode the given URI authority with the given encoding.static StringencodeFragment(String fragment, String encoding) Encode the given URI fragment with the given encoding.static StringencodeFragment(String fragment, Charset charset) Encode the given URI fragment with the given encoding.static StringencodeHost(String host, String encoding) Encode the given URI host with the given encoding.static StringencodeHost(String host, Charset charset) Encode the given URI host with the given encoding.static StringencodePath(String path, String encoding) Encode the given URI path with the given encoding.static StringencodePath(String path, Charset charset) Encode the given URI path with the given encoding.static StringencodePathSegment(String segment, String encoding) Encode the given URI path segment with the given encoding.static StringencodePathSegment(String segment, Charset charset) Encode the given URI path segment with the given encoding.static StringencodePort(String port, String encoding) Encode the given URI port with the given encoding.static StringencodePort(String port, Charset charset) Encode the given URI port with the given encoding.static StringencodeQuery(String query, String encoding) Encode the given URI query with the given encoding.static StringencodeQuery(String query, Charset charset) Encode the given URI query with the given encoding.static StringencodeQueryParam(String queryParam, String encoding) Encode the given URI query parameter with the given encoding.static StringencodeQueryParam(String queryParam, Charset charset) Encode the given URI query parameter with the given encoding.static MultiValueMap<String,String> encodeQueryParams(MultiValueMap<String, String> params) Encode the query parameters from the givenMultiValueMapwith UTF-8.static StringencodeScheme(String scheme, String encoding) Encode the given URI scheme with the given encoding.static StringencodeScheme(String scheme, Charset charset) Encode the given URI scheme with the given encoding.static Object[]encodeUriVariables(Object... uriVariables) Convenience method to applyencode(String, Charset)to all given URI variable values.encodeUriVariables(Map<String, ?> uriVariables) Convenience method to applyencode(String, Charset)to all given URI variable values.static StringencodeUserInfo(String userInfo, String encoding) Encode the given URI user info with the given encoding.static StringencodeUserInfo(String userInfo, Charset charset) Encode the given URI user info with the given encoding.static StringextractFileExtension(String path) Extract the file extension from the given URI path.
- 
Constructor Details- 
UriUtilspublic UriUtils()
 
- 
- 
Method Details- 
encodeSchemeEncode 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
 
- 
encodeSchemeEncode 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
- Since:
- 5.0
 
- 
encodeAuthorityEncode 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
 
- 
encodeAuthorityEncode 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
- Since:
- 5.0
 
- 
encodeUserInfoEncode 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
 
- 
encodeUserInfoEncode 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
- Since:
- 5.0
 
- 
encodeHostEncode 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
 
- 
encodeHostEncode 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
- Since:
- 5.0
 
- 
encodePortEncode 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
 
- 
encodePortEncode 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
- Since:
- 5.0
 
- 
encodePathEncode 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
 
- 
encodePathEncode 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
- Since:
- 5.0
 
- 
encodePathSegmentEncode 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
 
- 
encodePathSegmentEncode 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
- Since:
- 5.0
 
- 
encodeQueryEncode 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
 
- 
encodeQueryEncode 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
- Since:
- 5.0
 
- 
encodeQueryParamEncode 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
 
- 
encodeQueryParamEncode 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
- Since:
- 5.0
 
- 
encodeQueryParamsEncode the query parameters from the givenMultiValueMapwith UTF-8.This can be used with UriComponentsBuilder.queryParams(MultiValueMap)when building a URI from an already encoded template.MultiValueMap<String, String> params = new LinkedMultiValueMap<>(2); // add to params... ServletUriComponentsBuilder.fromCurrentRequest() .queryParams(UriUtils.encodeQueryParams(params)) .build(true) .toUriString();- Parameters:
- params- the parameters to encode
- Returns:
- a new MultiValueMapwith the encoded names and values
- Since:
- 5.2.3
 
- 
encodeFragmentEncode 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
 
- 
encodeFragmentEncode 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
- Since:
- 5.0
 
- 
encodeVariant ofencode(String, Charset)with a String charset.- Parameters:
- source- the String to be encoded
- encoding- the character encoding to encode to
- Returns:
- the encoded String
 
- 
encodeEncode 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
- Since:
- 5.0
 
- 
encodeUriVariablesConvenience method to applyencode(String, Charset)to all given URI variable values.- Parameters:
- uriVariables- the URI variable values to be encoded
- Returns:
- the encoded String
- Since:
- 5.0
 
- 
encodeUriVariablesConvenience method to applyencode(String, Charset)to all given URI variable values.- Parameters:
- uriVariables- the URI variable values to be encoded
- Returns:
- the encoded String
- Since:
- 5.0
 
- 
decodeDecode the given encoded URI component.See StringUtils.uriDecode(String, Charset)for the decoding rules.- 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:
 
- 
decodeDecode the given encoded URI component value by replacing "%xy" sequences by an hexadecimal representation of the character in the specified charset, letting other characters unchanged.- Parameters:
- source- the encoded- String
- charset- the character encoding to use to decode the "- %xy" sequences
- Returns:
- the decoded value
- Throws:
- IllegalArgumentException- when the given source contains invalid encoded sequences
- Since:
- 5.0
- See Also:
 
- 
extractFileExtensionExtract the file extension from the given URI path.- Parameters:
- path- the URI path (for example, "/products/index.html")
- Returns:
- the extracted file extension (for example, "html")
- Since:
- 4.3.2
 
 
-