From 9b57437b7af7d7cdde3eb1bd1547d3859af59d00 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 7 Nov 2016 12:55:35 +0100 Subject: [PATCH] Polishing --- .../AbstractAsyncClientHttpRequest.java | 9 ++- .../http/client/Netty4ClientHttpRequest.java | 65 +++++++++---------- .../web/client/HttpClientErrorException.java | 30 +++++---- .../web/client/HttpServerErrorException.java | 37 ++++++----- 4 files changed, 72 insertions(+), 69 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/client/AbstractAsyncClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/AbstractAsyncClientHttpRequest.java index 594c2fb9dd..e33a15381c 100644 --- a/spring-web/src/main/java/org/springframework/http/client/AbstractAsyncClientHttpRequest.java +++ b/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"); * 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; /** - * 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 executeInternal( - HttpHeaders headers) throws IOException; + protected abstract ListenableFuture executeInternal(HttpHeaders headers) + throws IOException; } diff --git a/spring-web/src/main/java/org/springframework/http/client/Netty4ClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/Netty4ClientHttpRequest.java index 41ac7108f2..376092237d 100644 --- a/spring-web/src/main/java/org/springframework/http/client/Netty4ClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/Netty4ClientHttpRequest.java @@ -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 }; 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> 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 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> entry : headers.entrySet()) { - nettyRequest.headers().add(entry.getKey(), entry.getValue()); - } - - return nettyRequest; - } - /** * A SimpleChannelInboundHandler to update the given SettableListenableFuture. diff --git a/spring-web/src/main/java/org/springframework/web/client/HttpClientErrorException.java b/spring-web/src/main/java/org/springframework/web/client/HttpClientErrorException.java index a768dfb78f..14883542a9 100644 --- a/spring-web/src/main/java/org/springframework/web/client/HttpClientErrorException.java +++ b/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"); * 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 - * {@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 { } /** - * 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 { } /** - * 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); } diff --git a/spring-web/src/main/java/org/springframework/web/client/HttpServerErrorException.java b/spring-web/src/main/java/org/springframework/web/client/HttpServerErrorException.java index 5f9ba72cd5..f4e494d931 100644 --- a/spring-web/src/main/java/org/springframework/web/client/HttpServerErrorException.java +++ b/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"); * 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. * * @author Arjen Poutsma - * @see DefaultResponseErrorHandler * @since 3.0 + * @see DefaultResponseErrorHandler */ 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 { } /** - * 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 { } /** - * 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); } + }