Package org.springframework.web.util
Class UriUtils
- java.lang.Object
- org.springframework.web.util.UriUtils
public abstract class UriUtils extends Object
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.
- Since:
- 3.0
- Author:
- Arjen Poutsma, Juergen Hoeller
- See Also:
- RFC 3986
Constructor Summary
Constructors Constructor Description UriUtils()
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
decode(String source, String encoding)
Decode the given encoded URI component.static String
encode(String source, String encoding)
Encode characters outside the unreserved character set as defined in RFC 3986 Section 2.static String
encodeAuthority(String authority, String encoding)
Encode the given URI authority with the given encoding.static String
encodeFragment(String fragment, String encoding)
Encode the given URI fragment with the given encoding.static String
encodeHost(String host, String encoding)
Encode the given URI host with the given encoding.static String
encodePath(String path, String encoding)
Encode the given URI path with the given encoding.static String
encodePathSegment(String segment, String encoding)
Encode the given URI path segment with the given encoding.static String
encodePort(String port, String encoding)
Encode the given URI port with the given encoding.static String
encodeQuery(String query, String encoding)
Encode the given URI query with the given encoding.static String
encodeQueryParam(String queryParam, String encoding)
Encode the given URI query parameter with the given encoding.static String
encodeScheme(String scheme, String encoding)
Encode the given URI scheme with the given encoding.static String
encodeUserInfo(String userInfo, String encoding)
Encode the given URI user info with the given encoding.static String
extractFileExtension(String path)
Extract the file extension from the given URI path.
Constructor Detail
UriUtils
public UriUtils()
Method Detail
encodeScheme
public static String encodeScheme(String scheme, String encoding) throws UnsupportedEncodingException
Encode the given URI scheme with the given encoding.- Parameters:
scheme
- the scheme to be encodedencoding
- the character encoding to encode to- Returns:
- the encoded scheme
- Throws:
UnsupportedEncodingException
- when the given encoding parameter is not supported
encodeAuthority
public static String encodeAuthority(String authority, String encoding) throws UnsupportedEncodingException
Encode the given URI authority with the given encoding.- Parameters:
authority
- the authority to be encodedencoding
- the character encoding to encode to- Returns:
- the encoded authority
- Throws:
UnsupportedEncodingException
- when the given encoding parameter is not supported
encodeUserInfo
public static String encodeUserInfo(String userInfo, String encoding) throws UnsupportedEncodingException
Encode the given URI user info with the given encoding.- Parameters:
userInfo
- the user info to be encodedencoding
- the character encoding to encode to- Returns:
- the encoded user info
- Throws:
UnsupportedEncodingException
- when the given encoding parameter is not supported
encodeHost
public static String encodeHost(String host, String encoding) throws UnsupportedEncodingException
Encode the given URI host with the given encoding.- Parameters:
host
- the host to be encodedencoding
- the character encoding to encode to- Returns:
- the encoded host
- Throws:
UnsupportedEncodingException
- when the given encoding parameter is not supported
encodePort
public static String encodePort(String port, String encoding) throws UnsupportedEncodingException
Encode the given URI port with the given encoding.- Parameters:
port
- the port to be encodedencoding
- the character encoding to encode to- Returns:
- the encoded port
- Throws:
UnsupportedEncodingException
- when the given encoding parameter is not supported
encodePath
public static String encodePath(String path, String encoding) throws UnsupportedEncodingException
Encode the given URI path with the given encoding.- Parameters:
path
- the path to be encodedencoding
- the character encoding to encode to- Returns:
- the encoded path
- Throws:
UnsupportedEncodingException
- when the given encoding parameter is not supported
encodePathSegment
public static String encodePathSegment(String segment, String encoding) throws UnsupportedEncodingException
Encode the given URI path segment with the given encoding.- Parameters:
segment
- the segment to be encodedencoding
- the character encoding to encode to- Returns:
- the encoded segment
- Throws:
UnsupportedEncodingException
- when the given encoding parameter is not supported
encodeQuery
public static String encodeQuery(String query, String encoding) throws UnsupportedEncodingException
Encode the given URI query with the given encoding.- Parameters:
query
- the query to be encodedencoding
- the character encoding to encode to- Returns:
- the encoded query
- Throws:
UnsupportedEncodingException
- when the given encoding parameter is not supported
encodeQueryParam
public static String encodeQueryParam(String queryParam, String encoding) throws UnsupportedEncodingException
Encode the given URI query parameter with the given encoding.- Parameters:
queryParam
- the query parameter to be encodedencoding
- the character encoding to encode to- Returns:
- the encoded query parameter
- Throws:
UnsupportedEncodingException
- when the given encoding parameter is not supported
encodeFragment
public static String encodeFragment(String fragment, String encoding) throws UnsupportedEncodingException
Encode the given URI fragment with the given encoding.- Parameters:
fragment
- the fragment to be encodedencoding
- the character encoding to encode to- Returns:
- the encoded fragment
- Throws:
UnsupportedEncodingException
- when the given encoding parameter is not supported
encode
public static String encode(String source, String encoding) throws UnsupportedEncodingException
Encode characters outside the unreserved character set as defined in RFC 3986 Section 2.This can be used to ensure the given String will not contain any characters with reserved URI meaning regardless of URI component.
- Parameters:
source
- the String to be encodedencoding
- the character encoding to encode to- Returns:
- the encoded String
- Throws:
UnsupportedEncodingException
- when the given encoding parameter is not supported
decode
public static String decode(String source, String encoding) throws UnsupportedEncodingException
Decode the given encoded URI component.- 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 Stringencoding
- the encoding- Returns:
- the decoded value
- Throws:
IllegalArgumentException
- when the given source contains invalid encoded sequencesUnsupportedEncodingException
- when the given encoding parameter is not supported- See Also:
URLDecoder.decode(String, String)
- Alphanumeric characters
extractFileExtension
public static String extractFileExtension(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")
- Since:
- 4.3.2