From 370e3a52bfacdb28d8e58388778086b8358279ce Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 17 Mar 2015 21:24:51 +0100 Subject: [PATCH] HttpComponentsClientHttpRequestFactory supports plain HttpClient (i.e. non-CloseableHttpClient implementations) as well Issue: SPR-12826 --- ...mponentsAsyncClientHttpRequestFactory.java | 10 ++--- .../HttpComponentsClientHttpRequest.java | 12 +++--- ...ttpComponentsClientHttpRequestFactory.java | 40 +++++++++---------- .../HttpComponentsClientHttpResponse.java | 13 +++--- ...pComponentsStreamingClientHttpRequest.java | 12 +++--- 5 files changed, 45 insertions(+), 42 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsAsyncClientHttpRequestFactory.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsAsyncClientHttpRequestFactory.java index e59d214c92..005e5cce34 100644 --- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsAsyncClientHttpRequestFactory.java +++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsAsyncClientHttpRequestFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -43,8 +43,7 @@ import org.springframework.util.Assert; * @since 4.0 * @see HttpAsyncClient */ -public class HttpComponentsAsyncClientHttpRequestFactory - extends HttpComponentsClientHttpRequestFactory +public class HttpComponentsAsyncClientHttpRequestFactory extends HttpComponentsClientHttpRequestFactory implements AsyncClientHttpRequestFactory, InitializingBean { private CloseableHttpAsyncClient httpAsyncClient; @@ -86,7 +85,7 @@ public class HttpComponentsAsyncClientHttpRequestFactory /** * Set the {@code HttpClient} used for - * {@linkplain #createAsyncRequest(java.net.URI, org.springframework.http.HttpMethod) asynchronous execution}. + * {@linkplain #createAsyncRequest(URI, HttpMethod) asynchronous execution}. */ public void setHttpAsyncClient(CloseableHttpAsyncClient httpAsyncClient) { this.httpAsyncClient = httpAsyncClient; @@ -97,9 +96,10 @@ public class HttpComponentsAsyncClientHttpRequestFactory * {@linkplain #createAsyncRequest(URI, HttpMethod) asynchronous execution}. */ public CloseableHttpAsyncClient getHttpAsyncClient() { - return httpAsyncClient; + return this.httpAsyncClient; } + @Override public void afterPropertiesSet() { startAsyncClient(); diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java index 7706b365f6..996a17e07e 100644 --- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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,10 +23,10 @@ import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.HttpEntityEnclosingRequest; -import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.entity.ByteArrayEntity; -import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HttpContext; @@ -50,14 +50,14 @@ import org.springframework.util.StringUtils; */ final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpRequest { - private final CloseableHttpClient httpClient; + private final HttpClient httpClient; private final HttpUriRequest httpRequest; private final HttpContext httpContext; - HttpComponentsClientHttpRequest(CloseableHttpClient httpClient, HttpUriRequest httpRequest, HttpContext httpContext) { + HttpComponentsClientHttpRequest(HttpClient httpClient, HttpUriRequest httpRequest, HttpContext httpContext) { this.httpClient = httpClient; this.httpRequest = httpRequest; this.httpContext = httpContext; @@ -88,7 +88,7 @@ final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpR HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput); entityEnclosingRequest.setEntity(requestEntity); } - CloseableHttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext); + HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext); return new HttpComponentsClientHttpResponse(httpResponse); } diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java index fe5afec96e..f83e78d69f 100644 --- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java +++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -16,6 +16,7 @@ package org.springframework.http.client; +import java.io.Closeable; import java.io.IOException; import java.net.URI; @@ -32,7 +33,6 @@ import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpTrace; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.protocol.HttpClientContext; -import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.protocol.HttpContext; @@ -57,7 +57,7 @@ import org.springframework.util.Assert; */ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequestFactory, DisposableBean { - private CloseableHttpClient httpClient; + private HttpClient httpClient; private RequestConfig requestConfig; @@ -75,25 +75,20 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest /** * Create a new instance of the {@code HttpComponentsClientHttpRequestFactory} * with the given {@link HttpClient} instance. - *

As of Spring Framework 4.0, the given client is expected to be of type - * {@link CloseableHttpClient} (requiring HttpClient 4.3+). * @param httpClient the HttpClient instance to use for this request factory */ public HttpComponentsClientHttpRequestFactory(HttpClient httpClient) { - Assert.notNull(httpClient, "'httpClient' must not be null"); - Assert.isInstanceOf(CloseableHttpClient.class, httpClient, "'httpClient' is not of type CloseableHttpClient"); - this.httpClient = (CloseableHttpClient) httpClient; + Assert.notNull(httpClient, "HttpClient must not be null"); + this.httpClient = httpClient; } /** * Set the {@code HttpClient} used for - *

As of Spring Framework 4.0, the given client is expected to be of type - * {@link CloseableHttpClient} (requiring HttpClient 4.3+). + * {@linkplain #createRequest(URI, HttpMethod) synchronous execution}. */ public void setHttpClient(HttpClient httpClient) { - Assert.isInstanceOf(CloseableHttpClient.class, httpClient, "'httpClient' is not of type CloseableHttpClient"); - this.httpClient = (CloseableHttpClient) httpClient; + this.httpClient = httpClient; } /** @@ -200,8 +195,9 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest @Override + @SuppressWarnings("deprecation") public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException { - CloseableHttpClient client = (CloseableHttpClient) getHttpClient(); + HttpClient client = getHttpClient(); Assert.state(client != null, "Synchronous execution requires an HttpClient to be set"); HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri); postProcessHttpRequest(httpRequest); @@ -232,15 +228,16 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest } /** - * Create a default {@link RequestConfig} to use with the given client. Can - * return {@code null} to indicate that no custom request config should be set - * and the defaults of the {@link HttpClient} should be used. - *

The default implementation tries to merge the defaults of the client with the - * local customizations of this instance, if any. + * Create a default {@link RequestConfig} to use with the given client. + * Can return {@code null} to indicate that no custom request config should + * be set and the defaults of the {@link HttpClient} should be used. + *

The default implementation tries to merge the defaults of the client + * with the local customizations of this instance, if any. * @param client the client * @return the RequestConfig to use + * @since 4.2 */ - protected RequestConfig createRequestConfig(CloseableHttpClient client) { + protected RequestConfig createRequestConfig(HttpClient client) { if (client instanceof Configurable) { RequestConfig clientRequestConfig = ((Configurable) client).getConfig(); return mergeRequestConfig(clientRequestConfig); @@ -325,7 +322,9 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest */ @Override public void destroy() throws Exception { - this.httpClient.close(); + if (this.httpClient instanceof Closeable) { + ((Closeable) this.httpClient).close(); + } } @@ -349,4 +348,5 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest return "DELETE"; } } + } 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 f8fa0eb702..f7bde9f791 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-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -16,12 +16,13 @@ package org.springframework.http.client; +import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import org.apache.http.Header; import org.apache.http.HttpEntity; -import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; import org.springframework.http.HttpHeaders; @@ -41,12 +42,12 @@ import org.springframework.http.HttpHeaders; */ final class HttpComponentsClientHttpResponse extends AbstractClientHttpResponse { - private final CloseableHttpResponse httpResponse; + private final HttpResponse httpResponse; private HttpHeaders headers; - HttpComponentsClientHttpResponse(CloseableHttpResponse httpResponse) { + HttpComponentsClientHttpResponse(HttpResponse httpResponse) { this.httpResponse = httpResponse; } @@ -87,7 +88,9 @@ final class HttpComponentsClientHttpResponse extends AbstractClientHttpResponse EntityUtils.consume(this.httpResponse.getEntity()); } finally { - this.httpResponse.close(); + if (this.httpResponse instanceof Closeable) { + ((Closeable) this.httpResponse).close(); + } } } catch (IOException ex) { 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 ddd96ce264..856a9ea684 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-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -24,9 +24,9 @@ import java.net.URI; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpEntityEnclosingRequest; -import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpUriRequest; -import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.message.BasicHeader; import org.apache.http.protocol.HttpContext; @@ -47,7 +47,7 @@ import org.springframework.http.StreamingHttpOutputMessage; */ final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpRequest implements StreamingHttpOutputMessage { - private final CloseableHttpClient httpClient; + private final HttpClient httpClient; private final HttpUriRequest httpRequest; @@ -56,7 +56,7 @@ final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpR private Body body; - HttpComponentsStreamingClientHttpRequest(CloseableHttpClient httpClient, HttpUriRequest httpRequest, HttpContext httpContext) { + HttpComponentsStreamingClientHttpRequest(HttpClient httpClient, HttpUriRequest httpRequest, HttpContext httpContext) { this.httpClient = httpClient; this.httpRequest = httpRequest; this.httpContext = httpContext; @@ -94,7 +94,7 @@ final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpR entityEnclosingRequest.setEntity(requestEntity); } - CloseableHttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext); + HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext); return new HttpComponentsClientHttpResponse(httpResponse); }