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_LENGTH; 19 import static software.amazon.awssdk.http.Header.CONTENT_TYPE; 20 21 import java.io.ByteArrayInputStream; 22 import java.util.Optional; 23 import software.amazon.awssdk.annotations.SdkInternalApi; 24 import software.amazon.awssdk.core.interceptor.Context; 25 import software.amazon.awssdk.core.interceptor.ExecutionAttributes; 26 import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; 27 import software.amazon.awssdk.core.sync.RequestBody; 28 import software.amazon.awssdk.http.SdkHttpRequest; 29 import software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest; 30 31 @SdkInternalApi 32 public class CreateMultipartUploadRequestInterceptor implements ExecutionInterceptor { 33 34 @Override modifyHttpContent(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes)35 public Optional<RequestBody> modifyHttpContent(Context.ModifyHttpRequest context, 36 ExecutionAttributes executionAttributes) { 37 if (context.request() instanceof CreateMultipartUploadRequest) { 38 return Optional.of(RequestBody.fromInputStream(new ByteArrayInputStream(new byte[0]), 0)); 39 } 40 41 return context.requestBody(); 42 } 43 44 @Override modifyHttpRequest(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes)45 public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context, 46 ExecutionAttributes executionAttributes) { 47 if (context.request() instanceof CreateMultipartUploadRequest) { 48 SdkHttpRequest.Builder builder = context.httpRequest() 49 .toBuilder() 50 .putHeader(CONTENT_LENGTH, String.valueOf(0)); 51 52 if (!context.httpRequest().firstMatchingHeader(CONTENT_TYPE).isPresent()) { 53 builder.putHeader(CONTENT_TYPE, "binary/octet-stream"); 54 } 55 56 return builder.build(); 57 } 58 59 return context.httpRequest(); 60 } 61 } 62