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;
 
Sets endpoint and resource path on a request object
 
 public class S3RequestEndpointResolver {
 
     private final ServiceEndpointBuilder endpointBuilder;
     private final boolean isPathStyleAccess;
     private final String bucketName;
     private final String key;
 
     public S3RequestEndpointResolver(ServiceEndpointBuilder endpointBuilderboolean isPathStyleAccess,
             String bucketNameString key) {
         this. = endpointBuilder;
         this. = isPathStyleAccess;
         this. = bucketName;
         this. = key;
     }
 
     public String getBucketName() {
         return this.;
     }

    
Set the request's endpoint and resource path with the same region it was originally configured for

Parameters:
request Request to set endpoint for
 
     public void resolveRequestEndpoint(Request<?> request) {
         resolveRequestEndpoint(requestnull);
     }

    
Set the request's endpoint and resource path with the new region provided

Parameters:
request Request to set endpoint for
region New region to determine endpoint to hit
 
     public void resolveRequestEndpoint(Request<?> requestString region) {
         if (region != null) {
             .withRegion(Region.getRegion(Regions.fromName(region)));
         }
         final URI endpoint = .getServiceEndpoint();
         if (shouldUseVirtualAddressing(endpoint)) {
             request.setEndpoint(convertToVirtualHostEndpoint(endpoint));
             request.setResourcePath(getHostStyleResourcePath());
         } else {
             request.setEndpoint(endpoint);
             if ( != null) {
                 request.setResourcePath(getPathStyleResourcePath());
             }
         }
     }
 
     private boolean shouldUseVirtualAddressing(final URI endpoint) {
         return ! && BucketNameUtils.isDNSBucketName()
                 && !isValidIpV4Address(endpoint.getHost());
     }
 
     private String getHostStyleResourcePath() {
         String resourcePath = ;
         /*
          * If the key name starts with a slash character, in order to prevent it being treated as a
          * path delimiter, we need to add another slash before the key name. {@see
          * com.amazonaws.http.HttpRequestFactory#createHttpRequest}
          */
         if ( != null && .startsWith("/")) {
             resourcePath = "/" + ;
         }
         return resourcePath;
     }
 
    private String getPathStyleResourcePath() {
        return  + "/" + ( != null ?  : "");
    }
    static boolean isValidIpV4Address(String ipAddr) {
        if (ipAddr == null) {
            return false;
        }
        String[] tokens = ipAddr.split("\\.");
        if (tokens.length != 4) {
            return false;
        }
        for (String token : tokens) {
            try {
                int tokenInt = Integer.parseInt(token);
                if (tokenInt < 0 || tokenInt > 255) {
                    return false;
                }
            } catch (NumberFormatException ase) {
                return false;
            }
        }
        return true;
    }

    
Converts the current endpoint set for this client into virtual addressing style, by placing the name of the specified bucket before the S3 service endpoint.

Parameters:
bucketName The name of the bucket to use in the virtual addressing style of the returned URI.
Returns:
A new URI, creating from the current service endpoint URI and the specified bucket.
    private static URI convertToVirtualHostEndpoint(URI endpointString bucketName) {
        try {
            return new URI(String.format("%s://%s.%s"endpoint.getScheme(), bucketNameendpoint.getAuthority()));
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Invalid bucket name: " + bucketNamee);
        }
    }
New to GrepCode? Check out our FAQ X