• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.services.s3.internal.handlers;
17 
18 import software.amazon.awssdk.annotations.SdkInternalApi;
19 import software.amazon.awssdk.awscore.AwsExecutionAttribute;
20 import software.amazon.awssdk.core.SdkRequest;
21 import software.amazon.awssdk.core.interceptor.Context;
22 import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
23 import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
24 import software.amazon.awssdk.regions.Region;
25 import software.amazon.awssdk.services.s3.internal.BucketUtils;
26 import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration;
27 import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
28 
29 @SdkInternalApi
30 public final class CreateBucketInterceptor implements ExecutionInterceptor {
31 
32     @Override
modifyRequest(Context.ModifyRequest context, ExecutionAttributes executionAttributes)33     public SdkRequest modifyRequest(Context.ModifyRequest context, ExecutionAttributes executionAttributes) {
34         SdkRequest sdkRequest = context.request();
35 
36         if (sdkRequest instanceof CreateBucketRequest) {
37             CreateBucketRequest request = (CreateBucketRequest) sdkRequest;
38             validateBucketNameIsS3Compatible(request.bucket());
39 
40             if (request.createBucketConfiguration() == null) {
41                 return addLocationConstraintToRequest(request,
42                                                       executionAttributes,
43                                                       CreateBucketConfiguration.builder().build());
44             }
45 
46             CreateBucketConfiguration bucketConfiguration = request.createBucketConfiguration();
47 
48             if (bucketConfiguration.locationConstraint() == null && bucketConfiguration.location() == null) {
49                 return addLocationConstraintToRequest(request, executionAttributes, bucketConfiguration);
50             }
51         }
52 
53         return sdkRequest;
54     }
55 
addLocationConstraintToRequest(CreateBucketRequest request, ExecutionAttributes executionAttributes, CreateBucketConfiguration configuration)56     private SdkRequest addLocationConstraintToRequest(CreateBucketRequest request,
57                                                       ExecutionAttributes executionAttributes,
58                                                       CreateBucketConfiguration configuration) {
59         Region region = executionAttributes.getAttribute(AwsExecutionAttribute.AWS_REGION);
60         return request.copy(r -> r.createBucketConfiguration(setLocationConstraint(region, configuration)));
61     }
62 
setLocationConstraint(Region region, CreateBucketConfiguration configuration)63     private CreateBucketConfiguration setLocationConstraint(Region region, CreateBucketConfiguration configuration) {
64         if (region.equals(Region.US_EAST_1)) {
65             // us-east-1 requires no location restraint. See http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUT.html
66             return null;
67         }
68         return configuration.copy(c -> c.locationConstraint(region.id()).build());
69     }
70 
71     /**
72      * Validates that the name of the bucket being requested to be created
73      * is a valid S3 bucket name according to their guidelines. If the bucket
74      * name is not valid, an {@link IllegalArgumentException} is thrown. See
75      * {@link BucketUtils#isValidDnsBucketName(String, boolean)} for additional
76      * details.
77      *
78      * @param bucketName Name of the bucket
79      */
validateBucketNameIsS3Compatible(String bucketName)80     private void validateBucketNameIsS3Compatible(String bucketName) {
81         BucketUtils.isValidDnsBucketName(bucketName, true);
82     }
83 }
84