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 static software.amazon.awssdk.services.s3.checksums.ChecksumConstant.ENABLE_CHECKSUM_REQUEST_HEADER;
19 import static software.amazon.awssdk.services.s3.checksums.ChecksumConstant.ENABLE_MD5_CHECKSUM_HEADER_VALUE;
20 import static software.amazon.awssdk.services.s3.checksums.ChecksumConstant.S3_MD5_CHECKSUM_LENGTH;
21 import static software.amazon.awssdk.services.s3.checksums.ChecksumsEnabledValidator.getObjectChecksumEnabledPerRequest;
22 import static software.amazon.awssdk.services.s3.checksums.ChecksumsEnabledValidator.getObjectChecksumEnabledPerResponse;
23
24 import software.amazon.awssdk.annotations.SdkInternalApi;
25 import software.amazon.awssdk.core.SdkResponse;
26 import software.amazon.awssdk.core.interceptor.Context;
27 import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
28 import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
29 import software.amazon.awssdk.http.SdkHttpRequest;
30 import software.amazon.awssdk.http.SdkHttpResponse;
31 import software.amazon.awssdk.services.s3.model.GetObjectRequest;
32 import software.amazon.awssdk.services.s3.model.GetObjectResponse;
33 import software.amazon.awssdk.utils.Validate;
34
35 @SdkInternalApi
36 public final class EnableTrailingChecksumInterceptor implements ExecutionInterceptor {
37
38     /**
39      * Append trailing checksum header for {@link GetObjectRequest} if trailing checksum is enabled from config.
40      */

41     @Override
42     public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context,
43                                             ExecutionAttributes executionAttributes) {
44
45         if (getObjectChecksumEnabledPerRequest(context.request(), executionAttributes)) {
46             return context.httpRequest().toBuilder().putHeader(ENABLE_CHECKSUM_REQUEST_HEADER,
47                                                                ENABLE_MD5_CHECKSUM_HEADER_VALUE)
48                           .build();
49         }
50
51         return context.httpRequest();
52     }
53
54     /**
55      * Subtract the contentLength of {@link GetObjectResponse} if trailing checksums is enabled.
56      */

57     @Override
58     public SdkResponse modifyResponse(Context.ModifyResponse context, ExecutionAttributes executionAttributes) {
59         SdkResponse response = context.response();
60         SdkHttpResponse httpResponse = context.httpResponse();
61
62         if (getObjectChecksumEnabledPerResponse(context.request(), httpResponse)) {
63             GetObjectResponse getResponse = (GetObjectResponse) response;
64             Long contentLength = getResponse.contentLength();
65             Validate.notNull(contentLength, "Service returned null 'Content-Length'.");
66             return getResponse.toBuilder()
67                               .contentLength(contentLength - S3_MD5_CHECKSUM_LENGTH)
68                               .build();
69         }
70
71         return response;
72     }
73 }
74