@ -47,6 +47,12 @@ public class Http2Client implements Client {
@@ -47,6 +47,12 @@ public class Http2Client implements Client {
. build ( ) ) ;
}
public Http2Client ( Options options ) {
this ( newClientBuilder ( options )
. version ( Version . HTTP_2 )
. build ( ) ) ;
}
public Http2Client ( HttpClient client ) {
this . client = Util . checkNotNull ( client , "HttpClient must not be null" ) ;
}
@ -54,10 +60,11 @@ public class Http2Client implements Client {
@@ -54,10 +60,11 @@ public class Http2Client implements Client {
@Override
public Response execute ( Request request , Options options ) throws IOException {
final HttpRequest httpRequest = newRequestBuilder ( request , options ) . build ( ) ;
HttpClient clientForRequest = getOrCreateClient ( options ) ;
HttpResponse < byte [ ] > httpResponse ;
try {
httpResponse = client . send ( httpRequest , BodyHandlers . ofByteArray ( ) ) ;
httpResponse = clientForRequest . send ( httpRequest , BodyHandlers . ofByteArray ( ) ) ;
} catch ( final InterruptedException e ) {
Thread . currentThread ( ) . interrupt ( ) ;
throw new IOException ( "Invalid uri " + request . url ( ) , e ) ;
@ -76,6 +83,39 @@ public class Http2Client implements Client {
@@ -76,6 +83,39 @@ public class Http2Client implements Client {
return response ;
}
private HttpClient getOrCreateClient ( Options options ) {
if ( doesClientConfigurationDiffer ( options ) ) {
// create a new client from the existing one - but with connectTimeout and followRedirect
// settings from options
java . net . http . HttpClient . Builder builder = newClientBuilder ( options )
. sslContext ( client . sslContext ( ) )
. sslParameters ( client . sslParameters ( ) )
. version ( client . version ( ) ) ;
client . authenticator ( ) . ifPresent ( builder : : authenticator ) ;
client . cookieHandler ( ) . ifPresent ( builder : : cookieHandler ) ;
client . executor ( ) . ifPresent ( builder : : executor ) ;
client . proxy ( ) . ifPresent ( builder : : proxy ) ;
return builder . build ( ) ;
}
return client ;
}
private boolean doesClientConfigurationDiffer ( Options options ) {
if ( ( client . followRedirects ( ) = = Redirect . ALWAYS ) ! = options . isFollowRedirects ( ) ) {
return true ;
}
return client . connectTimeout ( )
. map ( timeout - > timeout . toMillis ( ) ! = options . connectTimeoutMillis ( ) )
. orElse ( true ) ;
}
private static java . net . http . HttpClient . Builder newClientBuilder ( Options options ) {
return HttpClient
. newBuilder ( )
. followRedirects ( options . isFollowRedirects ( ) ? Redirect . ALWAYS : Redirect . NEVER )
. connectTimeout ( Duration . ofMillis ( options . connectTimeoutMillis ( ) ) ) ;
}
private Builder newRequestBuilder ( Request request , Options options ) throws IOException {
URI uri ;
try {