diff --git a/spring-web/src/main/java/org/springframework/http/client/ClientHttpResponse.java b/spring-web/src/main/java/org/springframework/http/client/ClientHttpResponse.java index 05fa7ddd58..431fdaf8da 100644 --- a/spring-web/src/main/java/org/springframework/http/client/ClientHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/client/ClientHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2017 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. @@ -38,13 +38,18 @@ public interface ClientHttpResponse extends HttpInputMessage, Closeable { * Return the HTTP status code of the response. * @return the HTTP status as an HttpStatus enum value * @throws IOException in case of I/O errors + * @throws IllegalArgumentException in case of an unknown HTTP status code + * @see HttpStatus#valueOf(int) */ HttpStatus getStatusCode() throws IOException; /** - * Return the HTTP status code of the response as integer + * Return the HTTP status code (potentially non-standard and not + * resolvable through the {@link HttpStatus} enum) as an integer. * @return the HTTP status as an integer * @throws IOException in case of I/O errors + * @since 3.1.1 + * @see #getStatusCode() */ int getRawStatusCode() throws IOException; diff --git a/spring-web/src/main/java/org/springframework/web/client/AsyncRestTemplate.java b/spring-web/src/main/java/org/springframework/web/client/AsyncRestTemplate.java index bcf7cfdeb2..5b8236c345 100644 --- a/spring-web/src/main/java/org/springframework/web/client/AsyncRestTemplate.java +++ b/spring-web/src/main/java/org/springframework/web/client/AsyncRestTemplate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -513,7 +513,7 @@ public class AsyncRestTemplate extends InterceptingAsyncHttpAccessor implements if (logger.isDebugEnabled()) { try { logger.debug("Async " + method.name() + " request for \"" + url + "\" resulted in " + - response.getStatusCode() + " (" + response.getStatusText() + ")"); + response.getRawStatusCode() + " (" + response.getStatusText() + ")"); } catch (IOException ex) { // ignore @@ -525,7 +525,7 @@ public class AsyncRestTemplate extends InterceptingAsyncHttpAccessor implements if (logger.isWarnEnabled()) { try { logger.warn("Async " + method.name() + " request for \"" + url + "\" resulted in " + - response.getStatusCode() + " (" + response.getStatusText() + "); invoking error handler"); + response.getRawStatusCode() + " (" + response.getStatusText() + "); invoking error handler"); } catch (IOException ex) { // ignore diff --git a/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java b/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java index 399187b46e..ced4e3f8bc 100644 --- a/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java +++ b/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java @@ -48,6 +48,21 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler { return hasError(getHttpStatusCode(response)); } + /** + * Template method called from {@link #hasError(ClientHttpResponse)}. + *
The default implementation checks if the given status code is + * {@link HttpStatus.Series#CLIENT_ERROR CLIENT_ERROR} or + * {@link HttpStatus.Series#SERVER_ERROR SERVER_ERROR}. + * Can be overridden in subclasses. + * @param statusCode the HTTP status code + * @return {@code true} if the response has an error; {@code false} otherwise + * @see #getHttpStatusCode(ClientHttpResponse) + */ + protected boolean hasError(HttpStatus statusCode) { + return (statusCode.series() == HttpStatus.Series.CLIENT_ERROR || + statusCode.series() == HttpStatus.Series.SERVER_ERROR); + } + /** * This default implementation throws a {@link HttpClientErrorException} if the response status code * is {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}, a {@link HttpServerErrorException} @@ -89,33 +104,6 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler { } } - /** - * Template method called from {@link #hasError(ClientHttpResponse)}. - *
The default implementation checks if the given status code is
- * {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR CLIENT_ERROR}
- * or {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR SERVER_ERROR}.
- * Can be overridden in subclasses.
- * @param statusCode the HTTP status code
- * @return {@code true} if the response has an error; {@code false} otherwise
- * @see #getHttpStatusCode(ClientHttpResponse)
- */
- protected boolean hasError(HttpStatus statusCode) {
- return (statusCode.series() == HttpStatus.Series.CLIENT_ERROR ||
- statusCode.series() == HttpStatus.Series.SERVER_ERROR);
- }
-
- /**
- * Determine the charset of the response (for inclusion in a status exception).
- * @param response the response to inspect
- * @return the associated charset, or {@code null} if none
- * @since 4.3.8
- */
- protected Charset getCharset(ClientHttpResponse response) {
- HttpHeaders headers = response.getHeaders();
- MediaType contentType = headers.getContentType();
- return (contentType != null ? contentType.getCharset() : null);
- }
-
/**
* Read the body of the given response (for inclusion in a status exception).
* @param response the response to inspect
@@ -133,4 +121,16 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
return new byte[0];
}
+ /**
+ * Determine the charset of the response (for inclusion in a status exception).
+ * @param response the response to inspect
+ * @return the associated charset, or {@code null} if none
+ * @since 4.3.8
+ */
+ protected Charset getCharset(ClientHttpResponse response) {
+ HttpHeaders headers = response.getHeaders();
+ MediaType contentType = headers.getContentType();
+ return (contentType != null ? contentType.getCharset() : null);
+ }
+
}
diff --git a/spring-web/src/main/java/org/springframework/web/client/MessageBodyClientHttpResponseWrapper.java b/spring-web/src/main/java/org/springframework/web/client/MessageBodyClientHttpResponseWrapper.java
index 8b732d8b64..8d3afc897e 100644
--- a/spring-web/src/main/java/org/springframework/web/client/MessageBodyClientHttpResponseWrapper.java
+++ b/spring-web/src/main/java/org/springframework/web/client/MessageBodyClientHttpResponseWrapper.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2015 the original author or authors.
+ * Copyright 2002-2017 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.
@@ -31,7 +31,7 @@ import org.springframework.http.client.ClientHttpResponse;
*
* @author Brian Clozel
* @since 4.1.5
- * @see rfc7230 Section 3.3.3
+ * @see RFC 7230 Section 3.3.3
*/
class MessageBodyClientHttpResponseWrapper implements ClientHttpResponse {
@@ -56,12 +56,17 @@ class MessageBodyClientHttpResponseWrapper implements ClientHttpResponse {
* @throws IOException in case of I/O errors
*/
public boolean hasMessageBody() throws IOException {
- HttpStatus responseStatus = this.getStatusCode();
- if (responseStatus.is1xxInformational() || responseStatus == HttpStatus.NO_CONTENT ||
- responseStatus == HttpStatus.NOT_MODIFIED) {
- return false;
+ try {
+ HttpStatus responseStatus = getStatusCode();
+ if (responseStatus.is1xxInformational() || responseStatus == HttpStatus.NO_CONTENT ||
+ responseStatus == HttpStatus.NOT_MODIFIED) {
+ return false;
+ }
}
- else if (this.getHeaders().getContentLength() == 0) {
+ catch (IllegalArgumentException ex) {
+ // Ignore - unknown HTTP status code...
+ }
+ if (getHeaders().getContentLength() == 0) {
return false;
}
return true;
diff --git a/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java b/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java
index 333ab0c23c..f2457f377f 100644
--- a/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java
+++ b/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2016 the original author or authors.
+ * Copyright 2002-2017 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.
@@ -583,7 +583,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
public