• 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.client;
17 
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.function.Predicate;
21 import software.amazon.awssdk.annotations.SdkInternalApi;
22 import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
23 import software.amazon.awssdk.core.client.config.SdkClientOption;
24 import software.amazon.awssdk.services.s3.S3AsyncClient;
25 import software.amazon.awssdk.services.s3.endpoints.S3ClientContextParams;
26 import software.amazon.awssdk.services.s3.internal.crossregion.S3CrossRegionAsyncClient;
27 import software.amazon.awssdk.services.s3.internal.multipart.MultipartS3AsyncClient;
28 import software.amazon.awssdk.services.s3.multipart.MultipartConfiguration;
29 import software.amazon.awssdk.utils.AttributeMap;
30 import software.amazon.awssdk.utils.ConditionalDecorator;
31 
32 @SdkInternalApi
33 public class S3AsyncClientDecorator {
34     public static final AttributeMap.Key<MultipartConfiguration> MULTIPART_CONFIGURATION_KEY =
35         new AttributeMap.Key<MultipartConfiguration>(MultipartConfiguration.class){};
36     public static final AttributeMap.Key<Boolean> MULTIPART_ENABLED_KEY =
37         new AttributeMap.Key<Boolean>(Boolean.class){};
38 
S3AsyncClientDecorator()39     public S3AsyncClientDecorator() {
40     }
41 
decorate(S3AsyncClient base, SdkClientConfiguration clientConfiguration)42     public S3AsyncClient decorate(S3AsyncClient base,
43                                   SdkClientConfiguration clientConfiguration) {
44         AttributeMap clientContextParams = clientConfiguration.option(SdkClientOption.CLIENT_CONTEXT_PARAMS);
45         List<ConditionalDecorator<S3AsyncClient>> decorators = new ArrayList<>();
46         decorators.add(ConditionalDecorator.create(
47             isCrossRegionEnabledAsync(clientContextParams),
48             S3CrossRegionAsyncClient::new));
49 
50         decorators.add(ConditionalDecorator.create(
51             isMultipartEnable(clientContextParams),
52             client -> {
53                 MultipartConfiguration multipartConfiguration = clientContextParams.get(MULTIPART_CONFIGURATION_KEY);
54                 return MultipartS3AsyncClient.create(client, multipartConfiguration);
55             }));
56         return ConditionalDecorator.decorate(base, decorators);
57     }
58 
isCrossRegionEnabledAsync(AttributeMap clientContextParams)59     private Predicate<S3AsyncClient> isCrossRegionEnabledAsync(AttributeMap clientContextParams) {
60         Boolean crossRegionEnabled = clientContextParams.get(S3ClientContextParams.CROSS_REGION_ACCESS_ENABLED);
61         return client -> crossRegionEnabled != null && crossRegionEnabled.booleanValue();
62     }
63 
isMultipartEnable(AttributeMap clientContextParams)64     private Predicate<S3AsyncClient> isMultipartEnable(AttributeMap clientContextParams) {
65         Boolean multipartEnabled = clientContextParams.get(MULTIPART_ENABLED_KEY);
66         return client -> multipartEnabled != null && multipartEnabled.booleanValue();
67     }
68 }
69