DefaultResponseErrorHandler.java 3.7 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
2
 * Copyright 2002-2012 the original author or authors.
A
Arjen Poutsma 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
package org.springframework.web.client;
A
Arjen Poutsma 已提交
18 19

import java.io.IOException;
20
import java.io.InputStream;
21
import java.nio.charset.Charset;
A
Arjen Poutsma 已提交
22

23
import org.springframework.http.HttpStatus;
24
import org.springframework.http.MediaType;
25
import org.springframework.http.client.ClientHttpResponse;
26
import org.springframework.util.FileCopyUtils;
A
Arjen Poutsma 已提交
27 28

/**
29
 * Default implementation of the {@link ResponseErrorHandler} interface.
A
Arjen Poutsma 已提交
30
 *
31 32 33 34 35
 * <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.
A
Arjen Poutsma 已提交
36 37 38
 *
 * @author Arjen Poutsma
 * @since 3.0
39
 * @see RestTemplate#setErrorHandler
A
Arjen Poutsma 已提交
40
 */
41
public class DefaultResponseErrorHandler implements ResponseErrorHandler {
A
Arjen Poutsma 已提交
42 43 44 45 46 47 48 49 50 51

	/**
	 * Delegates to {@link #hasError(HttpStatus)} with the response status code.
	 */
	public boolean hasError(ClientHttpResponse response) throws IOException {
		return hasError(response.getStatusCode());
	}

	/**
	 * Template method called from {@link #hasError(ClientHttpResponse)}.
A
Javadoc  
Arjen Poutsma 已提交
52 53
	 * <p>The default implementation checks if the given status code is
	 * {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR CLIENT_ERROR}
J
Juergen Hoeller 已提交
54 55
	 * or {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR SERVER_ERROR}.
	 * Can be overridden in subclasses.
A
Arjen Poutsma 已提交
56 57 58 59
	 * @param statusCode the HTTP status code
	 * @return <code>true</code> if the response has an error; <code>false</code> otherwise
	 */
	protected boolean hasError(HttpStatus statusCode) {
60 61
		return (statusCode.series() == HttpStatus.Series.CLIENT_ERROR ||
				statusCode.series() == HttpStatus.Series.SERVER_ERROR);
A
Arjen Poutsma 已提交
62 63
	}

A
Javadoc  
Arjen Poutsma 已提交
64
	/**
J
Juergen Hoeller 已提交
65 66 67 68
	 * This default implementation throws a {@link HttpClientErrorException} if the response status code
	 * is {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}, a {@link HttpServerErrorException}
	 * if it is {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR},
	 * and a {@link RestClientException} in other cases.
A
Javadoc  
Arjen Poutsma 已提交
69
	 */
A
Arjen Poutsma 已提交
70 71
	public void handleError(ClientHttpResponse response) throws IOException {
		HttpStatus statusCode = response.getStatusCode();
72 73
		MediaType contentType = response.getHeaders().getContentType();
		Charset charset = contentType != null ? contentType.getCharSet() : null;
74
		byte[] body = getResponseBody(response);
A
Arjen Poutsma 已提交
75 76
		switch (statusCode.series()) {
			case CLIENT_ERROR:
77
				throw new HttpClientErrorException(statusCode, response.getStatusText(), body, charset);
A
Arjen Poutsma 已提交
78
			case SERVER_ERROR:
79
				throw new HttpServerErrorException(statusCode, response.getStatusText(), body, charset);
A
Arjen Poutsma 已提交
80
			default:
81
				throw new RestClientException("Unknown status code [" + statusCode + "]");
A
Arjen Poutsma 已提交
82 83
		}
	}
84

85 86
	private byte[] getResponseBody(ClientHttpResponse response) {
		try {
87 88 89 90
			InputStream responseBody = response.getBody();
			if (responseBody != null) {
				return FileCopyUtils.copyToByteArray(responseBody);
			}
91
		}
J
Juergen Hoeller 已提交
92
		catch (IOException ex) {
93
			// ignore
94
		}
95
		return new byte[0];
96 97
	}

A
Arjen Poutsma 已提交
98
}