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.services.s3.internal;
 
 import java.net.URI;
 
 
The internal implementation of AuthErrorRetryStrategy which automatically switches to V4 signer when the S3 returns auth error asking for v4 authentication.
 
 public class S3V4AuthErrorRetryStrategy implements AuthErrorRetryStrategy {
 
     private static Log log = LogFactory.getLog(S3V4AuthErrorRetryStrategy.class);
 
     private static final String V4_REGION_WARNING = "please use region-specific endpoint to access "
             + "buckets located in regions that require V4 signing.";
 
     private final S3RequestEndpointResolver endpointResolver;
 
     public S3V4AuthErrorRetryStrategy(S3RequestEndpointResolver endpointResolver) {
         this. = endpointResolver;
         this. = new IsSigV4RetryablePredicate();
     }

    
Currently only used for testing
 
             SdkPredicate<AmazonServiceExceptionisSigV4Retryable) {
         this. = endpointResolver;
         this. = isSigV4Retryable;
     }
 
     @Override
                                                         HttpResponse response,
                                                         AmazonServiceException ase) {
         if (!.test(ase)) {
             return null;
         }
         if (hasServingRegionHeader(response)) {
             return redirectToRegionInHeader(requestresponse);
         } else if (canUseVirtualAddressing()) {
             return redirectToS3External();
         } else {
             throw new AmazonClientException(ase);
         }
     }
 
     private boolean canUseVirtualAddressing() {
         return BucketNameUtils.isDNSBucketName(.getBucketName());
     }
 
     private AuthRetryParameters redirectToRegionInHeader(Request<?> requestHttpResponse response) {
         final String region = getServingRegionHeader(response);
         AWSS3V4Signer v4Signer = buildSigV4Signer(region);
         .resolveRequestEndpoint(requestregion);
         return buildRetryParams(v4Signerrequest.getEndpoint());
     }

    
If the response doesn't have the x-amz-region header we have to resort to sending a request to s3-external-1

Returns:
 
         AWSS3V4Signer v4Signer = buildSigV4Signer(..toString());
        try {
            URI bucketEndpoint = new URI(String.format("https://%s.s3-external-1.amazonaws.com".getBucketName()));
            return buildRetryParams(v4SignerbucketEndpoint);
        } catch (URISyntaxException e) {
            throw new AmazonClientException("Failed to re-send the request to \"s3-external-1.amazonaws.com\". "
                    + e);
        }
    }
    private AWSS3V4Signer buildSigV4Signer(final String region) {
        AWSS3V4Signer v4Signer = new AWSS3V4Signer();
        v4Signer.setRegionName(region);
        return v4Signer;
    }
    private AuthRetryParameters buildRetryParams(AWSS3V4Signer signerURI endpoint) {
        .warn("Attempting to re-send the request to " + endpoint.getHost() + " with AWS V4 authentication. "
                + "To avoid this warning in the future, " + );
        return new AuthRetryParameters(signerendpoint);
    }
    private static boolean hasServingRegionHeader(HttpResponse response) {
        return !StringUtils.isNullOrEmpty(getServingRegionHeader(response));
    }
    private static String getServingRegionHeader(HttpResponse response) {
        return response.getHeaders().get(.);
    }
New to GrepCode? Check out our FAQ X