public void modifyRequest()

in alibabacloud-gateway-sls/java/src/main/java/com/aliyun/gateway/sls/Client.java [45:134]


    public void modifyRequest(com.aliyun.gateway.spi.models.InterceptorContext context, com.aliyun.gateway.spi.models.AttributeMap attributeMap) throws Exception {
        com.aliyun.gateway.spi.models.InterceptorContext.InterceptorContextRequest request = context.request;
        java.util.Map<String, String> hostMap = new java.util.HashMap<>();
        if (!com.aliyun.teautil.Common.isUnset(request.hostMap)) {
            hostMap = request.hostMap;
        }

        String project = hostMap.get("project");
        com.aliyun.gateway.spi.models.InterceptorContext.InterceptorContextConfiguration config = context.configuration;
        com.aliyun.credentials.Client credential = request.credential;
        com.aliyun.credentials.models.CredentialModel credentialModel = credential.getCredential();
        String accessKeyId = credentialModel.accessKeyId;
        String accessKeySecret = credentialModel.accessKeySecret;
        String securityToken = credentialModel.securityToken;
        if (!com.aliyun.teautil.Common.empty(securityToken)) {
            request.headers.put("x-acs-security-token", securityToken);
        }

        String signatureVersion = com.aliyun.teautil.Common.defaultString(request.signatureVersion, "v1");
        String finalCompressType = this.getFinalRequestCompressType(request.action, request.headers);
        String contentHash = "";
        // get body bytes
        byte[] bodyBytes = null;
        if (!com.aliyun.teautil.Common.isUnset(request.body)) {
            if (com.aliyun.darabonbastring.Client.equals(request.reqBodyType, "json") || com.aliyun.darabonbastring.Client.equals(request.reqBodyType, "formData")) {
                request.headers.put("content-type", "application/json");
                String bodyStr = com.aliyun.teautil.Common.toJSONString(request.body);
                bodyBytes = com.aliyun.teautil.Common.toBytes(bodyStr);
            } else if (com.aliyun.darabonbastring.Client.equals(request.reqBodyType, "binary")) {
                // content-type: application/octet-stream
                bodyBytes = com.aliyun.teautil.Common.assertAsBytes(request.body);
            }

        }

        // get body raw size
        String bodyRawSize = "0";
        String rawSizeRef = request.headers.get("x-log-bodyrawsize");
        // for php bug, Argument #1 ($value) could not be passed by reference
        if (!com.aliyun.teautil.Common.isUnset(rawSizeRef)) {
            bodyRawSize = rawSizeRef;
        } else if (!com.aliyun.teautil.Common.isUnset(request.body)) {
            bodyRawSize = "" + com.aliyun.gateway.sls.util.Client.bytesLength(bodyBytes) + "";
        }

        // compress if needed, and set body and hash
        if (!com.aliyun.teautil.Common.isUnset(request.body)) {
            if (!com.aliyun.teautil.Common.empty(finalCompressType)) {
                byte[] compressed = com.aliyun.gateway.sls.util.Client.compress(bodyBytes, finalCompressType);
                bodyBytes = compressed;
            }

            contentHash = this.makeContentHash(bodyBytes, signatureVersion);
            request.stream = Tea.toReadable(bodyBytes);
        }

        String host = this.getHost(config.network, project, config.endpoint);
        request.headers = TeaConverter.merge(String.class,
            TeaConverter.buildMap(
                new TeaPair("accept", "application/json"),
                new TeaPair("host", host),
                new TeaPair("user-agent", request.userAgent),
                new TeaPair("x-log-apiversion", "0.6.0")
            ),
            request.headers
        );
        request.headers.put("x-log-bodyrawsize", bodyRawSize);
        this.setDefaultAcceptEncoding(request.action, request.headers);
        this.buildRequest(context);
        // move param in path to query
        if (com.aliyun.darabonbastring.Client.equals(signatureVersion, "v4")) {
            if (com.aliyun.teautil.Common.empty(contentHash)) {
                contentHash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
            }

            String date = this.getDateISO8601();
            request.headers.put("x-log-date", date);
            request.headers.put("x-log-content-sha256", contentHash);
            request.headers.put("authorization", this.getAuthorizationV4(context, date, contentHash, accessKeyId, accessKeySecret));
            return ;
        }

        if (!com.aliyun.teautil.Common.empty(accessKeyId)) {
            request.headers.put("x-log-signaturemethod", "hmac-sha256");
        }

        request.headers.put("date", com.aliyun.teautil.Common.getDateUTCString());
        request.headers.put("content-md5", contentHash);
        request.headers.put("authorization", this.getAuthorization(request.pathname, request.method, request.query, request.headers, accessKeyId, accessKeySecret));
    }