14
14
15
15
package com .obs .services .internal .utils ;
16
16
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 ;
17
48
import java .io .IOException ;
18
49
import java .io .UnsupportedEncodingException ;
19
50
import java .lang .reflect .Method ;
36
67
import java .util .regex .Matcher ;
37
68
import java .util .regex .Pattern ;
38
69
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
-
71
70
public class RestUtils {
72
71
73
72
private static final ILogger log = LoggerBuilder .getLogger (RestUtils .class );
@@ -330,7 +329,7 @@ public Socket createSocket(InetAddress address, int port, InetAddress localAddre
330
329
331
330
public static OkHttpClient .Builder initHttpClientBuilder (ObsProperties obsProperties ,
332
331
KeyManagerFactory keyManagerFactory , TrustManagerFactory trustManagerFactory ,
333
- Dispatcher httpDispatcher , SecureRandom secureRandom ) {
332
+ Dispatcher httpDispatcher , Dns customizedDnsImpl , SecureRandom secureRandom ) {
334
333
335
334
List <Protocol > protocols = new ArrayList <Protocol >(2 );
336
335
protocols .add (Protocol .HTTP_1_1 );
@@ -351,6 +350,8 @@ public static OkHttpClient.Builder initHttpClientBuilder(ObsProperties obsProper
351
350
ObsConstraint .DEFAULT_IDLE_CONNECTION_TIME ),
352
351
TimeUnit .MILLISECONDS );
353
352
353
+ Dns dns = customizedDnsImpl == null ? new DefaultObsDns () : customizedDnsImpl ;
354
+
354
355
builder .protocols (protocols ).followRedirects (false ).followSslRedirects (false )
355
356
.retryOnConnectionFailure (
356
357
obsProperties .getBoolProperty (ExtObsConstraint .IS_RETRY_ON_CONNECTION_FAILURE_IN_OKHTTP , false ))
@@ -364,11 +365,7 @@ public static OkHttpClient.Builder initHttpClientBuilder(ObsProperties obsProper
364
365
.connectionPool (pool )
365
366
.hostnameVerifier ((hostname , session ) -> !obsProperties .getBoolProperty (ObsConstraint .HTTP_STRICT_HOSTNAME_VERIFICATION , false )
366
367
|| 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 );
372
369
373
370
int socketReadBufferSize = obsProperties .getIntProperty (ObsConstraint .SOCKET_READ_BUFFER_SIZE , -1 );
374
371
int socketWriteBufferSize = obsProperties .getIntProperty (ObsConstraint .SOCKET_WRITE_BUFFER_SIZE , -1 );
@@ -479,4 +476,22 @@ public static String readBodyFromResponse(Response response) {
479
476
}
480
477
return body ;
481
478
}
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
+ }
482
497
}
0 commit comments