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.auth.signer.S3SignerExecutionAttribute;
21 import software.amazon.awssdk.core.SdkRequest;
22 import software.amazon.awssdk.core.interceptor.Context;
23 import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
24 import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
25 import software.amazon.awssdk.services.s3.S3Configuration;
26 import software.amazon.awssdk.services.s3.model.PutObjectRequest;
27 import software.amazon.awssdk.services.s3.model.UploadPartRequest;
28
29 /**
30  * Interceptor to enable chunked encoding on specific upload operations if the option does not already have a value.
31  * <p>
32  * This affects the following requests:
33  * <ul>
34  *     <li>{@link PutObjectRequest}</li>
35  *     <li>{@link UploadPartRequest}</li>
36  * </ul>
37  */

38 @SdkInternalApi
39 public final class EnableChunkedEncodingInterceptor implements ExecutionInterceptor {
40
41     @Override
42     public SdkRequest modifyRequest(Context.ModifyRequest context, ExecutionAttributes executionAttributes) {
43         SdkRequest sdkRequest = context.request();
44
45         if (sdkRequest instanceof PutObjectRequest || sdkRequest instanceof UploadPartRequest) {
46             S3Configuration serviceConfiguration =
47                     (S3Configuration) executionAttributes.getAttribute(AwsSignerExecutionAttribute.SERVICE_CONFIG);
48
49             boolean enableChunkedEncoding;
50             if (serviceConfiguration != null) {
51                 enableChunkedEncoding = serviceConfiguration.chunkedEncodingEnabled();
52             } else {
53                 enableChunkedEncoding = true;
54             }
55
56             executionAttributes.putAttributeIfAbsent(S3SignerExecutionAttribute.ENABLE_CHUNKED_ENCODING, enableChunkedEncoding);
57         }
58
59         return sdkRequest;
60     }
61 }
62