Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2010-2015, Inc. or its affiliates. All Rights Reserved.
   * Portions copyright 2006-2009 James Murty. Please see LICENSE.txt
   * for applicable license terms and NOTICE.txt for applicable notices.
   * 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
  * or in the "license" file accompanying this file. This file is distributed
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
 import java.util.List;
 import java.util.Map;
Utilities useful for REST/HTTP S3Service implementations.
 public class RestUtils {

The set of request parameters which must be included in the canonical string to sign.
     private static final List<StringSIGNED_PARAMETERS = Arrays.asList(new String[] {

Calculate the canonical string for a REST/HTTP request to S3 by only including query parameters that are mentioned in SIGNED_PARAMETERS.

     public static <T> String makeS3CanonicalString(String method,
             String resourceSignableRequest<T> requestString expires) {
         return makeS3CanonicalString(methodresourcerequestexpiresnull);

Calculate the canonical string for a REST/HTTP request to S3.

method The HTTP verb.
resource The HTTP-encoded resource path.
request The request to be canonicalized.
expires When expires is non-null, it will be used instead of the Date header.
additionalQueryParamsToSign A collection of user-specified query parameters that should be included in the canonical request, in addition to those default parameters that are always signed.
The canonical string representation for the given S3 request.
     public static <T> String makeS3CanonicalString(String method,
             String resourceSignableRequest<T> requestString expires,
             Collection<StringadditionalQueryParamsToSign) {
         StringBuilder buf = new StringBuilder();
         buf.append(method + "\n");
         // Add all interesting headers to a list, then sort them.  "Interesting"
         // is defined as Content-MD5, Content-Type, Date, and x-amz-
         Map<StringStringheadersMap = request.getHeaders();
         SortedMap<StringStringinterestingHeaders = new TreeMap<StringString>();
         if (headersMap != null && headersMap.size() > 0) {
             Iterator<Map.Entry<StringString>> headerIter = headersMap.entrySet().iterator();
             while (headerIter.hasNext()) {
                 Map.Entry<StringStringentry = (Map.Entry<StringString>);
                 String key = entry.getKey();
                String value = entry.getValue();
                if (key == nullcontinue;
                String lk = key.toLowerCase(Locale.getDefault());
                // Ignore any headers that are not particularly interesting.
                if (lk.equals("content-type") || lk.equals("content-md5") || lk.equals("date") ||
        // Remove default date timestamp if "x-amz-date" is set.
        if (interestingHeaders.containsKey(.)) {
        // Use the expires value as the timestamp if it is available. This trumps both the default
        // "date" timestamp, and the "x-amz-date" header.
        if (expires != null) {
        // These headers require that we still put a new line in after them,
        // even if they don't exist.
        if (! interestingHeaders.containsKey("content-type")) {
        if (! interestingHeaders.containsKey("content-md5")) {
        // Any parameters that are prefixed with "x-amz-" need to be included
        // in the headers section of the canonical string to sign
        final Map<StringList<String>> requestParameters = request
        for (Map.Entry<StringList<String>> parameter : requestParameters
                .entrySet()) {
            if (parameter.getKey().startsWith("x-amz-")) {
                StringBuilder parameterValueBuilder = new StringBuilder();
We don't need to url encode here. If a parameter has multiple values, then those values needs to be combined to a comma separated list and assigned to the header. Reference : RESTAuthentication .html#RESTAuthenticationRequestCanonicalization
                for (String value : parameter.getValue()) {
                    if (parameterValueBuilder.length() > 0) {
        // Add all the interesting headers (i.e.: all that startwith x-amz- ;-))
        for (Iterator<Map.Entry<StringString>> i = interestingHeaders.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry<StringStringentry = (Map.Entry<StringString>);
            String key = (Stringentry.getKey();
            String value = entry.getValue();
            if (key.startsWith(.)) {
                if (value != null) {
            } else if (value != null) {
        // Add all the interesting parameters
        String[] parameterNames = requestParameters.keySet().toArray(
                                new String[request.getParameters().size()]);
        StringBuilder queryParams = new StringBuilder();
        for (String parameterName : parameterNames) {
            if ( !.contains(parameterName)
                 (additionalQueryParamsToSign == null ||
               ) {

As per the spec given in the below URL, it is not clear as to whether we need to sort the parameter values when forming the string to sign. This is something that needs to be watched if we receive signing problems. Reference : RESTAuthentication .html#RESTAuthenticationRequestCanonicalization
            List<Stringvalues = requestParameters.get(parameterName);
            for (String value : values) {
                queryParams = queryParams.length() > 0 ? queryParams
                        .append("&") : queryParams.append("?");
                if (value != null) {
        return buf.toString();
New to GrepCode? Check out our FAQ X