Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file is distributed
  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
  */
 package com.amazonaws.auth;
 
 import java.net.URI;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
Signer implementation responsible for signing an AWS query string request according to the various signature versions and hashing algorithms.
 
 public class QueryStringSigner extends AbstractAWSSigner implements Signer {
    
Date override for testing only
 
     private Date overriddenDate;

    
This signer will add "Signature" parameter to the request. Default signature version is "2" and default signing algorithm is "HmacSHA256". AWSAccessKeyId SignatureVersion SignatureMethod Timestamp Signature

Parameters:
request request to be signed.
credentials The credentials used to use to sign the request.
 
     public void sign(SignableRequest<?> requestAWSCredentials credentials)
             throws AmazonClientException {
         sign(request..credentials);
     }

    
This signer will add following authentication parameters to the request: AWSAccessKeyId SignatureVersion SignatureMethod Timestamp Signature

Parameters:
request request to be signed.
version signature version. "2" is recommended.
algorithm signature algorithm. "HmacSHA256" is recommended.
 
     public void sign(SignableRequest<?> requestSignatureVersion version,
             SigningAlgorithm algorithmAWSCredentials credentials)
             throws AmazonClientException {
         // annonymous credentials, don't sign
         if ( credentials instanceof AnonymousAWSCredentials ) {
             return;
         }
 
         AWSCredentials sanitizedCredentials = sanitizeCredentials(credentials);
         request.addParameter("AWSAccessKeyId"sanitizedCredentials.getAWSAccessKeyId());
         request.addParameter("SignatureVersion"version.toString());
 
         int timeOffset = request.getTimeOffset();
         request.addParameter("Timestamp"getFormattedTimestamp(timeOffset));
 
         if ( sanitizedCredentials instanceof AWSSessionCredentials ) {
             addSessionCredentials(request, (AWSSessionCredentialssanitizedCredentials);
         }
 
         String stringToSign = null;
         if ( version.equals. ) ) {
             stringToSign = calculateStringToSignV1(request.getParameters());
         } else if ( version.equals. ) ) {
             request.addParameter("SignatureMethod"algorithm.toString());
             stringToSign = calculateStringToSignV2(request);
         } else {
             throw new AmazonClientException("Invalid Signature Version specified");
         }
 
         String signatureValue = signAndBase64Encode(stringToSign,
                 sanitizedCredentials.getAWSSecretKey(), algorithm);
         request.addParameter("Signature"signatureValue);
     }

    
Calculates string to sign for signature version 1.

Parameters:
parameters request parameters
Returns:
String to sign
    private String calculateStringToSignV1(Map<StringList<String>> parameters) {
        StringBuilder data = new StringBuilder();
        SortedMap<StringList<String>> sorted =
            new TreeMap<StringList<String>>(.);
        sorted.putAll(parameters);
        for (Map.Entry<StringList<String>> entry : sorted.entrySet()) {
            for (String value : entry.getValue()) {
                data.append(entry.getKey())
                    .append(value);
            }
        }
        return data.toString();
    }

    
Calculate string to sign for signature version 2.

Parameters:
request The request being signed.
Returns:
String to sign
Throws:
com.amazonaws.AmazonClientException If the string to sign cannot be calculated.
    private String calculateStringToSignV2(SignableRequest<?> requestthrows AmazonClientException {
        URI endpoint = request.getEndpoint();
        StringBuilder data = new StringBuilder();
        data.append("POST")
            .append("\n")
            .append(getCanonicalizedEndpoint(endpoint))
            .append("\n")
            .append(getCanonicalizedResourcePath(request))
            .append("\n")
            .append(getCanonicalizedQueryString(request.getParameters()));
        return data.toString();
    }
    private String getCanonicalizedResourcePath(SignableRequest<?> request) {
        String resourcePath = "";
        if (request.getEndpoint().getPath() != null) {
            resourcePath += request.getEndpoint().getPath();
        }
        if (request.getResourcePath() != null) {
            if (resourcePath.length() > 0 &&
                !resourcePath.endsWith("/") &&
                !request.getResourcePath().startsWith("/")) {
                resourcePath += "/";
            }
            resourcePath += request.getResourcePath();
        } else if (!resourcePath.endsWith("/")) {
            resourcePath += "/";
        }
        if (!resourcePath.startsWith("/")) {
            resourcePath = "/" + resourcePath;
        }
        if (resourcePath.startsWith("//")) {
            resourcePath = resourcePath.substring(1);
        }
        return resourcePath;
    }

    
Formats date as ISO 8601 timestamp
    private String getFormattedTimestamp(int offset) {
        SimpleDateFormat df = new SimpleDateFormat(
                "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
        if ( != null) {
            return df.format();
        } else {
            return df.format(getSignatureDate(offset));
        }
    }

    
For testing purposes only, to control the date used in signing.
    void overrideDate(Date date) {
        this. = date;
    }
    @Override
    protected void addSessionCredentials(SignableRequest<?> requestAWSSessionCredentials credentials) {
        request.addParameter("SecurityToken"credentials.getSessionToken());
    }
New to GrepCode? Check out our FAQ X