-
Notifications
You must be signed in to change notification settings - Fork 37.7k
/
RestClientResponseException.java
167 lines (136 loc) · 5.05 KB
/
RestClientResponseException.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright 2002-2019 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
*
* https://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 java.nio.charset.StandardCharsets;
import org.springframework.http.HttpHeaders;
import org.springframework.lang.Nullable;
/**
* Common base class for exceptions that contain actual HTTP response data.
*
* @author Rossen Stoyanchev
* @since 4.3
*/
public class RestClientResponseException extends RestClientException {
private static final long serialVersionUID = -8803556342728481792L;
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
private final int rawStatusCode;
private final String statusText;
private final byte[] responseBody;
@Nullable
private final HttpHeaders responseHeaders;
@Nullable
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,
@Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable 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() : null);
}
public RestClientResponseException(String message, int statusCode, String statusText, @Nullable HttpHeaders responseHeaders, Throwable ex){
super(message, ex);
this.rawStatusCode = statusCode;
this.statusText = statusText;
this.responseHeaders = responseHeaders;
this.responseBody = new byte[0];
this.responseCharset = null;
}
/**
* 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.
*/
@Nullable
public HttpHeaders getResponseHeaders() {
return this.responseHeaders;
}
/**
* Return the response body as a byte array.
*/
public byte[] getResponseBodyAsByteArray() {
return this.responseBody;
}
/**
* Return the response body converted to String. The charset used is that
* of the response "Content-Type" or otherwise {@code "UTF-8"}.
*/
public String getResponseBodyAsString() {
return getResponseBodyAsString(DEFAULT_CHARSET);
}
/**
* Return the response body converted to String. The charset used is that
* of the response "Content-Type" or otherwise the one given.
* @param fallbackCharset the charset to use on if the response doesn't specify.
* @since 5.1.11
*/
public String getResponseBodyAsString(Charset fallbackCharset) {
if (this.responseCharset == null) {
return new String(this.responseBody, fallbackCharset);
}
try {
return new String(this.responseBody, this.responseCharset);
}
catch (UnsupportedEncodingException ex) {
// should not occur
throw new IllegalStateException(ex);
}
}
/**
* {@link RestClientResponseException} for can not find suitable message converter to extract body.
* @since 5.2.7
*/
@SuppressWarnings("serial")
public static final class MessageConverterNotFound extends RestClientResponseException {
public MessageConverterNotFound(String message, int statusCode, String statusText, HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset) {
super(message, statusCode, statusText, responseHeaders, responseBody, responseCharset);
}
public MessageConverterNotFound(String message, int statusCode, String statusText, HttpHeaders responseHeaders, Throwable ex) {
super(message, statusCode, statusText, responseHeaders, ex);
}
}
/**
* {@link RestClientResponseException} for exception raised when read body stream to extract body.
* @since 5.2.7
*/
@SuppressWarnings("serial")
public static final class FailedToReadResponseBody extends RestClientResponseException {
public FailedToReadResponseBody(String message, int statusCode, String statusText, HttpHeaders responseHeaders, Throwable ex) {
super(message, statusCode, statusText, responseHeaders, ex);
}
}
}