From bca2357be784d62bbcc8f4e4d520fd5c778c712c Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Fri, 1 Jun 2012 16:08:01 +0200 Subject: [PATCH] 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 --- .../org/springframework/http/HttpHeaders.java | 5 ++- .../client/DefaultResponseErrorHandler.java | 23 ++++++------- .../web/client/HttpClientErrorException.java | 25 ++++++++------- .../web/client/HttpServerErrorException.java | 21 ++++++------ .../web/client/HttpStatusCodeException.java | 32 +++++++++---------- .../web/client/ResourceAccessException.java | 5 ++- .../web/client/RestClientException.java | 11 +++++-- .../DefaultResponseErrorHandlerTests.java | 18 +++++++---- 8 files changed, 78 insertions(+), 62 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java index 2bc17bb2c7c..db44db0c80f 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -17,9 +17,12 @@ package org.springframework.http; import java.net.URI; + import java.nio.charset.Charset; + import java.text.ParseException; import java.text.SimpleDateFormat; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; diff --git a/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java b/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java index 0615f7a823a..924f025384e 100644 --- a/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java +++ b/spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -28,10 +28,11 @@ import org.springframework.util.FileCopyUtils; /** * Default implementation of the {@link ResponseErrorHandler} interface. * - *

This error handler checks for the status code on the {@link ClientHttpResponse}: any code with series - * {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} or - * {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR} is considered to be an error. - * This behavior can be changed by overriding the {@link #hasError(HttpStatus)} method. + *

This error handler checks for the status code on the {@link ClientHttpResponse}: any + * code with series {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} or + * {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR} is considered to be an + * error. This behavior can be changed by overriding the {@link #hasError(HttpStatus)} + * method. * * @author Arjen Poutsma * @since 3.0 @@ -83,15 +84,15 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler { private byte[] getResponseBody(ClientHttpResponse response) { try { - InputStream responseBody = response.getBody(); - if (responseBody != null) { - return FileCopyUtils.copyToByteArray(responseBody); - } + InputStream responseBody = response.getBody(); + if (responseBody != null) { + return FileCopyUtils.copyToByteArray(responseBody); + } } catch (IOException ex) { - // ignore + // ignore } - return new byte[0]; + return new byte[0]; } } diff --git a/spring-web/src/main/java/org/springframework/web/client/HttpClientErrorException.java b/spring-web/src/main/java/org/springframework/web/client/HttpClientErrorException.java index 99de72b1607..05126bf00c2 100644 --- a/spring-web/src/main/java/org/springframework/web/client/HttpClientErrorException.java +++ b/spring-web/src/main/java/org/springframework/web/client/HttpClientErrorException.java @@ -31,8 +31,10 @@ public class HttpClientErrorException extends HttpStatusCodeException { private static final long serialVersionUID = 6777393766937023392L; + /** - * Construct a new instance of {@code HttpClientErrorException} based on a {@link HttpStatus}. + * Construct a new instance of {@code HttpClientErrorException} based on an + * {@link HttpStatus}. * @param statusCode the status code */ public HttpClientErrorException(HttpStatus statusCode) { @@ -40,7 +42,8 @@ public class HttpClientErrorException extends HttpStatusCodeException { } /** - * Construct a new instance of {@code HttpClientErrorException} based on a {@link HttpStatus} and status text. + * Construct a new instance of {@code HttpClientErrorException} based on an + * {@link HttpStatus} and status text. * @param statusCode the status code * @param statusText the status text */ @@ -49,18 +52,16 @@ public class HttpClientErrorException extends HttpStatusCodeException { } /** - * Construct a new instance of {@code HttpClientErrorException} based on a {@link HttpStatus}, status text, and - * response body content. - * - * @param statusCode the status code - * @param statusText the status text - * @param responseBody the response body content, may be {@code null} + * Construct a new instance of {@code HttpClientErrorException} based on an + * {@link HttpStatus}, status text, and response body 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} */ - public HttpClientErrorException(HttpStatus statusCode, - String statusText, - byte[] responseBody, - Charset responseCharset) { + public HttpClientErrorException(HttpStatus statusCode, String statusText, + byte[] responseBody, Charset responseCharset) { super(statusCode, statusText, responseBody, responseCharset); } + } diff --git a/spring-web/src/main/java/org/springframework/web/client/HttpServerErrorException.java b/spring-web/src/main/java/org/springframework/web/client/HttpServerErrorException.java index 9549dcbd0e5..d6ad9653898 100644 --- a/spring-web/src/main/java/org/springframework/web/client/HttpServerErrorException.java +++ b/spring-web/src/main/java/org/springframework/web/client/HttpServerErrorException.java @@ -31,9 +31,10 @@ public class HttpServerErrorException extends HttpStatusCodeException { private static final long serialVersionUID = -2565832100451369997L; + /** - * Construct a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus}. - * + * Construct a new instance of {@code HttpServerErrorException} based on an + * {@link HttpStatus}. * @param statusCode the status code */ public HttpServerErrorException(HttpStatus statusCode) { @@ -41,8 +42,8 @@ public class HttpServerErrorException extends HttpStatusCodeException { } /** - * Construct a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus} and status text. - * + * Construct a new instance of {@code HttpServerErrorException} based on an + * {@link HttpStatus} and status text. * @param statusCode the status code * @param statusText the status text */ @@ -51,19 +52,17 @@ public class HttpServerErrorException extends HttpStatusCodeException { } /** - * Construct a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus}, status text, and - * response body content. - * + * Construct a new instance of {@code HttpServerErrorException} based on an + * {@link HttpStatus}, status text, and response body 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 */ - public HttpServerErrorException(HttpStatus statusCode, - String statusText, - byte[] responseBody, - Charset responseCharset) { + public HttpServerErrorException(HttpStatus statusCode, String statusText, + byte[] responseBody, Charset responseCharset) { super(statusCode, statusText, responseBody, responseCharset); } + } diff --git a/spring-web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java b/spring-web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java index e2396e0683d..7ec048d4d10 100644 --- a/spring-web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java +++ b/spring-web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java @@ -42,9 +42,10 @@ public abstract class HttpStatusCodeException extends RestClientException { private final String responseCharset; + /** - * Construct a new instance of {@code HttpStatusCodeException} based on a {@link HttpStatus}. - * + * Construct a new instance of {@code HttpStatusCodeException} based on an + * {@link HttpStatus}. * @param statusCode the status code */ protected HttpStatusCodeException(HttpStatus statusCode) { @@ -52,8 +53,8 @@ public abstract class HttpStatusCodeException extends RestClientException { } /** - * Construct a new instance of {@code HttpStatusCodeException} based on a {@link HttpStatus} and status text. - * + * Construct a new instance of {@code HttpStatusCodeException} based on an + * {@link HttpStatus} and status text. * @param statusCode the status code * @param statusText the status text */ @@ -62,12 +63,11 @@ public abstract class HttpStatusCodeException extends RestClientException { } /** - * Construct a new instance of {@code HttpStatusCodeException} based on a {@link HttpStatus}, status text, and - * response body content. - * - * @param statusCode the status code - * @param statusText the status text - * @param responseBody the response body content, may be {@code null} + * Construct a new instance of {@code HttpStatusCodeException} based on an + * {@link HttpStatus}, status text, and response body 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 */ @@ -82,23 +82,23 @@ public abstract class HttpStatusCodeException extends RestClientException { this.responseCharset = responseCharset != null ? responseCharset.name() : DEFAULT_CHARSET; } + /** - * Returns the HTTP status code. + * Return the HTTP status code. */ public HttpStatus getStatusCode() { return this.statusCode; } /** - * Returns the HTTP status text. + * Return the HTTP status text. */ public String getStatusText() { return this.statusText; } /** - * Returns the response body as a byte array. - * + * Return the response body as a byte array. * @since 3.0.5 */ public byte[] getResponseBodyAsByteArray() { @@ -106,8 +106,7 @@ public abstract class HttpStatusCodeException extends RestClientException { } /** - * Returns the response body as a string. - * + * Return the response body as a string. * @since 3.0.5 */ public String getResponseBodyAsString() { @@ -119,4 +118,5 @@ public abstract class HttpStatusCodeException extends RestClientException { throw new InternalError(ex.getMessage()); } } + } diff --git a/spring-web/src/main/java/org/springframework/web/client/ResourceAccessException.java b/spring-web/src/main/java/org/springframework/web/client/ResourceAccessException.java index 8ef27842520..3472b82d599 100644 --- a/spring-web/src/main/java/org/springframework/web/client/ResourceAccessException.java +++ b/spring-web/src/main/java/org/springframework/web/client/ResourceAccessException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2012 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. @@ -26,6 +26,9 @@ import java.io.IOException; */ public class ResourceAccessException extends RestClientException { + private static final long serialVersionUID = -8513182514355844870L; + + /** * Construct a new {@code HttpIOException} with the given message. * @param msg the message diff --git a/spring-web/src/main/java/org/springframework/web/client/RestClientException.java b/spring-web/src/main/java/org/springframework/web/client/RestClientException.java index f772ada851d..a741e663a51 100644 --- a/spring-web/src/main/java/org/springframework/web/client/RestClientException.java +++ b/spring-web/src/main/java/org/springframework/web/client/RestClientException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2012 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. @@ -19,13 +19,17 @@ package org.springframework.web.client; import org.springframework.core.NestedRuntimeException; /** - * Base class for exceptions thrown by {@link RestTemplate} whenever it encounters client-side HTTP errors. + * Base class for exceptions thrown by {@link RestTemplate} whenever it encounters + * client-side HTTP errors. * * @author Arjen Poutsma * @since 3.0 */ public class RestClientException extends NestedRuntimeException { + private static final long serialVersionUID = -4084444984163796577L; + + /** * Construct a new instance of {@code HttpClientException} with the given message. * @param msg the message @@ -35,7 +39,8 @@ public class RestClientException extends NestedRuntimeException { } /** - * Construct a new instance of {@code HttpClientException} with the given message and exception. + * Construct a new instance of {@code HttpClientException} with the given message and + * exception. * @param msg the message * @param ex the exception */ diff --git a/spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java b/spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java index 0c9866c19be..129a507543c 100644 --- a/spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -19,19 +19,23 @@ package org.springframework.web.client; import java.io.ByteArrayInputStream; import java.io.IOException; +import org.junit.Before; +import org.junit.Test; + import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.client.ClientHttpResponse; -import org.junit.Before; -import org.junit.Test; - import static org.easymock.EasyMock.*; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -/** @author Arjen Poutsma */ +import static org.junit.Assert.*; + +/** + * Unit tests for {@link DefaultResponseErrorHandler}. + * + * @author Arjen Poutsma + */ public class DefaultResponseErrorHandlerTests { private DefaultResponseErrorHandler handler;