Removed unnecessary HttpUrlTemplate subclass of UriTemplate

This commit is contained in:
Juergen Hoeller 2013-05-03 13:38:31 +02:00
parent b2bd319d47
commit 872cf6b076
2 changed files with 25 additions and 56 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,10 +17,8 @@
package org.springframework.web.client; package org.springframework.web.client;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -51,12 +49,12 @@ import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.web.util.UriTemplate; import org.springframework.web.util.UriTemplate;
import org.springframework.web.util.UriUtils;
/** /**
* <strong>The central class for client-side HTTP access.</strong> It simplifies communication with HTTP servers, and * <strong>Spring's central class for client-side HTTP access.</strong>
* enforces RESTful principles. It handles HTTP connections, leaving application code to provide URLs (with possible * It simplifies communication with HTTP servers, and enforces RESTful principles.
* template variables) and extract results. * It handles HTTP connections, leaving application code to provide URLs
* (with possible template variables) and extract results.
* *
* <p>The main entry points of this template are the methods named after the six main HTTP methods: * <p>The main entry points of this template are the methods named after the six main HTTP methods:
* <table> * <table>
@ -74,7 +72,7 @@ import org.springframework.web.util.UriUtils;
* *
* <p>The {@code exchange} and {@code execute} methods are generalized versions of the more specific methods listed * <p>The {@code exchange} and {@code execute} methods are generalized versions of the more specific methods listed
* above them. They support additional, less frequently used combinations including support for requests using the * above them. They support additional, less frequently used combinations including support for requests using the
* HTTP PATCH method. However, note that the underlying HTTP library must also support the desired combination.</p> * HTTP PATCH method. However, note that the underlying HTTP library must also support the desired combination.
* *
* <p>For each of these HTTP methods, there are three corresponding Java methods in the {@code RestTemplate}. Two * <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, Object[])}, {@link * variant take a {@code String} URI as first argument (eg. {@link #getForObject(String, Class, Object[])}, {@link
@ -115,11 +113,11 @@ import org.springframework.web.util.UriUtils;
* requestFactory} and {@link #setErrorHandler(ResponseErrorHandler) errorHandler} bean properties. * requestFactory} and {@link #setErrorHandler(ResponseErrorHandler) errorHandler} bean properties.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 3.0
* @see HttpMessageConverter * @see HttpMessageConverter
* @see RequestCallback * @see RequestCallback
* @see ResponseExtractor * @see ResponseExtractor
* @see ResponseErrorHandler * @see ResponseErrorHandler
* @since 3.0
*/ */
public class RestTemplate extends InterceptingHttpAccessor implements RestOperations { public class RestTemplate extends InterceptingHttpAccessor implements RestOperations {
@ -145,7 +143,9 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler(); private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler();
/** Create a new instance of the {@link RestTemplate} using default settings. */ /**
* Create a new instance of the {@link RestTemplate} using default settings.
*/
public RestTemplate() { public RestTemplate() {
this.messageConverters.add(new ByteArrayHttpMessageConverter()); this.messageConverters.add(new ByteArrayHttpMessageConverter());
this.messageConverters.add(new StringHttpMessageConverter()); this.messageConverters.add(new StringHttpMessageConverter());
@ -171,7 +171,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
* Create a new instance of the {@link RestTemplate} based on the given {@link ClientHttpRequestFactory}. * Create a new instance of the {@link RestTemplate} based on the given {@link ClientHttpRequestFactory}.
* @param requestFactory HTTP request factory to use * @param requestFactory HTTP request factory to use
* @see org.springframework.http.client.SimpleClientHttpRequestFactory * @see org.springframework.http.client.SimpleClientHttpRequestFactory
* @see org.springframework.http.client.CommonsClientHttpRequestFactory * @see org.springframework.http.client.HttpComponentsClientHttpRequestFactory
*/ */
public RestTemplate(ClientHttpRequestFactory requestFactory) { public RestTemplate(ClientHttpRequestFactory requestFactory) {
this(); this();
@ -180,8 +180,8 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
/** /**
* Set the message body converters to use. These converters are used to convert from and to HTTP requests and * Set the message body converters to use.
* responses. * <p><These converters are used to convert from and to HTTP requests and responses.
*/ */
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) { public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
Assert.notEmpty(messageConverters, "'messageConverters' must not be empty"); Assert.notEmpty(messageConverters, "'messageConverters' must not be empty");
@ -311,17 +311,16 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
public <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables) public <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables)
throws RestClientException { throws RestClientException {
HttpEntityRequestCallback requestCallback = new HttpEntityRequestCallback(request, responseType); HttpEntityRequestCallback requestCallback = new HttpEntityRequestCallback(request, responseType);
ResponseEntityResponseExtractor<T> responseExtractor = ResponseEntityResponseExtractor<T> responseExtractor =
new ResponseEntityResponseExtractor<T>(responseType); new ResponseEntityResponseExtractor<T>(responseType);
return execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables); return execute(url, HttpMethod.POST, requestCallback, responseExtractor, uriVariables);
} }
public <T> ResponseEntity<T> postForEntity(String url, public <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
Object request,
Class<T> responseType,
Map<String, ?> uriVariables)
throws RestClientException { throws RestClientException {
HttpEntityRequestCallback requestCallback = new HttpEntityRequestCallback(request, responseType); HttpEntityRequestCallback requestCallback = new HttpEntityRequestCallback(request, responseType);
ResponseEntityResponseExtractor<T> responseExtractor = ResponseEntityResponseExtractor<T> responseExtractor =
new ResponseEntityResponseExtractor<T>(responseType); new ResponseEntityResponseExtractor<T>(responseType);
@ -441,16 +440,14 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback, public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor, Object... urlVariables) throws RestClientException { ResponseExtractor<T> responseExtractor, Object... urlVariables) throws RestClientException {
UriTemplate uriTemplate = new HttpUrlTemplate(url); URI expanded = new UriTemplate(url).expand(urlVariables);
URI expanded = uriTemplate.expand(urlVariables);
return doExecute(expanded, method, requestCallback, responseExtractor); return doExecute(expanded, method, requestCallback, responseExtractor);
} }
public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback, public <T> T execute(String url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor, Map<String, ?> urlVariables) throws RestClientException { ResponseExtractor<T> responseExtractor, Map<String, ?> urlVariables) throws RestClientException {
UriTemplate uriTemplate = new HttpUrlTemplate(url); URI expanded = new UriTemplate(url).expand(urlVariables);
URI expanded = uriTemplate.expand(urlVariables);
return doExecute(expanded, method, requestCallback, responseExtractor); return doExecute(expanded, method, requestCallback, responseExtractor);
} }
@ -667,6 +664,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
} }
} }
/** /**
* Response extractor for {@link HttpEntity}. * Response extractor for {@link HttpEntity}.
*/ */
@ -683,8 +681,8 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
} }
public ResponseEntity<T> extractData(ClientHttpResponse response) throws IOException { public ResponseEntity<T> extractData(ClientHttpResponse response) throws IOException {
if (delegate != null) { if (this.delegate != null) {
T body = delegate.extractData(response); T body = this.delegate.extractData(response);
return new ResponseEntity<T>(body, response.getHeaders(), response.getStatusCode()); return new ResponseEntity<T>(body, response.getHeaders(), response.getStatusCode());
} }
else { else {
@ -704,30 +702,4 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
} }
} }
/**
* HTTP-specific subclass of UriTemplate, overriding the encode method.
*/
@SuppressWarnings("serial")
private static class HttpUrlTemplate extends UriTemplate {
public HttpUrlTemplate(String uriTemplate) {
super(uriTemplate);
}
@Override
protected URI encodeUri(String uri) {
try {
String encoded = UriUtils.encodeHttpUrl(uri, "UTF-8");
return new URI(encoded);
}
catch (UnsupportedEncodingException ex) {
// should not happen, UTF-8 is always supported
throw new IllegalStateException(ex);
}
catch (URISyntaxException ex) {
throw new IllegalArgumentException("Could not create HTTP URL from [" + uri + "]: " + ex, ex);
}
}
}
} }

View File

@ -71,6 +71,7 @@ public class UriTemplate implements Serializable {
this.uriComponents = UriComponentsBuilder.fromUriString(uriTemplate).build(); this.uriComponents = UriComponentsBuilder.fromUriString(uriTemplate).build();
} }
/** /**
* Return the names of the variables in the template, in order. * Return the names of the variables in the template, in order.
* @return the template variable names * @return the template variable names
@ -79,8 +80,6 @@ public class UriTemplate implements Serializable {
return this.variableNames; return this.variableNames;
} }
// expanding
/** /**
* Given the Map of variables, expands this template into a URI. The Map keys represent variable names, * 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. * the Map values variable values. The order of variables is not significant.
@ -124,8 +123,6 @@ public class UriTemplate implements Serializable {
return encodedComponents.toUri(); return encodedComponents.toUri();
} }
// matching
/** /**
* Indicate whether the given URI matches this template. * Indicate whether the given URI matches this template.
* @param uri the URI to match to * @param uri the URI to match to
@ -170,7 +167,7 @@ public class UriTemplate implements Serializable {
* <p>Defaults to {@link UriUtils#encodeUri(String, String)}. * <p>Defaults to {@link UriUtils#encodeUri(String, String)}.
* @param uri the URI to encode * @param uri the URI to encode
* @return the encoded URI * @return the encoded URI
* @deprecated No longer in use, with no direct replacement * @deprecated No longer in use; to be removed in Spring 4.0.
*/ */
@Deprecated @Deprecated
protected URI encodeUri(String uri) { protected URI encodeUri(String uri) {
@ -216,7 +213,8 @@ public class UriTemplate implements Serializable {
} }
else { else {
if (colonIdx + 1 == match.length()) { if (colonIdx + 1 == match.length()) {
throw new IllegalArgumentException("No custom regular expression specified after ':' in \"" + match + "\""); throw new IllegalArgumentException(
"No custom regular expression specified after ':' in \"" + match + "\"");
} }
String variablePattern = match.substring(colonIdx + 1, match.length()); String variablePattern = match.substring(colonIdx + 1, match.length());
this.patternBuilder.append('('); this.patternBuilder.append('(');
@ -250,5 +248,4 @@ public class UriTemplate implements Serializable {
} }
} }
} }