org.springframework.web.util
Class UriComponents

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

public final class UriComponents
extends java.lang.Object

Represents an immutable collection of URI components, mapping component type to string values. Contains convenience getters for all components. Effectively similar to URI, but with more powerful encoding options and support for URI template variables.

Since:
3.1
Author:
Arjen Poutsma
See Also:
UriComponentsBuilder

Nested Class Summary
(package private) static class UriComponents.FullPathComponent
          Represents a path backed by a string.
private static class UriComponents.MapTemplateVariables
          URI template variables backed by a map.
(package private) static interface UriComponents.PathComponent
          Defines the contract for path (segments).
(package private) static class UriComponents.PathComponentComposite
          Represents a collection of PathComponents.
(package private) static class UriComponents.PathSegmentComponent
          Represents a path backed by a string list (i.e.
(package private) static class UriComponents.Type
          Enumeration used to identify the parts of a URI.
private static interface UriComponents.UriTemplateVariables
          Defines the contract for URI Template variables
private static class UriComponents.VarArgsTemplateVariables
          URI template variables backed by a variable argument array.
 
Field Summary
private static java.lang.String DEFAULT_ENCODING
           
private  boolean encoded
           
private  java.lang.String fragment
           
private  java.lang.String host
           
private static java.util.regex.Pattern NAMES_PATTERN
          Captures URI template variable names.
(package private) static UriComponents.PathComponent NULL_PATH_COMPONENT
          Represents an empty path.
private  UriComponents.PathComponent path
           
private static char PATH_DELIMITER
           
private  int port
           
private  MultiValueMap<java.lang.String,java.lang.String> queryParams
           
private  java.lang.String scheme
           
private  java.lang.String userInfo
           
 
Constructor Summary
UriComponents(java.lang.String scheme, java.lang.String userInfo, java.lang.String host, int port, UriComponents.PathComponent path, MultiValueMap<java.lang.String,java.lang.String> queryParams, java.lang.String fragment, boolean encoded, boolean verify)
          Package-friendly constructor that creates a new UriComponents instance from the given parameters.
 
Method Summary
 UriComponents encode()
          Encodes all URI components using their specific encoding rules, and returns the result as a new UriComponents instance.
 UriComponents encode(java.lang.String encoding)
          Encodes all URI components using their specific encoding rules, and returns the result as a new UriComponents instance.
private static byte[] encodeBytes(byte[] source, UriComponents.Type type)
           
(package private) static java.lang.String encodeUriComponent(java.lang.String source, java.lang.String encoding, UriComponents.Type type)
          Encodes the given source into an encoded String using the rules specified by the given component and with the given options.
 boolean equals(java.lang.Object o)
           
 UriComponents expand(java.util.Map<java.lang.String,?> uriVariables)
          Replaces all URI template variables with the values from a given map.
 UriComponents expand(java.lang.Object... uriVariableValues)
          Replaces all URI template variables with the values from a given array.
private  UriComponents expandInternal(UriComponents.UriTemplateVariables uriVariables)
           
private static java.lang.String expandUriComponent(java.lang.String source, UriComponents.UriTemplateVariables uriVariables)
           
 java.lang.String getFragment()
          Returns the fragment.
 java.lang.String getHost()
          Returns the host.
 java.lang.String getPath()
          Returns the path.
 java.util.List<java.lang.String> getPathSegments()
          Returns the list of path segments.
 int getPort()
          Returns the port.
 java.lang.String getQuery()
          Returns the query.
 MultiValueMap<java.lang.String,java.lang.String> getQueryParams()
          Returns the map of query parameters.
 java.lang.String getScheme()
          Returns the scheme.
 java.lang.String getUserInfo()
          Returns the user info.
private static java.lang.String getVariableName(java.lang.String match)
           
private static java.lang.String getVariableValueAsString(java.lang.Object variableValue)
           
 int hashCode()
           
 java.lang.String toString()
           
 java.net.URI toUri()
          Returns a URI from this UriComponents instance.
 java.lang.String toUriString()
          Returns a URI string from this UriComponents instance.
private  void verify()
          Verifies all URI components to determine whether they contain any illegal characters, throwing an IllegalArgumentException if so.
private static void verifyUriComponent(java.lang.String source, UriComponents.Type type)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_ENCODING

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

PATH_DELIMITER

private static final char PATH_DELIMITER
See Also:
Constant Field Values

NAMES_PATTERN

private static final java.util.regex.Pattern NAMES_PATTERN
Captures URI template variable names.


scheme

private final java.lang.String scheme

userInfo

private final java.lang.String userInfo

host

private final java.lang.String host

port

private final int port

path

private final UriComponents.PathComponent path

queryParams

private final MultiValueMap<java.lang.String,java.lang.String> queryParams

fragment

private final java.lang.String fragment

encoded

private final boolean encoded

NULL_PATH_COMPONENT

static final UriComponents.PathComponent NULL_PATH_COMPONENT
Represents an empty path.

Constructor Detail

UriComponents

UriComponents(java.lang.String scheme,
              java.lang.String userInfo,
              java.lang.String host,
              int port,
              UriComponents.PathComponent path,
              MultiValueMap<java.lang.String,java.lang.String> queryParams,
              java.lang.String fragment,
              boolean encoded,
              boolean verify)
Package-friendly constructor that creates a new UriComponents instance from the given parameters. All parameters are optional, and can be null.

Parameters:
scheme - the scheme
userInfo - the user info
host - the host
port - the port
path - the path component
queryParams - the query parameters
fragment - the fragment
encoded - whether the components are encoded
verify - whether the components need to be verified to determine whether they contain illegal characters
Method Detail

getScheme

public java.lang.String getScheme()
Returns the scheme.

Returns:
the scheme. Can be null.

getUserInfo

public java.lang.String getUserInfo()
Returns the user info.

Returns:
the user info. Can be null.

getHost

public java.lang.String getHost()
Returns the host.

Returns:
the host. Can be null.

getPort

public int getPort()
Returns the port. Returns -1 if no port has been set.

Returns:
the port

getPath

public java.lang.String getPath()
Returns the path.

Returns:
the path. Can be null.

getPathSegments

public java.util.List<java.lang.String> getPathSegments()
Returns the list of path segments.

Returns:
the path segments. Empty if no path has been set.

getQuery

public java.lang.String getQuery()
Returns the query.

Returns:
the query. Can be null.

getQueryParams

public MultiValueMap<java.lang.String,java.lang.String> getQueryParams()
Returns the map of query parameters.

Returns:
the query parameters. Empty if no query has been set.

getFragment

public java.lang.String getFragment()
Returns the fragment.

Returns:
the fragment. Can be null.

encode

public UriComponents encode()
Encodes all URI components using their specific encoding rules, and returns the result as a new UriComponents instance. This method uses UTF-8 to encode.

Returns:
the encoded uri components

encode

public UriComponents encode(java.lang.String encoding)
                     throws java.io.UnsupportedEncodingException
Encodes all URI components using their specific encoding rules, and returns the result as a new UriComponents instance.

Parameters:
encoding - the encoding of the values contained in this map
Returns:
the encoded uri components
Throws:
java.io.UnsupportedEncodingException - if the given encoding is not supported

encodeUriComponent

static java.lang.String encodeUriComponent(java.lang.String source,
                                           java.lang.String encoding,
                                           UriComponents.Type type)
                                    throws java.io.UnsupportedEncodingException
Encodes the given source into an encoded String using the rules specified by the given component and with the given options.

Parameters:
source - the source string
encoding - the encoding of the source string
type - the URI component for the source
Returns:
the encoded URI
Throws:
java.lang.IllegalArgumentException - when the given uri parameter is not a valid URI
java.io.UnsupportedEncodingException

encodeBytes

private static byte[] encodeBytes(byte[] source,
                                  UriComponents.Type type)

verify

private void verify()
Verifies all URI components to determine whether they contain any illegal characters, throwing an IllegalArgumentException if so.

Throws:
java.lang.IllegalArgumentException - if any of the components contain illegal characters

verifyUriComponent

private static void verifyUriComponent(java.lang.String source,
                                       UriComponents.Type type)

expand

public UriComponents expand(java.util.Map<java.lang.String,?> uriVariables)
Replaces all URI template variables with the values from a given map. The map keys represent variable names; the values variable values. The order of variables is not significant.

Parameters:
uriVariables - the map of URI variables
Returns:
the expanded uri components

expand

public UriComponents expand(java.lang.Object... uriVariableValues)
Replaces all URI template variables with the values from a given array. The array represent variable values. The order of variables is significant.

Parameters:
uriVariableValues - URI variable values
Returns:
the expanded uri components

expandInternal

private UriComponents expandInternal(UriComponents.UriTemplateVariables uriVariables)

expandUriComponent

private static java.lang.String expandUriComponent(java.lang.String source,
                                                   UriComponents.UriTemplateVariables uriVariables)

getVariableName

private static java.lang.String getVariableName(java.lang.String match)

getVariableValueAsString

private static java.lang.String getVariableValueAsString(java.lang.Object variableValue)

toUriString

public java.lang.String toUriString()
Returns a URI string from this UriComponents instance.

Returns:
the URI string

toUri

public java.net.URI toUri()
Returns a URI from this UriComponents instance.

Returns:
the URI

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object