19
19
import java .io .UnsupportedEncodingException ;
20
20
import java .nio .charset .Charset ;
21
21
22
+ import org .springframework .http .HttpHeaders ;
22
23
import org .springframework .http .HttpStatus ;
23
24
24
25
/**
30
31
*/
31
32
public abstract class HttpStatusCodeException extends RestClientException {
32
33
33
- private static final long serialVersionUID = 1549626836533638803L ;
34
+ private static final long serialVersionUID = - 5807494703720513267L ;
34
35
35
36
private static final String DEFAULT_CHARSET = "ISO-8859-1" ;
36
37
@@ -40,64 +41,91 @@ public abstract class HttpStatusCodeException extends RestClientException {
40
41
41
42
private final byte [] responseBody ;
42
43
44
+ private final HttpHeaders responseHeaders ;
45
+
43
46
private final String responseCharset ;
44
47
48
+
45
49
/**
46
- * Construct a new instance of {@code HttpStatusCodeException} based on a {@link HttpStatus}.
47
- *
50
+ * Construct a new instance of {@code HttpStatusCodeException} based on an
51
+ * {@link HttpStatus}.
48
52
* @param statusCode the status code
49
53
*/
50
54
protected HttpStatusCodeException (HttpStatus statusCode ) {
51
- this (statusCode , statusCode .name (), null , null );
55
+ this (statusCode , statusCode .name (), null , null , null );
52
56
}
53
57
54
58
/**
55
- * Construct a new instance of {@code HttpStatusCodeException} based on a {@link HttpStatus} and status text.
56
- *
59
+ * Construct a new instance of {@code HttpStatusCodeException} based on an
60
+ * {@link HttpStatus} and status text.
57
61
* @param statusCode the status code
58
62
* @param statusText the status text
59
63
*/
60
64
protected HttpStatusCodeException (HttpStatus statusCode , String statusText ) {
61
- this (statusCode , statusText , null , null );
65
+ this (statusCode , statusText , null , null , null );
62
66
}
63
67
64
68
/**
65
- * Construct a new instance of {@code HttpStatusCodeException} based on a {@link HttpStatus}, status text, and
66
- * response body content.
67
- *
68
- * @param statusCode the status code
69
- * @param statusText the status text
70
- * @param responseBody the response body content, may be {@code null}
69
+ * Construct a new instance of {@code HttpStatusCodeException} based on an
70
+ * {@link HttpStatus}, status text, and response body content.
71
+ * @param statusCode the status code
72
+ * @param statusText the status text
73
+ * @param responseBody the response body content, may be {@code null}
71
74
* @param responseCharset the response body charset, may be {@code null}
72
75
* @since 3.0.5
73
76
*/
74
77
protected HttpStatusCodeException (HttpStatus statusCode ,
75
78
String statusText ,
76
79
byte [] responseBody ,
77
80
Charset responseCharset ) {
81
+ this (statusCode , statusText , null , responseBody , responseCharset );
82
+ }
83
+
84
+ /**
85
+ * Construct a new instance of {@code HttpStatusCodeException} based on an
86
+ * {@link HttpStatus}, status text, and response body content.
87
+ * @param statusCode the status code
88
+ * @param statusText the status text
89
+ * @param responseHeaders the response headers, may be {@code null}
90
+ * @param responseBody the response body content, may be {@code null}
91
+ * @param responseCharset the response body charset, may be {@code null}
92
+ * @since 3.2
93
+ */
94
+ protected HttpStatusCodeException (HttpStatus statusCode , String statusText ,
95
+ HttpHeaders responseHeaders , byte [] responseBody , Charset responseCharset ) {
78
96
super (statusCode .value () + " " + statusText );
79
97
this .statusCode = statusCode ;
80
98
this .statusText = statusText ;
99
+ this .responseHeaders = responseHeaders ;
81
100
this .responseBody = responseBody != null ? responseBody : new byte [0 ];
82
101
this .responseCharset = responseCharset != null ? responseCharset .name () : DEFAULT_CHARSET ;
83
102
}
84
103
104
+
85
105
/**
86
- * Returns the HTTP status code.
106
+ * Return the HTTP status code.
87
107
*/
88
108
public HttpStatus getStatusCode () {
89
109
return this .statusCode ;
90
110
}
91
111
92
112
/**
93
- * Returns the HTTP status text.
113
+ * Return the HTTP status text.
94
114
*/
95
115
public String getStatusText () {
96
116
return this .statusText ;
97
117
}
98
118
99
119
/**
100
- * Returns the response body as a byte array.
120
+ * Return the HTTP response headers.
121
+ * @since 3.2
122
+ */
123
+ public HttpHeaders getResponseHeaders () {
124
+ return this .responseHeaders ;
125
+ }
126
+
127
+ /**
128
+ * Return the response body as a byte array.
101
129
*
102
130
* @since 3.0.5
103
131
*/
@@ -106,8 +134,7 @@ public byte[] getResponseBodyAsByteArray() {
106
134
}
107
135
108
136
/**
109
- * Returns the response body as a string.
110
- *
137
+ * Return the response body as a string.
111
138
* @since 3.0.5
112
139
*/
113
140
public String getResponseBodyAsString () {
@@ -119,4 +146,5 @@ public String getResponseBodyAsString() {
119
146
throw new InternalError (ex .getMessage ());
120
147
}
121
148
}
149
+
122
150
}
0 commit comments