1414
1515package com .obs .services .internal .utils ;
1616
17+ import com .obs .log .ILogger ;
18+ import com .obs .log .LoggerBuilder ;
19+ import com .obs .services .exception .ObsException ;
20+ import com .obs .services .internal .Constants ;
21+ import com .obs .services .internal .Constants .CommonHeaders ;
22+ import com .obs .services .internal .ObsConstraint ;
23+ import com .obs .services .internal .ObsProperties ;
24+ import com .obs .services .internal .ServiceException ;
25+ import com .obs .services .internal .ext .ExtObsConstraint ;
26+ import com .obs .services .model .HttpProtocolTypeEnum ;
27+ import okhttp3 .Authenticator ;
28+ import okhttp3 .ConnectionPool ;
29+ import okhttp3 .Credentials ;
30+ import okhttp3 .Dispatcher ;
31+ import okhttp3 .Dns ;
32+ import okhttp3 .OkHttpClient ;
33+ import okhttp3 .Protocol ;
34+ import okhttp3 .Request ;
35+ import okhttp3 .Response ;
36+ import okhttp3 .Route ;
37+ import org .jetbrains .annotations .NotNull ;
38+
39+ import javax .net .SocketFactory ;
40+ import javax .net .ssl .HttpsURLConnection ;
41+ import javax .net .ssl .KeyManager ;
42+ import javax .net .ssl .KeyManagerFactory ;
43+ import javax .net .ssl .SSLContext ;
44+ import javax .net .ssl .SSLSocketFactory ;
45+ import javax .net .ssl .TrustManager ;
46+ import javax .net .ssl .TrustManagerFactory ;
47+ import javax .net .ssl .X509TrustManager ;
1748import java .io .IOException ;
1849import java .io .UnsupportedEncodingException ;
1950import java .lang .reflect .Method ;
3667import java .util .regex .Matcher ;
3768import java .util .regex .Pattern ;
3869
39- import javax .net .SocketFactory ;
40- import javax .net .ssl .HttpsURLConnection ;
41- import javax .net .ssl .KeyManager ;
42- import javax .net .ssl .KeyManagerFactory ;
43- import javax .net .ssl .SSLContext ;
44- import javax .net .ssl .SSLSocketFactory ;
45- import javax .net .ssl .TrustManager ;
46- import javax .net .ssl .TrustManagerFactory ;
47- import javax .net .ssl .X509TrustManager ;
48-
49- import com .obs .log .ILogger ;
50- import com .obs .log .LoggerBuilder ;
51- import com .obs .services .exception .ObsException ;
52- import com .obs .services .internal .Constants ;
53- import com .obs .services .internal .Constants .CommonHeaders ;
54- import com .obs .services .internal .ObsConstraint ;
55- import com .obs .services .internal .ObsProperties ;
56- import com .obs .services .internal .ServiceException ;
57- import com .obs .services .internal .ext .ExtObsConstraint ;
58- import com .obs .services .model .HttpProtocolTypeEnum ;
59-
60- import okhttp3 .Authenticator ;
61- import okhttp3 .ConnectionPool ;
62- import okhttp3 .Credentials ;
63- import okhttp3 .Dispatcher ;
64- import okhttp3 .Dns ;
65- import okhttp3 .OkHttpClient ;
66- import okhttp3 .Protocol ;
67- import okhttp3 .Request ;
68- import okhttp3 .Response ;
69- import okhttp3 .Route ;
70-
7170public class RestUtils {
7271
7372 private static final ILogger log = LoggerBuilder .getLogger (RestUtils .class );
@@ -330,7 +329,7 @@ public Socket createSocket(InetAddress address, int port, InetAddress localAddre
330329
331330 public static OkHttpClient .Builder initHttpClientBuilder (ObsProperties obsProperties ,
332331 KeyManagerFactory keyManagerFactory , TrustManagerFactory trustManagerFactory ,
333- Dispatcher httpDispatcher , SecureRandom secureRandom ) {
332+ Dispatcher httpDispatcher , Dns customizedDnsImpl , SecureRandom secureRandom ) {
334333
335334 List <Protocol > protocols = new ArrayList <Protocol >(2 );
336335 protocols .add (Protocol .HTTP_1_1 );
@@ -351,6 +350,8 @@ public static OkHttpClient.Builder initHttpClientBuilder(ObsProperties obsProper
351350 ObsConstraint .DEFAULT_IDLE_CONNECTION_TIME ),
352351 TimeUnit .MILLISECONDS );
353352
353+ Dns dns = customizedDnsImpl == null ? new DefaultObsDns () : customizedDnsImpl ;
354+
354355 builder .protocols (protocols ).followRedirects (false ).followSslRedirects (false )
355356 .retryOnConnectionFailure (
356357 obsProperties .getBoolProperty (ExtObsConstraint .IS_RETRY_ON_CONNECTION_FAILURE_IN_OKHTTP , false ))
@@ -364,11 +365,7 @@ public static OkHttpClient.Builder initHttpClientBuilder(ObsProperties obsProper
364365 .connectionPool (pool )
365366 .hostnameVerifier ((hostname , session ) -> !obsProperties .getBoolProperty (ObsConstraint .HTTP_STRICT_HOSTNAME_VERIFICATION , false )
366367 || HttpsURLConnection .getDefaultHostnameVerifier ().verify (obsProperties .getStringProperty (ObsConstraint .END_POINT , "" ), session ))
367- .dns (hostname -> {
368- List <InetAddress > adds = Dns .SYSTEM .lookup (hostname );
369- log .info ("internet host address:" + adds );
370- return adds ;
371- });
368+ .dns (dns );
372369
373370 int socketReadBufferSize = obsProperties .getIntProperty (ObsConstraint .SOCKET_READ_BUFFER_SIZE , -1 );
374371 int socketWriteBufferSize = obsProperties .getIntProperty (ObsConstraint .SOCKET_WRITE_BUFFER_SIZE , -1 );
@@ -479,4 +476,22 @@ public static String readBodyFromResponse(Response response) {
479476 }
480477 return body ;
481478 }
479+ public static class DefaultObsDns implements Dns {
480+ public DefaultObsDns () {
481+ log .info ("use Default Dns" );
482+ }
483+
484+ /**
485+ * @param hostname
486+ * @return
487+ * @throws UnknownHostException
488+ */
489+ @ NotNull
490+ @ Override
491+ public List <InetAddress > lookup (@ NotNull String hostname ) throws UnknownHostException {
492+ List <InetAddress > adds = Dns .SYSTEM .lookup (hostname );
493+ log .info ("internet host address:" + adds );
494+ return adds ;
495+ }
496+ }
482497}
0 commit comments