diff --git a/spring-web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java b/spring-web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java index 93b3c1106462d24b1a49ca9b474d4e0a3e5a0533..0d5c2fbc1954d497256307babb65d4297778ca08 100644 --- a/spring-web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java +++ b/spring-web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 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. @@ -16,7 +16,6 @@ package org.springframework.web.client; -import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import org.springframework.http.HttpHeaders; @@ -27,29 +26,19 @@ import org.springframework.http.HttpStatus; * * @author Arjen Poutsma * @author Chris Beams + * @author Rossen Stoyanchev * @since 3.0 */ -public abstract class HttpStatusCodeException extends RestClientException { +public abstract class HttpStatusCodeException extends RestClientResponseException { - private static final long serialVersionUID = -5807494703720513267L; - - private static final String DEFAULT_CHARSET = "ISO-8859-1"; + private static final long serialVersionUID = 5696801857651587810L; private final HttpStatus statusCode; - private final String statusText; - - private final byte[] responseBody; - - private final HttpHeaders responseHeaders; - - private final String responseCharset; - /** - * Construct a new instance of {@code HttpStatusCodeException} based on an - * {@link HttpStatus}. + * Construct a new instance with an {@link HttpStatus}. * @param statusCode the status code */ protected HttpStatusCodeException(HttpStatus statusCode) { @@ -57,8 +46,7 @@ public abstract class HttpStatusCodeException extends RestClientException { } /** - * Construct a new instance of {@code HttpStatusCodeException} based on an - * {@link HttpStatus} and status text. + * Construct a new instance with an {@link HttpStatus} and status text. * @param statusCode the status code * @param statusText the status text */ @@ -67,23 +55,22 @@ public abstract class HttpStatusCodeException extends RestClientException { } /** - * Construct a new instance of {@code HttpStatusCodeException} based on an - * {@link HttpStatus}, status text, and response body content. + * Construct instance with an {@link HttpStatus}, status text, and content. * @param statusCode the status code * @param statusText the status text * @param responseBody the response body content, may be {@code null} * @param responseCharset the response body charset, may be {@code null} * @since 3.0.5 */ - protected HttpStatusCodeException( - HttpStatus statusCode, String statusText, byte[] responseBody, Charset responseCharset) { + protected HttpStatusCodeException(HttpStatus statusCode, String statusText, + byte[] responseBody, Charset responseCharset) { this(statusCode, statusText, null, responseBody, responseCharset); } /** - * Construct a new instance of {@code HttpStatusCodeException} based on an - * {@link HttpStatus}, status text, and response body content. + * Construct instance with an {@link HttpStatus}, status text, content, and + * a response charset. * @param statusCode the status code * @param statusText the status text * @param responseHeaders the response headers, may be {@code null} @@ -94,12 +81,9 @@ public abstract class HttpStatusCodeException extends RestClientException { protected HttpStatusCodeException(HttpStatus statusCode, String statusText, HttpHeaders responseHeaders, byte[] responseBody, Charset responseCharset) { - super(statusCode.value() + " " + statusText); + super(statusCode.value() + " " + statusText, statusCode.value(), statusText, + responseHeaders, responseBody, responseCharset); this.statusCode = statusCode; - this.statusText = statusText; - this.responseHeaders = responseHeaders; - this.responseBody = responseBody != null ? responseBody : new byte[0]; - this.responseCharset = responseCharset != null ? responseCharset.name() : DEFAULT_CHARSET; } @@ -110,41 +94,4 @@ public abstract class HttpStatusCodeException extends RestClientException { return this.statusCode; } - /** - * Return the HTTP status text. - */ - public String getStatusText() { - return this.statusText; - } - - /** - * Return the HTTP response headers. - * @since 3.1.2 - */ - public HttpHeaders getResponseHeaders() { - return this.responseHeaders; - } - - /** - * Return the response body as a byte array. - * @since 3.0.5 - */ - public byte[] getResponseBodyAsByteArray() { - return this.responseBody; - } - - /** - * Return the response body as a string. - * @since 3.0.5 - */ - public String getResponseBodyAsString() { - try { - return new String(this.responseBody, this.responseCharset); - } - catch (UnsupportedEncodingException ex) { - // should not occur - throw new IllegalStateException(ex); - } - } - } diff --git a/spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java b/spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java new file mode 100644 index 0000000000000000000000000000000000000000..3c6a4b3b0cd0ff47645224129ed2f38966ca0ed1 --- /dev/null +++ b/spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java @@ -0,0 +1,108 @@ +/* + * Copyright 2002-2016 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.UnsupportedEncodingException; +import java.nio.charset.Charset; + +import org.springframework.http.HttpHeaders; + +/** + * Abstract base class for exceptions that contain actual HTTP response data. + * + * @author Rossen Stoyanchev + * @since 4.3 + */ +public abstract class RestClientResponseException extends RestClientException { + + private static final long serialVersionUID = -8803556342728481792L; + + private static final String DEFAULT_CHARSET = "ISO-8859-1"; + + + private final int rawStatusCode; + + private final String statusText; + + private final byte[] responseBody; + + private final HttpHeaders responseHeaders; + + private final String responseCharset; + + + /** + * Construct a new instance of with the given response data. + * @param statusCode the raw status code value + * @param statusText the status text + * @param responseHeaders the response headers, may be {@code null} + * @param responseBody the response body content, may be {@code null} + * @param responseCharset the response body charset, may be {@code null} + */ + public RestClientResponseException(String message, int statusCode, String statusText, + HttpHeaders responseHeaders, byte[] responseBody, Charset responseCharset) { + + super(message); + this.rawStatusCode = statusCode; + this.statusText = statusText; + this.responseHeaders = responseHeaders; + this.responseBody = responseBody != null ? responseBody : new byte[0]; + this.responseCharset = responseCharset != null ? responseCharset.name() : DEFAULT_CHARSET; + } + + + /** + * Return the raw HTTP status code value. + */ + public int getRawStatusCode() { + return this.rawStatusCode; + } + + /** + * Return the HTTP status text. + */ + public String getStatusText() { + return this.statusText; + } + + /** + * Return the HTTP response headers. + */ + public HttpHeaders getResponseHeaders() { + return this.responseHeaders; + } + + /** + * Return the response body as a byte array. + */ + public byte[] getResponseBodyAsByteArray() { + return this.responseBody; + } + + /** + * Return the response body as a string. + */ + public String getResponseBodyAsString() { + try { + return new String(this.responseBody, this.responseCharset); + } + catch (UnsupportedEncodingException ex) { + // should not occur + throw new IllegalStateException(ex); + } + } + +} diff --git a/spring-web/src/main/java/org/springframework/web/client/UnknownHttpStatusCodeException.java b/spring-web/src/main/java/org/springframework/web/client/UnknownHttpStatusCodeException.java index 40ac50474f5073ee0f46a99737c4f31fb0794b43..b8e894b925471c29a2dd733339319ef70c6672c1 100644 --- a/spring-web/src/main/java/org/springframework/web/client/UnknownHttpStatusCodeException.java +++ b/spring-web/src/main/java/org/springframework/web/client/UnknownHttpStatusCodeException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 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. @@ -16,7 +16,6 @@ package org.springframework.web.client; -import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import org.springframework.http.HttpHeaders; @@ -28,21 +27,9 @@ import org.springframework.http.HttpStatus; * @author Rossen Stoyanchev * @since 3.2 */ -public class UnknownHttpStatusCodeException extends RestClientException { +public class UnknownHttpStatusCodeException extends RestClientResponseException { - private static final long serialVersionUID = 4702443689088991600L; - - private static final String DEFAULT_CHARSET = "ISO-8859-1"; - - private final int rawStatusCode; - - private final String statusText; - - private final byte[] responseBody; - - private final HttpHeaders responseHeaders; - - private final String responseCharset; + private static final long serialVersionUID = 7103980251635005491L; /** @@ -57,54 +44,8 @@ public class UnknownHttpStatusCodeException extends RestClientException { public UnknownHttpStatusCodeException(int rawStatusCode, String statusText, HttpHeaders responseHeaders, byte[] responseBody, Charset responseCharset) { - super("Unknown status code [" + String.valueOf(rawStatusCode) + "]" + " " + statusText); - this.rawStatusCode = rawStatusCode; - this.statusText = statusText; - this.responseHeaders = responseHeaders; - this.responseBody = responseBody != null ? responseBody : new byte[0]; - this.responseCharset = responseCharset != null ? responseCharset.name() : DEFAULT_CHARSET; - } - - - /** - * Return the raw HTTP status code value. - */ - public int getRawStatusCode() { - return this.rawStatusCode; - } - - /** - * Return the HTTP status text. - */ - public String getStatusText() { - return this.statusText; - } - - /** - * Return the HTTP response headers. - */ - public HttpHeaders getResponseHeaders() { - return this.responseHeaders; - } - - /** - * Return the response body as a byte array. - */ - public byte[] getResponseBodyAsByteArray() { - return this.responseBody; - } - - /** - * Return the response body as a string. - */ - public String getResponseBodyAsString() { - try { - return new String(this.responseBody, this.responseCharset); - } - catch (UnsupportedEncodingException ex) { - // should not occur - throw new IllegalStateException(ex); - } + super("Unknown status code [" + String.valueOf(rawStatusCode) + "]" + " " + statusText, + rawStatusCode, statusText, responseHeaders, responseBody, responseCharset); } }