@ -47,6 +47,12 @@ public class Http2Client implements Client {
. build ( ) ) ;
. build ( ) ) ;
}
}
public Http2Client ( Options options ) {
this ( newClientBuilder ( options )
. version ( Version . HTTP_2 )
. build ( ) ) ;
}
public Http2Client ( HttpClient client ) {
public Http2Client ( HttpClient client ) {
this . client = Util . checkNotNull ( client , "HttpClient must not be null" ) ;
this . client = Util . checkNotNull ( client , "HttpClient must not be null" ) ;
}
}
@ -54,10 +60,11 @@ public class Http2Client implements Client {
@Override
@Override
public Response execute ( Request request , Options options ) throws IOException {
public Response execute ( Request request , Options options ) throws IOException {
final HttpRequest httpRequest = newRequestBuilder ( request , options ) . build ( ) ;
final HttpRequest httpRequest = newRequestBuilder ( request , options ) . build ( ) ;
HttpClient clientForRequest = getOrCreateClient ( options ) ;
HttpResponse < byte [ ] > httpResponse ;
HttpResponse < byte [ ] > httpResponse ;
try {
try {
httpResponse = client . send ( httpRequest , BodyHandlers . ofByteArray ( ) ) ;
httpResponse = clientForRequest . send ( httpRequest , BodyHandlers . ofByteArray ( ) ) ;
} catch ( final InterruptedException e ) {
} catch ( final InterruptedException e ) {
Thread . currentThread ( ) . interrupt ( ) ;
Thread . currentThread ( ) . interrupt ( ) ;
throw new IOException ( "Invalid uri " + request . url ( ) , e ) ;
throw new IOException ( "Invalid uri " + request . url ( ) , e ) ;
@ -76,6 +83,39 @@ public class Http2Client implements Client {
return response ;
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 {
private Builder newRequestBuilder ( Request request , Options options ) throws IOException {
URI uri ;
URI uri ;
try {
try {