diff --git a/spring-web/src/main/java/org/springframework/http/client/AsyncClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/AsyncClientHttpRequest.java index c7b136c7c9..9e2e32eea1 100644 --- a/spring-web/src/main/java/org/springframework/http/client/AsyncClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/AsyncClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -23,24 +23,24 @@ import org.springframework.http.HttpRequest; import org.springframework.util.concurrent.ListenableFuture; /** - * Represents a client-side asynchronous HTTP request. Created via an implementation of - * the {@link AsyncClientHttpRequestFactory}. - *
A {@code AsyncHttpRequest} can be {@linkplain #executeAsync() executed}, getting a - * future {@link ClientHttpResponse} which can be read from. + * Represents a client-side asynchronous HTTP request. Created via an + * implementation of the {@link AsyncClientHttpRequestFactory}. + * + *
A {@code AsyncHttpRequest} can be {@linkplain #executeAsync() executed},
+ * getting a future {@link ClientHttpResponse} which can be read from.
*
* @author Arjen Poutsma
* @since 4.0
- * @see AsyncClientHttpRequestFactory#createAsyncRequest(java.net.URI, org.springframework.http.HttpMethod)
+ * @see AsyncClientHttpRequestFactory#createAsyncRequest
*/
public interface AsyncClientHttpRequest extends HttpRequest, HttpOutputMessage {
/**
- * Execute this request asynchronously, resulting in a future
+ * Execute this request asynchronously, resulting in a Future handle.
* {@link ClientHttpResponse} that can be read.
* @return the future response result of the execution
* @throws java.io.IOException in case of I/O errors
*/
ListenableFuture The returned request can be written to, and then executed by calling
* {@link AsyncClientHttpRequest#executeAsync()}.
* @param uri the URI to create a request for
@@ -40,7 +40,6 @@ public interface AsyncClientHttpRequestFactory {
* @return the created request
* @throws IOException in case of I/O errors
*/
- AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod)
- throws IOException;
+ AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException;
}
diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsAsyncClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsAsyncClientHttpRequest.java
index 0344c0bb72..0f7d277de2 100644
--- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsAsyncClientHttpRequest.java
+++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsAsyncClientHttpRequest.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2013 the original author or authors.
+ * Copyright 2002-2014 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.
@@ -44,9 +44,8 @@ import org.springframework.util.concurrent.ListenableFutureCallbackRegistry;
*
* @author Oleg Kalnichevski
* @author Arjen Poutsma
- * @see org.springframework.http.client.HttpComponentsClientHttpRequestFactory#createRequest(java.net.URI,
- * org.springframework.http.HttpMethod)
- * @since 3.1
+ * @since 4.0
+ * @see org.springframework.http.client.HttpComponentsClientHttpRequestFactory#createRequest
*/
final class HttpComponentsAsyncClientHttpRequest extends AbstractBufferingAsyncClientHttpRequest {
@@ -56,13 +55,14 @@ final class HttpComponentsAsyncClientHttpRequest extends AbstractBufferingAsyncC
private final HttpContext httpContext;
- public HttpComponentsAsyncClientHttpRequest(HttpAsyncClient httpClient,
- HttpUriRequest httpRequest, HttpContext httpContext) {
+
+ HttpComponentsAsyncClientHttpRequest(HttpAsyncClient httpClient, HttpUriRequest httpRequest, HttpContext httpContext) {
this.httpClient = httpClient;
this.httpRequest = httpRequest;
this.httpContext = httpContext;
}
+
@Override
public HttpMethod getMethod() {
return HttpMethod.valueOf(this.httpRequest.getMethod());
@@ -74,48 +74,46 @@ final class HttpComponentsAsyncClientHttpRequest extends AbstractBufferingAsyncC
}
@Override
- protected ListenableFuture Allows to use a pre-configured {@link HttpClient} instance -
* potentially with authentication, HTTP connection pooling, etc.
*
+ * NOTE: Requires Apache HttpComponents 4.3 or higher, as of Spring 4.0.
+ *
* @author Oleg Kalnichevski
* @author Arjen Poutsma
* @since 3.1
diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpResponse.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpResponse.java
index f0c8ff1e57..7b6ca8b894 100644
--- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpResponse.java
+++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpResponse.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2012 the original author or authors.
+ * Copyright 2002-2014 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,7 +73,7 @@ final class HttpComponentsClientHttpResponse extends AbstractClientHttpResponse
@Override
public InputStream getBody() throws IOException {
HttpEntity entity = this.httpResponse.getEntity();
- return entity != null ? entity.getContent() : null;
+ return (entity != null ? entity.getContent() : null);
}
@Override
@@ -83,12 +83,13 @@ final class HttpComponentsClientHttpResponse extends AbstractClientHttpResponse
try {
// Attempt to keep connection alive by consuming its remaining content
EntityUtils.consume(this.httpResponse.getEntity());
- } finally {
- // Paranoia
+ }
+ finally {
this.httpResponse.close();
}
}
- catch (IOException ignore) {
+ catch (IOException ex) {
+ // Ignore exception on close...
}
}
diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsStreamingClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsStreamingClientHttpRequest.java
index 6be05620ba..ddd96ce264 100644
--- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsStreamingClientHttpRequest.java
+++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsStreamingClientHttpRequest.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2013 the original author or authors.
+ * Copyright 2002-2014 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.
@@ -36,18 +36,16 @@ import org.springframework.http.MediaType;
import org.springframework.http.StreamingHttpOutputMessage;
/**
- * {@link ClientHttpRequest} implementation that uses Apache HttpComponents HttpClient to
- * execute requests.
+ * {@link ClientHttpRequest} implementation that uses Apache HttpComponents
+ * HttpClient to execute requests.
*
- * Created via the
- * {@link org.springframework.http.client.HttpComponentsClientHttpRequestFactory}.
+ * Created via the {@link HttpComponentsClientHttpRequestFactory}.
*
* @author Arjen Poutsma
- * @see org.springframework.http.client.HttpComponentsClientHttpRequestFactory#createRequest(java.net.URI, org.springframework.http.HttpMethod)
* @since 4.0
+ * @see HttpComponentsClientHttpRequestFactory#createRequest(java.net.URI, org.springframework.http.HttpMethod)
*/
-final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpRequest
- implements StreamingHttpOutputMessage {
+final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpRequest implements StreamingHttpOutputMessage {
private final CloseableHttpClient httpClient;
@@ -57,13 +55,14 @@ final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpR
private Body body;
- public HttpComponentsStreamingClientHttpRequest(CloseableHttpClient httpClient,
- HttpUriRequest httpRequest, HttpContext httpContext) {
+
+ HttpComponentsStreamingClientHttpRequest(CloseableHttpClient httpClient, HttpUriRequest httpRequest, HttpContext httpContext) {
this.httpClient = httpClient;
this.httpRequest = httpRequest;
this.httpContext = httpContext;
}
+
@Override
public HttpMethod getMethod() {
return HttpMethod.valueOf(this.httpRequest.getMethod());
@@ -82,8 +81,7 @@ final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpR
@Override
protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException {
- throw new UnsupportedOperationException(
- "getBody not supported when bufferRequestBody is false");
+ throw new UnsupportedOperationException("getBody not supported");
}
@Override
@@ -91,26 +89,23 @@ final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpR
HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers);
if (this.httpRequest instanceof HttpEntityEnclosingRequest && body != null) {
- HttpEntityEnclosingRequest entityEnclosingRequest =
- (HttpEntityEnclosingRequest) this.httpRequest;
-
+ HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
HttpEntity requestEntity = new StreamingHttpEntity(getHeaders(), body);
entityEnclosingRequest.setEntity(requestEntity);
}
- CloseableHttpResponse httpResponse =
- this.httpClient.execute(this.httpRequest, this.httpContext);
+
+ CloseableHttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
return new HttpComponentsClientHttpResponse(httpResponse);
}
+
private static class StreamingHttpEntity implements HttpEntity {
private final HttpHeaders headers;
private final StreamingHttpOutputMessage.Body body;
-
- private StreamingHttpEntity(HttpHeaders headers,
- StreamingHttpOutputMessage.Body body) {
+ public StreamingHttpEntity(HttpHeaders headers, StreamingHttpOutputMessage.Body body) {
this.headers = headers;
this.body = body;
}
@@ -127,32 +122,30 @@ final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpR
@Override
public long getContentLength() {
- return headers.getContentLength();
+ return this.headers.getContentLength();
}
@Override
public Header getContentType() {
- MediaType contentType = headers.getContentType();
- return contentType != null ?
- new BasicHeader("Content-Type", contentType.toString()) : null;
+ MediaType contentType = this.headers.getContentType();
+ return (contentType != null ? new BasicHeader("Content-Type", contentType.toString()) : null);
}
@Override
public Header getContentEncoding() {
- String contentEncoding = headers.getFirst("Content-Encoding");
- return contentEncoding != null ?
- new BasicHeader("Content-Encoding", contentEncoding) : null;
+ String contentEncoding = this.headers.getFirst("Content-Encoding");
+ return (contentEncoding != null ? new BasicHeader("Content-Encoding", contentEncoding) : null);
}
@Override
public InputStream getContent() throws IOException, IllegalStateException {
- throw new IllegalStateException();
+ throw new IllegalStateException("No content available");
}
@Override
public void writeTo(OutputStream outputStream) throws IOException {
- body.writeTo(outputStream);
+ this.body.writeTo(outputStream);
}
@Override