提交 8980ce71 编写于 作者: A Arjen Poutsma

SPR-8986 RestTemplate throws IllegalArgumentException when HTTP status is not...

SPR-8986 RestTemplate throws IllegalArgumentException when HTTP status is not in the HttpStatus enum

- Added getRawStatusCode
上级 ff9ad7ad
/*
* 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.
* 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.
*/
package org.springframework.http.client;
import java.io.IOException;
import org.springframework.http.HttpStatus;
/**
* Abstract base for {@link ClientHttpResponse}.
*
* @author Arjen Poutsma
* @since 3.1.1
*/
public abstract class AbstractClientHttpResponse implements ClientHttpResponse {
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.valueOf(getRawStatusCode());
}
}
/*
* 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.
......@@ -47,6 +47,10 @@ final class BufferingClientHttpResponseWrapper implements ClientHttpResponse {
return this.response.getStatusCode();
}
public int getRawStatusCode() throws IOException {
return this.response.getRawStatusCode();
}
public String getStatusText() throws IOException {
return this.response.getStatusText();
}
......
/*
* 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.
......@@ -39,6 +39,13 @@ public interface ClientHttpResponse extends HttpInputMessage {
*/
HttpStatus getStatusCode() throws IOException;
/**
* Return the HTTP status code of the response as integer
* @return the HTTP status as an integer
* @throws IOException in case of I/O errors
*/
int getRawStatusCode() throws IOException;
/**
* Return the HTTP status text of the response.
* @return the HTTP status text
......
/*
* 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.
......@@ -23,7 +23,6 @@ import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethod;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
/**
* {@link org.springframework.http.client.ClientHttpResponse} implementation that uses
......@@ -37,7 +36,7 @@ import org.springframework.http.HttpStatus;
* @deprecated In favor of {@link HttpComponentsClientHttpResponse}
*/
@Deprecated
final class CommonsClientHttpResponse implements ClientHttpResponse {
final class CommonsClientHttpResponse extends AbstractClientHttpResponse {
private final HttpMethod httpMethod;
......@@ -49,8 +48,8 @@ final class CommonsClientHttpResponse implements ClientHttpResponse {
}
public HttpStatus getStatusCode() {
return HttpStatus.valueOf(this.httpMethod.getStatusCode());
public int getRawStatusCode() {
return this.httpMethod.getStatusCode();
}
public String getStatusText() {
......
/*
* 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.
......@@ -25,7 +25,6 @@ import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
/**
* {@link org.springframework.http.client.ClientHttpResponse} implementation that uses
......@@ -38,20 +37,20 @@ import org.springframework.http.HttpStatus;
* @since 3.1
* @see HttpComponentsClientHttpRequest#execute()
*/
final class HttpComponentsClientHttpResponse implements ClientHttpResponse {
final class HttpComponentsClientHttpResponse extends AbstractClientHttpResponse {
private final HttpResponse httpResponse;
private HttpHeaders headers;
public HttpComponentsClientHttpResponse(HttpResponse httpResponse) {
HttpComponentsClientHttpResponse(HttpResponse httpResponse) {
this.httpResponse = httpResponse;
}
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.valueOf(this.httpResponse.getStatusLine().getStatusCode());
public int getRawStatusCode() throws IOException {
return this.httpResponse.getStatusLine().getStatusCode();
}
public String getStatusText() throws IOException {
......
/*
* 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.
......@@ -21,7 +21,6 @@ import java.io.InputStream;
import java.net.HttpURLConnection;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
/**
......@@ -32,7 +31,7 @@ import org.springframework.util.StringUtils;
* @author Arjen Poutsma
* @since 3.0
*/
final class SimpleClientHttpResponse implements ClientHttpResponse {
final class SimpleClientHttpResponse extends AbstractClientHttpResponse {
private final HttpURLConnection connection;
......@@ -44,8 +43,8 @@ final class SimpleClientHttpResponse implements ClientHttpResponse {
}
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.valueOf(this.connection.getResponseCode());
public int getRawStatusCode() throws IOException {
return this.connection.getResponseCode();
}
public String getStatusText() throws IOException {
......
/*
* 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.
......@@ -291,6 +291,10 @@ public class InterceptingClientHttpRequestFactoryTests {
return statusCode;
}
public int getRawStatusCode() throws IOException {
return statusCode.value();
}
public String getStatusText() throws IOException {
return statusText;
}
......@@ -300,7 +304,7 @@ public class InterceptingClientHttpRequestFactoryTests {
}
public InputStream getBody() throws IOException {
return null; //To change body of implemented methods use File | Settings | File Templates.
return null;
}
public void close() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册