All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyuncs.v5.auth.OssSignatureComposer Maven / Gradle / Ivy

Go to download

Aliyun Open API SDK for Java Copyright (C) Alibaba Cloud Computing All rights reserved. 版权所有 (C)阿里云计算有限公司 http://www.aliyun.com

The newest version!
package com.aliyuncs.v5.auth;

import com.aliyuncs.v5.http.FormatType;
import com.aliyuncs.v5.http.MethodType;
import com.aliyuncs.v5.utils.ParameterHelper;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class OssSignatureComposer extends RoaSignatureComposer {
    private static ISignatureComposer composer = null;

    public static ISignatureComposer getComposer() {
        if (null == composer) {
            composer = new OssSignatureComposer();
        }
        return composer;
    }

    @Override
    public Map refreshSignParameters(Map parameters, Signer signer, String accessKeyId,
            FormatType format) {
        Map immutableMap = new HashMap(parameters);
        immutableMap.put("Date", ParameterHelper.getRFC2616Date(new Date()));
        return immutableMap;
    }

    private String buildQueryString(String uri, Map queries) {
        StringBuilder queryBuilder = new StringBuilder(uri);
        if (0 < queries.size()) {
            queryBuilder.append("?");
        }
        for (Map.Entry e : queries.entrySet()) {
            queryBuilder.append(e.getKey());
            if (null != e.getValue()) {
                queryBuilder.append("=").append(e.getValue());
            }
            queryBuilder.append(QUERY_SEPARATOR);
        }
        String queryString = queryBuilder.toString();
        if (queryString.endsWith(QUERY_SEPARATOR)) {
            queryString = queryString.substring(0, queryString.length() - 1);
        }

        return queryString;
    }

    @Override
    public String composeStringToSign(MethodType method, String uriPattern, Signer signer, Map queries,
            Map headers, Map paths) {
        StringBuilder sb = new StringBuilder();
        sb.append(method).append(HEADER_SEPARATOR);
        if (headers.get("Content-MD5") != null) {
            sb.append(headers.get("Content-MD5"));
        }
        sb.append(HEADER_SEPARATOR);
        if (headers.get("Content-Type") != null) {
            sb.append(headers.get("Content-Type"));
        }
        sb.append(HEADER_SEPARATOR);
        if (headers.get("Date") != null) {
            sb.append(headers.get("Date"));
        }
        sb.append(HEADER_SEPARATOR);
        sb.append(buildCanonicalHeaders(headers, "x-oss-"));
        sb.append(buildQueryString(uriPattern, queries));
        return sb.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy