Skip to content

HttpClientErrorException should provide access to response headers #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,6 +16,7 @@

package org.springframework.http;

import java.io.Serializable;
import java.net.URI;
import java.nio.charset.Charset;
import java.text.ParseException;
Expand Down Expand Up @@ -54,7 +55,9 @@
* @author Arjen Poutsma
* @since 3.0
*/
public class HttpHeaders implements MultiValueMap<String, String> {
public class HttpHeaders implements MultiValueMap<String, String>, Serializable {

private static final long serialVersionUID = -8578554704772377436L;

private static final String ACCEPT = "Accept";

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,6 +20,7 @@
import java.io.InputStream;
import java.nio.charset.Charset;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
Expand Down Expand Up @@ -68,14 +69,15 @@ protected boolean hasError(HttpStatus statusCode) {
*/
public void handleError(ClientHttpResponse response) throws IOException {
HttpStatus statusCode = response.getStatusCode();
MediaType contentType = response.getHeaders().getContentType();
HttpHeaders headers = response.getHeaders();
MediaType contentType = headers.getContentType();
Charset charset = contentType != null ? contentType.getCharSet() : null;
byte[] body = getResponseBody(response);
switch (statusCode.series()) {
case CLIENT_ERROR:
throw new HttpClientErrorException(statusCode, response.getStatusText(), body, charset);
throw new HttpClientErrorException(statusCode, response.getStatusText(), headers, body, charset);
case SERVER_ERROR:
throw new HttpServerErrorException(statusCode, response.getStatusText(), body, charset);
throw new HttpServerErrorException(statusCode, response.getStatusText(), headers, body, charset);
default:
throw new RestClientException("Unknown status code [" + statusCode + "]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.nio.charset.Charset;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;

/**
Expand All @@ -29,7 +30,7 @@
*/
public class HttpClientErrorException extends HttpStatusCodeException {

private static final long serialVersionUID = 6777393766937023392L;
private static final long serialVersionUID = 5177019431887513952L;

/**
* Construct a new instance of {@code HttpClientErrorException} based on a {@link HttpStatus}.
Expand Down Expand Up @@ -63,4 +64,22 @@ public HttpClientErrorException(HttpStatus statusCode,
Charset responseCharset) {
super(statusCode, statusText, responseBody, responseCharset);
}

/**
* Construct a new instance of {@code HttpClientErrorException} based on a {@link HttpStatus}, status text, and
* response body content.
*
* @param statusCode the status code
* @param statusText the status text
* @param responseHeaders the response headers, may be {@code null}
* @param responseBody the response body content, may be {@code null}
* @param responseCharset the response body charset, may be {@code null}
*/
public HttpClientErrorException(HttpStatus statusCode,
String statusText,
HttpHeaders responseHeaders,
byte[] responseBody,
Charset responseCharset) {
super(statusCode, statusText, responseHeaders, responseBody, responseCharset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.nio.charset.Charset;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;

/**
Expand All @@ -29,7 +30,7 @@
*/
public class HttpServerErrorException extends HttpStatusCodeException {

private static final long serialVersionUID = -2565832100451369997L;
private static final long serialVersionUID = -2915754006618138282L;

/**
* Construct a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus}.
Expand Down Expand Up @@ -66,4 +67,23 @@ public HttpServerErrorException(HttpStatus statusCode,
Charset responseCharset) {
super(statusCode, statusText, responseBody, responseCharset);
}

/**
* Construct a new instance of {@code HttpServerErrorException} based on a {@link HttpStatus}, status text, and
* response body content.
*
* @param statusCode the status code
* @param statusText the status text
* @param responseHeaders the response headers, may be {@code null}
* @param responseBody the response body content, may be {@code null}
* @param responseCharset the response body charset, may be {@code null}
* @since 3.2.0
*/
public HttpServerErrorException(HttpStatus statusCode,
String statusText,
HttpHeaders responseHeaders,
byte[] responseBody,
Charset responseCharset) {
super(statusCode, statusText, responseHeaders, responseBody, responseCharset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;

/**
Expand All @@ -30,7 +31,7 @@
*/
public abstract class HttpStatusCodeException extends RestClientException {

private static final long serialVersionUID = 1549626836533638803L;
private static final long serialVersionUID = -5807494703720513267L;

private static final String DEFAULT_CHARSET = "ISO-8859-1";

Expand All @@ -40,6 +41,8 @@ public abstract class HttpStatusCodeException extends RestClientException {

private final byte[] responseBody;

private final HttpHeaders responseHeaders;

private final String responseCharset;

/**
Expand All @@ -48,7 +51,7 @@ public abstract class HttpStatusCodeException extends RestClientException {
* @param statusCode the status code
*/
protected HttpStatusCodeException(HttpStatus statusCode) {
this(statusCode, statusCode.name(), null, null);
this(statusCode, statusCode.name(), null, null, null);
}

/**
Expand All @@ -58,7 +61,7 @@ protected HttpStatusCodeException(HttpStatus statusCode) {
* @param statusText the status text
*/
protected HttpStatusCodeException(HttpStatus statusCode, String statusText) {
this(statusCode, statusText, null, null);
this(statusCode, statusText, null, null, null);
}

/**
Expand All @@ -75,9 +78,29 @@ protected HttpStatusCodeException(HttpStatus statusCode,
String statusText,
byte[] responseBody,
Charset responseCharset) {
this(statusCode, statusText, null, responseBody, responseCharset);
}

/**
* Construct a new instance of {@code HttpStatusCodeException} based on a {@link HttpStatus}, status text, and
* response body content.
*
* @param statusCode the status code
* @param statusText the status text
* @param responseHeaders the response headers, may be {@code null}
* @param responseBody the response body content, may be {@code null}
* @param responseCharset the response body charset, may be {@code null}
* @since 3.2.0
*/
protected HttpStatusCodeException(HttpStatus statusCode,
String statusText,
HttpHeaders responseHeaders,
byte[] responseBody,
Charset responseCharset) {
super(statusCode.value() + " " + statusText);
this.statusCode = statusCode;
this.statusText = statusText;
this.responseHeaders = responseHeaders;
this.responseBody = responseBody != null ? responseBody : new byte[0];
this.responseCharset = responseCharset != null ? responseCharset.name() : DEFAULT_CHARSET;
}
Expand All @@ -96,6 +119,15 @@ public String getStatusText() {
return this.statusText;
}

/**
* Returns the HTTP response headers.
*
* @since 3.2.0
*/
public HttpHeaders getResponseHeaders() {
return this.responseHeaders;
}

/**
* Returns the response body as a byte array.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -29,7 +29,9 @@

import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/** @author Arjen Poutsma */
public class DefaultResponseErrorHandlerTests {
Expand Down Expand Up @@ -64,7 +66,7 @@ public void hasErrorFalse() throws Exception {
verify(response);
}

@Test(expected = HttpClientErrorException.class)
@Test
public void handleError() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
Expand All @@ -76,7 +78,13 @@ public void handleError() throws Exception {

replay(response);

handler.handleError(response);
try {
handler.handleError(response);
fail("expected HttpClientErrorException");
}
catch (HttpClientErrorException e) {
assertSame(headers, e.getResponseHeaders());
}

verify(response);
}
Expand Down