Skip to content

Commit c471bdd

Browse files
committed
Merge pull request #86 from scothis/SPR-7938
* SPR-7938: Include response headers in RestTemplate exceptions Polish web.client exceptions and related classes
2 parents b8ff6c1 + b992c3d commit c471bdd

File tree

8 files changed

+159
-72
lines changed

8 files changed

+159
-72
lines changed

Diff for: spring-web/src/main/java/org/springframework/http/HttpHeaders.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,15 @@
1616

1717
package org.springframework.http;
1818

19+
import java.io.Serializable;
20+
1921
import java.net.URI;
22+
2023
import java.nio.charset.Charset;
24+
2125
import java.text.ParseException;
2226
import java.text.SimpleDateFormat;
27+
2328
import java.util.ArrayList;
2429
import java.util.Collection;
2530
import java.util.Collections;
@@ -54,7 +59,9 @@
5459
* @author Arjen Poutsma
5560
* @since 3.0
5661
*/
57-
public class HttpHeaders implements MultiValueMap<String, String> {
62+
public class HttpHeaders implements MultiValueMap<String, String>, Serializable {
63+
64+
private static final long serialVersionUID = -8578554704772377436L;
5865

5966
private static final String ACCEPT = "Accept";
6067

Diff for: spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.io.InputStream;
2121
import java.nio.charset.Charset;
2222

23+
import org.springframework.http.HttpHeaders;
2324
import org.springframework.http.HttpStatus;
2425
import org.springframework.http.MediaType;
2526
import org.springframework.http.client.ClientHttpResponse;
@@ -28,10 +29,11 @@
2829
/**
2930
* Default implementation of the {@link ResponseErrorHandler} interface.
3031
*
31-
* <p>This error handler checks for the status code on the {@link ClientHttpResponse}: any code with series
32-
* {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} or
33-
* {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR} is considered to be an error.
34-
* This behavior can be changed by overriding the {@link #hasError(HttpStatus)} method.
32+
* <p>This error handler checks for the status code on the {@link ClientHttpResponse}: any
33+
* code with series {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} or
34+
* {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR} is considered to be an
35+
* error. This behavior can be changed by overriding the {@link #hasError(HttpStatus)}
36+
* method.
3537
*
3638
* @author Arjen Poutsma
3739
* @since 3.0
@@ -68,30 +70,31 @@ protected boolean hasError(HttpStatus statusCode) {
6870
*/
6971
public void handleError(ClientHttpResponse response) throws IOException {
7072
HttpStatus statusCode = response.getStatusCode();
71-
MediaType contentType = response.getHeaders().getContentType();
73+
HttpHeaders headers = response.getHeaders();
74+
MediaType contentType = headers.getContentType();
7275
Charset charset = contentType != null ? contentType.getCharSet() : null;
7376
byte[] body = getResponseBody(response);
7477
switch (statusCode.series()) {
7578
case CLIENT_ERROR:
76-
throw new HttpClientErrorException(statusCode, response.getStatusText(), body, charset);
79+
throw new HttpClientErrorException(statusCode, response.getStatusText(), headers, body, charset);
7780
case SERVER_ERROR:
78-
throw new HttpServerErrorException(statusCode, response.getStatusText(), body, charset);
81+
throw new HttpServerErrorException(statusCode, response.getStatusText(), headers, body, charset);
7982
default:
8083
throw new RestClientException("Unknown status code [" + statusCode + "]");
8184
}
8285
}
8386

8487
private byte[] getResponseBody(ClientHttpResponse response) {
8588
try {
86-
InputStream responseBody = response.getBody();
87-
if (responseBody != null) {
88-
return FileCopyUtils.copyToByteArray(responseBody);
89-
}
89+
InputStream responseBody = response.getBody();
90+
if (responseBody != null) {
91+
return FileCopyUtils.copyToByteArray(responseBody);
92+
}
9093
}
9194
catch (IOException ex) {
92-
// ignore
95+
// ignore
9396
}
94-
return new byte[0];
97+
return new byte[0];
9598
}
9699

97100
}

Diff for: spring-web/src/main/java/org/springframework/web/client/HttpClientErrorException.java

+30-13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.nio.charset.Charset;
2020

21+
import org.springframework.http.HttpHeaders;
2122
import org.springframework.http.HttpStatus;
2223

2324
/**
@@ -29,18 +30,21 @@
2930
*/
3031
public class HttpClientErrorException extends HttpStatusCodeException {
3132

32-
private static final long serialVersionUID = 6777393766937023392L;
33+
private static final long serialVersionUID = 5177019431887513952L;
34+
3335

3436
/**
35-
* Construct a new instance of {@code HttpClientErrorException} based on a {@link HttpStatus}.
37+
* Construct a new instance of {@code HttpClientErrorException} based on an
38+
* {@link HttpStatus}.
3639
* @param statusCode the status code
3740
*/
3841
public HttpClientErrorException(HttpStatus statusCode) {
3942
super(statusCode);
4043
}
4144

4245
/**
43-
* Construct a new instance of {@code HttpClientErrorException} based on a {@link HttpStatus} and status text.
46+
* Construct a new instance of {@code HttpClientErrorException} based on an
47+
* {@link HttpStatus} and status text.
4448
* @param statusCode the status code
4549
* @param statusText the status text
4650
*/
@@ -49,18 +53,31 @@ public HttpClientErrorException(HttpStatus statusCode, String statusText) {
4953
}
5054

5155
/**
52-
* Construct a new instance of {@code HttpClientErrorException} based on a {@link HttpStatus}, status text, and
53-
* response body content.
54-
*
55-
* @param statusCode the status code
56-
* @param statusText the status text
57-
* @param responseBody the response body content, may be {@code null}
56+
* Construct a new instance of {@code HttpClientErrorException} based on an
57+
* {@link HttpStatus}, status text, and response body content.
58+
* @param statusCode the status code
59+
* @param statusText the status text
60+
* @param responseBody the response body content, may be {@code null}
5861
* @param responseCharset the response body charset, may be {@code null}
5962
*/
60-
public HttpClientErrorException(HttpStatus statusCode,
61-
String statusText,
62-
byte[] responseBody,
63-
Charset responseCharset) {
63+
public HttpClientErrorException(HttpStatus statusCode, String statusText,
64+
byte[] responseBody, Charset responseCharset) {
6465
super(statusCode, statusText, responseBody, responseCharset);
6566
}
67+
68+
/**
69+
* Construct a new instance of {@code HttpClientErrorException} 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 responseHeaders the response headers, may be {@code null}
74+
* @param responseBody the response body content, may be {@code null}
75+
* @param responseCharset the response body charset, may be {@code null}
76+
* @since 3.2
77+
*/
78+
public HttpClientErrorException(HttpStatus statusCode, String statusText,
79+
HttpHeaders responseHeaders, byte[] responseBody, Charset responseCharset) {
80+
super(statusCode, statusText, responseHeaders, responseBody, responseCharset);
81+
}
82+
6683
}

Diff for: spring-web/src/main/java/org/springframework/web/client/HttpServerErrorException.java

+26-12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.nio.charset.Charset;
2020

21+
import org.springframework.http.HttpHeaders;
2122
import org.springframework.http.HttpStatus;
2223

2324
/**
@@ -29,20 +30,21 @@
2930
*/
3031
public class HttpServerErrorException extends HttpStatusCodeException {
3132

32-
private static final long serialVersionUID = -2565832100451369997L;
33+
private static final long serialVersionUID = -2915754006618138282L;
34+
3335

3436
/**
35-
* Construct a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus}.
36-
*
37+
* Construct a new instance of {@code HttpServerErrorException} based on an
38+
* {@link HttpStatus}.
3739
* @param statusCode the status code
3840
*/
3941
public HttpServerErrorException(HttpStatus statusCode) {
4042
super(statusCode);
4143
}
4244

4345
/**
44-
* Construct a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus} and status text.
45-
*
46+
* Construct a new instance of {@code HttpServerErrorException} based on an
47+
* {@link HttpStatus} and status text.
4648
* @param statusCode the status code
4749
* @param statusText the status text
4850
*/
@@ -51,19 +53,31 @@ public HttpServerErrorException(HttpStatus statusCode, String statusText) {
5153
}
5254

5355
/**
54-
* Construct a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus}, status text, and
55-
* response body content.
56-
*
56+
* Construct a new instance of {@code HttpServerErrorException} based on an
57+
* {@link HttpStatus}, status text, and response body content.
5758
* @param statusCode the status code
5859
* @param statusText the status text
5960
* @param responseBody the response body content, may be {@code null}
6061
* @param responseCharset the response body charset, may be {@code null}
6162
* @since 3.0.5
6263
*/
63-
public HttpServerErrorException(HttpStatus statusCode,
64-
String statusText,
65-
byte[] responseBody,
66-
Charset responseCharset) {
64+
public HttpServerErrorException(HttpStatus statusCode, String statusText,
65+
byte[] responseBody, Charset responseCharset) {
6766
super(statusCode, statusText, responseBody, responseCharset);
6867
}
68+
69+
/**
70+
* Construct a new instance of {@code HttpServerErrorException} based on a
71+
* {@link HttpStatus}, status text, and response body content.
72+
* @param statusCode the status code
73+
* @param statusText the status text
74+
* @param responseHeaders the response headers, may be {@code null}
75+
* @param responseBody the response body content, may be {@code null}
76+
* @param responseCharset the response body charset, may be {@code null}
77+
* @since 3.2
78+
*/
79+
public HttpServerErrorException(HttpStatus statusCode, String statusText,
80+
HttpHeaders responseHeaders, byte[] responseBody, Charset responseCharset) {
81+
super(statusCode, statusText, responseHeaders, responseBody, responseCharset);
82+
}
6983
}

Diff for: spring-web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java

+46-18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.UnsupportedEncodingException;
2020
import java.nio.charset.Charset;
2121

22+
import org.springframework.http.HttpHeaders;
2223
import org.springframework.http.HttpStatus;
2324

2425
/**
@@ -30,7 +31,7 @@
3031
*/
3132
public abstract class HttpStatusCodeException extends RestClientException {
3233

33-
private static final long serialVersionUID = 1549626836533638803L;
34+
private static final long serialVersionUID = -5807494703720513267L;
3435

3536
private static final String DEFAULT_CHARSET = "ISO-8859-1";
3637

@@ -40,64 +41,91 @@ public abstract class HttpStatusCodeException extends RestClientException {
4041

4142
private final byte[] responseBody;
4243

44+
private final HttpHeaders responseHeaders;
45+
4346
private final String responseCharset;
4447

48+
4549
/**
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}.
4852
* @param statusCode the status code
4953
*/
5054
protected HttpStatusCodeException(HttpStatus statusCode) {
51-
this(statusCode, statusCode.name(), null, null);
55+
this(statusCode, statusCode.name(), null, null, null);
5256
}
5357

5458
/**
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.
5761
* @param statusCode the status code
5862
* @param statusText the status text
5963
*/
6064
protected HttpStatusCodeException(HttpStatus statusCode, String statusText) {
61-
this(statusCode, statusText, null, null);
65+
this(statusCode, statusText, null, null, null);
6266
}
6367

6468
/**
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}
7174
* @param responseCharset the response body charset, may be {@code null}
7275
* @since 3.0.5
7376
*/
7477
protected HttpStatusCodeException(HttpStatus statusCode,
7578
String statusText,
7679
byte[] responseBody,
7780
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) {
7896
super(statusCode.value() + " " + statusText);
7997
this.statusCode = statusCode;
8098
this.statusText = statusText;
99+
this.responseHeaders = responseHeaders;
81100
this.responseBody = responseBody != null ? responseBody : new byte[0];
82101
this.responseCharset = responseCharset != null ? responseCharset.name() : DEFAULT_CHARSET;
83102
}
84103

104+
85105
/**
86-
* Returns the HTTP status code.
106+
* Return the HTTP status code.
87107
*/
88108
public HttpStatus getStatusCode() {
89109
return this.statusCode;
90110
}
91111

92112
/**
93-
* Returns the HTTP status text.
113+
* Return the HTTP status text.
94114
*/
95115
public String getStatusText() {
96116
return this.statusText;
97117
}
98118

99119
/**
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.
101129
*
102130
* @since 3.0.5
103131
*/
@@ -106,8 +134,7 @@ public byte[] getResponseBodyAsByteArray() {
106134
}
107135

108136
/**
109-
* Returns the response body as a string.
110-
*
137+
* Return the response body as a string.
111138
* @since 3.0.5
112139
*/
113140
public String getResponseBodyAsString() {
@@ -119,4 +146,5 @@ public String getResponseBodyAsString() {
119146
throw new InternalError(ex.getMessage());
120147
}
121148
}
149+
122150
}

0 commit comments

Comments
 (0)