Browse Source

Consistent use of headersExtractor() template method

pull/453/head
Juergen Hoeller 11 years ago
parent
commit
c5f908b174
  1. 45
      spring-web/src/main/java/org/springframework/web/client/RestTemplate.java

45
spring-web/src/main/java/org/springframework/web/client/RestTemplate.java

@ -23,7 +23,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.xml.transform.Source; import javax.xml.transform.Source;
import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.ParameterizedTypeReference;
@ -140,12 +139,12 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
ClassUtils.isPresent("org.codehaus.jackson.JsonGenerator", RestTemplate.class.getClassLoader()); ClassUtils.isPresent("org.codehaus.jackson.JsonGenerator", RestTemplate.class.getClassLoader());
private final ResponseExtractor<HttpHeaders> headersExtractor = new HeadersExtractor();
private final List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>(); private final List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler(); private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler();
private final ResponseExtractor<HttpHeaders> headersExtractor = new HeadersExtractor();
/** /**
* Create a new instance of the {@link RestTemplate} using default settings. * Create a new instance of the {@link RestTemplate} using default settings.
@ -188,7 +187,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
* Create a new instance of the {@link RestTemplate} using the given list of * Create a new instance of the {@link RestTemplate} using the given list of
* {@link HttpMessageConverter} to use * {@link HttpMessageConverter} to use
* @param messageConverters the list of {@link HttpMessageConverter} to use * @param messageConverters the list of {@link HttpMessageConverter} to use
* @since 4.0.1 * @since 3.2.7
*/ */
public RestTemplate(List<HttpMessageConverter<?>> messageConverters) { public RestTemplate(List<HttpMessageConverter<?>> messageConverters) {
Assert.notEmpty(messageConverters, "'messageConverters' must not be empty"); Assert.notEmpty(messageConverters, "'messageConverters' must not be empty");
@ -259,6 +258,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
@Override @Override
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... urlVariables) public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... urlVariables)
throws RestClientException { throws RestClientException {
RequestCallback requestCallback = acceptHeaderRequestCallback(responseType); RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType); ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
return execute(url, HttpMethod.GET, requestCallback, responseExtractor, urlVariables); return execute(url, HttpMethod.GET, requestCallback, responseExtractor, urlVariables);
@ -267,6 +267,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
@Override @Override
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> urlVariables) public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> urlVariables)
throws RestClientException { throws RestClientException {
RequestCallback requestCallback = acceptHeaderRequestCallback(responseType); RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType); ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
return execute(url, HttpMethod.GET, requestCallback, responseExtractor, urlVariables); return execute(url, HttpMethod.GET, requestCallback, responseExtractor, urlVariables);
@ -283,20 +284,17 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
@Override @Override
public HttpHeaders headForHeaders(String url, Object... urlVariables) throws RestClientException { public HttpHeaders headForHeaders(String url, Object... urlVariables) throws RestClientException {
ResponseExtractor<HttpHeaders> headersExtractor = headersExtractor(); return execute(url, HttpMethod.HEAD, null, headersExtractor(), urlVariables);
return execute(url, HttpMethod.HEAD, null, headersExtractor, urlVariables);
} }
@Override @Override
public HttpHeaders headForHeaders(String url, Map<String, ?> urlVariables) throws RestClientException { public HttpHeaders headForHeaders(String url, Map<String, ?> urlVariables) throws RestClientException {
ResponseExtractor<HttpHeaders> headersExtractor = headersExtractor(); return execute(url, HttpMethod.HEAD, null, headersExtractor(), urlVariables);
return execute(url, HttpMethod.HEAD, null, headersExtractor, urlVariables);
} }
@Override @Override
public HttpHeaders headForHeaders(URI url) throws RestClientException { public HttpHeaders headForHeaders(URI url) throws RestClientException {
ResponseExtractor<HttpHeaders> headersExtractor = headersExtractor(); return execute(url, HttpMethod.HEAD, null, headersExtractor());
return execute(url, HttpMethod.HEAD, null, headersExtractor);
} }
// POST // POST
@ -304,28 +302,28 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
@Override @Override
public URI postForLocation(String url, Object request, Object... urlVariables) throws RestClientException { public URI postForLocation(String url, Object request, Object... urlVariables) throws RestClientException {
RequestCallback requestCallback = httpEntityCallback(request); RequestCallback requestCallback = httpEntityCallback(request);
HttpHeaders headers = execute(url, HttpMethod.POST, requestCallback, this.headersExtractor, urlVariables); HttpHeaders headers = execute(url, HttpMethod.POST, requestCallback, headersExtractor(), urlVariables);
return headers.getLocation(); return headers.getLocation();
} }
@Override @Override
public URI postForLocation(String url, Object request, Map<String, ?> urlVariables) public URI postForLocation(String url, Object request, Map<String, ?> urlVariables) throws RestClientException {
throws RestClientException {
RequestCallback requestCallback = httpEntityCallback(request); RequestCallback requestCallback = httpEntityCallback(request);
HttpHeaders headers = execute(url, HttpMethod.POST, requestCallback, this.headersExtractor, urlVariables); HttpHeaders headers = execute(url, HttpMethod.POST, requestCallback, headersExtractor(), urlVariables);
return headers.getLocation(); return headers.getLocation();
} }
@Override @Override
public URI postForLocation(URI url, Object request) throws RestClientException { public URI postForLocation(URI url, Object request) throws RestClientException {
RequestCallback requestCallback = httpEntityCallback(request); RequestCallback requestCallback = httpEntityCallback(request);
HttpHeaders headers = execute(url, HttpMethod.POST, requestCallback, this.headersExtractor); HttpHeaders headers = execute(url, HttpMethod.POST, requestCallback, headersExtractor());
return headers.getLocation(); return headers.getLocation();
} }
@Override @Override
public <T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables) public <T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables)
throws RestClientException { throws RestClientException {
RequestCallback requestCallback = httpEntityCallback(request, responseType); RequestCallback requestCallback = httpEntityCallback(request, responseType);
HttpMessageConverterExtractor<T> responseExtractor = HttpMessageConverterExtractor<T> responseExtractor =
new HttpMessageConverterExtractor<T>(responseType, getMessageConverters(), logger); new HttpMessageConverterExtractor<T>(responseType, getMessageConverters(), logger);
@ -335,6 +333,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
@Override @Override
public <T> T postForObject(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables) public <T> T postForObject(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
throws RestClientException { throws RestClientException {
RequestCallback requestCallback = httpEntityCallback(request, responseType); RequestCallback requestCallback = httpEntityCallback(request, responseType);
HttpMessageConverterExtractor<T> responseExtractor = HttpMessageConverterExtractor<T> responseExtractor =
new HttpMessageConverterExtractor<T>(responseType, getMessageConverters(), logger); new HttpMessageConverterExtractor<T>(responseType, getMessageConverters(), logger);
@ -519,8 +518,9 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
} }
/** /**
* Execute the given method on the provided URI. The {@link ClientHttpRequest} is processed using the {@link * Execute the given method on the provided URI.
* RequestCallback}; the response with the {@link ResponseExtractor}. * <p>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 url the fully-expanded URL to connect to
* @param method the HTTP method to execute (GET, POST, etc.) * @param method the HTTP method to execute (GET, POST, etc.)
* @param requestCallback object that prepares the request (can be {@code null}) * @param requestCallback object that prepares the request (can be {@code null})
@ -567,8 +567,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
try { try {
logger.debug(method.name() + " request for \"" + url + "\" resulted in " + logger.debug(method.name() + " request for \"" + url + "\" resulted in " +
response.getStatusCode() + " (" + response.getStatusCode() + " (" + response.getStatusText() + ")");
response.getStatusText() + ")");
} }
catch (IOException e) { catch (IOException e) {
// ignore // ignore
@ -579,9 +578,8 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
private void handleResponseError(HttpMethod method, URI url, ClientHttpResponse response) throws IOException { private void handleResponseError(HttpMethod method, URI url, ClientHttpResponse response) throws IOException {
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {
try { try {
logger.warn( logger.warn(method.name() + " request for \"" + url + "\" resulted in " +
method.name() + " request for \"" + url + "\" resulted in " + response.getStatusCode() + " (" + response.getStatusCode() + " (" + response.getStatusText() + "); invoking error handler");
response.getStatusText() + "); invoking error handler");
} }
catch (IOException e) { catch (IOException e) {
// ignore // ignore
@ -611,8 +609,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
* Returns a request callback implementation that writes the given object to the * Returns a request callback implementation that writes the given object to the
* request stream. * request stream.
*/ */
protected <T> RequestCallback httpEntityCallback(Object requestBody, protected <T> RequestCallback httpEntityCallback(Object requestBody, Type responseType) {
Type responseType) {
return new HttpEntityRequestCallback(requestBody, responseType); return new HttpEntityRequestCallback(requestBody, responseType);
} }

Loading…
Cancel
Save