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.core.internal.util; 17 18 import java.util.ArrayList; 19 import java.util.List; 20 import software.amazon.awssdk.annotations.SdkInternalApi; 21 import software.amazon.awssdk.core.checksums.Algorithm; 22 import software.amazon.awssdk.core.checksums.ChecksumSpecs; 23 import software.amazon.awssdk.core.interceptor.ExecutionAttributes; 24 import software.amazon.awssdk.core.interceptor.SdkExecutionAttribute; 25 import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute; 26 import software.amazon.awssdk.core.interceptor.trait.HttpChecksum; 27 28 /** 29 * Class to resolve the different Checksums specs from ExecutionAttributes. 30 */ 31 @SdkInternalApi 32 public final class HttpChecksumResolver { 33 HttpChecksumResolver()34 private HttpChecksumResolver() { 35 } 36 getResolvedChecksumSpecs(ExecutionAttributes executionAttributes)37 public static ChecksumSpecs getResolvedChecksumSpecs(ExecutionAttributes executionAttributes) { 38 ChecksumSpecs checksumSpecs = executionAttributes.getAttribute(SdkExecutionAttribute.RESOLVED_CHECKSUM_SPECS); 39 if (checksumSpecs != null) { 40 return checksumSpecs; 41 } 42 return resolveChecksumSpecs(executionAttributes); 43 } 44 resolveChecksumSpecs(ExecutionAttributes executionAttributes)45 public static ChecksumSpecs resolveChecksumSpecs(ExecutionAttributes executionAttributes) { 46 HttpChecksum httpChecksumTraitInOperation = executionAttributes.getAttribute(SdkInternalExecutionAttribute.HTTP_CHECKSUM); 47 if (httpChecksumTraitInOperation == null) { 48 return null; 49 } 50 boolean hasRequestValidation = hasRequestValidationMode(httpChecksumTraitInOperation); 51 String requestAlgorithm = httpChecksumTraitInOperation.requestAlgorithm(); 52 String checksumHeaderName = requestAlgorithm != null ? HttpChecksumUtils.httpChecksumHeader(requestAlgorithm) : null; 53 List<Algorithm> responseValidationAlgorithms = getResponseValidationAlgorithms(httpChecksumTraitInOperation); 54 55 return ChecksumSpecs.builder() 56 .algorithm(Algorithm.fromValue(httpChecksumTraitInOperation.requestAlgorithm())) 57 .headerName(checksumHeaderName) 58 .responseValidationAlgorithms(responseValidationAlgorithms) 59 .isValidationEnabled(hasRequestValidation) 60 .isRequestChecksumRequired(httpChecksumTraitInOperation.isRequestChecksumRequired()) 61 .isRequestStreaming(httpChecksumTraitInOperation.isRequestStreaming()) 62 .build(); 63 } 64 hasRequestValidationMode(HttpChecksum httpChecksum)65 private static boolean hasRequestValidationMode(HttpChecksum httpChecksum) { 66 return httpChecksum.requestValidationMode() != null; 67 } 68 getResponseValidationAlgorithms(HttpChecksum httpChecksumTraitInOperation)69 private static List<Algorithm> getResponseValidationAlgorithms(HttpChecksum httpChecksumTraitInOperation) { 70 List<String> responseAlgorithms = httpChecksumTraitInOperation.responseAlgorithms(); 71 if (responseAlgorithms != null && !responseAlgorithms.isEmpty()) { 72 List<Algorithm> responseValidationAlgorithms = new ArrayList<>(responseAlgorithms.size()); 73 for (String algorithmName : responseAlgorithms) { 74 responseValidationAlgorithms.add(Algorithm.fromValue(algorithmName)); 75 } 76 return responseValidationAlgorithms; 77 } 78 return null; 79 } 80 } 81