org.springframework.web.util
Class UriUtils

java.lang.Object
  extended by org.springframework.web.util.UriUtils

public abstract class UriUtils
extends java.lang.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:

Since:
3.0
Author:
Arjen Poutsma
See Also:
RFC 3986

Field Summary
private static java.util.BitSet FRAGMENT
           
private static java.util.BitSet HOST
           
private static java.lang.String HOST_PATTERN
           
private static java.lang.String HTTP_PATTERN
           
private static java.util.regex.Pattern HTTP_URL_PATTERN
           
private static java.lang.String LAST_PATTERN
           
private static java.util.BitSet PATH
           
private static java.lang.String PATH_PATTERN
           
private static java.util.BitSet PORT
           
private static java.lang.String PORT_PATTERN
           
private static java.util.BitSet QUERY
           
private static java.util.BitSet QUERY_PARAM
           
private static java.lang.String QUERY_PATTERN
           
private static java.util.BitSet SCHEME
           
private static java.lang.String SCHEME_PATTERN
           
private static java.util.BitSet SEGMENT
           
private static java.util.regex.Pattern URI_PATTERN
           
private static java.util.BitSet USER_INFO
           
private static java.lang.String USERINFO_PATTERN
           
 
Constructor Summary
UriUtils()
           
 
Method Summary
static java.lang.String decode(java.lang.String source, java.lang.String encoding)
          Decodes the given encoded source String into an URI.
private static byte[] encode(byte[] source, java.util.BitSet notEncoded)
           
private static java.lang.String encode(java.lang.String source, java.lang.String encoding, java.util.BitSet notEncoded)
           
static java.lang.String encodeFragment(java.lang.String fragment, java.lang.String encoding)
          Encodes the given URI fragment.
static java.lang.String encodeHost(java.lang.String host, java.lang.String encoding)
          Encodes the given URI host.
static java.lang.String encodeHttpUrl(java.lang.String httpUrl, java.lang.String encoding)
          Encodes the given HTTP URI into an encoded String.
static java.lang.String encodePath(java.lang.String path, java.lang.String encoding)
          Encodes the given URI path.
static java.lang.String encodePathSegment(java.lang.String segment, java.lang.String encoding)
          Encodes the given URI path segment.
static java.lang.String encodePort(java.lang.String port, java.lang.String encoding)
          Encodes the given URI port.
static java.lang.String encodeQuery(java.lang.String query, java.lang.String encoding)
          Encodes the given URI query.
static java.lang.String encodeQueryParam(java.lang.String queryParam, java.lang.String encoding)
          Encodes the given URI query parameter.
static java.lang.String encodeScheme(java.lang.String scheme, java.lang.String encoding)
          Encodes the given URI scheme.
static java.lang.String encodeUri(java.lang.String uri, java.lang.String encoding)
          Encodes the given source URI into an encoded String.
static java.lang.String encodeUriComponents(java.lang.String scheme, java.lang.String authority, java.lang.String userinfo, java.lang.String host, java.lang.String port, java.lang.String path, java.lang.String query, java.lang.String fragment, java.lang.String encoding)
          Encodes the given source URI components into an encoded String.
static java.lang.String encodeUserInfo(java.lang.String userInfo, java.lang.String encoding)
          Encodes the given URI user info.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SCHEME

private static final java.util.BitSet SCHEME

USER_INFO

private static final java.util.BitSet USER_INFO

HOST

private static final java.util.BitSet HOST

PORT

private static final java.util.BitSet PORT

PATH

private static final java.util.BitSet PATH

SEGMENT

private static final java.util.BitSet SEGMENT

QUERY

private static final java.util.BitSet QUERY

QUERY_PARAM

private static final java.util.BitSet QUERY_PARAM

FRAGMENT

private static final java.util.BitSet FRAGMENT

SCHEME_PATTERN

private static final java.lang.String SCHEME_PATTERN
See Also:
Constant Field Values

HTTP_PATTERN

private static final java.lang.String HTTP_PATTERN
See Also:
Constant Field Values

USERINFO_PATTERN

private static final java.lang.String USERINFO_PATTERN
See Also:
Constant Field Values

HOST_PATTERN

private static final java.lang.String HOST_PATTERN
See Also:
Constant Field Values

PORT_PATTERN

private static final java.lang.String PORT_PATTERN
See Also:
Constant Field Values

PATH_PATTERN

private static final java.lang.String PATH_PATTERN
See Also:
Constant Field Values

QUERY_PATTERN

private static final java.lang.String QUERY_PATTERN
See Also:
Constant Field Values

LAST_PATTERN

private static final java.lang.String LAST_PATTERN
See Also:
Constant Field Values

URI_PATTERN

private static final java.util.regex.Pattern URI_PATTERN

HTTP_URL_PATTERN

private static final java.util.regex.Pattern HTTP_URL_PATTERN
Constructor Detail

UriUtils

public UriUtils()
Method Detail

encodeUri

public static java.lang.String encodeUri(java.lang.String uri,
                                         java.lang.String encoding)
                                  throws java.io.UnsupportedEncodingException
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:
java.lang.IllegalArgumentException - when the given uri parameter is not a valid URI
java.io.UnsupportedEncodingException - when the given encoding parameter is not supported

encodeHttpUrl

public static java.lang.String encodeHttpUrl(java.lang.String httpUrl,
                                             java.lang.String encoding)
                                      throws java.io.UnsupportedEncodingException
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:
java.lang.IllegalArgumentException - when the given uri parameter is not a valid URI
java.io.UnsupportedEncodingException - when the given encoding parameter is not supported

encodeUriComponents

public static java.lang.String encodeUriComponents(java.lang.String scheme,
                                                   java.lang.String authority,
                                                   java.lang.String userinfo,
                                                   java.lang.String host,
                                                   java.lang.String port,
                                                   java.lang.String path,
                                                   java.lang.String query,
                                                   java.lang.String fragment,
                                                   java.lang.String encoding)
                                            throws java.io.UnsupportedEncodingException
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:
java.lang.IllegalArgumentException - when the given uri parameter is not a valid URI
java.io.UnsupportedEncodingException - when the given encoding parameter is not supported

encodeScheme

public static java.lang.String encodeScheme(java.lang.String scheme,
                                            java.lang.String encoding)
                                     throws java.io.UnsupportedEncodingException
Encodes the given URI scheme.

Parameters:
scheme - the scheme to be encoded
encoding - the character encoding to encode to
Returns:
the encoded scheme
Throws:
java.io.UnsupportedEncodingException - when the given encoding parameter is not supported

encodeUserInfo

public static java.lang.String encodeUserInfo(java.lang.String userInfo,
                                              java.lang.String encoding)
                                       throws java.io.UnsupportedEncodingException
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:
java.io.UnsupportedEncodingException - when the given encoding parameter is not supported

encodeHost

public static java.lang.String encodeHost(java.lang.String host,
                                          java.lang.String encoding)
                                   throws java.io.UnsupportedEncodingException
Encodes the given URI host.

Parameters:
host - the host to be encoded
encoding - the character encoding to encode to
Returns:
the encoded host
Throws:
java.io.UnsupportedEncodingException - when the given encoding parameter is not supported

encodePort

public static java.lang.String encodePort(java.lang.String port,
                                          java.lang.String encoding)
                                   throws java.io.UnsupportedEncodingException
Encodes the given URI port.

Parameters:
port - the port to be encoded
encoding - the character encoding to encode to
Returns:
the encoded port
Throws:
java.io.UnsupportedEncodingException - when the given encoding parameter is not supported

encodePath

public static java.lang.String encodePath(java.lang.String path,
                                          java.lang.String encoding)
                                   throws java.io.UnsupportedEncodingException
Encodes the given URI path.

Parameters:
path - the path to be encoded
encoding - the character encoding to encode to
Returns:
the encoded path
Throws:
java.io.UnsupportedEncodingException - when the given encoding parameter is not supported

encodePathSegment

public static java.lang.String encodePathSegment(java.lang.String segment,
                                                 java.lang.String encoding)
                                          throws java.io.UnsupportedEncodingException
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:
java.io.UnsupportedEncodingException - when the given encoding parameter is not supported

encodeQuery

public static java.lang.String encodeQuery(java.lang.String query,
                                           java.lang.String encoding)
                                    throws java.io.UnsupportedEncodingException
Encodes the given URI query.

Parameters:
query - the query to be encoded
encoding - the character encoding to encode to
Returns:
the encoded query
Throws:
java.io.UnsupportedEncodingException - when the given encoding parameter is not supported

encodeQueryParam

public static java.lang.String encodeQueryParam(java.lang.String queryParam,
                                                java.lang.String encoding)
                                         throws java.io.UnsupportedEncodingException
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:
java.io.UnsupportedEncodingException - when the given encoding parameter is not supported

encodeFragment

public static java.lang.String encodeFragment(java.lang.String fragment,
                                              java.lang.String encoding)
                                       throws java.io.UnsupportedEncodingException
Encodes the given URI fragment.

Parameters:
fragment - the fragment to be encoded
encoding - the character encoding to encode to
Returns:
the encoded fragment
Throws:
java.io.UnsupportedEncodingException - when the given encoding parameter is not supported

encode

private static java.lang.String encode(java.lang.String source,
                                       java.lang.String encoding,
                                       java.util.BitSet notEncoded)
                                throws java.io.UnsupportedEncodingException
Throws:
java.io.UnsupportedEncodingException

encode

private static byte[] encode(byte[] source,
                             java.util.BitSet notEncoded)

decode

public static java.lang.String decode(java.lang.String source,
                                      java.lang.String encoding)
                               throws java.io.UnsupportedEncodingException
Decodes the given encoded source String into an URI. Based on the following rules:

Parameters:
source - the source string
encoding - the encoding
Returns:
the decoded URI
Throws:
java.io.UnsupportedEncodingException - when the given encoding parameter is not supported
See Also:
URLDecoder.decode(String, String)