SPR-5808 - Make HttpMessageConverterExtractor top level class
This commit is contained in:
parent
58d3e704bf
commit
59e41a270d
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Response extractor that uses the given {@linkplain HttpMessageConverter entity converters} to convert the response
|
||||
* into a type <code>T</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see RestTemplate
|
||||
* @since 3.0
|
||||
*/
|
||||
public class HttpMessageConverterExtractor<T> implements ResponseExtractor<T> {
|
||||
|
||||
private final Class<T> responseType;
|
||||
|
||||
private final List<HttpMessageConverter<T>> messageConverters;
|
||||
|
||||
/**
|
||||
* Creates a new instance of the {@code HttpMessageConverterExtractor} with the given response type and message
|
||||
* converters. The given converters must support the response type.
|
||||
*/
|
||||
public HttpMessageConverterExtractor(Class<T> responseType, List<HttpMessageConverter<T>> messageConverters) {
|
||||
Assert.notNull(responseType, "'responseType' must not be null");
|
||||
Assert.notEmpty(messageConverters, "'messageConverters' must not be empty");
|
||||
this.responseType = responseType;
|
||||
this.messageConverters = messageConverters;
|
||||
}
|
||||
|
||||
public T extractData(ClientHttpResponse response) throws IOException {
|
||||
MediaType contentType = response.getHeaders().getContentType();
|
||||
if (contentType == null) {
|
||||
throw new RestClientException("Cannot extract response: no Content-Type found");
|
||||
}
|
||||
for (HttpMessageConverter<T> messageConverter : messageConverters) {
|
||||
for (MediaType supportedMediaType : messageConverter.getSupportedMediaTypes()) {
|
||||
if (supportedMediaType.includes(contentType)) {
|
||||
return messageConverter.read(this.responseType, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new RestClientException(
|
||||
"Could not extract response: no suitable HttpMessageConverter found for response type [" +
|
||||
this.responseType.getName() + "] and content type [" + contentType + "]");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -166,16 +166,18 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
|
|||
public <T> T getForObject(String url, Class<T> responseType, String... urlVariables) throws RestClientException {
|
||||
|
||||
checkForSupportedMessageConverter(responseType);
|
||||
List<HttpMessageConverter<T>> supportedMessageConverters = getSupportedMessageConverters(responseType);
|
||||
return execute(url, HttpMethod.GET, new GetCallback<T>(responseType),
|
||||
new HttpMessageConverterExtractor<T>(responseType), urlVariables);
|
||||
new HttpMessageConverterExtractor<T>(responseType, supportedMessageConverters), urlVariables);
|
||||
}
|
||||
|
||||
public <T> T getForObject(String url, Class<T> responseType, Map<String, String> urlVariables)
|
||||
throws RestClientException {
|
||||
|
||||
checkForSupportedMessageConverter(responseType);
|
||||
List<HttpMessageConverter<T>> supportedMessageConverters = getSupportedMessageConverters(responseType);
|
||||
return execute(url, HttpMethod.GET, new GetCallback<T>(responseType),
|
||||
new HttpMessageConverterExtractor<T>(responseType), urlVariables);
|
||||
new HttpMessageConverterExtractor<T>(responseType, supportedMessageConverters), urlVariables);
|
||||
}
|
||||
|
||||
// HEAD
|
||||
|
|
@ -381,37 +383,6 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Response extractor that uses the registered {@linkplain HttpMessageConverter entity converters}
|
||||
* to convert the response into a type <code>T</code>.
|
||||
*/
|
||||
private class HttpMessageConverterExtractor<T> implements ResponseExtractor<T> {
|
||||
|
||||
private final Class<T> responseType;
|
||||
|
||||
private HttpMessageConverterExtractor(Class<T> responseType) {
|
||||
this.responseType = responseType;
|
||||
}
|
||||
|
||||
public T extractData(ClientHttpResponse response) throws IOException {
|
||||
MediaType contentType = response.getHeaders().getContentType();
|
||||
if (contentType == null) {
|
||||
throw new RestClientException("Cannot extract response: no Content-Type found");
|
||||
}
|
||||
for (HttpMessageConverter<T> messageConverter : getSupportedMessageConverters(this.responseType)) {
|
||||
for (MediaType supportedMediaType : messageConverter.getSupportedMediaTypes()) {
|
||||
if (supportedMediaType.includes(contentType)) {
|
||||
return messageConverter.read(this.responseType, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new RestClientException(
|
||||
"Could not extract response: no suitable HttpMessageConverter found for response type [" +
|
||||
this.responseType.getName() + "] and content type [" + contentType + "]");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Response extractor that extracts the response {@link HttpHeaders}. */
|
||||
private static class HeadersExtractor implements ResponseExtractor<HttpHeaders> {
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue