HttpComponentsClientHttpRequestFactory supports plain HttpClient (i.e. non-CloseableHttpClient implementations) as well
Issue: SPR-12826
This commit is contained in:
parent
8ee0e98540
commit
370e3a52bf
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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.
|
||||
* <p>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
|
||||
* <p>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.
|
||||
* <p>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.
|
||||
* <p>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";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue