Skip to content

Commit bca2357

Browse files
committed
Polish web.client exceptions and related classes
The following style updates have been made in anticipation of substantive changes in subsequent commits: - organize imports - correct whitespace errors (leading spaces in code, tabs in Javadoc) - wrap Javadoc at 90 chars; make imperative ("Return" vs. "Returns") - use conventional constructor argument wrapping A serialVersionUID has also been added to RestClientException and its ResourceAccessException subclass for consistency with the rest of that same exception hierarchy. Issue: SPR-7938
1 parent b8ff6c1 commit bca2357

File tree

8 files changed

+78
-62
lines changed

8 files changed

+78
-62
lines changed

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

Lines changed: 4 additions & 1 deletion
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.
@@ -17,9 +17,12 @@
1717
package org.springframework.http;
1818

1919
import java.net.URI;
20+
2021
import java.nio.charset.Charset;
22+
2123
import java.text.ParseException;
2224
import java.text.SimpleDateFormat;
25+
2326
import java.util.ArrayList;
2427
import java.util.Collection;
2528
import java.util.Collections;

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

Lines changed: 12 additions & 11 deletions
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.
@@ -28,10 +28,11 @@
2828
/**
2929
* Default implementation of the {@link ResponseErrorHandler} interface.
3030
*
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.
31+
* <p>This error handler checks for the status code on the {@link ClientHttpResponse}: any
32+
* code with series {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} or
33+
* {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR} is considered to be an
34+
* error. This behavior can be changed by overriding the {@link #hasError(HttpStatus)}
35+
* method.
3536
*
3637
* @author Arjen Poutsma
3738
* @since 3.0
@@ -83,15 +84,15 @@ public void handleError(ClientHttpResponse response) throws IOException {
8384

8485
private byte[] getResponseBody(ClientHttpResponse response) {
8586
try {
86-
InputStream responseBody = response.getBody();
87-
if (responseBody != null) {
88-
return FileCopyUtils.copyToByteArray(responseBody);
89-
}
87+
InputStream responseBody = response.getBody();
88+
if (responseBody != null) {
89+
return FileCopyUtils.copyToByteArray(responseBody);
90+
}
9091
}
9192
catch (IOException ex) {
92-
// ignore
93+
// ignore
9394
}
94-
return new byte[0];
95+
return new byte[0];
9596
}
9697

9798
}

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,19 @@ public class HttpClientErrorException extends HttpStatusCodeException {
3131

3232
private static final long serialVersionUID = 6777393766937023392L;
3333

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

4244
/**
43-
* Construct a new instance of {@code HttpClientErrorException} based on a {@link HttpStatus} and status text.
45+
* Construct a new instance of {@code HttpClientErrorException} based on an
46+
* {@link HttpStatus} and status text.
4447
* @param statusCode the status code
4548
* @param statusText the status text
4649
*/
@@ -49,18 +52,16 @@ public HttpClientErrorException(HttpStatus statusCode, String statusText) {
4952
}
5053

5154
/**
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}
55+
* Construct a new instance of {@code HttpClientErrorException} based on an
56+
* {@link HttpStatus}, status text, and response body content.
57+
* @param statusCode the status code
58+
* @param statusText the status text
59+
* @param responseBody the response body content, may be {@code null}
5860
* @param responseCharset the response body charset, may be {@code null}
5961
*/
60-
public HttpClientErrorException(HttpStatus statusCode,
61-
String statusText,
62-
byte[] responseBody,
63-
Charset responseCharset) {
62+
public HttpClientErrorException(HttpStatus statusCode, String statusText,
63+
byte[] responseBody, Charset responseCharset) {
6464
super(statusCode, statusText, responseBody, responseCharset);
6565
}
66+
6667
}

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,19 @@ public class HttpServerErrorException extends HttpStatusCodeException {
3131

3232
private static final long serialVersionUID = -2565832100451369997L;
3333

34+
3435
/**
35-
* Construct a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus}.
36-
*
36+
* Construct a new instance of {@code HttpServerErrorException} based on an
37+
* {@link HttpStatus}.
3738
* @param statusCode the status code
3839
*/
3940
public HttpServerErrorException(HttpStatus statusCode) {
4041
super(statusCode);
4142
}
4243

4344
/**
44-
* Construct a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus} and status text.
45-
*
45+
* Construct a new instance of {@code HttpServerErrorException} based on an
46+
* {@link HttpStatus} and status text.
4647
* @param statusCode the status code
4748
* @param statusText the status text
4849
*/
@@ -51,19 +52,17 @@ public HttpServerErrorException(HttpStatus statusCode, String statusText) {
5152
}
5253

5354
/**
54-
* Construct a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus}, status text, and
55-
* response body content.
56-
*
55+
* Construct a new instance of {@code HttpServerErrorException} based on an
56+
* {@link HttpStatus}, status text, and response body content.
5757
* @param statusCode the status code
5858
* @param statusText the status text
5959
* @param responseBody the response body content, may be {@code null}
6060
* @param responseCharset the response body charset, may be {@code null}
6161
* @since 3.0.5
6262
*/
63-
public HttpServerErrorException(HttpStatus statusCode,
64-
String statusText,
65-
byte[] responseBody,
66-
Charset responseCharset) {
63+
public HttpServerErrorException(HttpStatus statusCode, String statusText,
64+
byte[] responseBody, Charset responseCharset) {
6765
super(statusCode, statusText, responseBody, responseCharset);
6866
}
67+
6968
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,19 @@ public abstract class HttpStatusCodeException extends RestClientException {
4242

4343
private final String responseCharset;
4444

45+
4546
/**
46-
* Construct a new instance of {@code HttpStatusCodeException} based on a {@link HttpStatus}.
47-
*
47+
* Construct a new instance of {@code HttpStatusCodeException} based on an
48+
* {@link HttpStatus}.
4849
* @param statusCode the status code
4950
*/
5051
protected HttpStatusCodeException(HttpStatus statusCode) {
5152
this(statusCode, statusCode.name(), null, null);
5253
}
5354

5455
/**
55-
* Construct a new instance of {@code HttpStatusCodeException} based on a {@link HttpStatus} and status text.
56-
*
56+
* Construct a new instance of {@code HttpStatusCodeException} based on an
57+
* {@link HttpStatus} and status text.
5758
* @param statusCode the status code
5859
* @param statusText the status text
5960
*/
@@ -62,12 +63,11 @@ protected HttpStatusCodeException(HttpStatus statusCode, String statusText) {
6263
}
6364

6465
/**
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}
66+
* Construct a new instance of {@code HttpStatusCodeException} based on an
67+
* {@link HttpStatus}, status text, and response body content.
68+
* @param statusCode the status code
69+
* @param statusText the status text
70+
* @param responseBody the response body content, may be {@code null}
7171
* @param responseCharset the response body charset, may be {@code null}
7272
* @since 3.0.5
7373
*/
@@ -82,32 +82,31 @@ protected HttpStatusCodeException(HttpStatus statusCode,
8282
this.responseCharset = responseCharset != null ? responseCharset.name() : DEFAULT_CHARSET;
8383
}
8484

85+
8586
/**
86-
* Returns the HTTP status code.
87+
* Return the HTTP status code.
8788
*/
8889
public HttpStatus getStatusCode() {
8990
return this.statusCode;
9091
}
9192

9293
/**
93-
* Returns the HTTP status text.
94+
* Return the HTTP status text.
9495
*/
9596
public String getStatusText() {
9697
return this.statusText;
9798
}
9899

99100
/**
100-
* Returns the response body as a byte array.
101-
*
101+
* Return the response body as a byte array.
102102
* @since 3.0.5
103103
*/
104104
public byte[] getResponseBodyAsByteArray() {
105105
return responseBody;
106106
}
107107

108108
/**
109-
* Returns the response body as a string.
110-
*
109+
* Return the response body as a string.
111110
* @since 3.0.5
112111
*/
113112
public String getResponseBodyAsString() {
@@ -119,4 +118,5 @@ public String getResponseBodyAsString() {
119118
throw new InternalError(ex.getMessage());
120119
}
121120
}
121+
122122
}

spring-web/src/main/java/org/springframework/web/client/ResourceAccessException.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 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.
@@ -26,6 +26,9 @@
2626
*/
2727
public class ResourceAccessException extends RestClientException {
2828

29+
private static final long serialVersionUID = -8513182514355844870L;
30+
31+
2932
/**
3033
* Construct a new {@code HttpIOException} with the given message.
3134
* @param msg the message

spring-web/src/main/java/org/springframework/web/client/RestClientException.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 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.
@@ -19,13 +19,17 @@
1919
import org.springframework.core.NestedRuntimeException;
2020

2121
/**
22-
* Base class for exceptions thrown by {@link RestTemplate} whenever it encounters client-side HTTP errors.
22+
* Base class for exceptions thrown by {@link RestTemplate} whenever it encounters
23+
* client-side HTTP errors.
2324
*
2425
* @author Arjen Poutsma
2526
* @since 3.0
2627
*/
2728
public class RestClientException extends NestedRuntimeException {
2829

30+
private static final long serialVersionUID = -4084444984163796577L;
31+
32+
2933
/**
3034
* Construct a new instance of {@code HttpClientException} with the given message.
3135
* @param msg the message
@@ -35,7 +39,8 @@ public RestClientException(String msg) {
3539
}
3640

3741
/**
38-
* Construct a new instance of {@code HttpClientException} with the given message and exception.
42+
* Construct a new instance of {@code HttpClientException} with the given message and
43+
* exception.
3944
* @param msg the message
4045
* @param ex the exception
4146
*/

spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java

Lines changed: 11 additions & 7 deletions
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.
@@ -19,19 +19,23 @@
1919
import java.io.ByteArrayInputStream;
2020
import java.io.IOException;
2121

22+
import org.junit.Before;
23+
import org.junit.Test;
24+
2225
import org.springframework.http.HttpHeaders;
2326
import org.springframework.http.HttpStatus;
2427
import org.springframework.http.MediaType;
2528
import org.springframework.http.client.ClientHttpResponse;
2629

27-
import org.junit.Before;
28-
import org.junit.Test;
29-
3030
import static org.easymock.EasyMock.*;
31-
import static org.junit.Assert.assertFalse;
32-
import static org.junit.Assert.assertTrue;
3331

34-
/** @author Arjen Poutsma */
32+
import static org.junit.Assert.*;
33+
34+
/**
35+
* Unit tests for {@link DefaultResponseErrorHandler}.
36+
*
37+
* @author Arjen Poutsma
38+
*/
3539
public class DefaultResponseErrorHandlerTests {
3640

3741
private DefaultResponseErrorHandler handler;

0 commit comments

Comments
 (0)