-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathAbstractClient.java
More file actions
868 lines (800 loc) · 37.2 KB
/
AbstractClient.java
File metadata and controls
868 lines (800 loc) · 37.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
/*
* Copyright (c) 2018 THL A29 Limited, a Tencent company. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.tencentcloudapi.common;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.http.HttpConnection;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import okhttp3.*;
import okhttp3.Headers.Builder;
import javax.crypto.Mac;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.*;
public abstract class AbstractClient {
public static final int HTTP_RSP_OK = 200;
public static final String SDK_VERSION = "SDK_JAVA_3.1.1086";
public Gson gson;
private Credential credential;
private ClientProfile profile;
private String endpoint;
private String service;
private String region;
private String path;
private String sdkVersion;
private String apiVersion;
private TCLog log;
private HttpConnection httpConnection;
private CircuitBreaker regionBreaker;
public AbstractClient(String endpoint, String version, Credential credential, String region) {
this(endpoint, version, credential, region, new ClientProfile());
}
public AbstractClient(
String endpoint,
String version,
Credential credential,
String region,
ClientProfile profile) {
this.credential = credential;
this.profile = profile;
this.endpoint = endpoint;
this.service = endpoint.split("\\.")[0];
this.region = region;
this.path = "/";
this.sdkVersion = AbstractClient.SDK_VERSION;
this.apiVersion = version;
this.gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
this.log = new TCLog(getClass().getName(), profile.isDebug());
this.httpConnection = new HttpConnection(
this.profile.getHttpProfile().getConnTimeout(),
this.profile.getHttpProfile().getReadTimeout(),
this.profile.getHttpProfile().getWriteTimeout()
);
this.httpConnection.addInterceptors(this.log);
this.trySetProxy(this.httpConnection);
this.trySetSSLSocketFactory(this.httpConnection);
this.trySetRegionBreaker();
this.trySetHostnameVerifier(this.httpConnection);
warmup();
}
public String getRegion() {
return this.region;
}
public void setRegion(String region) {
this.region = region;
}
public ClientProfile getClientProfile() {
return this.profile;
}
public void setClientProfile(ClientProfile profile) {
this.profile = profile;
}
public Credential getCredential() {
return this.credential;
}
public void setCredential(Credential credential) {
this.credential = credential;
}
/**
* Use post/json with tc3-hmac-sha256 signature to call any action. Ignore request method and
* signature method defined in profile.
*
* @param action Name of action to be called.
* @param jsonPayload Parameters of action serialized in json string format.
* @return Raw response from API if request succeeded, otherwise an exception will be raised
* instead of raw response
* @throws TencentCloudSDKException
*/
public String call(String action, String jsonPayload) throws TencentCloudSDKException {
HashMap<String, String> headers = this.getHeaders();
headers.put("X-TC-Action", action);
headers.put("Content-Type", "application/json; charset=utf-8");
byte[] requestPayload = jsonPayload.getBytes(StandardCharsets.UTF_8);
String authorization = this.getAuthorization(headers, requestPayload);
headers.put("Authorization", authorization);
String url = this.profile.getHttpProfile().getProtocol() + this.getEndpoint() + this.path;
return this.getResponseBody(url, headers, requestPayload);
}
/**
* Use post application/octet-stream with tc3-hmac-sha256 signature to call specific action.
* Ignore request method and signature method defined in profile.
*
* @param action Name of action to be called.
* @param headers Parameters of the action, will be put in http header.
* @param body octet-stream binary body.
* @return Raw response from API if request succeeded, otherwise an exception will be raised
* instead of raw response
* @throws TencentCloudSDKException
*/
public String callOctetStream(String action, HashMap<String, String> headers, byte[] body)
throws TencentCloudSDKException {
headers.putAll(this.getHeaders());
headers.put("X-TC-Action", action);
headers.put("Content-Type", "application/octet-stream; charset=utf-8");
String authorization = this.getAuthorization(headers, body);
headers.put("Authorization", authorization);
String url = this.profile.getHttpProfile().getProtocol() + this.getEndpoint() + this.path;
return this.getResponseBody(url, headers, body);
}
private HashMap<String, String> getHeaders() {
HashMap<String, String> headers = new HashMap<String, String>();
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
headers.put("X-TC-Timestamp", timestamp);
headers.put("X-TC-Version", this.apiVersion);
headers.put("X-TC-Region", this.getRegion());
headers.put("X-TC-RequestClient", SDK_VERSION);
headers.put("Host", this.getEndpoint());
String token = this.credential.getToken();
if (token != null && !token.isEmpty()) {
headers.put("X-TC-Token", token);
}
if (this.profile.isUnsignedPayload()) {
headers.put("X-TC-Content-SHA256", "UNSIGNED-PAYLOAD");
}
if (null != this.profile.getLanguage()) {
headers.put("X-TC-Language", this.profile.getLanguage().getValue());
}
return headers;
}
private String getAuthorization(HashMap<String, String> headers, byte[] body)
throws TencentCloudSDKException {
String endpoint = this.getEndpoint();
// always use post tc3-hmac-sha256 signature process
// okhttp always set charset even we don't specify it,
// to ensure signature be correct, we have to set it here as well.
String contentType = headers.get("Content-Type");
byte[] requestPayload = body;
String canonicalUri = "/";
String canonicalQueryString = "";
String canonicalHeaders = "content-type:" + contentType + "\nhost:" + endpoint + "\n";
String signedHeaders = "content-type;host";
String hashedRequestPayload = "";
if (this.profile.isUnsignedPayload()) {
hashedRequestPayload = Sign.sha256Hex("UNSIGNED-PAYLOAD".getBytes(StandardCharsets.UTF_8));
} else {
hashedRequestPayload = Sign.sha256Hex(requestPayload);
}
String canonicalRequest =
HttpProfile.REQ_POST
+ "\n"
+ canonicalUri
+ "\n"
+ canonicalQueryString
+ "\n"
+ canonicalHeaders
+ "\n"
+ signedHeaders
+ "\n"
+ hashedRequestPayload;
String timestamp = headers.get("X-TC-Timestamp");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String date = sdf.format(new Date(Long.valueOf(timestamp + "000")));
String service = endpoint.split("\\.")[0];
String credentialScope = date + "/" + service + "/" + "tc3_request";
String hashedCanonicalRequest =
Sign.sha256Hex(canonicalRequest.getBytes(StandardCharsets.UTF_8));
String stringToSign =
"TC3-HMAC-SHA256\n" + timestamp + "\n" + credentialScope + "\n" + hashedCanonicalRequest;
String secretId = this.credential.getSecretId();
String secretKey = this.credential.getSecretKey();
byte[] secretDate = Sign.hmac256(("TC3" + secretKey).getBytes(StandardCharsets.UTF_8), date);
byte[] secretService = Sign.hmac256(secretDate, service);
byte[] secretSigning = Sign.hmac256(secretService, "tc3_request");
String signature =
DatatypeConverter.printHexBinary(Sign.hmac256(secretSigning, stringToSign)).toLowerCase();
return "TC3-HMAC-SHA256 "
+ "Credential="
+ secretId
+ "/"
+ credentialScope
+ ", "
+ "SignedHeaders="
+ signedHeaders
+ ", "
+ "Signature="
+ signature;
}
private String getResponseBody(String url, HashMap<String, String> headers, byte[] body)
throws TencentCloudSDKException {
Builder hb = new Headers.Builder();
for (String key : headers.keySet()) {
hb.add(key, headers.get(key));
}
Response resp = null;
try {
resp = this.httpConnection.postRequest(url, body, hb.build());
} catch (IOException e) {
throw new TencentCloudSDKException(e.getClass().getName() + "-" + e.getMessage(), e);
}
if (resp.code() != AbstractClient.HTTP_RSP_OK) {
String msg = "response code is " + resp.code() + ", not 200";
log.info(msg);
throw new TencentCloudSDKException(msg, "", "ServerSideError");
}
String respbody = null;
try {
respbody = resp.body().string();
} catch (IOException e) {
String msg =
"Cannot transfer response body to string, because Content-Length is too large, or Content-Length and stream length disagree.";
log.info(msg);
throw new TencentCloudSDKException(msg, e);
}
JsonResponseModel<JsonResponseErrModel> errResp = null;
try {
Type errType = new TypeToken<JsonResponseModel<JsonResponseErrModel>>() {
}.getType();
errResp = gson.fromJson(respbody, errType);
} catch (JsonSyntaxException e) {
String msg = "json is not a valid representation for an object of type";
log.info(msg);
throw new TencentCloudSDKException(msg, e);
}
if (errResp.response.error != null) {
throw new TencentCloudSDKException(
errResp.response.error.message, errResp.response.requestId, errResp.response.error.code);
}
return respbody;
}
private void trySetProxy(HttpConnection conn) {
String host = this.profile.getHttpProfile().getProxyHost();
int port = this.profile.getHttpProfile().getProxyPort();
if (host == null || host.isEmpty()) {
return;
}
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
conn.setProxy(proxy);
final String username = this.profile.getHttpProfile().getProxyUsername();
final String password = this.profile.getHttpProfile().getProxyPassword();
if (username == null || username.isEmpty()) {
return;
}
conn.setProxyAuthenticator(
new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
String credential = Credentials.basic(username, password);
return response
.request()
.newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
});
}
private void trySetSSLSocketFactory(HttpConnection conn) {
SSLSocketFactory sslSocketFactory = this.profile.getHttpProfile().getSslSocketFactory();
X509TrustManager trustManager = this.profile.getHttpProfile().getX509TrustManager();
if (sslSocketFactory != null) {
if (trustManager != null) {
this.httpConnection.setSSLSocketFactory(sslSocketFactory, trustManager);
} else {
this.httpConnection.setSSLSocketFactory(sslSocketFactory);
}
}
}
private void trySetHostnameVerifier(HttpConnection conn) {
HostnameVerifier hostnameVerifier = this.profile.getHttpProfile().getHostnameVerifier();
if (hostnameVerifier != null) {
this.httpConnection.setHostnameVerifier(hostnameVerifier);
}
}
private void trySetRegionBreaker() {
String ep = profile.getBackupEndpoint();
if (ep != null && !ep.isEmpty()) {
this.regionBreaker = new CircuitBreaker();
}
}
protected String internalRequest(AbstractModel request, String actionName)
throws TencentCloudSDKException {
CircuitBreaker.Token breakerToken = null;
if (regionBreaker != null) {
breakerToken = regionBreaker.allow();
if (!breakerToken.allowed) {
endpoint = service + "." + profile.getBackupEndpoint();
}
}
Response okRsp;
try {
okRsp = internalRequestRaw(request, actionName);
} catch (IOException e) {
// network failure, consider region failure
if (breakerToken != null) {
breakerToken.report(false);
}
throw new TencentCloudSDKException("", e);
}
String strResp;
try {
strResp = okRsp.body().string();
} catch (IOException e) {
String msg = "Cannot transfer response body to string, because Content-Length is too large, or Content-Length and stream length disagree.";
log.info(msg);
throw new TencentCloudSDKException(msg, e);
}
JsonResponseModel<JsonResponseErrModel> errResp;
try {
Type errType = new TypeToken<JsonResponseModel<JsonResponseErrModel>>() {
}.getType();
errResp = gson.fromJson(strResp, errType);
} catch (JsonSyntaxException e) {
String msg = "json is not a valid representation for an object of type";
log.info(msg);
throw new TencentCloudSDKException(msg, e);
}
if (errResp.response.error != null) {
if (breakerToken != null) {
JsonResponseErrModel error = errResp.response;
boolean regionOk = error.requestId != null
&& !error.requestId.isEmpty()
&& error.error.code != null
&& !error.error.code.equals("InternalError");
breakerToken.report(regionOk);
}
throw new TencentCloudSDKException(
errResp.response.error.message,
errResp.response.requestId,
errResp.response.error.code);
}
return strResp;
}
protected <T> T internalRequest(AbstractModel request, String actionName, Class<T> typeOfT)
throws TencentCloudSDKException {
CircuitBreaker.Token breakerToken = null;
if (regionBreaker != null) {
breakerToken = regionBreaker.allow();
if (!breakerToken.allowed) {
endpoint = service + "." + profile.getBackupEndpoint();
}
}
try {
Response resp = internalRequestRaw(request, actionName);
if (Objects.equals(resp.header("Content-Type"), "text/event-stream")) {
return processResponseSSE(resp, typeOfT, breakerToken);
}
return processResponseJson(resp, typeOfT, breakerToken);
} catch (IOException e) {
// network failure, consider region failure
if (breakerToken != null) {
breakerToken.report(false);
}
throw new TencentCloudSDKException("", e);
}
}
protected <T> T processResponseSSE(Response resp, Class<T> typeOfT, CircuitBreaker.Token breakerToken) throws TencentCloudSDKException {
SSEResponseModel responseModel;
try {
responseModel = (SSEResponseModel) typeOfT.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new TencentCloudSDKException("", e);
}
responseModel.setRequestId(resp.header("X-TC-RequestId"));
responseModel.setToken(breakerToken);
responseModel.setResponse(resp);
return (T) responseModel;
}
protected <T> T processResponseJson(Response resp, Class<T> typeOfT, CircuitBreaker.Token breakerToken) throws TencentCloudSDKException {
String body;
try {
body = resp.body().string();
} catch (IOException e) {
String msg = "Cannot transfer response body to string, because Content-Length is too large, or Content-Length and stream length disagree.";
log.info(msg);
throw new TencentCloudSDKException(msg, e);
}
JsonResponseModel<JsonResponseErrModel> errResp;
try {
Type errType = new TypeToken<JsonResponseModel<JsonResponseErrModel>>() {
}.getType();
errResp = gson.fromJson(body, errType);
} catch (JsonSyntaxException e) {
String msg = "json is not a valid representation for an object of type";
log.info(msg);
throw new TencentCloudSDKException(msg, e);
}
if (errResp.response.error != null) {
if (breakerToken != null) {
JsonResponseErrModel error = errResp.response;
boolean regionOk = error.requestId != null
&& !error.requestId.isEmpty()
&& error.error.code != null
&& !error.error.code.equals("InternalError");
breakerToken.report(regionOk);
}
throw new TencentCloudSDKException(
errResp.response.error.message,
errResp.response.requestId,
errResp.response.error.code);
}
Type type = TypeToken.getParameterized(JsonResponseModel.class, typeOfT).getType();
return ((JsonResponseModel<T>) gson.fromJson(body, type)).response;
}
protected Response internalRequestRaw(AbstractModel request, String actionName)
throws TencentCloudSDKException, IOException {
Response okRsp = null;
String endpoint = this.getEndpoint();
String[] binaryParams = request.getBinaryParams();
String sm = this.profile.getSignMethod();
String reqMethod = this.profile.getHttpProfile().getReqMethod();
// currently, customized params only can be supported via post json tc3-hmac-sha256
HashMap<String, Object> customizedParams = request.any();
if (customizedParams.size() > 0) {
if (binaryParams.length > 0) {
throw new TencentCloudSDKException(
"WrongUsage: Cannot post multipart with customized parameters.");
}
if (sm.equals(ClientProfile.SIGN_SHA1) || sm.equals(ClientProfile.SIGN_SHA256)) {
throw new TencentCloudSDKException(
"WrongUsage: Cannot use HmacSHA1 or HmacSHA256 with customized parameters.");
}
if (reqMethod.equals(HttpProfile.REQ_GET)) {
throw new TencentCloudSDKException(
"WrongUsage: Cannot use get method with customized parameters.");
}
}
if (binaryParams.length > 0 || sm.equals(ClientProfile.SIGN_TC3_256)) {
okRsp = doRequestWithTC3(endpoint, request, actionName);
} else if (sm.equals(ClientProfile.SIGN_SHA1) || sm.equals(ClientProfile.SIGN_SHA256)) {
okRsp = doRequest(endpoint, request, actionName);
} else {
throw new TencentCloudSDKException(
"Signature method " + sm + " is invalid or not supported yet.");
}
if (okRsp.code() != AbstractClient.HTTP_RSP_OK) {
String msg = "response code is " + okRsp.code() + ", not 200";
log.info(msg);
throw new TencentCloudSDKException(msg, "", "ServerSideError");
}
return okRsp;
}
private Response doRequest(String endpoint, AbstractModel request, String action)
throws TencentCloudSDKException, IOException {
HashMap<String, String> param = new HashMap<String, String>();
request.toMap(param, "");
String strParam = this.formatRequestData(action, param);
String reqMethod = this.profile.getHttpProfile().getReqMethod();
String protocol = this.profile.getHttpProfile().getProtocol();
String url = protocol + endpoint + this.path;
String apigwEndpoint = this.profile.getHttpProfile().getApigwEndpoint();
if (null != apigwEndpoint) {
url = protocol + apigwEndpoint;
}
if (reqMethod.equals(HttpProfile.REQ_GET)) {
return this.httpConnection.getRequest(url + "?" + strParam);
} else if (reqMethod.equals(HttpProfile.REQ_POST)) {
return this.httpConnection.postRequest(url, strParam);
} else {
throw new TencentCloudSDKException("Method only support (GET, POST)");
}
}
private Response doRequestWithTC3(String endpoint, AbstractModel request, String action)
throws TencentCloudSDKException, IOException {
String httpRequestMethod = this.profile.getHttpProfile().getReqMethod();
if (httpRequestMethod == null) {
throw new TencentCloudSDKException(
"Request method should not be null, can only be GET or POST");
}
String contentType = "application/x-www-form-urlencoded";
byte[] requestPayload = "".getBytes(StandardCharsets.UTF_8);
HashMap<String, String> params = new HashMap<String, String>();
request.toMap(params, "");
String[] binaryParams = request.getBinaryParams();
if (binaryParams.length > 0) {
httpRequestMethod = HttpProfile.REQ_POST;
String boundary = UUID.randomUUID().toString();
// okhttp always set charset even we don't specify it,
// to ensure signature be correct, we have to set it here as well.
contentType = "multipart/form-data; charset=utf-8" + "; boundary=" + boundary;
try {
requestPayload = getMultipartPayload(request, boundary);
} catch (Exception e) {
throw new TencentCloudSDKException("Failed to generate multipart.", e);
}
} else if (httpRequestMethod.equals(HttpProfile.REQ_POST)) {
requestPayload = AbstractModel.toJsonString(request).getBytes(StandardCharsets.UTF_8);
// okhttp always set charset even we don't specify it,
// to ensure signature be correct, we have to set it here as well.
contentType = "application/json; charset=utf-8";
}
String canonicalUri = "/";
String canonicalQueryString = this.getCanonicalQueryString(params, httpRequestMethod);
String canonicalHeaders = "content-type:" + contentType + "\nhost:" + endpoint + "\n";
String signedHeaders = "content-type;host";
String hashedRequestPayload = "";
if (this.profile.isUnsignedPayload()) {
hashedRequestPayload = Sign.sha256Hex("UNSIGNED-PAYLOAD".getBytes(StandardCharsets.UTF_8));
} else {
hashedRequestPayload = Sign.sha256Hex(requestPayload);
}
String canonicalRequest =
httpRequestMethod
+ "\n"
+ canonicalUri
+ "\n"
+ canonicalQueryString
+ "\n"
+ canonicalHeaders
+ "\n"
+ signedHeaders
+ "\n"
+ hashedRequestPayload;
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String date = sdf.format(new Date(Long.valueOf(timestamp + "000")));
String service = endpoint.split("\\.")[0];
String credentialScope = date + "/" + service + "/" + "tc3_request";
String hashedCanonicalRequest =
Sign.sha256Hex(canonicalRequest.getBytes(StandardCharsets.UTF_8));
String stringToSign =
"TC3-HMAC-SHA256\n" + timestamp + "\n" + credentialScope + "\n" + hashedCanonicalRequest;
boolean skipSign = request.getSkipSign();
String authorization = "";
if (skipSign) {
authorization = "SKIP";
} else {
String secretId = this.credential.getSecretId();
String secretKey = this.credential.getSecretKey();
byte[] secretDate = Sign.hmac256(("TC3" + secretKey).getBytes(StandardCharsets.UTF_8), date);
byte[] secretService = Sign.hmac256(secretDate, service);
byte[] secretSigning = Sign.hmac256(secretService, "tc3_request");
String signature =
DatatypeConverter.printHexBinary(Sign.hmac256(secretSigning, stringToSign)).toLowerCase();
authorization =
"TC3-HMAC-SHA256 "
+ "Credential="
+ secretId
+ "/"
+ credentialScope
+ ", "
+ "SignedHeaders="
+ signedHeaders
+ ", "
+ "Signature="
+ signature;
}
Builder hb = new Headers.Builder();
hb.add("Content-Type", contentType)
.add("Host", endpoint)
.add("Authorization", authorization)
.add("X-TC-Action", action)
.add("X-TC-Timestamp", timestamp)
.add("X-TC-Version", this.apiVersion)
.add("X-TC-RequestClient", SDK_VERSION);
if (null != request.GetHeader()) {
for (Map.Entry<String, String> entry : request.GetHeader().entrySet()) {
hb.add(entry.getKey(), entry.getValue());
}
}
if (null != this.getRegion()) {
hb.add("X-TC-Region", this.getRegion());
}
String token = this.credential.getToken();
if (token != null && !token.isEmpty()) {
hb.add("X-TC-Token", token);
}
if (this.profile.isUnsignedPayload()) {
hb.add("X-TC-Content-SHA256", "UNSIGNED-PAYLOAD");
}
if (null != this.profile.getLanguage()) {
hb.add("X-TC-Language", this.profile.getLanguage().getValue());
}
String protocol = this.profile.getHttpProfile().getProtocol();
String url = protocol + endpoint + this.path;
String apigwEndpoint = this.profile.getHttpProfile().getApigwEndpoint();
if (null != apigwEndpoint) {
url = protocol + apigwEndpoint;
hb.set("Host", apigwEndpoint.contains("/") ? apigwEndpoint.substring(0, apigwEndpoint.indexOf('/')) : apigwEndpoint);
}
Headers headers = hb.build();
if (httpRequestMethod.equals(HttpProfile.REQ_GET)) {
return this.httpConnection.getRequest(url + "?" + canonicalQueryString, headers);
} else if (httpRequestMethod.equals(HttpProfile.REQ_POST)) {
return this.httpConnection.postRequest(url, requestPayload, headers);
} else {
throw new TencentCloudSDKException("Method only support GET, POST");
}
}
private byte[] getMultipartPayload(AbstractModel request, String boundary) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String[] binaryParams = request.getBinaryParams();
for (Map.Entry<String, byte[]> entry : request.getMultipartRequestParams().entrySet()) {
baos.write("--".getBytes(StandardCharsets.UTF_8));
baos.write(boundary.getBytes(StandardCharsets.UTF_8));
baos.write("\r\n".getBytes(StandardCharsets.UTF_8));
baos.write("Content-Disposition: form-data; name=\"".getBytes(StandardCharsets.UTF_8));
baos.write(entry.getKey().getBytes(StandardCharsets.UTF_8));
if (Arrays.asList(binaryParams).contains(entry.getKey())) {
baos.write("\"; filename=\"".getBytes(StandardCharsets.UTF_8));
baos.write(entry.getKey().getBytes(StandardCharsets.UTF_8));
baos.write("\"\r\n".getBytes(StandardCharsets.UTF_8));
} else {
baos.write("\"\r\n".getBytes(StandardCharsets.UTF_8));
}
baos.write("\r\n".getBytes(StandardCharsets.UTF_8));
baos.write(entry.getValue());
baos.write("\r\n".getBytes(StandardCharsets.UTF_8));
}
if (baos.size() != 0) {
baos.write("--".getBytes(StandardCharsets.UTF_8));
baos.write(boundary.getBytes(StandardCharsets.UTF_8));
baos.write("--\r\n".getBytes(StandardCharsets.UTF_8));
}
byte[] bytes = baos.toByteArray();
baos.close();
return bytes;
}
private String getCanonicalQueryString(HashMap<String, String> params, String method)
throws TencentCloudSDKException {
if (method != null && method.equals(HttpProfile.REQ_POST)) {
return "";
}
StringBuilder queryString = new StringBuilder("");
for (Map.Entry<String, String> entry : params.entrySet()) {
String v;
try {
v = URLEncoder.encode(entry.getValue(), "UTF8");
} catch (UnsupportedEncodingException e) {
throw new TencentCloudSDKException("UTF8 is not supported.", e);
}
queryString.append("&").append(entry.getKey()).append("=").append(v);
}
if (queryString.length() == 0) {
return "";
} else {
return queryString.toString().substring(1);
}
}
private String formatRequestData(String action, Map<String, String> param)
throws TencentCloudSDKException {
param.put("Action", action);
param.put("RequestClient", this.sdkVersion);
param.put("Nonce", String.valueOf(Math.abs(new SecureRandom().nextInt())));
param.put("Timestamp", String.valueOf(System.currentTimeMillis() / 1000));
param.put("Version", this.apiVersion);
if (this.credential.getSecretId() != null && (!this.credential.getSecretId().isEmpty())) {
param.put("SecretId", this.credential.getSecretId());
}
if (this.region != null && (!this.region.isEmpty())) {
param.put("Region", this.region);
}
if (this.profile.getSignMethod() != null && (!this.profile.getSignMethod().isEmpty())) {
param.put("SignatureMethod", this.profile.getSignMethod());
}
if (this.credential.getToken() != null && (!this.credential.getToken().isEmpty())) {
param.put("Token", this.credential.getToken());
}
if (null != this.profile.getLanguage()) {
param.put("Language", this.profile.getLanguage().getValue());
}
String endpoint = this.getEndpoint();
String sigInParam =
Sign.makeSignPlainText(
new TreeMap<String, String>(param),
this.profile.getHttpProfile().getReqMethod(),
endpoint,
this.path);
String sigOutParam =
Sign.sign(this.credential.getSecretKey(), sigInParam, this.profile.getSignMethod());
String strParam = "";
try {
for (Map.Entry<String, String> entry : param.entrySet()) {
strParam +=
(URLEncoder.encode(entry.getKey(), "utf-8")
+ "="
+ URLEncoder.encode(entry.getValue(), "utf-8")
+ "&");
}
strParam += ("Signature=" + URLEncoder.encode(sigOutParam, "utf-8"));
} catch (UnsupportedEncodingException e) {
throw new TencentCloudSDKException("", e);
}
return strParam;
}
/**
* warm up, try to avoid unnecessary cost in the first request
*/
private void warmup() {
try {
// it happens in SDK signature process.
// first invoke costs around 250 ms.
Mac.getInstance("HmacSHA1");
Mac.getInstance("HmacSHA256");
// it happens inside okhttp, but I think any https framework/package will do the same.
// first invoke costs around 150 ms.
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, null);
} catch (Exception e) {
// ignore but print message to console
e.printStackTrace();
}
}
private String getEndpoint() {
// in case user has reset endpoint after init this client
if (null != this.profile.getHttpProfile().getEndpoint()) {
return this.profile.getHttpProfile().getEndpoint();
} else {
// protected abstract String getService();
// use this.getService() from overrided subclass will be better
return this.service + "." + this.profile.getHttpProfile().getRootDomain();
}
}
/**
* 请注意购买类接口谨慎调用,可能导致多次购买
* 仅幂等接口推荐使用
*
* @param req
* @param retryTimes
* @throws TencentCloudSDKException
*/
public Object retry(AbstractModel req, int retryTimes) throws TencentCloudSDKException {
if (retryTimes < 0 || retryTimes > 10) {
throw new TencentCloudSDKException("The number of retryTimes supported is 0 to 10.", "", "ClientSideError");
}
Class cls = this.getClass();
String methodName = req.getClass().getSimpleName().replace("Request", "");
Method method;
try {
method = cls.getMethod(methodName, req.getClass());
} catch (NoSuchMethodException e) {
throw new TencentCloudSDKException("ClientSideError", e);
}
do {
try {
return method.invoke(this, req);
} catch (IllegalAccessException e) {
throw new TencentCloudSDKException("ClientSideError", e);
} catch (InvocationTargetException e) {
if (retryTimes == 0) {
throw (TencentCloudSDKException) e.getTargetException();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new TencentCloudSDKException("ClientSideError", e);
}
} while (--retryTimes >= 0);
return null;
}
public CircuitBreaker getRegionBreaker() {
return regionBreaker;
}
public void setRegionBreaker(CircuitBreaker regionBreaker) {
this.regionBreaker = regionBreaker;
}
}