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
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 || request.createBucketConfiguration().locationConstraint() == null) {
41                 Region region = executionAttributes.getAttribute(AwsExecutionAttribute.AWS_REGION);
42                 sdkRequest = request.toBuilder()
43                                     .createBucketConfiguration(toLocationConstraint(region))
44                                     .build();
45             }
46         }
47
48         return sdkRequest;
49     }
50
51     private CreateBucketConfiguration toLocationConstraint(Region region) {
52         if (region.equals(Region.US_EAST_1)) {
53             // us-east-1 requires no location restraint. See http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUT.html
54             return null;
55         }
56         return CreateBucketConfiguration.builder()
57                                         .locationConstraint(region.id())
58                                         .build();
59     }
60
61     /**
62      * Validates that the name of the bucket being requested to be created
63      * is a valid S3 bucket name according to their guidelines. If the bucket
64      * name is not valid, an {@link IllegalArgumentException} is thrown. See
65      * {@link BucketUtils#isValidDnsBucketName(String, boolean)} for additional
66      * details.
67      *
68      * @param bucketName Name of the bucket
69      */

70     private void validateBucketNameIsS3Compatible(String bucketName) {
71         BucketUtils.isValidDnsBucketName(bucketName, true);
72     }
73 }
74