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

com.ksyun.api.sdk.auth.RpcSignatureComposer Maven / Gradle / Ivy

There is a newer version: 2.1.8
Show newest version
package com.ksyun.api.sdk.auth;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.ksyun.api.sdk.http.FormatType;
import com.ksyun.api.sdk.http.MethodType;
import com.ksyun.api.sdk.utils.ParameterHelper;

public class RpcSignatureComposer implements ISignatureComposer {

	private static ISignatureComposer composer = null;
	private final static String SEPARATOR = "&";
	
	private RpcSignatureComposer() {
		
	}
	
	public Map refreshSignParameters(Map parameters,
			ISigner signer, String accessKeyId, FormatType format) {
		Map immutableMap = new HashMap(parameters);
		immutableMap.put("Timestamp", ParameterHelper.getISO8601Time(null));
		immutableMap.put("SignatureMethod", signer.getSignerName());
		immutableMap.put("SignatureVersion",signer.getSignerVersion());
		immutableMap.put("SignatureNonce", ParameterHelper.getUniqueNonce());
		immutableMap.put("AccessKeyId", accessKeyId);
		if (null != format)
			immutableMap.put("Format", format.toString());
		return immutableMap;
	}

	public String composeStringToSign(MethodType method, String uriPattern,
			ISigner signer, Map queries,
			Map headers, Map paths) {
		
		String[] sortedKeys = queries.keySet().toArray(new String[]{});
        Arrays.sort(sortedKeys);
        StringBuilder canonicalizedQueryString = new StringBuilder();
        try { 
	        for(String key : sortedKeys) {
	            canonicalizedQueryString.append("&")
	            .append(KscURLEncoder.percentEncode(key)).append("=")
	            .append(KscURLEncoder.percentEncode(queries.get(key)));
	        }
	
	        StringBuilder stringToSign = new StringBuilder();
	        stringToSign.append(method.toString());
	        stringToSign.append(SEPARATOR);
	        stringToSign.append(KscURLEncoder.percentEncode("/"));
	        stringToSign.append(SEPARATOR);
	        stringToSign.append(KscURLEncoder.percentEncode(
	                canonicalizedQueryString.toString().substring(1)));
	        
	        return stringToSign.toString();
        } catch (UnsupportedEncodingException exp) {
        	throw new RuntimeException("UTF-8 encoding is not supported.");
        }
        
	}
	
	public static ISignatureComposer getComposer() {
		if (null == composer)
			composer = new RpcSignatureComposer();
		return composer;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy