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.http.Header.CONTENT_MD5;
19
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.UncheckedIOException;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Optional;
26 import software.amazon.awssdk.annotations.SdkInternalApi;
27 import software.amazon.awssdk.core.interceptor.Context;
28 import software.amazon.awssdk.core.interceptor.ExecutionAttribute;
29 import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
30 import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
31 import software.amazon.awssdk.core.sync.RequestBody;
32 import software.amazon.awssdk.http.SdkHttpRequest;
33 import software.amazon.awssdk.services.s3.model.PutObjectRequest;
34 import software.amazon.awssdk.services.s3.model.UploadPartRequest;
35 import software.amazon.awssdk.utils.IoUtils;
36 import software.amazon.awssdk.utils.Md5Utils;
37
38 @SdkInternalApi
39 public class AddContentMd5HeaderInterceptor implements ExecutionInterceptor {
40
41     private static final ExecutionAttribute<String> CONTENT_MD5_ATTRIBUTE = new ExecutionAttribute<>("contentMd5");
42
43     // List of operations that should be ignored by this interceptor.
44     // These are costly operations, so adding the md5 header will take a performance hit
45     private static final List<Class> BLACKLIST_METHODS = Arrays.asList(PutObjectRequest.class, UploadPartRequest.class);
46
47     @Override
48     public Optional<RequestBody> modifyHttpContent(Context.ModifyHttpRequest context,
49                                                    ExecutionAttributes executionAttributes) {
50
51         if (!BLACKLIST_METHODS.contains(context.request().getClass()) && context.requestBody().isPresent()
52             && !context.httpRequest().firstMatchingHeader(CONTENT_MD5).isPresent()) {
53
54             try {
55                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
56                 IoUtils.copy(context.requestBody().get().contentStreamProvider().newStream(), baos);
57                 executionAttributes.putAttribute(CONTENT_MD5_ATTRIBUTE, Md5Utils.md5AsBase64(baos.toByteArray()));
58                 return context.requestBody();
59             } catch (IOException e) {
60                 throw new UncheckedIOException(e);
61             }
62         }
63
64         return context.requestBody();
65     }
66
67     @Override
68     public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context,
69                                             ExecutionAttributes executionAttributes) {
70         String contentMd5 = executionAttributes.getAttribute(CONTENT_MD5_ATTRIBUTE);
71
72         if (contentMd5 != null) {
73             return context.httpRequest().toBuilder().putHeader(CONTENT_MD5, contentMd5).build();
74         }
75
76         return context.httpRequest();
77     }
78 }
79