org.springframework.web.util
Class UriTemplate

java.lang.Object
  extended by org.springframework.web.util.UriTemplate
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
RestTemplate.HttpUrlTemplate

public class UriTemplate
extends java.lang.Object
implements java.io.Serializable

Represents a URI template. A URI template is a URI-like String that contains variables enclosed by braces ({, }), which can be expanded to produce an actual URI.

See expand(Map), expand(Object[]), and match(String) for example usages.

Since:
3.0
Author:
Arjen Poutsma, Juergen Hoeller
See Also:
URI Templates, Serialized Form

Nested Class Summary
private static class UriTemplate.Parser
          Static inner class to parse URI template strings into a matching regular expression.
 
Field Summary
private static java.lang.String DEFAULT_VARIABLE_PATTERN
          Replaces template variables in the URI template.
private  java.util.regex.Pattern matchPattern
           
private static java.util.regex.Pattern NAMES_PATTERN
          Captures URI template variable names.
private  UriComponents uriComponents
           
private  java.lang.String uriTemplate
           
private  java.util.List<java.lang.String> variableNames
           
 
Constructor Summary
UriTemplate(java.lang.String uriTemplate)
          Construct a new UriTemplate with the given URI String.
 
Method Summary
protected  java.net.URI encodeUri(java.lang.String uri)
          Deprecated. No longer in use, with no direct replacement
 java.net.URI expand(java.util.Map<java.lang.String,?> uriVariables)
          Given the Map of variables, expands this template into a URI.
 java.net.URI expand(java.lang.Object... uriVariableValues)
          Given an array of variables, expand this template into a full URI.
 java.util.List<java.lang.String> getVariableNames()
          Return the names of the variables in the template, in order.
 java.util.Map<java.lang.String,java.lang.String> match(java.lang.String uri)
          Match the given URI to a map of variable values.
 boolean matches(java.lang.String uri)
          Indicate whether the given URI matches this template.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NAMES_PATTERN

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


DEFAULT_VARIABLE_PATTERN

private static final java.lang.String DEFAULT_VARIABLE_PATTERN
Replaces template variables in the URI template.

See Also:
Constant Field Values

uriComponents

private final UriComponents uriComponents

variableNames

private final java.util.List<java.lang.String> variableNames

matchPattern

private final java.util.regex.Pattern matchPattern

uriTemplate

private final java.lang.String uriTemplate
Constructor Detail

UriTemplate

public UriTemplate(java.lang.String uriTemplate)
Construct a new UriTemplate with the given URI String.

Parameters:
uriTemplate - the URI template string
Method Detail

getVariableNames

public java.util.List<java.lang.String> getVariableNames()
Return the names of the variables in the template, in order.

Returns:
the template variable names

expand

public java.net.URI expand(java.util.Map<java.lang.String,?> uriVariables)
Given the Map of variables, expands this template into a URI. The Map keys represent variable names, the Map values variable values. The order of variables is not significant.

Example:

 UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
 Map<String, String> uriVariables = new HashMap<String, String>();
 uriVariables.put("booking", "42");
 uriVariables.put("hotel", "1");
 System.out.println(template.expand(uriVariables));
 
will print:
http://example.com/hotels/1/bookings/42

Parameters:
uriVariables - the map of URI variables
Returns:
the expanded URI
Throws:
java.lang.IllegalArgumentException - if uriVariables is null; or if it does not contain values for all the variable names

expand

public java.net.URI expand(java.lang.Object... uriVariableValues)
Given an array of variables, expand this template into a full URI. The array represent variable values. The order of variables is significant.

Example:

 UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
 System.out.println(template.expand("1", "42));
 
will print:
http://example.com/hotels/1/bookings/42

Parameters:
uriVariableValues - the array of URI variables
Returns:
the expanded URI
Throws:
java.lang.IllegalArgumentException - if uriVariables is null or if it does not contain sufficient variables

matches

public boolean matches(java.lang.String uri)
Indicate whether the given URI matches this template.

Parameters:
uri - the URI to match to
Returns:
true if it matches; false otherwise

match

public java.util.Map<java.lang.String,java.lang.String> match(java.lang.String uri)
Match the given URI to a map of variable values. Keys in the returned map are variable names, values are variable values, as occurred in the given URI.

Example:

 UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
 System.out.println(template.match("http://example.com/hotels/1/bookings/42"));
 
will print:
{hotel=1, booking=42}

Parameters:
uri - the URI to match to
Returns:
a map of variable values

encodeUri

@Deprecated
protected java.net.URI encodeUri(java.lang.String uri)
Deprecated. No longer in use, with no direct replacement

Encodes the given String as URL.

Defaults to UriUtils.encodeUri(String, String).

Parameters:
uri - the URI to encode
Returns:
the encoded URI

toString

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