提交 b11d345c 编写于 作者: R Rossen Stoyanchev

Common base class for [Unknown]HttpStatusCodeException

Issue: SPR-13928
上级 901c2d5f
/*
* 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);
}
}
}
/*
* 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);
}
}
}
/*
* 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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册