From 8980ce712da209aa5cf742dd4bd58f09e5d46870 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Mon, 23 Jan 2012 14:26:11 +0100 Subject: [PATCH] SPR-8986 RestTemplate throws IllegalArgumentException when HTTP status is not in the HttpStatus enum - Added getRawStatusCode --- .../client/AbstractClientHttpResponse.java | 35 +++++++++++++++++++ .../BufferingClientHttpResponseWrapper.java | 6 +++- .../http/client/ClientHttpResponse.java | 9 ++++- .../client/CommonsClientHttpResponse.java | 9 +++-- .../HttpComponentsClientHttpResponse.java | 11 +++--- .../http/client/SimpleClientHttpResponse.java | 9 +++-- ...rceptingClientHttpRequestFactoryTests.java | 8 +++-- 7 files changed, 67 insertions(+), 20 deletions(-) create mode 100644 org.springframework.web/src/main/java/org/springframework/http/client/AbstractClientHttpResponse.java diff --git a/org.springframework.web/src/main/java/org/springframework/http/client/AbstractClientHttpResponse.java b/org.springframework.web/src/main/java/org/springframework/http/client/AbstractClientHttpResponse.java new file mode 100644 index 0000000000..cd6166575b --- /dev/null +++ b/org.springframework.web/src/main/java/org/springframework/http/client/AbstractClientHttpResponse.java @@ -0,0 +1,35 @@ +/* + * 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()); + } + +} diff --git a/org.springframework.web/src/main/java/org/springframework/http/client/BufferingClientHttpResponseWrapper.java b/org.springframework.web/src/main/java/org/springframework/http/client/BufferingClientHttpResponseWrapper.java index da9f39a52d..f280790fec 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/client/BufferingClientHttpResponseWrapper.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/BufferingClientHttpResponseWrapper.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. @@ -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(); } diff --git a/org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpResponse.java b/org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpResponse.java index aba6fb0699..38aea117c7 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpResponse.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpResponse.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. @@ -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 diff --git a/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpResponse.java b/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpResponse.java index d3b93faee2..7cc8cec6e9 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpResponse.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpResponse.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. @@ -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() { diff --git a/org.springframework.web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpResponse.java b/org.springframework.web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpResponse.java index 4b5e9a23bb..d4033e4216 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpResponse.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpResponse.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. @@ -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 { diff --git a/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java b/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java index 5a8e3f04b3..1fc33f0b69 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java +++ b/org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.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. @@ -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 { diff --git a/org.springframework.web/src/test/java/org/springframework/http/client/InterceptingClientHttpRequestFactoryTests.java b/org.springframework.web/src/test/java/org/springframework/http/client/InterceptingClientHttpRequestFactoryTests.java index b663a95570..70f5e257d2 100644 --- a/org.springframework.web/src/test/java/org/springframework/http/client/InterceptingClientHttpRequestFactoryTests.java +++ b/org.springframework.web/src/test/java/org/springframework/http/client/InterceptingClientHttpRequestFactoryTests.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. @@ -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() { -- GitLab