Search in sources :

Example 1 with COSSessionCredentials

use of com.qcloud.cos.auth.COSSessionCredentials in project cos-java-sdk-v5 by tencentyun.

the class COSClient method generatePresignedUrl.

@Override
public URL generatePresignedUrl(GeneratePresignedUrlRequest req, Boolean signHost) throws CosClientException {
    rejectNull(clientConfig.getRegion(), "region is null, region in clientConfig must be specified when generating a pre-signed URL");
    rejectNull(req, "The request parameter must be specified when generating a pre-signed URL");
    req.rejectIllegalArguments();
    final String bucketName = req.getBucketName();
    final String key = req.getKey();
    if (req.getExpiration() == null) {
        req.setExpiration(new Date(System.currentTimeMillis() + this.clientConfig.getSignExpired() * 1000));
    }
    HttpMethodName httpMethod = req.getMethod();
    CosHttpRequest<GeneratePresignedUrlRequest> request = createRequest(bucketName, key, req, httpMethod);
    addParameterIfNotNull(request, "versionId", req.getVersionId());
    for (Entry<String, String> entry : req.getRequestParameters().entrySet()) {
        request.addParameter(entry.getKey(), entry.getValue());
    }
    addHeaderIfNotNull(request, Headers.CONTENT_TYPE, req.getContentType());
    addHeaderIfNotNull(request, Headers.CONTENT_MD5, req.getContentMd5());
    // Custom headers that open up the possibility of supporting unexpected
    // cases.
    Map<String, String> customHeaders = req.getCustomRequestHeaders();
    if (customHeaders != null) {
        for (Map.Entry<String, String> e : customHeaders.entrySet()) {
            request.addHeader(e.getKey(), e.getValue());
        }
    }
    addResponseHeaderParameters(request, req.getResponseHeaders());
    COSSigner cosSigner = new COSSigner();
    COSCredentials cred = fetchCredential();
    String authStr = cosSigner.buildAuthorizationStr(request.getHttpMethod(), request.getResourcePath(), request.getHeaders(), request.getParameters(), cred, req.getExpiration(), signHost);
    StringBuilder strBuilder = new StringBuilder();
    strBuilder.append(clientConfig.getHttpProtocol().toString()).append("://");
    strBuilder.append(clientConfig.getEndpointBuilder().buildGeneralApiEndpoint(formatBucket(bucketName, cred.getCOSAppId())));
    strBuilder.append(UrlEncoderUtils.encodeUrlPath(formatKey(key)));
    boolean hasAppendFirstParameter = false;
    if (authStr != null) {
        if (req.isSignPrefixMode()) {
            strBuilder.append("?sign=").append(UrlEncoderUtils.encode(authStr));
        } else {
            // urlencode auth string key & value
            String[] authParts = authStr.split("&");
            String[] encodeAuthParts = new String[authParts.length];
            for (int i = 0; i < authParts.length; i++) {
                String[] kv = authParts[i].split("=", 2);
                if (kv.length == 2) {
                    encodeAuthParts[i] = StringUtils.join("=", UrlEncoderUtils.encode(kv[0]), UrlEncoderUtils.encode(kv[1]));
                } else if (kv.length == 1) {
                    encodeAuthParts[i] = StringUtils.join("=", UrlEncoderUtils.encode(kv[0]));
                }
            }
            authStr = StringUtils.join("&", encodeAuthParts);
            strBuilder.append("?").append(authStr);
        }
        if (cred instanceof COSSessionCredentials) {
            strBuilder.append("&").append(Headers.SECURITY_TOKEN).append("=").append(UrlEncoderUtils.encode(((COSSessionCredentials) cred).getSessionToken()));
        }
        hasAppendFirstParameter = true;
    }
    for (Entry<String, String> entry : request.getParameters().entrySet()) {
        String paramKey = entry.getKey();
        String paramValue = entry.getValue();
        if (!hasAppendFirstParameter) {
            strBuilder.append("?");
            hasAppendFirstParameter = true;
        } else {
            strBuilder.append("&");
        }
        strBuilder.append(UrlEncoderUtils.encode(paramKey));
        if (paramValue != null) {
            strBuilder.append("=").append(UrlEncoderUtils.encode(paramValue));
        }
    }
    try {
        return new URL(strBuilder.toString());
    } catch (MalformedURLException e) {
        throw new CosClientException(e.toString());
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) COSCredentials(com.qcloud.cos.auth.COSCredentials) CosClientException(com.qcloud.cos.exception.CosClientException) Date(java.util.Date) URL(java.net.URL) HttpMethodName(com.qcloud.cos.http.HttpMethodName) COSSessionCredentials(com.qcloud.cos.auth.COSSessionCredentials) COSSigner(com.qcloud.cos.auth.COSSigner) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with COSSessionCredentials

use of com.qcloud.cos.auth.COSSessionCredentials in project cos-java-sdk-v5 by tencentyun.

the class CachedTemporyTokenCredentialsProvider method fetchNewCOSCredentials.

@Override
public COSCredentials fetchNewCOSCredentials() {
    TemporyToken temporyToken = AbstractCOSClientTest.fetchTempToken(this.temporyTokenDuration);
    if (temporyToken == null) {
        return null;
    }
    if (temporyToken.getTempSecretId() == null || temporyToken.getTempSecretKey() == null || temporyToken.getTempToken() == null) {
        return null;
    }
    COSSessionCredentials credentials = new BasicSessionCredentials(temporyToken.getTempSecretId(), temporyToken.getTempSecretKey(), temporyToken.getTempToken());
    return credentials;
}
Also used : COSSessionCredentials(com.qcloud.cos.auth.COSSessionCredentials) BasicSessionCredentials(com.qcloud.cos.auth.BasicSessionCredentials)

Aggregations

COSSessionCredentials (com.qcloud.cos.auth.COSSessionCredentials)2 BasicSessionCredentials (com.qcloud.cos.auth.BasicSessionCredentials)1 COSCredentials (com.qcloud.cos.auth.COSCredentials)1 COSSigner (com.qcloud.cos.auth.COSSigner)1 CosClientException (com.qcloud.cos.exception.CosClientException)1 HttpMethodName (com.qcloud.cos.http.HttpMethodName)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1