提交 bca2357b 编写于 作者: C Chris Beams

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
上级 b8ff6c1f
/*
* 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;
......
/*
* 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.
*
* <p>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.
* <p>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];
}
}
......@@ -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);
}
}
......@@ -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);
}
}
......@@ -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());
}
}
}
/*
* 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
......
/*
* 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
*/
......
/*
* 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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册