1
1
package com .aliyuncs .http .clients ;
2
2
3
- import com .aliyuncs .exceptions .ClientException ;
4
- import com .aliyuncs .http .*;
5
- import com .aliyuncs .utils .IOUtils ;
6
- import com .aliyuncs .utils .StringUtils ;
3
+ import java .io .IOException ;
4
+ import java .security .KeyManagementException ;
5
+ import java .security .NoSuchAlgorithmException ;
6
+ import java .security .cert .CertificateException ;
7
+ import java .security .cert .X509Certificate ;
8
+ import java .util .Map ;
9
+ import java .util .concurrent .Callable ;
10
+ import java .util .concurrent .ExecutorService ;
11
+ import java .util .concurrent .Future ;
12
+ import java .util .concurrent .SynchronousQueue ;
13
+ import java .util .concurrent .ThreadFactory ;
14
+ import java .util .concurrent .ThreadPoolExecutor ;
15
+ import java .util .concurrent .TimeUnit ;
16
+ import java .util .concurrent .atomic .AtomicInteger ;
17
+
18
+ import javax .net .ssl .SSLContext ;
19
+
7
20
import org .apache .http .Header ;
8
21
import org .apache .http .HttpResponse ;
9
22
import org .apache .http .client .config .RequestConfig ;
28
41
import org .apache .http .ssl .TrustStrategy ;
29
42
import org .apache .http .util .EntityUtils ;
30
43
31
- import javax .net .ssl .SSLContext ;
32
- import java .io .IOException ;
33
- import java .security .KeyManagementException ;
34
- import java .security .NoSuchAlgorithmException ;
35
- import java .security .cert .CertificateException ;
36
- import java .security .cert .X509Certificate ;
37
- import java .util .Map ;
38
- import java .util .concurrent .*;
39
- import java .util .concurrent .atomic .AtomicInteger ;
40
-
44
+ import com .aliyuncs .exceptions .ClientException ;
45
+ import com .aliyuncs .http .CallBack ;
46
+ import com .aliyuncs .http .FormatType ;
47
+ import com .aliyuncs .http .HttpClientConfig ;
48
+ import com .aliyuncs .http .HttpRequest ;
49
+ import com .aliyuncs .http .IHttpClient ;
50
+ import com .aliyuncs .utils .IOUtils ;
51
+ import com .aliyuncs .utils .StringUtils ;
41
52
42
53
public class ApacheHttpClient extends IHttpClient {
43
54
@@ -65,11 +76,9 @@ protected void init(final HttpClientConfig config) throws ClientException {
65
76
}
66
77
67
78
// default request config
68
- RequestConfig defaultConfig = RequestConfig .custom ()
69
- .setConnectTimeout ((int ) config .getConnectionTimeoutMillis ())
70
- .setSocketTimeout ((int ) config .getReadTimeoutMillis ())
71
- .setConnectionRequestTimeout ((int ) config .getWriteTimeoutMillis ())
72
- .build ();
79
+ RequestConfig defaultConfig = RequestConfig .custom ().setConnectTimeout ((int ) config
80
+ .getConnectionTimeoutMillis ()).setSocketTimeout ((int ) config .getReadTimeoutMillis ())
81
+ .setConnectionRequestTimeout ((int ) config .getWriteTimeoutMillis ()).build ();
73
82
builder .setDefaultRequestConfig (defaultConfig );
74
83
75
84
// https
@@ -85,7 +94,8 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif
85
94
}
86
95
}).build ();
87
96
88
- SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory (sslContext , NoopHostnameVerifier .INSTANCE );
97
+ SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory (sslContext ,
98
+ NoopHostnameVerifier .INSTANCE );
89
99
90
100
socketFactoryRegistryBuilder .register ("https" , connectionFactory );
91
101
@@ -94,10 +104,11 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif
94
104
}
95
105
} else {
96
106
if (config .getSslSocketFactory () != null ) {
97
- SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory (config . getSslSocketFactory (),
98
- config .getHostnameVerifier ());
107
+ SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory (config
108
+ . getSslSocketFactory (), config .getHostnameVerifier ());
99
109
socketFactoryRegistryBuilder .register ("https" , sslConnectionSocketFactory );
100
- } else if (config .getKeyManagers () != null || config .getX509TrustManagers () != null || config .getSecureRandom () != null ) {
110
+ } else if (config .getKeyManagers () != null || config .getX509TrustManagers () != null || config
111
+ .getSecureRandom () != null ) {
101
112
try {
102
113
SSLContext sslContext = SSLContext .getInstance ("TLS" );
103
114
sslContext .init (config .getKeyManagers (), config .getX509TrustManagers (), config .getSecureRandom ());
@@ -120,9 +131,8 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif
120
131
121
132
// async
122
133
if (config .getExecutorService () == null ) {
123
- executorService = new ThreadPoolExecutor (0 , config .getMaxRequests (), DEFAULT_THREAD_KEEP_ALIVE_TIME , TimeUnit .SECONDS ,
124
- new SynchronousQueue <Runnable >(),
125
- new DefaultAsyncThreadFactory ());
134
+ executorService = new ThreadPoolExecutor (0 , config .getMaxRequests (), DEFAULT_THREAD_KEEP_ALIVE_TIME ,
135
+ TimeUnit .SECONDS , new SynchronousQueue <Runnable >(), new DefaultAsyncThreadFactory ());
126
136
} else {
127
137
executorService = config .getExecutorService ();
128
138
}
@@ -226,7 +236,8 @@ public final com.aliyuncs.http.HttpResponse syncInvoke(HttpRequest apiRequest) t
226
236
}
227
237
228
238
@ Override
229
- public final Future <com .aliyuncs .http .HttpResponse > asyncInvoke (final HttpRequest apiRequest , final CallBack callback ) {
239
+ public final Future <com .aliyuncs .http .HttpResponse > asyncInvoke (final HttpRequest apiRequest ,
240
+ final CallBack callback ) {
230
241
return executorService .submit (new Callable <com .aliyuncs .http .HttpResponse >() {
231
242
@ Override
232
243
public com .aliyuncs .http .HttpResponse call () throws Exception {
0 commit comments