Skip to content

Commit 1a1c00a

Browse files
committed
address feedback
1 parent 14ae980 commit 1a1c00a

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

src/main/java/software/amazon/encryption/s3/internal/ContentMetadataStrategy.java

+13-28
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
package software.amazon.encryption.s3.internal;
44

5-
import software.amazon.awssdk.core.ResponseBytes;
5+
import software.amazon.awssdk.core.ResponseInputStream;
66
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
77
import software.amazon.awssdk.protocols.jsoncore.JsonNode;
88
import software.amazon.awssdk.protocols.jsoncore.JsonNodeParser;
@@ -18,6 +18,7 @@
1818
import software.amazon.encryption.s3.materials.EncryptionMaterials;
1919
import software.amazon.encryption.s3.materials.S3Keyring;
2020

21+
import java.io.IOException;
2122
import java.nio.charset.StandardCharsets;
2223
import java.util.Base64;
2324
import java.util.HashMap;
@@ -40,38 +41,22 @@ public ContentMetadata decodeMetadata(GetObjectRequest getObjectRequest, GetObje
4041
.key(getObjectRequest.key() + INSTRUCTION_FILE_SUFFIX)
4142
.build();
4243

43-
try (ResponseInputStream<GetObjectResponse> instruction = s3AsyncClient.getObject(instructionGetObjectRequest, AsyncResponseTransformer.toBlockingInputStream()).join()){
44-
Map<String, String> metadata = new HashMap<>();
45-
JsonNodeParser parser = JsonNodeParser.create();
46-
JsonNode objectNode = parser.parse(instruction);
47-
for (Map.Entry<String, JsonNode> entry : objectNode.asObject().entrySet()) {
48-
metadata.put(entry.getKey(), entry.getValue().asString());
49-
}
50-
return ContentMetadataStrategy.readFromMap(metadata, response);
51-
} catch (IOException ioException) {
52-
throw new S3EncryptionClientException("IOException while reading instruction file!", ioException);
53-
} catch (NoSuchKeyException exception) {
54-
// Most likely, the customer is attempting to decrypt an object
55-
// which is not encrypted with the S3 EC.
56-
throw new S3EncryptionClientException("Instruction file not found! Please ensure the object you are" +
57-
" attempting to decrypt has been encrypted using the S3 Encryption Client.", exception);
58-
}
59-
try {
60-
instruction = s3AsyncClient.getObject(instructionGetObjectRequest, AsyncResponseTransformer.toBytes()).join();
44+
try (ResponseInputStream<GetObjectResponse> instruction = s3AsyncClient.getObject(instructionGetObjectRequest, AsyncResponseTransformer.toBlockingInputStream()).join()){
45+
Map<String, String> metadata = new HashMap<>();
46+
JsonNodeParser parser = JsonNodeParser.create();
47+
JsonNode objectNode = parser.parse(instruction);
48+
for (Map.Entry<String, JsonNode> entry : objectNode.asObject().entrySet()) {
49+
metadata.put(entry.getKey(), entry.getValue().asString());
50+
}
51+
return ContentMetadataStrategy.readFromMap(metadata, response);
52+
} catch (IOException ioException) {
53+
throw new S3EncryptionClientException("IOException while reading instruction file!", ioException);
6154
} catch (NoSuchKeyException exception) {
6255
// Most likely, the customer is attempting to decrypt an object
6356
// which is not encrypted with the S3 EC.
6457
throw new S3EncryptionClientException("Instruction file not found! Please ensure the object you are" +
65-
" attempting to decrypt has been encrypted using the S3 Encryption Client.", exception);
66-
}
67-
68-
Map<String, String> metadata = new HashMap<>();
69-
JsonNodeParser parser = JsonNodeParser.create();
70-
JsonNode objectNode = parser.parse(instruction.asByteArray());
71-
for (Map.Entry<String, JsonNode> entry : objectNode.asObject().entrySet()) {
72-
metadata.put(entry.getKey(), entry.getValue().asString());
58+
" attempting to decrypt has been encrypted using the S3 Encryption Client.", exception);
7359
}
74-
return ContentMetadataStrategy.readFromMap(metadata, response);
7560
}
7661
};
7762

0 commit comments

Comments
 (0)