36
36
import com .aliyuncs .regions .Endpoint ;
37
37
import com .aliyuncs .regions .ProductDomain ;
38
38
import com .aliyuncs .transform .UnmarshallerContext ;
39
+ import com .aliyuncs .utils .HttpsUtils ;
39
40
40
41
import java .io .IOException ;
41
42
import java .io .UnsupportedEncodingException ;
42
43
import java .net .SocketTimeoutException ;
44
+ import java .security .GeneralSecurityException ;
43
45
import java .security .InvalidKeyException ;
44
46
import java .security .NoSuchAlgorithmException ;
45
47
import java .util .ArrayList ;
46
48
import java .util .List ;
47
49
50
+ import javax .net .ssl .SSLException ;
51
+ import javax .net .ssl .SSLSocketFactory ;
52
+
48
53
@ SuppressWarnings ("deprecation" )
49
54
public class DefaultAcsClient implements IAcsClient {
50
55
private int maxRetryNumber = 3 ;
51
56
private boolean autoRetry = true ;
52
57
private IClientProfile clientProfile = null ;
53
58
private AlibabaCloudCredentialsProvider credentialsProvider ;
54
59
60
+ private SSLSocketFactory sslSocketFactory = null ;
61
+
55
62
public DefaultAcsClient () {
56
63
this .clientProfile = DefaultProfile .getProfile ();
64
+ initSslSocketFactory ();
57
65
}
58
66
59
67
public DefaultAcsClient (IClientProfile profile ) {
60
68
this .clientProfile = profile ;
61
69
this .credentialsProvider = new StaticCredentialsProvider (profile );
62
70
this .clientProfile .setCredentialsProvider (this .credentialsProvider );
71
+ initSslSocketFactory ();
63
72
}
64
73
65
74
public DefaultAcsClient (IClientProfile profile , AlibabaCloudCredentials credentials ) {
66
75
this .clientProfile = profile ;
67
76
this .credentialsProvider = new StaticCredentialsProvider (credentials );
68
77
this .clientProfile .setCredentialsProvider (this .credentialsProvider );
78
+ initSslSocketFactory ();
69
79
}
70
80
71
81
public DefaultAcsClient (IClientProfile profile , AlibabaCloudCredentialsProvider credentialsProvider ) {
72
82
this .clientProfile = profile ;
73
83
this .credentialsProvider = credentialsProvider ;
74
84
this .clientProfile .setCredentialsProvider (this .credentialsProvider );
85
+ initSslSocketFactory ();
86
+ }
87
+
88
+ private void initSslSocketFactory (){
89
+ try {
90
+ this .sslSocketFactory = HttpsUtils .buildJavaSSLSocketFactory (clientProfile .getCertPath ());
91
+ }catch (SSLException e ){
92
+ // keep exceptions for keep compatible
93
+ System .err .println ("buildSSLSocketFactory failed" + e .toString ());
94
+ }
75
95
}
76
96
77
97
@ Override
@@ -146,18 +166,18 @@ public <T extends AcsResponse> T getAcsResponse(AcsRequest<T> request, String re
146
166
HttpResponse baseResponse = this .doAction (request , regionId , credential );
147
167
return parseAcsResponse (request .getResponseClass (), baseResponse );
148
168
}
149
-
169
+
150
170
@ SuppressWarnings ("unchecked" )
151
171
@ Override
152
- public CommonResponse getCommonResponse (CommonRequest request )
153
- throws ServerException , ClientException {
172
+ public CommonResponse getCommonResponse (CommonRequest request )
173
+ throws ServerException , ClientException {
154
174
HttpResponse baseResponse = this .doAction (request .buildRequest ());
155
175
String stringContent = getResponseContent (baseResponse );
156
176
CommonResponse response = new CommonResponse ();
157
177
response .setData (stringContent );
158
178
response .setHttpStatus (baseResponse .getStatus ());
159
179
response .setHttpResponse (baseResponse );
160
-
180
+
161
181
return response ;
162
182
}
163
183
@@ -218,12 +238,12 @@ request, autoRetry, maxRetryNumber, regionId, new LegacyCredentials(credential),
218
238
);
219
239
}
220
240
221
- private <T extends AcsResponse > HttpResponse doAction (AcsRequest <T > request ,
222
- boolean autoRetry , int maxRetryNumber ,
223
- String regionId ,
224
- AlibabaCloudCredentials credentials ,
225
- Signer signer , FormatType format ,
226
- List <Endpoint > endpoints )
241
+ private <T extends AcsResponse > HttpResponse doAction (AcsRequest <T > request ,
242
+ boolean autoRetry , int maxRetryNumber ,
243
+ String regionId ,
244
+ AlibabaCloudCredentials credentials ,
245
+ Signer signer , FormatType format ,
246
+ List <Endpoint > endpoints )
227
247
throws ClientException , ServerException {
228
248
229
249
try {
@@ -242,11 +262,12 @@ private <T extends AcsResponse> HttpResponse doAction(AcsRequest<T> request,
242
262
}
243
263
244
264
boolean shouldRetry = true ;
245
- for (int retryTimes = 0 ; shouldRetry ; retryTimes ++) {
265
+ for (int retryTimes = 0 ; shouldRetry ; retryTimes ++) {
246
266
247
267
shouldRetry = autoRetry && retryTimes < maxRetryNumber ;
248
268
249
269
HttpRequest httpRequest = request .signRequest (signer , credentials , format , domain );
270
+ httpRequest .setSslSocketFactory (this .sslSocketFactory );
250
271
251
272
HttpResponse response ;
252
273
response = HttpResponse .getResponse (httpRequest );
@@ -274,6 +295,8 @@ private <T extends AcsResponse> HttpResponse doAction(AcsRequest<T> request,
274
295
throw new ClientException ("SDK.ServerUnreachable" , "Server unreachable: " + exp .toString ());
275
296
} catch (NoSuchAlgorithmException exp ) {
276
297
throw new ClientException ("SDK.InvalidMD5Algorithm" , "MD5 hash is not supported by client side." );
298
+ } catch (GeneralSecurityException exp ) {
299
+ throw new ClientException ("SDK.SecureConnectorError" , "Send request with specific SecureConnector failed: " + exp .toString ());
277
300
}
278
301
279
302
return null ;
@@ -290,14 +313,14 @@ private <T extends AcsResponse> T readResponse(Class<T> clasz, HttpResponse http
290
313
} catch (Exception e ) {
291
314
throw new ClientException ("SDK.InvalidResponseClass" , "Unable to allocate " + clasz .getName () + " class" );
292
315
}
293
-
316
+
294
317
String responseEndpoint = clasz .getName ().substring (clasz .getName ().lastIndexOf ("." ) + 1 );
295
318
if (response .checkShowJsonItemName ()) {
296
319
context .setResponseMap (reader .read (stringContent , responseEndpoint ));
297
320
} else {
298
- context .setResponseMap (reader .readForHideArrayItem (stringContent , responseEndpoint ));
321
+ context .setResponseMap (reader .readForHideArrayItem (stringContent , responseEndpoint ));
299
322
}
300
-
323
+
301
324
context .setHttpResponse (httpResponse );
302
325
response .getInstance (context );
303
326
return response ;
0 commit comments