Browse Source

Polishing

pull/1224/merge
Juergen Hoeller 8 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 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -73,12 +73,11 @@ abstract class AbstractAsyncClientHttpRequest implements AsyncClientHttpRequest @@ -73,12 +73,11 @@ abstract class AbstractAsyncClientHttpRequest implements AsyncClientHttpRequest
protected abstract OutputStream getBodyInternal(HttpHeaders headers) throws IOException;
/**
* Abstract template method that writes the given headers and content to the HTTP
* request.
* Abstract template method that writes the given headers and content to the HTTP request.
* @param headers the HTTP headers
* @return the response object for the executed request
*/
protected abstract ListenableFuture<ClientHttpResponse> executeInternal(
HttpHeaders headers) throws IOException;
protected abstract ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers)
throws IOException;
}

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

@ -79,6 +79,24 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements @@ -79,6 +79,24 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements
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
protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException {
return this.body;
@ -105,26 +123,24 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements @@ -105,26 +123,24 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements
};
this.bootstrap.connect(this.uri.getHost(), getPort(this.uri)).addListener(connectionListener);
return responseFuture;
}
@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);
}
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;
}
private static int getPort(URI uri) {
@ -140,23 +156,6 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements @@ -140,23 +156,6 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements
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.

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

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -34,8 +34,8 @@ public class HttpClientErrorException extends HttpStatusCodeException { @@ -34,8 +34,8 @@ public class HttpClientErrorException extends HttpStatusCodeException {
/**
* Construct a new instance of {@code HttpClientErrorException} based on an
* {@link HttpStatus}.
* Construct a new instance of {@code HttpClientErrorException} based on
* an {@link HttpStatus}.
* @param statusCode the status code
*/
public HttpClientErrorException(HttpStatus statusCode) {
@ -43,8 +43,8 @@ public class HttpClientErrorException extends HttpStatusCodeException { @@ -43,8 +43,8 @@ public class HttpClientErrorException extends HttpStatusCodeException {
}
/**
* Construct a new instance of {@code HttpClientErrorException} based on an
* {@link HttpStatus} and status text.
* Construct a new instance of {@code HttpClientErrorException} based on
* an {@link HttpStatus} and status text.
* @param statusCode the status code
* @param statusText the status text
*/
@ -53,30 +53,32 @@ public class HttpClientErrorException extends HttpStatusCodeException { @@ -53,30 +53,32 @@ public class HttpClientErrorException extends HttpStatusCodeException {
}
/**
* Construct a new instance of {@code HttpClientErrorException} based on an
* {@link HttpStatus}, status text, and response body content.
* Construct a new instance of {@code HttpClientErrorException} based on
* an {@link HttpStatus}, status text, and response body content.
* @param statusCode the status code
* @param statusText the status text
* @param responseBody the response body content, may be {@code null}
* @param responseCharset the response body charset, 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,
byte[] responseBody, Charset responseCharset) {
super(statusCode, statusText, responseBody, responseCharset);
}
/**
* Construct a new instance of {@code HttpClientErrorException} based on an
* {@link HttpStatus}, status text, and response body content.
* Construct a new instance of {@code HttpClientErrorException} based on
* an {@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}
* @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.1.2
*/
public HttpClientErrorException(HttpStatus statusCode, String statusText,
HttpHeaders responseHeaders, byte[] responseBody, Charset responseCharset) {
super(statusCode, statusText, responseHeaders, responseBody, responseCharset);
}

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

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

Loading…
Cancel
Save