28
28
import javax .net .ssl .SSLContext ;
29
29
import javax .net .ssl .TrustManager ;
30
30
import javax .net .ssl .X509TrustManager ;
31
+ import java .io .IOException ;
31
32
import java .security .SecureRandom ;
32
33
import java .util .Optional ;
33
34
import java .util .logging .Level ;
@@ -72,16 +73,19 @@ public static OkHttpClient initOkHttpClient(Configuration optionalConfiguration)
72
73
log .log (Level .WARNING , String .format ("Error while loading certificates: %s" , e .getMessage ()), e );
73
74
}
74
75
builder .addInterceptor (c -> {
75
- final String currentUserName = configuration .getUsername ().get ();
76
- final String currentPassword = configuration .getPassword ().get ();
76
+ final String currentUserName = Optional .ofNullable (configuration .getUsername ())
77
+ .map (OkHttpClientConfigurator ::getConfiguration ).orElse (null );
78
+ final String currentPassword = Optional .ofNullable (configuration .getPassword ())
79
+ .map (OkHttpClientConfigurator ::getConfiguration ).orElse (null );
77
80
if (currentUserName != null ) {
78
81
c .proceed (c .request ().newBuilder ().addHeader (HEADER_AUTHORIZATION ,
79
82
Credentials .basic (currentUserName , currentPassword )).build ());
80
83
}
81
84
return c .proceed (c .request ());
82
85
});
83
86
builder .addInterceptor (c -> {
84
- final String currentToken = configuration .getToken ().get ();
87
+ final String currentToken = Optional .ofNullable (configuration .getToken ())
88
+ .map (OkHttpClientConfigurator ::getConfiguration ).orElse (null );
85
89
if (currentToken != null ) {
86
90
return c .proceed (c .request ().newBuilder ().header (HEADER_AUTHORIZATION ,
87
91
String .format ("Bearer %s" , currentToken )).build ());
@@ -90,4 +94,12 @@ public static OkHttpClient initOkHttpClient(Configuration optionalConfiguration)
90
94
});
91
95
return builder .build ();
92
96
}
97
+
98
+ private static <T > T getConfiguration (ConfigurationSupplier <T > cs ) {
99
+ try {
100
+ return cs .get ();
101
+ } catch (IOException e ) {
102
+ return null ;
103
+ }
104
+ }
93
105
}
0 commit comments