|
3 | 3 | import static io.github.jpmorganchase.fusion.api.tools.ResponseChecker.checkResponseStatus; |
4 | 4 |
|
5 | 5 | import io.github.jpmorganchase.fusion.FusionConfiguration; |
| 6 | +import io.github.jpmorganchase.fusion.api.exception.FileDownloadException; |
6 | 7 | import io.github.jpmorganchase.fusion.api.response.GetPartResponse; |
7 | 8 | import io.github.jpmorganchase.fusion.api.response.Head; |
8 | 9 | import io.github.jpmorganchase.fusion.api.stream.IntegrityCheckingInputStream; |
@@ -55,23 +56,32 @@ public GetPartResponse fetch(PartRequest pr) { |
55 | 56 | } |
56 | 57 |
|
57 | 58 | private InputStream getIntegrityCheckingInputStream(HttpResponse<InputStream> response, Head head, PartRequest pr) { |
58 | | - if (shouldSkipChecksumValidation(head, pr.getDownloadRequest())) { |
59 | | - return response.getBody(); |
| 59 | + String checksum = head.getChecksum(); |
| 60 | + boolean checksumMissing = Objects.isNull(checksum) || checksum.isEmpty(); |
| 61 | + DownloadRequest downloadRequest = pr.getDownloadRequest(); |
| 62 | + |
| 63 | + if (checksumMissing) { |
| 64 | + if (downloadRequest.isSkipChecksumValidationIfMissing()) { |
| 65 | + log.debug( |
| 66 | + "Skipping checksum validation for download request {}", |
| 67 | + downloadRequest); |
| 68 | + return response.getBody(); |
| 69 | + } else { |
| 70 | + throw new FileDownloadException( |
| 71 | + "Checksum validation failed: checksum is missing for download request: " |
| 72 | + + downloadRequest); |
| 73 | + } |
60 | 74 | } |
61 | 75 |
|
| 76 | + log.debug("Verifying checksum for download request {} with checksum: {}", downloadRequest, checksum); |
62 | 77 | return IntegrityCheckingInputStream.builder() |
63 | 78 | .part(response.getBody()) |
64 | | - .checksum(head.getChecksum()) |
| 79 | + .checksum(checksum) |
65 | 80 | .partChecker( |
66 | 81 | PartChecker.builder().digestAlgo(getDigestAlgo(head)).build()) |
67 | 82 | .build(); |
68 | 83 | } |
69 | 84 |
|
70 | | - private boolean shouldSkipChecksumValidation(Head head, DownloadRequest downloadRequest) { |
71 | | - String checksum = head.getChecksum(); |
72 | | - return (Objects.isNull(checksum) || checksum.isEmpty()) && downloadRequest.isSkipChecksumValidationIfMissing(); |
73 | | - } |
74 | | - |
75 | 85 | private String getDigestAlgo(Head head) { |
76 | 86 | return head.getChecksumAlgorithm() != null ? head.getChecksumAlgorithm() : configuration.getDigestAlgorithm(); |
77 | 87 | } |
|
0 commit comments