2828import org .apache .hc .client5 .http .auth .KerberosCredentials ;
2929import org .apache .hc .client5 .http .auth .StandardAuthScheme ;
3030import org .apache .hc .client5 .http .auth .UsernamePasswordCredentials ;
31+ import org .apache .hc .client5 .http .classic .methods .HttpGet ;
3132import org .apache .hc .client5 .http .classic .methods .HttpPost ;
3233import org .apache .hc .client5 .http .config .RequestConfig ;
3334import org .apache .hc .client5 .http .impl .auth .BasicAuthCache ;
3637import org .apache .hc .client5 .http .impl .auth .DigestSchemeFactory ;
3738import org .apache .hc .client5 .http .impl .auth .SPNegoSchemeFactory ;
3839import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
39- import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
4040import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
4141import org .apache .hc .client5 .http .impl .classic .HttpClients ;
4242import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManager ;
4343import org .apache .hc .client5 .http .protocol .HttpClientContext ;
44- import org .apache .hc .client5 .http .socket .ConnectionSocketFactory ;
44+ import org .apache .hc .client5 .http .routing .RoutingSupport ;
45+ import org .apache .hc .core5 .http .ClassicHttpResponse ;
4546import org .apache .hc .core5 .http .ContentType ;
47+ import org .apache .hc .core5 .http .HttpException ;
48+ import org .apache .hc .core5 .http .HttpHost ;
4649import org .apache .hc .core5 .http .NoHttpResponseException ;
4750import org .apache .hc .core5 .http .config .Lookup ;
48- import org .apache .hc .core5 .http .config .Registry ;
4951import org .apache .hc .core5 .http .config .RegistryBuilder ;
5052import org .apache .hc .core5 .http .io .entity .ByteArrayEntity ;
5153import org .apache .hc .core5 .http .io .entity .EntityUtils ;
@@ -84,9 +86,9 @@ public class AvaticaCommonsHttpClientImpl implements AvaticaHttpClient, HttpClie
8486 private static AuthScope anyAuthScope = new AuthScope (null , -1 );
8587
8688 protected final URI uri ;
89+ protected HttpHost httpHost ;
8790 protected BasicAuthCache authCache ;
8891 protected CloseableHttpClient client ;
89- protected Registry <ConnectionSocketFactory > socketFactoryRegistry ;
9092 protected PoolingHttpClientConnectionManager pool ;
9193
9294 protected UsernamePasswordCredentials credentials = null ;
@@ -126,6 +128,12 @@ protected void initializeClient(PoolingHttpClientConnectionManager pool,
126128
127129 }
128130
131+ private org .apache .hc .client5 .http .config .ConnectionConfig createConnectionConfig () {
132+ return org .apache .hc .client5 .http .config .ConnectionConfig .custom ()
133+ .setConnectTimeout (this .connectTimeout , TimeUnit .MILLISECONDS )
134+ .build ();
135+ }
136+
129137 // This is needed because we initialize the client object too early.
130138 private RequestConfig createRequestConfig () {
131139 RequestConfig .Builder requestConfigBuilder = RequestConfig .custom ();
@@ -151,14 +159,21 @@ private RequestConfig createRequestConfig() {
151159 }
152160
153161 @ Override public byte [] send (byte [] request ) {
162+ try {
163+ // Doing this earlier would break API backwards compatibility
164+ determineHost ();
165+ } catch (HttpException e ) {
166+ LOG .debug ("Failed to execute HTTP request" , e );
167+ throw new RuntimeException ("Could not determine Http Host from URI" , e );
168+ }
154169 while (true ) {
155170 ByteArrayEntity entity = new ByteArrayEntity (request , ContentType .APPLICATION_OCTET_STREAM );
156171
157172 // Create the client with the AuthSchemeRegistry and manager
158173 HttpPost post = new HttpPost (uri );
159174 post .setEntity (entity );
160175
161- try (CloseableHttpResponse response = execute ( post , context )) {
176+ try (ClassicHttpResponse response = executeOpen ( httpHost , post , context )) {
162177 final int statusCode = response .getCode ();
163178 if (HttpURLConnection .HTTP_OK == statusCode
164179 || HttpURLConnection .HTTP_INTERNAL_ERROR == statusCode ) {
@@ -184,10 +199,17 @@ private RequestConfig createRequestConfig() {
184199 }
185200 }
186201
202+ private void determineHost () throws HttpException {
203+ if (httpHost == null ) {
204+ HttpGet dummy = new HttpGet (uri );
205+ this .httpHost = RoutingSupport .determineHost (dummy );
206+ }
207+ }
208+
187209 // Visible for testing
188- CloseableHttpResponse execute ( HttpPost post , HttpClientContext context )
210+ ClassicHttpResponse executeOpen ( HttpHost httpHost , HttpPost post , HttpClientContext context )
189211 throws IOException , ClientProtocolException {
190- return client .execute ( post , context );
212+ return client .executeOpen ( httpHost , post , context );
191213 }
192214
193215 @ Override public void setUsernamePassword (AuthenticationType authType , String username ,
0 commit comments