1
1
package com .aliyuncs .http .clients ;
2
2
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
-
3
+ import com .aliyuncs .exceptions .ClientException ;
4
+ import com .aliyuncs .http .*;
5
+ import com .aliyuncs .utils .IOUtils ;
6
+ import com .aliyuncs .utils .StringUtils ;
20
7
import org .apache .http .Header ;
21
8
import org .apache .http .HttpResponse ;
22
9
import org .apache .http .client .config .RequestConfig ;
41
28
import org .apache .http .ssl .TrustStrategy ;
42
29
import org .apache .http .util .EntityUtils ;
43
30
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 ;
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 ;
52
40
53
41
public class ApacheHttpClient extends IHttpClient {
54
42
@@ -81,28 +69,36 @@ protected void init(final HttpClientConfig config) throws ClientException {
81
69
.setConnectionRequestTimeout ((int ) config .getWriteTimeoutMillis ()).build ();
82
70
builder .setDefaultRequestConfig (defaultConfig );
83
71
84
- // https
72
+ // http
85
73
RegistryBuilder <ConnectionSocketFactory > socketFactoryRegistryBuilder = RegistryBuilder .create ();
86
74
socketFactoryRegistryBuilder .register ("http" , new PlainConnectionSocketFactory ());
87
- if (config .isIgnoreSSLCerts ()) {
88
- try {
89
- SSLContext sslContext = new SSLContextBuilder ().loadTrustMaterial (null , new TrustStrategy () {
90
- // trust all
91
- @ Override
92
- public boolean isTrusted (X509Certificate [] chain , String authType ) throws CertificateException {
93
- return true ;
94
- }
95
- }).build ();
96
75
97
- SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory ( sslContext ,
98
- NoopHostnameVerifier . INSTANCE );
76
+ // https
77
+ // register default https connector(ignore untrusted cert)
99
78
100
- socketFactoryRegistryBuilder .register ("https" , connectionFactory );
79
+ // SSLContext sc = SSLContext.getInstance("SSL");
80
+ // sc.init(null, new TrustManager[]{trustAll}, new java.security.SecureRandom());
81
+ //
82
+ try {
83
+ SSLContext sslContext = new SSLContextBuilder ().loadTrustMaterial (null , new TrustStrategy () {
84
+ // trust all
85
+ @ Override
86
+ public boolean isTrusted (X509Certificate [] chain , String authType ) throws CertificateException {
87
+ return true ;
88
+ }
89
+ }).build ();
101
90
102
- } catch (Exception e ) {
103
- throw new ClientException ("SDK.InitFailed" , "Init https with SSL certs ignore failed" , e );
104
- }
105
- } else {
91
+ SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory (sslContext ,
92
+ NoopHostnameVerifier .INSTANCE );
93
+
94
+ socketFactoryRegistryBuilder .register ("https" , connectionFactory );
95
+
96
+ } catch (Exception e ) {
97
+ throw new ClientException ("SDK.InitFailed" , "Init https with SSL certs ignore failed" , e );
98
+ }
99
+
100
+ // override default https connector if possible
101
+ if (!config .isIgnoreSSLCerts ()) {
106
102
if (config .getSslSocketFactory () != null ) {
107
103
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory (config
108
104
.getSslSocketFactory (), config .getHostnameVerifier ());
@@ -122,6 +118,7 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif
122
118
}
123
119
}
124
120
121
+
125
122
// connPool
126
123
connectionManager = new PoolingHttpClientConnectionManager (socketFactoryRegistryBuilder .build ());
127
124
connectionManager .setMaxTotal (config .getMaxRequests ());
@@ -167,9 +164,8 @@ private HttpUriRequest parseToHttpRequest(HttpRequest apiReq) throws IOException
167
164
String contentType = apiReq .getHeaderValue (CONTENT_TYPE );
168
165
if (StringUtils .isEmpty (contentType )) {
169
166
contentType = apiReq .getContentTypeValue (apiReq .getHttpContentType (), apiReq .getSysEncoding ());
170
- } else {
171
- bodyBuilder .setContentType (ContentType .parse (contentType ));
172
167
}
168
+ bodyBuilder .setContentType (ContentType .parse (contentType ));
173
169
bodyBuilder .setBinary (apiReq .getHttpContent ());
174
170
builder .setEntity (bodyBuilder .build ());
175
171
}
@@ -183,6 +179,7 @@ private HttpUriRequest parseToHttpRequest(HttpRequest apiReq) throws IOException
183
179
builder .addHeader (entry .getKey (), entry .getValue ());
184
180
}
185
181
182
+
186
183
return builder .build ();
187
184
}
188
185
@@ -191,8 +188,8 @@ private com.aliyuncs.http.HttpResponse parseToHttpResponse(HttpResponse httpResp
191
188
192
189
// status code
193
190
result .setStatus (httpResponse .getStatusLine ().getStatusCode ());
194
-
195
- if (httpResponse .getEntity () != null ) {
191
+ if (( httpResponse . getEntity () != null &&
192
+ (httpResponse .getEntity (). getContentLength () > 0 || httpResponse . getEntity (). isChunked ())) ) {
196
193
// content type
197
194
Header contentTypeHeader = httpResponse .getEntity ().getContentType ();
198
195
ContentType contentType = ContentType .parse (contentTypeHeader .getValue ());
@@ -206,13 +203,6 @@ private com.aliyuncs.http.HttpResponse parseToHttpResponse(HttpResponse httpResp
206
203
207
204
// body
208
205
result .setHttpContent (EntityUtils .toByteArray (httpResponse .getEntity ()), charset , formatType );
209
- } else {
210
- Header contentTypeHeader = httpResponse .getFirstHeader ("Content-Type" );
211
- if (contentTypeHeader != null ) {
212
- ContentType contentType = ContentType .parse (contentTypeHeader .getValue ());
213
- FormatType formatType = FormatType .mapAcceptToFormat (contentType .getMimeType ());
214
- result .setHttpContentType (formatType );
215
- }
216
206
}
217
207
218
208
// headers
@@ -237,7 +227,7 @@ public final com.aliyuncs.http.HttpResponse syncInvoke(HttpRequest apiRequest) t
237
227
238
228
@ Override
239
229
public final Future <com .aliyuncs .http .HttpResponse > asyncInvoke (final HttpRequest apiRequest ,
240
- final CallBack callback ) {
230
+ final CallBack callback ) {
241
231
return executorService .submit (new Callable <com .aliyuncs .http .HttpResponse >() {
242
232
@ Override
243
233
public com .aliyuncs .http .HttpResponse call () throws Exception {
0 commit comments