Browse Source

Polishing

pull/1224/merge
Juergen Hoeller 9 years ago
parent
commit
9b57437b7a
  1. 9
      spring-web/src/main/java/org/springframework/http/client/AbstractAsyncClientHttpRequest.java
  2. 65
      spring-web/src/main/java/org/springframework/http/client/Netty4ClientHttpRequest.java
  3. 30
      spring-web/src/main/java/org/springframework/web/client/HttpClientErrorException.java
  4. 37
      spring-web/src/main/java/org/springframework/web/client/HttpServerErrorException.java

9
spring-web/src/main/java/org/springframework/http/client/AbstractAsyncClientHttpRequest.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2016 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.
@ -73,12 +73,11 @@ abstract class AbstractAsyncClientHttpRequest implements AsyncClientHttpRequest
protected abstract OutputStream getBodyInternal(HttpHeaders headers) throws IOException; protected abstract OutputStream getBodyInternal(HttpHeaders headers) throws IOException;
/** /**
* Abstract template method that writes the given headers and content to the HTTP * Abstract template method that writes the given headers and content to the HTTP request.
* request.
* @param headers the HTTP headers * @param headers the HTTP headers
* @return the response object for the executed request * @return the response object for the executed request
*/ */
protected abstract ListenableFuture<ClientHttpResponse> executeInternal( protected abstract ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers)
HttpHeaders headers) throws IOException; throws IOException;
} }

65
spring-web/src/main/java/org/springframework/http/client/Netty4ClientHttpRequest.java

@ -79,6 +79,24 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements
return this.uri; return this.uri;
} }
@Override
public ClientHttpResponse execute() throws IOException {
try {
return executeAsync().get();
}
catch (InterruptedException ex) {
throw new IOException(ex.getMessage(), ex);
}
catch (ExecutionException ex) {
if (ex.getCause() instanceof IOException) {
throw (IOException) ex.getCause();
}
else {
throw new IOException(ex.getMessage(), ex.getCause());
}
}
}
@Override @Override
protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException { protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException {
return this.body; return this.body;
@ -105,26 +123,24 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements
}; };
this.bootstrap.connect(this.uri.getHost(), getPort(this.uri)).addListener(connectionListener); this.bootstrap.connect(this.uri.getHost(), getPort(this.uri)).addListener(connectionListener);
return responseFuture; return responseFuture;
} }
@Override private FullHttpRequest createFullHttpRequest(HttpHeaders headers) {
public ClientHttpResponse execute() throws IOException { io.netty.handler.codec.http.HttpMethod nettyMethod =
try { io.netty.handler.codec.http.HttpMethod.valueOf(this.method.name());
return executeAsync().get();
} FullHttpRequest nettyRequest = new DefaultFullHttpRequest(
catch (InterruptedException ex) { HttpVersion.HTTP_1_1, nettyMethod, this.uri.toString(), this.body.buffer());
throw new IOException(ex.getMessage(), ex);
} nettyRequest.headers().set(HttpHeaders.HOST, this.uri.getHost());
catch (ExecutionException ex) { nettyRequest.headers().set(HttpHeaders.CONNECTION, "close");
if (ex.getCause() instanceof IOException) {
throw (IOException) ex.getCause(); for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
} nettyRequest.headers().add(entry.getKey(), entry.getValue());
else {
throw new IOException(ex.getMessage(), ex);
}
} }
return nettyRequest;
} }
private static int getPort(URI uri) { private static int getPort(URI uri) {
@ -140,23 +156,6 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements
return port; return port;
} }
private FullHttpRequest createFullHttpRequest(HttpHeaders headers) {
io.netty.handler.codec.http.HttpMethod nettyMethod =
io.netty.handler.codec.http.HttpMethod.valueOf(this.method.name());
FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
nettyMethod, this.uri.toString(), this.body.buffer());
nettyRequest.headers().set(HttpHeaders.HOST, this.uri.getHost());
nettyRequest.headers().set(HttpHeaders.CONNECTION, "close");
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
nettyRequest.headers().add(entry.getKey(), entry.getValue());
}
return nettyRequest;
}
/** /**
* A SimpleChannelInboundHandler to update the given SettableListenableFuture. * A SimpleChannelInboundHandler to update the given SettableListenableFuture.

30
spring-web/src/main/java/org/springframework/web/client/HttpClientErrorException.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2016 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.
@ -34,8 +34,8 @@ public class HttpClientErrorException extends HttpStatusCodeException {
/** /**
* Construct a new instance of {@code HttpClientErrorException} based on an * Construct a new instance of {@code HttpClientErrorException} based on
* {@link HttpStatus}. * an {@link HttpStatus}.
* @param statusCode the status code * @param statusCode the status code
*/ */
public HttpClientErrorException(HttpStatus statusCode) { public HttpClientErrorException(HttpStatus statusCode) {
@ -43,8 +43,8 @@ public class HttpClientErrorException extends HttpStatusCodeException {
} }
/** /**
* Construct a new instance of {@code HttpClientErrorException} based on an * Construct a new instance of {@code HttpClientErrorException} based on
* {@link HttpStatus} and status text. * an {@link HttpStatus} and status text.
* @param statusCode the status code * @param statusCode the status code
* @param statusText the status text * @param statusText the status text
*/ */
@ -53,30 +53,32 @@ public class HttpClientErrorException extends HttpStatusCodeException {
} }
/** /**
* Construct a new instance of {@code HttpClientErrorException} based on an * Construct a new instance of {@code HttpClientErrorException} based on
* {@link HttpStatus}, status text, and response body content. * an {@link HttpStatus}, status text, and response body content.
* @param statusCode the status code * @param statusCode the status code
* @param statusText the status text * @param statusText the status text
* @param responseBody the response body content, may be {@code null} * @param responseBody the response body content (may be {@code null})
* @param responseCharset the response body charset, may be {@code null} * @param responseCharset the response body charset (may be {@code null})
*/ */
public HttpClientErrorException(HttpStatus statusCode, String statusText, public HttpClientErrorException(HttpStatus statusCode, String statusText,
byte[] responseBody, Charset responseCharset) { byte[] responseBody, Charset responseCharset) {
super(statusCode, statusText, responseBody, responseCharset); super(statusCode, statusText, responseBody, responseCharset);
} }
/** /**
* Construct a new instance of {@code HttpClientErrorException} based on an * Construct a new instance of {@code HttpClientErrorException} based on
* {@link HttpStatus}, status text, and response body content. * an {@link HttpStatus}, status text, and response body content.
* @param statusCode the status code * @param statusCode the status code
* @param statusText the status text * @param statusText the status text
* @param responseHeaders the response headers, may be {@code null} * @param responseHeaders the response headers (may be {@code null})
* @param responseBody the response body content, may be {@code null} * @param responseBody the response body content (may be {@code null})
* @param responseCharset the response body charset, may be {@code null} * @param responseCharset the response body charset (may be {@code null})
* @since 3.1.2 * @since 3.1.2
*/ */
public HttpClientErrorException(HttpStatus statusCode, String statusText, public HttpClientErrorException(HttpStatus statusCode, String statusText,
HttpHeaders responseHeaders, byte[] responseBody, Charset responseCharset) { HttpHeaders responseHeaders, byte[] responseBody, Charset responseCharset) {
super(statusCode, statusText, responseHeaders, responseBody, responseCharset); super(statusCode, statusText, responseHeaders, responseBody, responseCharset);
} }

37
spring-web/src/main/java/org/springframework/web/client/HttpServerErrorException.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2016 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,8 +25,8 @@ import org.springframework.http.HttpStatus;
* Exception thrown when an HTTP 5xx is received. * Exception thrown when an HTTP 5xx is received.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @see DefaultResponseErrorHandler
* @since 3.0 * @since 3.0
* @see DefaultResponseErrorHandler
*/ */
public class HttpServerErrorException extends HttpStatusCodeException { public class HttpServerErrorException extends HttpStatusCodeException {
@ -34,8 +34,8 @@ public class HttpServerErrorException extends HttpStatusCodeException {
/** /**
* Construct a new instance of {@code HttpServerErrorException} based on an * Construct a new instance of {@code HttpServerErrorException} based on
* {@link HttpStatus}. * an {@link HttpStatus}.
* @param statusCode the status code * @param statusCode the status code
*/ */
public HttpServerErrorException(HttpStatus statusCode) { public HttpServerErrorException(HttpStatus statusCode) {
@ -43,8 +43,8 @@ public class HttpServerErrorException extends HttpStatusCodeException {
} }
/** /**
* Construct a new instance of {@code HttpServerErrorException} based on an * Construct a new instance of {@code HttpServerErrorException} based on
* {@link HttpStatus} and status text. * an {@link HttpStatus} and status text.
* @param statusCode the status code * @param statusCode the status code
* @param statusText the status text * @param statusText the status text
*/ */
@ -53,31 +53,34 @@ public class HttpServerErrorException extends HttpStatusCodeException {
} }
/** /**
* Construct a new instance of {@code HttpServerErrorException} based on an * Construct a new instance of {@code HttpServerErrorException} based on
* {@link HttpStatus}, status text, and response body content. * an {@link HttpStatus}, status text, and response body content.
* @param statusCode the status code * @param statusCode the status code
* @param statusText the status text * @param statusText the status text
* @param responseBody the response body content, may be {@code null} * @param responseBody the response body content (may be {@code null})
* @param responseCharset the response body charset, may be {@code null} * @param responseCharset the response body charset (may be {@code null})
* @since 3.0.5 * @since 3.0.5
*/ */
public HttpServerErrorException(HttpStatus statusCode, String statusText, public HttpServerErrorException(HttpStatus statusCode, String statusText,
byte[] responseBody, Charset responseCharset) { byte[] responseBody, Charset responseCharset) {
super(statusCode, statusText, responseBody, responseCharset); super(statusCode, statusText, responseBody, responseCharset);
} }
/** /**
* Construct a new instance of {@code HttpServerErrorException} based on a * Construct a new instance of {@code HttpServerErrorException} based on
* {@link HttpStatus}, status text, and response body content. * an {@link HttpStatus}, status text, and response body content.
* @param statusCode the status code * @param statusCode the status code
* @param statusText the status text * @param statusText the status text
* @param responseHeaders the response headers, may be {@code null} * @param responseHeaders the response headers (may be {@code null})
* @param responseBody the response body content, may be {@code null} * @param responseBody the response body content (may be {@code null})
* @param responseCharset the response body charset, may be {@code null} * @param responseCharset the response body charset (may be {@code null})
* @since 3.1.2 * @since 3.1.2
*/ */
public HttpServerErrorException(HttpStatus statusCode, String statusText, public HttpServerErrorException(HttpStatus statusCode, String statusText,
HttpHeaders responseHeaders, byte[] responseBody, Charset responseCharset) { HttpHeaders responseHeaders, byte[] responseBody, Charset responseCharset) {
super(statusCode, statusText, responseHeaders, responseBody, responseCharset); super(statusCode, statusText, responseHeaders, responseBody, responseCharset);
} }
} }

Loading…
Cancel
Save