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.auth.signer.AwsSignerExecutionAttribute;
20 import software.amazon.awssdk.awscore.AwsExecutionAttribute;
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.core.interceptor.SdkExecutionAttribute;
25 import software.amazon.awssdk.http.SdkHttpRequest;
26 import software.amazon.awssdk.services.s3.S3Configuration;
27 import software.amazon.awssdk.services.s3.internal.ConfiguredS3SdkHttpRequest;
28 import software.amazon.awssdk.services.s3.internal.S3EndpointUtils;
29
30 @SdkInternalApi
31 public final class EndpointAddressInterceptor implements ExecutionInterceptor {
32
33     @Override
34     public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context,
35                                             ExecutionAttributes executionAttributes) {
36         ConfiguredS3SdkHttpRequest configuredRequest =
37             S3EndpointUtils.applyEndpointConfiguration(
38                     context.httpRequest(),
39                     context.request(),
40                     executionAttributes.getAttribute(AwsExecutionAttribute.AWS_REGION),
41                     (S3Configuration) executionAttributes.getAttribute(AwsSignerExecutionAttribute.SERVICE_CONFIG),
42                     Boolean.TRUE.equals(executionAttributes.getAttribute(SdkExecutionAttribute.ENDPOINT_OVERRIDDEN)));
43
44         configuredRequest.signingRegionModification().ifPresent(
45             region -> executionAttributes.putAttribute(AwsSignerExecutionAttribute.SIGNING_REGION, region));
46
47         return configuredRequest.sdkHttpRequest();
48     }
49 }
50