xref: /aosp_15_r20/external/aws-crt-java/src/main/java/software/amazon/awssdk/crt/s3/ChecksumConfig.java (revision 3c7ae9de214676c52d19f01067dc1a404272dc11)
1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 package software.amazon.awssdk.crt.s3;
6 
7 import java.util.List;
8 import java.util.Collections;
9 
10 public class ChecksumConfig {
11 
12     public enum ChecksumLocation {
13 
14         NONE(0),
15         HEADER(1),
16         TRAILER(2);
17 
ChecksumLocation(int nativeValue)18         ChecksumLocation(int nativeValue) {
19             this.nativeValue = nativeValue;
20         }
21 
getNativeValue()22         public int getNativeValue() {
23             return nativeValue;
24         }
25 
26         private int nativeValue;
27     };
28 
29     private ChecksumLocation location = ChecksumLocation.NONE;
30     private ChecksumAlgorithm checksumAlgorithm = ChecksumAlgorithm.NONE;
31     private boolean validateChecksum = false;
32     private List<ChecksumAlgorithm> validateChecksumAlgorithmList = null;
33 
ChecksumConfig()34     public ChecksumConfig() {
35     }
36 
37     /**
38      * If NONE. No request payload checksum will be added and calculated.
39      *
40      * If HEADER, the checksum will be calculated by client and related header added
41      * to the request sent.
42      *
43      * If TRAILER, the payload will be aws_chunked encoded, The checksum will be
44      * calculated while reading the
45      * payload by client. Related header will be added to the trailer part of the
46      * encoded payload. Note the payload of
47      * the original request cannot be aws-chunked encoded already. Otherwise, error
48      * will be raised.
49      *
50      * @param location The location of client added request payload checksum header.
51      * @return this
52      */
withChecksumLocation(ChecksumLocation location)53     public ChecksumConfig withChecksumLocation(ChecksumLocation location) {
54         this.location = location;
55         return this;
56     }
57 
58     /**
59      * @return The location of client added checksum header.
60      */
getChecksumLocation()61     public ChecksumLocation getChecksumLocation() {
62         return this.location;
63     }
64 
65     /**
66      * The checksum algorithm used to calculate the checksum of payload uploaded.
67      * Must be set if location is not AWS_SCL_NONE. Must be AWS_SCA_NONE if location
68      * is AWS_SCL_NONE.
69      *
70      * @param algorithm The checksum algorithm used to calculate the checksum of
71      *                  payload uploaded.
72      * @return this
73      */
withChecksumAlgorithm(ChecksumAlgorithm algorithm)74     public ChecksumConfig withChecksumAlgorithm(ChecksumAlgorithm algorithm) {
75         this.checksumAlgorithm = algorithm;
76         return this;
77     }
78 
79     /**
80      * @return The checksum algorithm used to calculate the checksum of payload
81      *         uploaded.
82      */
getChecksumAlgorithm()83     public ChecksumAlgorithm getChecksumAlgorithm() {
84         return this.checksumAlgorithm;
85     }
86 
87     /**
88      * Enable checksum mode header will be attached to get requests, this will tell
89      * s3 to send back checksums headers if they exist.
90      *
91      * For object that has checksum, the checksum of whole object will be calculated
92      * and validated. The result will finish with a did validate field.
93      *
94      * For object has checksum for parts, if ALL the parts have been validated, the
95      * result will finish with a did validate field. If any part failed the
96      * validation, AWS_ERROR_S3_RESPONSE_CHECKSUM_MISMATCH will be raised.
97      *
98      * @param validateChecksum Validate the checksum of response if server provides.
99      * @return this
100      */
withValidateChecksum(boolean validateChecksum)101     public ChecksumConfig withValidateChecksum(boolean validateChecksum) {
102         this.validateChecksum = validateChecksum;
103         return this;
104     }
105 
getValidateChecksum()106     public boolean getValidateChecksum() {
107         return validateChecksum;
108     }
109 
110     /**
111      * Ignored when validate_response_checksum is not set.
112      * If not set all the algorithms will be selected as default behavior.
113      *
114      * The list of algorithms for user to pick up when validate the checksum. Client
115      * will pick up the algorithm from the list with the priority based on
116      * performance, and the algorithm sent by server. The priority based on
117      * performance is [CRC32C, CRC32, SHA1, SHA256].
118      *
119      * If the response checksum was validated by client, the result will indicate
120      * which algorithm was picked.
121      *
122      * @param validateChecksumAlgorithmList The list of algorithm picked to validate
123      *                                      checksum from response.
124      * @return this
125      */
withValidateChecksumAlgorithmList(List<ChecksumAlgorithm> validateChecksumAlgorithmList)126     public ChecksumConfig withValidateChecksumAlgorithmList(List<ChecksumAlgorithm> validateChecksumAlgorithmList) {
127         this.validateChecksumAlgorithmList = validateChecksumAlgorithmList != null
128                 ? Collections.unmodifiableList(validateChecksumAlgorithmList)
129                 : null;
130         return this;
131     }
132 
133     /**
134      * @return The list of algorithm picked to validate checksum from response.
135      */
getValidateChecksumAlgorithmList()136     public List<ChecksumAlgorithm> getValidateChecksumAlgorithmList() {
137         return this.validateChecksumAlgorithmList;
138     }
139 }
140