in aliyun-java-sdk-core/src/main/java/com/aliyuncs/RpcAcsRequest.java [145:225]
public HttpRequest signRequest(Signer signer, AlibabaCloudCredentials credentials, FormatType format,
ProductDomain domain) throws InvalidKeyException, IllegalStateException,
UnsupportedEncodingException, NoSuchAlgorithmException {
this.resolveSignatureComposer();
Map<String, String> headerMap = new HashMap<String, String>();
Map<String, String> bodyParams = this.getSysBodyParameters();
String hashedRequestPayload = hexEncode(signer != null ? signer.hash("".getBytes("UTF-8")) : null);
if (bodyParams != null && !bodyParams.isEmpty()) {
byte[] data;
if (FormatType.JSON == this.getHttpContentType()) {
data = ParameterHelper.getJsonData(bodyParams);
} else if (FormatType.XML == this.getHttpContentType()) {
data = ParameterHelper.getXmlData(bodyParams);
} else {
// For contentType RAW and Form, the actual data format will be form
data = ParameterHelper.getFormData(bodyParams);
}
this.setHttpContent(data, "UTF-8", null);
hashedRequestPayload = hexEncode(signer != null ? signer.hash(data) : null);
}
Map<String, String> imutableMap = this.composer.refreshSignParameters(this.getSysQueryParameters(), signer, null,
format);
headerMap.putAll(this.getSysHeaders());
headerMap = this.composer.refreshSignParameters(headerMap, signer, null, null);
if (imutableMap.get("RegionId") == null && this.getSysRegionId() != null && !this.getSysRegionId().isEmpty()) {
if ((bodyParams == null || bodyParams.get("RegionId") == null)) {
imutableMap.put("RegionId", getSysRegionId());
}
}
if (null != signer && null != credentials && !(credentials instanceof AnonymousCredentials)) {
String accessKeyId = credentials.getAccessKeyId();
String accessSecret = credentials.getAccessKeySecret();
if (credentials instanceof BasicSessionCredentials) {
String sessionToken = ((BasicSessionCredentials) credentials).getSessionToken();
if (null != sessionToken) {
imutableMap.put("SecurityToken", sessionToken);
headerMap.put("x-acs-security-token", sessionToken);
}
}
if (credentials instanceof BearerTokenCredentials) {
String bearerToken = ((BearerTokenCredentials) credentials).getBearerToken();
if (null != ((BearerTokenCredentials) credentials).getBearerToken()) {
imutableMap.put("BearerToken", bearerToken);
headerMap.put("x-acs-bearer-token", bearerToken);
}
}
if (signer.getContent() != null && hashedRequestPayload != null) {
headerMap.put(signer.getContent(), hashedRequestPayload);
}
imutableMap.put("AccessKeyId", accessKeyId);
if (this.getSysSignatureVersion() == SignatureVersion.V3) {
String strToSign = this.composer.composeStringToSign(this.getSysMethod(), null, signer,
this.getSysQueryParameters(), headerMap, null) + "\n" + hashedRequestPayload;
this.strToSign = strToSign;
strToSign = signer.getSignerName() + "\n" + hexEncode(signer.hash(strToSign.getBytes("UTF-8")));
String signature = signer.signString(strToSign, accessSecret);
headerMap.put("Authorization", this.composer.getAuthorization(signer, accessKeyId, signature)
+ ",SignedHeaders=" + this.getSysSignedHeaders(headerMap));
imutableMap = this.getSysQueryParameters();
} else {
Map<String, String> paramsToSign = new HashMap<String, String>();
paramsToSign.putAll(bodyParams);
paramsToSign.putAll(imutableMap);
String strToSign = this.composer.composeStringToSign(
this.getSysMethod(), null, signer, paramsToSign, null, null);
this.strToSign = strToSign;
String signature;
if (credentials instanceof KeyPairCredentials) {
signature = signer.signString(strToSign, credentials);
} else {
signature = signer.signString(strToSign, accessSecret + "&");
}
imutableMap.put("Signature", this.composer.getAuthorization(signer, accessKeyId, signature));
headerMap.clear();
headerMap.putAll(this.getSysHeaders());
}
}
this.setSysUrl(this.composeUrl(domain.getDomainName(), imutableMap));
this.headers = headerMap;
return this;
}