Skip to content

Commit 59e41a2

Browse files
committed
SPR-5808 - Make HttpMessageConverterExtractor top level class
1 parent 58d3e70 commit 59e41a2

File tree

2 files changed

+73
-33
lines changed

2 files changed

+73
-33
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2002-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.client;
18+
19+
import java.io.IOException;
20+
import java.util.List;
21+
22+
import org.springframework.http.MediaType;
23+
import org.springframework.http.client.ClientHttpResponse;
24+
import org.springframework.http.converter.HttpMessageConverter;
25+
import org.springframework.util.Assert;
26+
27+
/**
28+
* Response extractor that uses the given {@linkplain HttpMessageConverter entity converters} to convert the response
29+
* into a type <code>T</code>.
30+
*
31+
* @author Arjen Poutsma
32+
* @see RestTemplate
33+
* @since 3.0
34+
*/
35+
public class HttpMessageConverterExtractor<T> implements ResponseExtractor<T> {
36+
37+
private final Class<T> responseType;
38+
39+
private final List<HttpMessageConverter<T>> messageConverters;
40+
41+
/**
42+
* Creates a new instance of the {@code HttpMessageConverterExtractor} with the given response type and message
43+
* converters. The given converters must support the response type.
44+
*/
45+
public HttpMessageConverterExtractor(Class<T> responseType, List<HttpMessageConverter<T>> messageConverters) {
46+
Assert.notNull(responseType, "'responseType' must not be null");
47+
Assert.notEmpty(messageConverters, "'messageConverters' must not be empty");
48+
this.responseType = responseType;
49+
this.messageConverters = messageConverters;
50+
}
51+
52+
public T extractData(ClientHttpResponse response) throws IOException {
53+
MediaType contentType = response.getHeaders().getContentType();
54+
if (contentType == null) {
55+
throw new RestClientException("Cannot extract response: no Content-Type found");
56+
}
57+
for (HttpMessageConverter<T> messageConverter : messageConverters) {
58+
for (MediaType supportedMediaType : messageConverter.getSupportedMediaTypes()) {
59+
if (supportedMediaType.includes(contentType)) {
60+
return messageConverter.read(this.responseType, response);
61+
}
62+
}
63+
}
64+
throw new RestClientException(
65+
"Could not extract response: no suitable HttpMessageConverter found for response type [" +
66+
this.responseType.getName() + "] and content type [" + contentType + "]");
67+
}
68+
69+
}

org.springframework.web/src/main/java/org/springframework/web/client/RestTemplate.java

+4-33
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,18 @@ public ResponseErrorHandler getErrorHandler() {
166166
public <T> T getForObject(String url, Class<T> responseType, String... urlVariables) throws RestClientException {
167167

168168
checkForSupportedMessageConverter(responseType);
169+
List<HttpMessageConverter<T>> supportedMessageConverters = getSupportedMessageConverters(responseType);
169170
return execute(url, HttpMethod.GET, new GetCallback<T>(responseType),
170-
new HttpMessageConverterExtractor<T>(responseType), urlVariables);
171+
new HttpMessageConverterExtractor<T>(responseType, supportedMessageConverters), urlVariables);
171172
}
172173

173174
public <T> T getForObject(String url, Class<T> responseType, Map<String, String> urlVariables)
174175
throws RestClientException {
175176

176177
checkForSupportedMessageConverter(responseType);
178+
List<HttpMessageConverter<T>> supportedMessageConverters = getSupportedMessageConverters(responseType);
177179
return execute(url, HttpMethod.GET, new GetCallback<T>(responseType),
178-
new HttpMessageConverterExtractor<T>(responseType), urlVariables);
180+
new HttpMessageConverterExtractor<T>(responseType, supportedMessageConverters), urlVariables);
179181
}
180182

181183
// HEAD
@@ -381,37 +383,6 @@ public void doWithRequest(ClientHttpRequest httpRequest) throws IOException {
381383
}
382384
}
383385

384-
/**
385-
* Response extractor that uses the registered {@linkplain HttpMessageConverter entity converters}
386-
* to convert the response into a type <code>T</code>.
387-
*/
388-
private class HttpMessageConverterExtractor<T> implements ResponseExtractor<T> {
389-
390-
private final Class<T> responseType;
391-
392-
private HttpMessageConverterExtractor(Class<T> responseType) {
393-
this.responseType = responseType;
394-
}
395-
396-
public T extractData(ClientHttpResponse response) throws IOException {
397-
MediaType contentType = response.getHeaders().getContentType();
398-
if (contentType == null) {
399-
throw new RestClientException("Cannot extract response: no Content-Type found");
400-
}
401-
for (HttpMessageConverter<T> messageConverter : getSupportedMessageConverters(this.responseType)) {
402-
for (MediaType supportedMediaType : messageConverter.getSupportedMediaTypes()) {
403-
if (supportedMediaType.includes(contentType)) {
404-
return messageConverter.read(this.responseType, response);
405-
}
406-
}
407-
}
408-
throw new RestClientException(
409-
"Could not extract response: no suitable HttpMessageConverter found for response type [" +
410-
this.responseType.getName() + "] and content type [" + contentType + "]");
411-
}
412-
413-
}
414-
415386
/** Response extractor that extracts the response {@link HttpHeaders}. */
416387
private static class HeadersExtractor implements ResponseExtractor<HttpHeaders> {
417388

0 commit comments

Comments
 (0)