Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

SPR-7591 - HttpStatusCodeException should contain response body

  • Loading branch information...
commit 70cb81b4b5d9633d6eeb8967b7289fb9394734cd 1 parent 6e516b7
authored September 24, 2010
13  org.springframework.web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2  
- * Copyright 2002-2009 the original author or authors.
  2
+ * Copyright 2002-2010 the original author or authors.
3 3
  *
4 4
  * Licensed under the Apache License, Version 2.0 (the "License");
5 5
  * you may not use this file except in compliance with the License.
@@ -17,9 +17,12 @@
17 17
 package org.springframework.web.client;
18 18
 
19 19
 import java.io.IOException;
  20
+import java.nio.charset.Charset;
20 21
 
21 22
 import org.springframework.http.HttpStatus;
  23
+import org.springframework.http.MediaType;
22 24
 import org.springframework.http.client.ClientHttpResponse;
  25
+import org.springframework.util.FileCopyUtils;
23 26
 
24 27
 /**
25 28
  * Default implementation of the {@link ResponseErrorHandler} interface.
@@ -64,14 +67,18 @@ protected boolean hasError(HttpStatus statusCode) {
64 67
 	 */
65 68
 	public void handleError(ClientHttpResponse response) throws IOException {
66 69
 		HttpStatus statusCode = response.getStatusCode();
  70
+		MediaType contentType = response.getHeaders().getContentType();
  71
+		Charset charset = contentType != null ? contentType.getCharSet() : null;
  72
+		byte[] body = FileCopyUtils.copyToByteArray(response.getBody());
67 73
 		switch (statusCode.series()) {
68 74
 			case CLIENT_ERROR:
69  
-				throw new HttpClientErrorException(statusCode, response.getStatusText());
  75
+				throw new HttpClientErrorException(statusCode, response.getStatusText(), body, charset);
70 76
 			case SERVER_ERROR:
71  
-				throw new HttpServerErrorException(statusCode, response.getStatusText());
  77
+				throw new HttpServerErrorException(statusCode, response.getStatusText(), body, charset);
72 78
 			default:
73 79
 				throw new RestClientException("Unknown status code [" + statusCode + "]");
74 80
 		}
75 81
 	}
  82
+
76 83
 }
77 84
 
17  org.springframework.web/src/main/java/org/springframework/web/client/HttpClientErrorException.java
@@ -16,6 +16,8 @@
16 16
 
17 17
 package org.springframework.web.client;
18 18
 
  19
+import java.nio.charset.Charset;
  20
+
19 21
 import org.springframework.http.HttpStatus;
20 22
 
21 23
 /**
@@ -44,4 +46,19 @@ public HttpClientErrorException(HttpStatus statusCode, String statusText) {
44 46
 		super(statusCode, statusText);
45 47
 	}
46 48
 
  49
+	/**
  50
+	 * Construct a new instance of {@code HttpClientErrorException} based on a {@link HttpStatus}, status text, and
  51
+	 * response body content.
  52
+	 *
  53
+	 * @param statusCode	  the status code
  54
+	 * @param statusText	  the status text
  55
+	 * @param responseBody	the response body content, may be {@code null}
  56
+	 * @param responseCharset the response body charset, may be {@code null}
  57
+	 */
  58
+	public HttpClientErrorException(HttpStatus statusCode,
  59
+			String statusText,
  60
+			byte[] responseBody,
  61
+			Charset responseCharset) {
  62
+		super(statusCode, statusText, responseBody, responseCharset);
  63
+	}
47 64
 }
22  org.springframework.web/src/main/java/org/springframework/web/client/HttpServerErrorException.java
@@ -16,19 +16,22 @@
16 16
 
17 17
 package org.springframework.web.client;
18 18
 
  19
+import java.nio.charset.Charset;
  20
+
19 21
 import org.springframework.http.HttpStatus;
20 22
 
21 23
 /**
22 24
  * Exception thrown when an HTTP 5xx is received.
23 25
  *
24 26
  * @author Arjen Poutsma
25  
- * @since 3.0
26 27
  * @see DefaultResponseErrorHandler
  28
+ * @since 3.0
27 29
  */
28 30
 public class HttpServerErrorException extends HttpStatusCodeException {
29 31
 
30 32
 	/**
31 33
 	 * Construct a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus}.
  34
+	 *
32 35
 	 * @param statusCode the status code
33 36
 	 */
34 37
 	public HttpServerErrorException(HttpStatus statusCode) {
@@ -37,6 +40,7 @@ public HttpServerErrorException(HttpStatus statusCode) {
37 40
 
38 41
 	/**
39 42
 	 * Construct a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus} and status text.
  43
+	 *
40 44
 	 * @param statusCode the status code
41 45
 	 * @param statusText the status text
42 46
 	 */
@@ -44,4 +48,20 @@ public HttpServerErrorException(HttpStatus statusCode, String statusText) {
44 48
 		super(statusCode, statusText);
45 49
 	}
46 50
 
  51
+	/**
  52
+	 * Construct a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus}, status text, and
  53
+	 * response body content.
  54
+	 *
  55
+	 * @param statusCode	  the status code
  56
+	 * @param statusText	  the status text
  57
+	 * @param responseBody	the response body content, may be {@code null}
  58
+	 * @param responseCharset the response body charset, may be {@code null}
  59
+	 * @since 3.0.5
  60
+	 */
  61
+	public HttpServerErrorException(HttpStatus statusCode,
  62
+			String statusText,
  63
+			byte[] responseBody,
  64
+			Charset responseCharset) {
  65
+		super(statusCode, statusText, responseBody, responseCharset);
  66
+	}
47 67
 }
59  org.springframework.web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2  
- * Copyright 2002-2009 the original author or authors.
  2
+ * Copyright 2002-2010 the original author or authors.
3 3
  *
4 4
  * Licensed under the Apache License, Version 2.0 (the "License");
5 5
  * you may not use this file except in compliance with the License.
@@ -16,6 +16,9 @@
16 16
 
17 17
 package org.springframework.web.client;
18 18
 
  19
+import java.io.UnsupportedEncodingException;
  20
+import java.nio.charset.Charset;
  21
+
19 22
 import org.springframework.http.HttpStatus;
20 23
 
21 24
 /**
@@ -26,33 +29,56 @@
26 29
  */
27 30
 public abstract class HttpStatusCodeException extends RestClientException {
28 31
 
  32
+	private static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
  33
+
29 34
 	private final HttpStatus statusCode;
30 35
 
31 36
 	private final String statusText;
32 37
 
  38
+	private final byte[] responseBody;
  39
+
  40
+	private final Charset responseCharset;
33 41
 
34 42
 	/**
35 43
 	 * Construct a new instance of {@code HttpStatusCodeException} based on a {@link HttpStatus}.
  44
+	 *
36 45
 	 * @param statusCode the status code
37 46
 	 */
38 47
 	protected HttpStatusCodeException(HttpStatus statusCode) {
39  
-		super(statusCode.toString());
40  
-		this.statusCode = statusCode;
41  
-		this.statusText = statusCode.name();
  48
+		this(statusCode, statusCode.name(), null, null);
42 49
 	}
43 50
 
44 51
 	/**
45 52
 	 * Construct a new instance of {@code HttpStatusCodeException} based on a {@link HttpStatus} and status text.
  53
+	 *
46 54
 	 * @param statusCode the status code
47 55
 	 * @param statusText the status text
48 56
 	 */
49 57
 	protected HttpStatusCodeException(HttpStatus statusCode, String statusText) {
  58
+		this(statusCode, statusText, null, null);
  59
+	}
  60
+
  61
+	/**
  62
+	 * Construct a new instance of {@code HttpStatusCodeException} based on a {@link HttpStatus}, status text, and
  63
+	 * response body content.
  64
+	 *
  65
+	 * @param statusCode	  the status code
  66
+	 * @param statusText	  the status text
  67
+	 * @param responseBody	the response body content, may be {@code null}
  68
+	 * @param responseCharset the response body charset, may be {@code null}
  69
+	 * @since 3.0.5
  70
+	 */
  71
+	protected HttpStatusCodeException(HttpStatus statusCode,
  72
+			String statusText,
  73
+			byte[] responseBody,
  74
+			Charset responseCharset) {
50 75
 		super(statusCode.value() + " " + statusText);
51 76
 		this.statusCode = statusCode;
52 77
 		this.statusText = statusText;
  78
+		this.responseBody = responseBody != null ? responseBody : new byte[0];
  79
+		this.responseCharset = responseCharset != null ? responseCharset : DEFAULT_CHARSET;
53 80
 	}
54 81
 
55  
-
56 82
 	/**
57 83
 	 * Returns the HTTP status code.
58 84
 	 */
@@ -67,4 +93,27 @@ public String getStatusText() {
67 93
 		return this.statusText;
68 94
 	}
69 95
 
  96
+	/**
  97
+	 * Returns the response body as a byte array.
  98
+	 *
  99
+	 * @since 3.0.5
  100
+	 */
  101
+	public byte[] getResponseBodyAsByteArray() {
  102
+		return responseBody;
  103
+	}
  104
+
  105
+	/**
  106
+	 * Returns the response body as a string.
  107
+	 *
  108
+	 * @since 3.0.5
  109
+	 */
  110
+	public String getResponseBodyAsString() {
  111
+		try {
  112
+			return new String(responseBody, responseCharset.name());
  113
+		}
  114
+		catch (UnsupportedEncodingException ex) {
  115
+			// should not occur
  116
+			throw new InternalError(ex.getMessage());
  117
+		}
  118
+	}
70 119
 }
24  org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java
@@ -145,14 +145,30 @@ public void postForObject() throws URISyntaxException {
145 145
 		assertEquals("Invalid content", helloWorld, s);
146 146
 	}
147 147
 
148  
-	@Test(expected = HttpClientErrorException.class)
  148
+	@Test
149 149
 	public void notFound() {
150  
-		template.execute(URI + "/errors/notfound", HttpMethod.GET, null, null);
  150
+		try {
  151
+			template.execute(URI + "/errors/notfound", HttpMethod.GET, null, null);
  152
+			fail("HttpClientErrorException expected");
  153
+		}
  154
+		catch (HttpClientErrorException ex) {
  155
+			assertEquals(HttpStatus.NOT_FOUND, ex.getStatusCode());
  156
+			assertNotNull(ex.getStatusText());
  157
+			assertNotNull(ex.getResponseBodyAsString());
  158
+		}
151 159
 	}
152 160
 
153  
-	@Test(expected = HttpServerErrorException.class)
  161
+	@Test
154 162
 	public void serverError() {
155  
-		template.execute(URI + "/errors/server", HttpMethod.GET, null, null);
  163
+		try {
  164
+			template.execute(URI + "/errors/server", HttpMethod.GET, null, null);
  165
+			fail("HttpServerErrorException expected");
  166
+		}
  167
+		catch (HttpServerErrorException ex) {
  168
+			assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, ex.getStatusCode());
  169
+			assertNotNull(ex.getStatusText());
  170
+			assertNotNull(ex.getResponseBodyAsString());
  171
+		}
156 172
 	}
157 173
 
158 174
 	@Test

0 notes on commit 70cb81b

Please sign in to comment.
Something went wrong with that request. Please try again.