Browse Source

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

- Added getRawStatusCode
pull/23/head
Arjen Poutsma 14 years ago
parent
commit
8980ce712d
  1. 35
      org.springframework.web/src/main/java/org/springframework/http/client/AbstractClientHttpResponse.java
  2. 6
      org.springframework.web/src/main/java/org/springframework/http/client/BufferingClientHttpResponseWrapper.java
  3. 9
      org.springframework.web/src/main/java/org/springframework/http/client/ClientHttpResponse.java
  4. 9
      org.springframework.web/src/main/java/org/springframework/http/client/CommonsClientHttpResponse.java
  5. 11
      org.springframework.web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpResponse.java
  6. 9
      org.springframework.web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java
  7. 8
      org.springframework.web/src/test/java/org/springframework/http/client/InterceptingClientHttpRequestFactoryTests.java

35
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());
}
}

6
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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(); return this.response.getStatusCode();
} }
public int getRawStatusCode() throws IOException {
return this.response.getRawStatusCode();
}
public String getStatusText() throws IOException { public String getStatusText() throws IOException {
return this.response.getStatusText(); return this.response.getStatusText();
} }

9
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; 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 of the response.
* @return the HTTP status text * @return the HTTP status text

9
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.apache.commons.httpclient.HttpMethod;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
/** /**
* {@link org.springframework.http.client.ClientHttpResponse} implementation that uses * {@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 In favor of {@link HttpComponentsClientHttpResponse}
*/ */
@Deprecated @Deprecated
final class CommonsClientHttpResponse implements ClientHttpResponse { final class CommonsClientHttpResponse extends AbstractClientHttpResponse {
private final HttpMethod httpMethod; private final HttpMethod httpMethod;
@ -49,8 +48,8 @@ final class CommonsClientHttpResponse implements ClientHttpResponse {
} }
public HttpStatus getStatusCode() { public int getRawStatusCode() {
return HttpStatus.valueOf(this.httpMethod.getStatusCode()); return this.httpMethod.getStatusCode();
} }
public String getStatusText() { public String getStatusText() {

11
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.apache.http.util.EntityUtils;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
/** /**
* {@link org.springframework.http.client.ClientHttpResponse} implementation that uses * {@link org.springframework.http.client.ClientHttpResponse} implementation that uses
@ -38,20 +37,20 @@ import org.springframework.http.HttpStatus;
* @since 3.1 * @since 3.1
* @see HttpComponentsClientHttpRequest#execute() * @see HttpComponentsClientHttpRequest#execute()
*/ */
final class HttpComponentsClientHttpResponse implements ClientHttpResponse { final class HttpComponentsClientHttpResponse extends AbstractClientHttpResponse {
private final HttpResponse httpResponse; private final HttpResponse httpResponse;
private HttpHeaders headers; private HttpHeaders headers;
public HttpComponentsClientHttpResponse(HttpResponse httpResponse) { HttpComponentsClientHttpResponse(HttpResponse httpResponse) {
this.httpResponse = httpResponse; this.httpResponse = httpResponse;
} }
public HttpStatus getStatusCode() throws IOException { public int getRawStatusCode() throws IOException {
return HttpStatus.valueOf(this.httpResponse.getStatusLine().getStatusCode()); return this.httpResponse.getStatusLine().getStatusCode();
} }
public String getStatusText() throws IOException { public String getStatusText() throws IOException {

9
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 java.net.HttpURLConnection;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
@ -32,7 +31,7 @@ import org.springframework.util.StringUtils;
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 3.0 * @since 3.0
*/ */
final class SimpleClientHttpResponse implements ClientHttpResponse { final class SimpleClientHttpResponse extends AbstractClientHttpResponse {
private final HttpURLConnection connection; private final HttpURLConnection connection;
@ -44,8 +43,8 @@ final class SimpleClientHttpResponse implements ClientHttpResponse {
} }
public HttpStatus getStatusCode() throws IOException { public int getRawStatusCode() throws IOException {
return HttpStatus.valueOf(this.connection.getResponseCode()); return this.connection.getResponseCode();
} }
public String getStatusText() throws IOException { public String getStatusText() throws IOException {

8
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -291,6 +291,10 @@ public class InterceptingClientHttpRequestFactoryTests {
return statusCode; return statusCode;
} }
public int getRawStatusCode() throws IOException {
return statusCode.value();
}
public String getStatusText() throws IOException { public String getStatusText() throws IOException {
return statusText; return statusText;
} }
@ -300,7 +304,7 @@ public class InterceptingClientHttpRequestFactoryTests {
} }
public InputStream getBody() throws IOException { public InputStream getBody() throws IOException {
return null; //To change body of implemented methods use File | Settings | File Templates. return null;
} }
public void close() { public void close() {

Loading…
Cancel
Save