Example usage for com.amazonaws.auth SigningAlgorithm HmacSHA1

List of usage examples for com.amazonaws.auth SigningAlgorithm HmacSHA1

Introduction

In this page you can find the example usage for com.amazonaws.auth SigningAlgorithm HmacSHA1.

Prototype

SigningAlgorithm HmacSHA1

To view the source code for com.amazonaws.auth SigningAlgorithm HmacSHA1.

Click Source Link

Usage

From source file:com.emc.vipr.services.s3.ViPRS3Signer.java

License:Open Source License

@Override
public void sign(Request<?> request, AWSCredentials credentials) throws AmazonClientException {
    if (credentials == null || credentials.getAWSSecretKey() == null) {
        log.debug("Canonical string will not be signed, as no AWS Secret Key was provided");
        return;//from   www.ja va  2 s  .  c o  m
    }

    AWSCredentials sanitizedCredentials = sanitizeCredentials(credentials);
    if (sanitizedCredentials instanceof AWSSessionCredentials) {
        addSessionCredentials(request, (AWSSessionCredentials) sanitizedCredentials);
    }

    /*
     * In s3 sigv2, the way slash characters are encoded should be
     * consistent in both the request url and the encoded resource path.
     * Since we have to encode "//" to "/%2F" in the request url to make
     * httpclient works, we need to do the same encoding here for the
     * resource path.
     */
    String encodedResourcePath = HttpUtils.appendUri(request.getEndpoint().getPath(), resourcePath, true);

    Date date = getSignatureDate(request.getTimeOffset());
    request.addHeader(Headers.DATE, ServiceUtils.formatRfc822Date(date));
    String canonicalString = makeS3CanonicalString(httpVerb, encodedResourcePath, request, null);
    log.debug("Calculated string to sign:\n\"" + canonicalString + "\"");

    String signature = super.signAndBase64Encode(canonicalString, sanitizedCredentials.getAWSSecretKey(),
            SigningAlgorithm.HmacSHA1);
    request.addHeader("Authorization", "AWS " + sanitizedCredentials.getAWSAccessKeyId() + ":" + signature);
}