Content decoding in client exceptions
Closes gh-28190
This commit is contained in:
@@ -74,6 +74,12 @@ public class ProblemDetail {
|
||||
this.instance = other.instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* For deserialization.
|
||||
*/
|
||||
protected ProblemDetail() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Variant of {@link #setType(URI)} for chained initialization.
|
||||
|
||||
@@ -16,17 +16,26 @@
|
||||
|
||||
package org.springframework.web.client;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.log.LogFormatUtils;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
@@ -50,6 +59,20 @@ import org.springframework.util.ObjectUtils;
|
||||
*/
|
||||
public class DefaultResponseErrorHandler implements ResponseErrorHandler {
|
||||
|
||||
@Nullable
|
||||
private List<HttpMessageConverter<?>> messageConverters;
|
||||
|
||||
|
||||
/**
|
||||
* For internal use from the RestTemplate, to pass the message converters
|
||||
* to use to decode error content.
|
||||
* @since 6.0
|
||||
*/
|
||||
void setMessageConverters(List<HttpMessageConverter<?>> converters) {
|
||||
this.messageConverters = Collections.unmodifiableList(converters);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delegates to {@link #hasError(HttpStatusCode)} with the response status code.
|
||||
* @see ClientHttpResponse#getStatusCode()
|
||||
@@ -155,15 +178,48 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
|
||||
Charset charset = getCharset(response);
|
||||
String message = getErrorMessage(statusCode.value(), statusText, body, charset);
|
||||
|
||||
RestClientResponseException ex;
|
||||
if (statusCode.is4xxClientError()) {
|
||||
throw HttpClientErrorException.create(message, statusCode, statusText, headers, body, charset);
|
||||
ex = HttpClientErrorException.create(message, statusCode, statusText, headers, body, charset);
|
||||
}
|
||||
else if (statusCode.is5xxServerError()) {
|
||||
throw HttpServerErrorException.create(message, statusCode, statusText, headers, body, charset);
|
||||
ex = HttpServerErrorException.create(message, statusCode, statusText, headers, body, charset);
|
||||
}
|
||||
else {
|
||||
throw new UnknownHttpStatusCodeException(message, statusCode.value(), statusText, headers, body, charset);
|
||||
ex = new UnknownHttpStatusCodeException(message, statusCode.value(), statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
if (!CollectionUtils.isEmpty(this.messageConverters)) {
|
||||
ex.setBodyConvertFunction(initBodyConvertFunction(response, body));
|
||||
}
|
||||
|
||||
throw ex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a function for decoding the error content. This can be passed to
|
||||
* {@link RestClientResponseException#setBodyConvertFunction(Function)}.
|
||||
* @since 6.0
|
||||
*/
|
||||
protected Function<ResolvableType, ?> initBodyConvertFunction(ClientHttpResponse response, byte[] body) {
|
||||
Assert.state(!CollectionUtils.isEmpty(this.messageConverters), "Expected message converters");
|
||||
return resolvableType -> {
|
||||
try {
|
||||
HttpMessageConverterExtractor<?> extractor =
|
||||
new HttpMessageConverterExtractor<>(resolvableType.getType(), this.messageConverters);
|
||||
|
||||
return extractor.extractData(new ClientHttpResponseDecorator(response) {
|
||||
@Override
|
||||
public InputStream getBody() {
|
||||
return new ByteArrayInputStream(body);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new RestClientException(
|
||||
"Error while extracting response for type [" + resolvableType + "]", ex);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,10 +19,14 @@ package org.springframework.web.client;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Common base class for exceptions that contain actual HTTP response data.
|
||||
@@ -49,6 +53,9 @@ public class RestClientResponseException extends RestClientException {
|
||||
@Nullable
|
||||
private final String responseCharset;
|
||||
|
||||
@Nullable
|
||||
private Function<ResolvableType, ?> bodyConvertFunction;
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new instance of with the given response data.
|
||||
@@ -153,4 +160,43 @@ public class RestClientResponseException extends RestClientException {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the error response content to the specified type.
|
||||
* @param targetType the type to convert to
|
||||
* @param <E> the expected target type
|
||||
* @return the converted object, or {@code null} if there is no content
|
||||
* @since 6.0
|
||||
*/
|
||||
@Nullable
|
||||
public <E> E getResponseBodyAs(Class<E> targetType) {
|
||||
return getResponseBodyAs(ResolvableType.forClass(targetType));
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of {@link #getResponseBodyAs(Class)} with
|
||||
* {@link ParameterizedTypeReference}.
|
||||
* @since 6.0
|
||||
*/
|
||||
@Nullable
|
||||
public <E> E getResponseBodyAs(ParameterizedTypeReference<E> targetType) {
|
||||
return getResponseBodyAs(ResolvableType.forType(targetType.getType()));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Nullable
|
||||
private <E> E getResponseBodyAs(ResolvableType targetType) {
|
||||
Assert.state(this.bodyConvertFunction != null, "Function to convert body not set");
|
||||
return (E) this.bodyConvertFunction.apply(targetType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide a function to use to decode the response error content
|
||||
* via {@link #getResponseBodyAs(Class)}.
|
||||
* @param bodyConvertFunction the function to use
|
||||
* @since 6.0
|
||||
*/
|
||||
public void setBodyConvertFunction(Function<ResolvableType, ?> bodyConvertFunction) {
|
||||
this.bodyConvertFunction = bodyConvertFunction;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2022 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.
|
||||
@@ -195,6 +195,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
|
||||
this.messageConverters.add(new MappingJackson2CborHttpMessageConverter());
|
||||
}
|
||||
|
||||
updateErrorHandlerConverters();
|
||||
this.uriTemplateHandler = initUriTemplateHandler();
|
||||
}
|
||||
|
||||
@@ -219,9 +220,16 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
|
||||
validateConverters(messageConverters);
|
||||
this.messageConverters.addAll(messageConverters);
|
||||
this.uriTemplateHandler = initUriTemplateHandler();
|
||||
updateErrorHandlerConverters();
|
||||
}
|
||||
|
||||
|
||||
private void updateErrorHandlerConverters() {
|
||||
if (this.errorHandler instanceof DefaultResponseErrorHandler handler) {
|
||||
handler.setMessageConverters(this.messageConverters);
|
||||
}
|
||||
}
|
||||
|
||||
private static DefaultUriBuilderFactory initUriTemplateHandler() {
|
||||
DefaultUriBuilderFactory uriFactory = new DefaultUriBuilderFactory();
|
||||
uriFactory.setEncodingMode(EncodingMode.URI_COMPONENT); // for backwards compatibility..
|
||||
@@ -240,6 +248,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
|
||||
this.messageConverters.clear();
|
||||
this.messageConverters.addAll(messageConverters);
|
||||
}
|
||||
updateErrorHandlerConverters();
|
||||
}
|
||||
|
||||
private void validateConverters(List<HttpMessageConverter<?>> messageConverters) {
|
||||
@@ -262,6 +271,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
|
||||
public void setErrorHandler(ResponseErrorHandler errorHandler) {
|
||||
Assert.notNull(errorHandler, "ResponseErrorHandler must not be null");
|
||||
this.errorHandler = errorHandler;
|
||||
updateErrorHandlerConverters();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user