opened up RestTemplate method signatures to Map<String, ?> and Object array instead of enforcing String values

This commit is contained in:
Juergen Hoeller 2009-12-04 18:29:53 +00:00
parent ad3fa505f3
commit 0ea5b5ef0b
3 changed files with 121 additions and 150 deletions

View File

@ -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.
* <p>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> T getForObject(String url, Class<T> responseType, String... uriVariables) throws RestClientException;
<T> T getForObject(String url, Class<T> 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.
* <p>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> T getForObject(String url, Class<T> responseType, Map<String, String> uriVariables) throws RestClientException;
<T> T getForObject(String url, Class<T> responseType, Map<String, ?> 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. <p>URI Template variables are expanded using the
* given URI variables, if any.
*
* Retrieve all headers of the resource specified by the URI template.
* <p>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. <p>URI Template variables are expanded using the
* given map.
*
* Retrieve all headers of the resource specified by the URI template.
* <p>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<String, String> uriVariables) throws RestClientException;
HttpHeaders headForHeaders(String url, Map<String, ?> 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
* <code>Location</code> header. This header typically indicates where the new resource is stored. <p>URI Template
* variables are expanded using the given URI variables, if any.
*
* <code>Location</code> header. This header typically indicates where the new resource is stored.
* <p>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 <code>null</code>
* @param uriVariables the variables to expand the template
* @return the value for the <code>Location</code> 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
* <code>Location</code> header. This header typically indicates where the new resource is stored. <p>URI Template
* variables are expanded using the given map.
*
* <code>Location</code> header. This header typically indicates where the new resource is stored.
* <p>URI Template variables are expanded using the given map.
* @param url the URL
* @param request the Object to be POSTed, may be <code>null</code>
* @param uriVariables the variables to expand the template
* @return the value for the <code>Location</code> header
*/
URI postForLocation(String url, Object request, Map<String, String> uriVariables) throws RestClientException;
URI postForLocation(String url, Object request, Map<String, ?> uriVariables) throws RestClientException;
/**
* Create a new resource by POSTing the given object to the URL, and returns the value of the
* <code>Location</code> header. This header typically indicates where the new resource is stored.
*
* @param url the URL
* @param request the Object to be POSTed, may be <code>null</code>
* @return the value for the <code>Location</code> 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. <p>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.
* <p>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 <code>null</code>
* @param uriVariables the variables to expand the template
* @return the converted object
*/
<T> T postForObject(String url, Object request, Class<T> responseType, String... uriVariables)
<T> T postForObject(String url, Object request, Class<T> 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. <p>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.
* <p>URI Template variables are expanded using the given map.
* @param url the URL
* @param request the Object to be POSTed, may be <code>null</code>
* @param uriVariables the variables to expand the template
* @return the converted object
*/
<T> T postForObject(String url, Object request, Class<T> responseType, Map<String, String> uriVariables)
<T> T postForObject(String url, Object request, Class<T> responseType, Map<String, ?> 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 <code>null</code>
* @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. <p>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.
* <p>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 <code>null</code>
* @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. <p>URI Template variables are expanded using the
* given map.
*
* Creates a new resource by PUTting the given object to URI template.
* <p>URI Template variables are expanded using the given map.
* @param url the URL
* @param request the Object to be PUT, may be <code>null</code>
* @param uriVariables the variables to expand the template
*/
void put(String url, Object request, Map<String, String> uriVariables) throws RestClientException;
void put(String url, Object request, Map<String, ?> 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 <code>null</code>
*/
@ -199,25 +191,24 @@ public interface RestOperations {
// DELETE
/**
* Delete the resources at the specified URI. <p>URI Template variables are expanded using the given URI variables, if
* any.
*
* Delete the resources at the specified URI.
* <p>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. <p>URI Template variables are expanded using the given map.
* Delete the resources at the specified URI.
* <p>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<String, String> uriVariables) throws RestClientException;
void delete(String url, Map<String, ?> 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. <p>URI Template variables are expanded using the given URI
* variables, if any.
*
* Return the value of the Allow header for the given URI.
* <p>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<HttpMethod> optionsForAllow(String url, String... uriVariables) throws RestClientException;
Set<HttpMethod> optionsForAllow(String url, Object... uriVariables) throws RestClientException;
/**
* Return the value of the Allow header for the given URI. <p>URI Template variables are expanded using the given map.
*
* Return the value of the Allow header for the given URI.
* <p>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<HttpMethod> optionsForAllow(String url, Map<String, String> uriVariables) throws RestClientException;
Set<HttpMethod> optionsForAllow(String url, Map<String, ?> 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}. <p>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}.
* <p>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> T execute(String url,
HttpMethod method,
RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor,
String... uriVariables) throws RestClientException;
<T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> 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}. <p>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}.
* <p>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> T execute(String url,
HttpMethod method,
RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor,
Map<String, String> uriVariables) throws RestClientException;
<T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor, Map<String, ?> 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> T execute(URI url,
HttpMethod method,
RequestCallback requestCallback,
<T> T execute(URI url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor) throws RestClientException;
}

View File

@ -55,7 +55,7 @@ import org.springframework.web.util.UriTemplate;
* <tr><td>any</td><td>{@link #execute}</td></tr> </table>
*
* <p>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<String, String>}. 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<HttpHeaders> headersExtractor = new HeadersExtractor();
private List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
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> T getForObject(String url, Class<T> responseType, String... urlVariables) throws RestClientException {
public <T> T getForObject(String url, Class<T> responseType, Object... urlVariables) throws RestClientException {
AcceptHeaderRequestCallback<T> requestCallback = new AcceptHeaderRequestCallback<T>(responseType);
HttpMessageConverterExtractor<T> responseExtractor =
new HttpMessageConverterExtractor<T>(responseType, getMessageConverters());
return execute(url, HttpMethod.GET, requestCallback, responseExtractor, urlVariables);
}
public <T> T getForObject(String url, Class<T> responseType, Map<String, String> urlVariables)
throws RestClientException {
public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> urlVariables) throws RestClientException {
AcceptHeaderRequestCallback<T> requestCallback = new AcceptHeaderRequestCallback<T>(responseType);
HttpMessageConverterExtractor<T> responseExtractor =
new HttpMessageConverterExtractor<T>(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<String, String> urlVariables) throws RestClientException {
public HttpHeaders headForHeaders(String url, Map<String, ?> 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<String, String> urlVariables)
public URI postForLocation(String url, Object request, Map<String, ?> 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> T postForObject(String url, Object request, Class<T> responseType, String... uriVariables)
public <T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables)
throws RestClientException {
PostPutCallback<T> requestCallback = new PostPutCallback<T>(request, responseType);
HttpMessageConverterExtractor<T> responseExtractor =
@ -232,7 +234,7 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
return execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables);
}
public <T> T postForObject(String url, Object request, Class<T> responseType, Map<String, String> uriVariables)
public <T> T postForObject(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
throws RestClientException {
PostPutCallback<T> requestCallback = new PostPutCallback<T>(request, responseType);
HttpMessageConverterExtractor<T> 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<String, String> urlVariables) throws RestClientException {
public void put(String url, Object request, Map<String, ?> 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<String, String> urlVariables) throws RestClientException {
public void delete(String url, Map<String, ?> urlVariables) throws RestClientException {
execute(url, HttpMethod.DELETE, null, null, urlVariables);
}
@ -280,68 +282,55 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
// OPTIONS
public Set<HttpMethod> optionsForAllow(String url, String... urlVariables) throws RestClientException {
public Set<HttpMethod> optionsForAllow(String url, Object... urlVariables) throws RestClientException {
HttpHeaders headers = execute(url, HttpMethod.OPTIONS, null, this.headersExtractor, urlVariables);
return headers.getAllow();
}
public Set<HttpMethod> optionsForAllow(String url, Map<String, String> urlVariables) throws RestClientException {
public Set<HttpMethod> optionsForAllow(String url, Map<String, ?> urlVariables) throws RestClientException {
HttpHeaders headers = execute(url, HttpMethod.OPTIONS, null, this.headersExtractor, urlVariables);
return headers.getAllow();
}
public Set<HttpMethod> optionsForAllow(URI url) throws RestClientException {
HttpHeaders headers = execute(url, HttpMethod.OPTIONS, null, this.headersExtractor);
return headers.getAllow();
}
// general execution
public <T> T execute(String url,
HttpMethod method,
RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor,
String... urlVariables) throws RestClientException {
public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor, Object... urlVariables) throws RestClientException {
UriTemplate uriTemplate = new UriTemplate(url);
URI expanded = uriTemplate.expand(urlVariables);
return doExecute(expanded, method, requestCallback, responseExtractor);
}
public <T> T execute(String url,
HttpMethod method,
RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor,
Map<String, String> urlVariables) throws RestClientException {
public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor, Map<String, ?> urlVariables) throws RestClientException {
UriTemplate uriTemplate = new UriTemplate(url);
URI expanded = uriTemplate.expand(urlVariables);
return doExecute(expanded, method, requestCallback, responseExtractor);
}
public <T> T execute(URI url,
HttpMethod method,
RequestCallback requestCallback,
public <T> T execute(URI url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> 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 <code>null</code>)
* @param responseExtractor object that extracts the return value from the response (can be <code>null</code>)
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
*/
protected <T> T doExecute(URI url,
HttpMethod method,
RequestCallback requestCallback,
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> 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<T> implements RequestCallback {
private final Class<T> 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<T> extends AcceptHeaderRequestCallback<T> {
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<T> 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<HttpHeaders> {
public HttpHeaders extractData(ClientHttpResponse response) throws IOException {

View File

@ -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
* (<code>{</code>, <code>}</code>), which can be expanded to produce a URI. <p>See {@link #expand(Map)}, {@link
* #expand(String[])}, and {@link #match(String)} for example usages.
* (<code>{</code>, <code>}</code>), which can be expanded to produce a URI. <p>See {@link #expand(Map)},
* {@link #expand(Object[])}, and {@link #match(String)} for example usages.
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @see <a href="http://bitworking.org/projects/URI-Templates/">URI Templates</a>
* @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<String> 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<String> 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. <p>Example:
@ -84,15 +86,14 @@ public class UriTemplate {
* System.out.println(template.expand(uriVariables));
* </pre>
* will print: <blockquote><code>http://example.com/hotels/1/bookings/42</code></blockquote>
*
* @param uriVariables the map of URI variables
* @return the expanded URI
* @throws IllegalArgumentException if <code>uriVariables</code> is <code>null</code>; or if it does not contain values
* for all the variable names
*/
public URI expand(Map<String, String> uriVariables) {
public URI expand(Map<String, ?> 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. <p>Example: <pre class="code> UriTemplate template = new
* Given an array of variables, expand this template into a full URI. The array represent variable values.
* The order of variables is significant. <p>Example: <pre class="code> UriTemplate template = new
* UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); System.out.println(template.expand("1", "42));
* </pre> will print: <blockquote><code>http://example.com/hotels/1/bookings/42</code></blockquote>
*
* @param uriVariableValues the array of URI variables
* @return the expanded URI
* @throws IllegalArgumentException if <code>uriVariables</code> is <code>null</code>; 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 <code>true</code> if it matches; <code>false</code> otherwise
*/
@ -151,7 +150,6 @@ public class UriTemplate {
* values, as occurred in the given URI. <p>Example: <pre class="code"> UriTemplate template = new
* UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); System.out.println(template.match("http://example.com/hotels/1/bookings/42"));
* </pre> will print: <blockquote><code>{hotel=1, booking=42}</code></blockquote>
*
* @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<String> variableNames = new LinkedList<String>();