diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/RestOperations.java b/org.springframework.web/src/main/java/org/springframework/web/client/RestOperations.java
index 3f47d060af2..656e8d6c32e 100644
--- a/org.springframework.web/src/main/java/org/springframework/web/client/RestOperations.java
+++ b/org.springframework.web/src/main/java/org/springframework/web/client/RestOperations.java
@@ -24,42 +24,44 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
/**
- * Interface specifying a basic set of RESTful operations. Implemented by {@link RestTemplate}. Not often used directly,
- * but a useful option to enhance testability, as it can easily be mocked or stubbed.
+ * Interface specifying a basic set of RESTful operations. Implemented by {@link RestTemplate}.
+ * Not often used directly, but a useful option to enhance testability, as it can easily
+ * be mocked or stubbed.
*
* @author Arjen Poutsma
- * @see RestTemplate
+ * @author Juergen Hoeller
* @since 3.0
+ * @see RestTemplate
*/
public interface RestOperations {
// GET
/**
- * Retrieve a representation by doing a GET on the specified URL. The response (if any) is converted and returned.
+ * Retrieve a representation by doing a GET on the specified URL.
+ * The response (if any) is converted and returned.
*
URI Template variables are expanded using the given URI variables, if any.
- *
* @param url the URL
* @param responseType the type of the return value
* @param uriVariables the variables to expand the template
* @return the converted object
*/
- T getForObject(String url, Class responseType, String... uriVariables) throws RestClientException;
+ T getForObject(String url, Class responseType, Object... uriVariables) throws RestClientException;
/**
- * Retrieve a representation by doing a GET on the URI template. The response (if any) is converted and returned.
+ * Retrieve a representation by doing a GET on the URI template.
+ * The response (if any) is converted and returned.
* URI Template variables are expanded using the given map.
- *
* @param url the URL
* @param responseType the type of the return value
* @param uriVariables the map containing variables for the URI template
* @return the converted object
*/
- T getForObject(String url, Class responseType, Map uriVariables) throws RestClientException;
+ T getForObject(String url, Class responseType, Map uriVariables) throws RestClientException;
/**
- * Retrieve a representation by doing a GET on the URL . The response (if any) is converted and returned.
- *
+ * Retrieve a representation by doing a GET on the URL .
+ * The response (if any) is converted and returned.
* @param url the URL
* @param responseType the type of the return value
* @return the converted object
@@ -69,28 +71,25 @@ public interface RestOperations {
// HEAD
/**
- * Retrieve all headers of the resource specified by the URI template. URI Template variables are expanded using the
- * given URI variables, if any.
- *
+ * Retrieve all headers of the resource specified by the URI template.
+ *
URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param uriVariables the variables to expand the template
* @return all HTTP headers of that resource
*/
- HttpHeaders headForHeaders(String url, String... uriVariables) throws RestClientException;
+ HttpHeaders headForHeaders(String url, Object... uriVariables) throws RestClientException;
/**
- * Retrieve all headers of the resource specified by the URI template.
URI Template variables are expanded using the
- * given map.
- *
+ * Retrieve all headers of the resource specified by the URI template.
+ *
URI Template variables are expanded using the given map.
* @param url the URL
* @param uriVariables the map containing variables for the URI template
* @return all HTTP headers of that resource
*/
- HttpHeaders headForHeaders(String url, Map uriVariables) throws RestClientException;
+ HttpHeaders headForHeaders(String url, Map uriVariables) throws RestClientException;
/**
* Retrieve all headers of the resource specified by the URL.
- *
* @param url the URL
* @return all HTTP headers of that resource
*/
@@ -100,32 +99,29 @@ public interface RestOperations {
/**
* Create a new resource by POSTing the given object to the URI template, and returns the value of the
- * Location header. This header typically indicates where the new resource is stored. URI Template
- * variables are expanded using the given URI variables, if any.
- *
+ * Location header. This header typically indicates where the new resource is stored.
+ *
URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param request the Object to be POSTed, may be null
* @param uriVariables the variables to expand the template
* @return the value for the Location header
*/
- URI postForLocation(String url, Object request, String... uriVariables) throws RestClientException;
+ URI postForLocation(String url, Object request, Object... uriVariables) throws RestClientException;
/**
* Create a new resource by POSTing the given object to the URI template, and returns the value of the
- * Location header. This header typically indicates where the new resource is stored.
URI Template
- * variables are expanded using the given map.
- *
+ * Location header. This header typically indicates where the new resource is stored.
+ *
URI Template variables are expanded using the given map.
* @param url the URL
* @param request the Object to be POSTed, may be null
* @param uriVariables the variables to expand the template
* @return the value for the Location header
*/
- URI postForLocation(String url, Object request, Map uriVariables) throws RestClientException;
+ URI postForLocation(String url, Object request, Map uriVariables) throws RestClientException;
/**
* Create a new resource by POSTing the given object to the URL, and returns the value of the
* Location header. This header typically indicates where the new resource is stored.
- *
* @param url the URL
* @param request the Object to be POSTed, may be null
* @return the value for the Location header
@@ -133,33 +129,32 @@ public interface RestOperations {
URI postForLocation(URI url, Object request) throws RestClientException;
/**
- * Create a new resource by POSTing the given object to the URI template, and returns the representation
- * found in the response. URI Template variables are expanded using the given URI variables, if any.
- *
+ * Create a new resource by POSTing the given object to the URI template,
+ * and returns the representation found in the response.
+ *
URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param request the Object to be POSTed, may be null
* @param uriVariables the variables to expand the template
* @return the converted object
*/
- T postForObject(String url, Object request, Class responseType, String... uriVariables)
+ T postForObject(String url, Object request, Class responseType, Object... uriVariables)
throws RestClientException;
/**
- * Create a new resource by POSTing the given object to the URI template, and returns the representation
- * found in the response. URI Template variables are expanded using the given map.
- *
+ * Create a new resource by POSTing the given object to the URI template,
+ * and returns the representation found in the response.
+ *
URI Template variables are expanded using the given map.
* @param url the URL
* @param request the Object to be POSTed, may be null
* @param uriVariables the variables to expand the template
* @return the converted object
*/
- T postForObject(String url, Object request, Class responseType, Map uriVariables)
+ T postForObject(String url, Object request, Class responseType, Map uriVariables)
throws RestClientException;
/**
- * Create a new resource by POSTing the given object to the URL, and returns the representation
- * found in the response.
- *
+ * Create a new resource by POSTing the given object to the URL,
+ * and returns the representation found in the response.
* @param url the URL
* @param request the Object to be POSTed, may be null
* @return the converted object
@@ -169,28 +164,25 @@ public interface RestOperations {
// PUT
/**
- * Create or update a resource by PUTting the given object to the URI. URI Template variables are expanded using the
- * given URI variables, if any.
- *
+ * Create or update a resource by PUTting the given object to the URI.
+ *
URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param request the Object to be PUT, may be null
* @param uriVariables the variables to expand the template
*/
- void put(String url, Object request, String... uriVariables) throws RestClientException;
+ void put(String url, Object request, Object... uriVariables) throws RestClientException;
/**
- * Creates a new resource by PUTting the given object to URI template.
URI Template variables are expanded using the
- * given map.
- *
+ * Creates a new resource by PUTting the given object to URI template.
+ *
URI Template variables are expanded using the given map.
* @param url the URL
* @param request the Object to be PUT, may be null
* @param uriVariables the variables to expand the template
*/
- void put(String url, Object request, Map uriVariables) throws RestClientException;
+ void put(String url, Object request, Map uriVariables) throws RestClientException;
/**
* Creates a new resource by PUTting the given object to URL.
- *
* @param url the URL
* @param request the Object to be PUT, may be null
*/
@@ -199,25 +191,24 @@ public interface RestOperations {
// DELETE
/**
- * Delete the resources at the specified URI. URI Template variables are expanded using the given URI variables, if
- * any.
- *
+ * Delete the resources at the specified URI.
+ *
URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param uriVariables the variables to expand in the template
*/
- void delete(String url, String... uriVariables) throws RestClientException;
+ void delete(String url, Object... uriVariables) throws RestClientException;
/**
- * Delete the resources at the specified URI.
URI Template variables are expanded using the given map.
+ * Delete the resources at the specified URI.
+ *
URI Template variables are expanded using the given map.
*
* @param url the URL
* @param uriVariables the variables to expand the template
*/
- void delete(String url, Map uriVariables) throws RestClientException;
+ void delete(String url, Map uriVariables) throws RestClientException;
/**
* Delete the resources at the specified URL.
- *
* @param url the URL
*/
void delete(URI url) throws RestClientException;
@@ -225,27 +216,25 @@ public interface RestOperations {
// OPTIONS
/**
- * Return the value of the Allow header for the given URI. URI Template variables are expanded using the given URI
- * variables, if any.
- *
+ * Return the value of the Allow header for the given URI.
+ *
URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param uriVariables the variables to expand in the template
* @return the value of the allow header
*/
- Set optionsForAllow(String url, String... uriVariables) throws RestClientException;
+ Set optionsForAllow(String url, Object... uriVariables) throws RestClientException;
/**
- * Return the value of the Allow header for the given URI. URI Template variables are expanded using the given map.
- *
+ * Return the value of the Allow header for the given URI.
+ *
URI Template variables are expanded using the given map.
* @param url the URL
* @param uriVariables the variables to expand in the template
* @return the value of the allow header
*/
- Set optionsForAllow(String url, Map uriVariables) throws RestClientException;
+ Set optionsForAllow(String url, Map uriVariables) throws RestClientException;
/**
* Return the value of the Allow header for the given URL.
- *
* @param url the URL
* @return the value of the allow header
*/
@@ -254,10 +243,9 @@ public interface RestOperations {
// general execution
/**
- * Execute the HTTP methods to the given URI template, preparing the request with the {@link RequestCallback}, and reading the
- * response with a {@link ResponseExtractor}. URI Template variables are expanded using the given URI variables, if
- * any.
- *
+ * Execute the HTTP methods to the given URI template, preparing the request with the
+ * {@link RequestCallback}, and reading the response with a{@link ResponseExtractor}.
+ *
URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param method the HTTP method (GET, POST, etc)
* @param requestCallback object that prepares the request
@@ -265,17 +253,13 @@ public interface RestOperations {
* @param uriVariables the variables to expand in the template
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
*/
- T execute(String url,
- HttpMethod method,
- RequestCallback requestCallback,
- ResponseExtractor responseExtractor,
- String... uriVariables) throws RestClientException;
+ T execute(String url, HttpMethod method, RequestCallback requestCallback,
+ ResponseExtractor responseExtractor, Object... uriVariables) throws RestClientException;
/**
- * Execute the HTTP methods to the given URI template, preparing the request with the {@link RequestCallback}, and reading the
- * response with a {@link ResponseExtractor}. URI Template variables are expanded using the given URI variables
- * map.
- *
+ * Execute the HTTP methods to the given URI template, preparing the request with the
+ * {@link RequestCallback}, and reading the response with a {@link ResponseExtractor}.
+ *
URI Template variables are expanded using the given URI variables map.
* @param url the URL
* @param method the HTTP method (GET, POST, etc)
* @param requestCallback object that prepares the request
@@ -283,25 +267,19 @@ public interface RestOperations {
* @param uriVariables the variables to expand in the template
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
*/
- T execute(String url,
- HttpMethod method,
- RequestCallback requestCallback,
- ResponseExtractor responseExtractor,
- Map uriVariables) throws RestClientException;
+ T execute(String url, HttpMethod method, RequestCallback requestCallback,
+ ResponseExtractor responseExtractor, Map uriVariables) throws RestClientException;
/**
- * Execute the HTTP methods to the given URL, preparing the request with the {@link RequestCallback}, and reading the
- * response with a {@link ResponseExtractor}.
- *
+ * Execute the HTTP methods to the given URL, preparing the request with the
+ * {@link RequestCallback}, and reading the response with a {@link ResponseExtractor}.
* @param url the URL
* @param method the HTTP method (GET, POST, etc)
* @param requestCallback object that prepares the request
* @param responseExtractor object that extracts the return value from the response
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
*/
- T execute(URI url,
- HttpMethod method,
- RequestCallback requestCallback,
+ T execute(URI url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor responseExtractor) throws RestClientException;
}
diff --git a/org.springframework.web/src/main/java/org/springframework/web/client/RestTemplate.java b/org.springframework.web/src/main/java/org/springframework/web/client/RestTemplate.java
index da82f49c8b9..a1a18f51852 100644
--- a/org.springframework.web/src/main/java/org/springframework/web/client/RestTemplate.java
+++ b/org.springframework.web/src/main/java/org/springframework/web/client/RestTemplate.java
@@ -55,7 +55,7 @@ import org.springframework.web.util.UriTemplate;
* | any | {@link #execute} |
*
* For each of these HTTP methods, there are three corresponding Java methods in the {@code RestTemplate}. Two
- * variant take a {@code String} URI as first argument (eg. {@link #getForObject(String, Class, String[])}, {@link
+ * variant take a {@code String} URI as first argument (eg. {@link #getForObject(String, Class, Object[])}, {@link
* #getForObject(String, Class, Map)}), and are capable of substituting any {@linkplain UriTemplate URI templates} in
* that URL using either a {@code String} variable arguments array, or a {@code Map}. The string varargs
* variant expands the given template variables in order, so that
@@ -108,12 +108,14 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
ClassUtils.isPresent("org.codehaus.jackson.map.ObjectMapper", RestTemplate.class.getClassLoader()) &&
ClassUtils.isPresent("org.codehaus.jackson.JsonGenerator", RestTemplate.class.getClassLoader());
+
private final ResponseExtractor headersExtractor = new HeadersExtractor();
private List> messageConverters = new ArrayList>();
private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler();
+
/** Create a new instance of the {@link RestTemplate} using default settings. */
public RestTemplate() {
this.messageConverters.add(new ByteArrayHttpMessageConverter());
@@ -130,7 +132,6 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
/**
* Create a new instance of the {@link RestTemplate} based on the given {@link ClientHttpRequestFactory}.
- *
* @param requestFactory HTTP request factory to use
* @see org.springframework.http.client.SimpleClientHttpRequestFactory
* @see org.springframework.http.client.CommonsClientHttpRequestFactory
@@ -140,6 +141,7 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
setRequestFactory(requestFactory);
}
+
/**
* Set the message body converters to use. These converters are used to convert from and to HTTP requests and
* responses.
@@ -165,17 +167,17 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
return this.errorHandler;
}
+
// GET
- public T getForObject(String url, Class responseType, String... urlVariables) throws RestClientException {
+ public T getForObject(String url, Class responseType, Object... urlVariables) throws RestClientException {
AcceptHeaderRequestCallback requestCallback = new AcceptHeaderRequestCallback(responseType);
HttpMessageConverterExtractor responseExtractor =
new HttpMessageConverterExtractor(responseType, getMessageConverters());
return execute(url, HttpMethod.GET, requestCallback, responseExtractor, urlVariables);
}
- public T getForObject(String url, Class responseType, Map urlVariables)
- throws RestClientException {
+ public T getForObject(String url, Class responseType, Map urlVariables) throws RestClientException {
AcceptHeaderRequestCallback requestCallback = new AcceptHeaderRequestCallback(responseType);
HttpMessageConverterExtractor responseExtractor =
new HttpMessageConverterExtractor(responseType, getMessageConverters());
@@ -191,11 +193,11 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
// HEAD
- public HttpHeaders headForHeaders(String url, String... urlVariables) throws RestClientException {
+ public HttpHeaders headForHeaders(String url, Object... urlVariables) throws RestClientException {
return execute(url, HttpMethod.HEAD, null, this.headersExtractor, urlVariables);
}
- public HttpHeaders headForHeaders(String url, Map urlVariables) throws RestClientException {
+ public HttpHeaders headForHeaders(String url, Map urlVariables) throws RestClientException {
return execute(url, HttpMethod.HEAD, null, this.headersExtractor, urlVariables);
}
@@ -205,13 +207,13 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
// POST
- public URI postForLocation(String url, Object request, String... urlVariables) throws RestClientException {
+ public URI postForLocation(String url, Object request, Object... urlVariables) throws RestClientException {
PostPutCallback requestCallback = new PostPutCallback(request);
HttpHeaders headers = execute(url, HttpMethod.POST, requestCallback, this.headersExtractor, urlVariables);
return headers.getLocation();
}
- public URI postForLocation(String url, Object request, Map urlVariables)
+ public URI postForLocation(String url, Object request, Map urlVariables)
throws RestClientException {
PostPutCallback requestCallback = new PostPutCallback(request);
HttpHeaders headers = execute(url, HttpMethod.POST, requestCallback, this.headersExtractor, urlVariables);
@@ -224,7 +226,7 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
return headers.getLocation();
}
- public T postForObject(String url, Object request, Class responseType, String... uriVariables)
+ public T postForObject(String url, Object request, Class responseType, Object... uriVariables)
throws RestClientException {
PostPutCallback requestCallback = new PostPutCallback(request, responseType);
HttpMessageConverterExtractor responseExtractor =
@@ -232,7 +234,7 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
return execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables);
}
- public T postForObject(String url, Object request, Class responseType, Map uriVariables)
+ public T postForObject(String url, Object request, Class responseType, Map uriVariables)
throws RestClientException {
PostPutCallback requestCallback = new PostPutCallback(request, responseType);
HttpMessageConverterExtractor responseExtractor =
@@ -249,12 +251,12 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
// PUT
- public void put(String url, Object request, String... urlVariables) throws RestClientException {
+ public void put(String url, Object request, Object... urlVariables) throws RestClientException {
PostPutCallback requestCallback = new PostPutCallback(request);
execute(url, HttpMethod.PUT, requestCallback, null, urlVariables);
}
- public void put(String url, Object request, Map urlVariables) throws RestClientException {
+ public void put(String url, Object request, Map urlVariables) throws RestClientException {
PostPutCallback requestCallback = new PostPutCallback(request);
execute(url, HttpMethod.PUT, requestCallback, null, urlVariables);
}
@@ -266,11 +268,11 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
// DELETE
- public void delete(String url, String... urlVariables) throws RestClientException {
+ public void delete(String url, Object... urlVariables) throws RestClientException {
execute(url, HttpMethod.DELETE, null, null, urlVariables);
}
- public void delete(String url, Map urlVariables) throws RestClientException {
+ public void delete(String url, Map urlVariables) throws RestClientException {
execute(url, HttpMethod.DELETE, null, null, urlVariables);
}
@@ -280,68 +282,55 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
// OPTIONS
- public Set optionsForAllow(String url, String... urlVariables) throws RestClientException {
-
+ public Set optionsForAllow(String url, Object... urlVariables) throws RestClientException {
HttpHeaders headers = execute(url, HttpMethod.OPTIONS, null, this.headersExtractor, urlVariables);
return headers.getAllow();
}
- public Set optionsForAllow(String url, Map urlVariables) throws RestClientException {
-
+ public Set optionsForAllow(String url, Map urlVariables) throws RestClientException {
HttpHeaders headers = execute(url, HttpMethod.OPTIONS, null, this.headersExtractor, urlVariables);
return headers.getAllow();
}
public Set optionsForAllow(URI url) throws RestClientException {
-
HttpHeaders headers = execute(url, HttpMethod.OPTIONS, null, this.headersExtractor);
return headers.getAllow();
}
// general execution
- public T execute(String url,
- HttpMethod method,
- RequestCallback requestCallback,
- ResponseExtractor responseExtractor,
- String... urlVariables) throws RestClientException {
+ public T execute(String url, HttpMethod method, RequestCallback requestCallback,
+ ResponseExtractor responseExtractor, Object... urlVariables) throws RestClientException {
UriTemplate uriTemplate = new UriTemplate(url);
URI expanded = uriTemplate.expand(urlVariables);
return doExecute(expanded, method, requestCallback, responseExtractor);
}
- public T execute(String url,
- HttpMethod method,
- RequestCallback requestCallback,
- ResponseExtractor responseExtractor,
- Map urlVariables) throws RestClientException {
+ public T execute(String url, HttpMethod method, RequestCallback requestCallback,
+ ResponseExtractor responseExtractor, Map urlVariables) throws RestClientException {
UriTemplate uriTemplate = new UriTemplate(url);
URI expanded = uriTemplate.expand(urlVariables);
return doExecute(expanded, method, requestCallback, responseExtractor);
}
- public T execute(URI url,
- HttpMethod method,
- RequestCallback requestCallback,
+ public T execute(URI url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor responseExtractor) throws RestClientException {
+
return doExecute(url, method, requestCallback, responseExtractor);
}
/**
* Execute the given method on the provided URI. The {@link ClientHttpRequest} is processed using the {@link
* RequestCallback}; the response with the {@link ResponseExtractor}.
- *
* @param url the fully-expanded URL to connect to
* @param method the HTTP method to execute (GET, POST, etc.)
* @param requestCallback object that prepares the request (can be null)
* @param responseExtractor object that extracts the return value from the response (can be null)
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
*/
- protected T doExecute(URI url,
- HttpMethod method,
- RequestCallback requestCallback,
+ protected T doExecute(URI url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor responseExtractor) throws RestClientException {
Assert.notNull(url, "'url' must not be null");
@@ -403,7 +392,10 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
getErrorHandler().handleError(response);
}
- /** Request callback implementation that prepares the request's accept headers. */
+
+ /**
+ * Request callback implementation that prepares the request's accept headers.
+ */
private class AcceptHeaderRequestCallback implements RequestCallback {
private final Class responseType;
@@ -440,7 +432,10 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
}
}
- /** Request callback implementation that writes the given object to the request stream. */
+
+ /**
+ * Request callback implementation that writes the given object to the request stream.
+ */
private class PostPutCallback extends AcceptHeaderRequestCallback {
private final Object requestBody;
@@ -458,12 +453,6 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
this.requestContentType = null;
}
- private PostPutCallback(Object requestBody, MediaType requestContentType, Class responseType) {
- super(responseType);
- this.requestBody = requestBody;
- this.requestContentType = requestContentType;
- }
-
@Override
@SuppressWarnings("unchecked")
public void doWithRequest(ClientHttpRequest httpRequest) throws IOException {
@@ -489,7 +478,10 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
}
}
- /** Response extractor that extracts the response {@link HttpHeaders}. */
+
+ /**
+ * Response extractor that extracts the response {@link HttpHeaders}.
+ */
private static class HeadersExtractor implements ResponseExtractor {
public HttpHeaders extractData(ClientHttpResponse response) throws IOException {
diff --git a/org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java b/org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java
index a532fd763b8..f005806a85a 100644
--- a/org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java
+++ b/org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java
@@ -16,6 +16,7 @@
package org.springframework.web.util;
+import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
@@ -25,16 +26,16 @@ import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import java.io.UnsupportedEncodingException;
import org.springframework.util.Assert;
/**
* Represents a URI template. An URI template is a URI-like String that contained variables marked of in braces
- * ({, }), which can be expanded to produce a URI. See {@link #expand(Map)}, {@link
- * #expand(String[])}, and {@link #match(String)} for example usages.
+ * ({, }), which can be expanded to produce a URI.
See {@link #expand(Map)},
+ * {@link #expand(Object[])}, and {@link #match(String)} for example usages.
*
* @author Arjen Poutsma
+ * @author Juergen Hoeller
* @see URI Templates
* @since 3.0
*/
@@ -46,15 +47,16 @@ public class UriTemplate {
/** Replaces template variables in the URI template. */
private static final String VALUE_REGEX = "(.*)";
+
private final List variableNames;
private final Pattern matchPattern;
private final String uriTemplate;
+
/**
* Construct a new {@link UriTemplate} with the given URI String.
- *
* @param uriTemplate the URI template string
*/
public UriTemplate(String uriTemplate) {
@@ -66,13 +68,13 @@ public class UriTemplate {
/**
* Return the names of the variables in the template, in order.
- *
* @return the template variable names
*/
public final List getVariableNames() {
return this.variableNames;
}
+
/**
* 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:
@@ -84,15 +86,14 @@ public class UriTemplate {
* System.out.println(template.expand(uriVariables));
*
* will print:
http://example.com/hotels/1/bookings/42
- *
* @param uriVariables the map of URI variables
* @return the expanded URI
* @throws IllegalArgumentException if uriVariables is null; or if it does not contain values
* for all the variable names
*/
- public URI expand(Map uriVariables) {
+ public URI expand(Map uriVariables) {
Assert.notNull(uriVariables, "'uriVariables' must not be null");
- String[] values = new String[this.variableNames.size()];
+ Object[] values = new String[this.variableNames.size()];
for (int i = 0; i < this.variableNames.size(); i++) {
String name = this.variableNames.get(i);
if (!uriVariables.containsKey(name)) {
@@ -104,17 +105,16 @@ public class UriTemplate {
}
/**
- * 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
- *
* @param uriVariableValues the array of URI variables
* @return the expanded URI
* @throws IllegalArgumentException if uriVariables is null; or if it does not contain
* sufficient variables
*/
- public URI expand(String... uriVariableValues) {
+ public URI expand(Object... uriVariableValues) {
Assert.notNull(uriVariableValues, "'uriVariableValues' must not be null");
if (uriVariableValues.length != this.variableNames.size()) {
throw new IllegalArgumentException(
@@ -125,7 +125,7 @@ public class UriTemplate {
StringBuffer buffer = new StringBuffer();
int i = 0;
while (matcher.find()) {
- String uriVariable = uriVariableValues[i++];
+ String uriVariable = uriVariableValues[i++].toString();
matcher.appendReplacement(buffer, uriVariable);
}
matcher.appendTail(buffer);
@@ -134,7 +134,6 @@ public class UriTemplate {
/**
* Indicate whether the given URI matches this template.
- *
* @param uri the URI to match to
* @return true if it matches; false otherwise
*/
@@ -151,7 +150,6 @@ public class UriTemplate {
* 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}
- *
* @param uri the URI to match to
* @return a map of variable values
*/
@@ -188,7 +186,10 @@ public class UriTemplate {
}
}
- /** Static inner class to parse uri template strings into a matching regular expression. */
+
+ /**
+ * Static inner class to parse uri template strings into a matching regular expression.
+ */
private static class Parser {
private final List variableNames = new LinkedList();