Skip to content

Commit

Permalink
MCR-3303 account for polymorphic return values of MCRFileAttributes#d…
Browse files Browse the repository at this point in the history
…igest()
  • Loading branch information
toKrause committed Nov 20, 2024
1 parent 20e07c7 commit b243c5e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,18 @@ protected MCRDigest(byte[] value) throws MCRDigestValidationException {
validate();
}


/**
* Returns the value of the digest as a byte array.
*
* @return Digest value.
*/
public byte[] toBytes() {
return Arrays.copyOf(value, value.length);
}

/**
* Returns the value of the digest.
* Returns the value of the digest as a hex encoded string.
*
* @return Digest value.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.Base64;
import java.util.Comparator;
import java.util.Date;
import java.util.HexFormat;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -54,6 +53,7 @@
import org.mycore.common.config.MCRConfiguration2;
import org.mycore.common.content.MCRPathContent;
import org.mycore.common.content.util.MCRRestContentHelper;
import org.mycore.common.digest.MCRDigest;
import org.mycore.datamodel.metadata.MCRObjectID;
import org.mycore.datamodel.niofs.MCRDigestAttributeView;
import org.mycore.datamodel.niofs.MCRFileAttributes;
Expand Down Expand Up @@ -252,9 +252,10 @@ private static int getUploadMemThreshold() {
* @see <a href="https://tools.ietf.org/html/rfc3230">RFC 3230</a>
* @see <a href="https://tools.ietf.org/html/rfc5843">RFC 45843</a>
*/
private static String getDigestHeader(String md5sum) {
final String md5Base64 = Base64.getEncoder().encodeToString(HexFormat.of().parseHex(md5sum));
return "md5=:" + md5Base64 + ":";
private static String getDigestHeader(MCRDigest digest) {
final String algorithm = digest.getAlgorithm().toLowerCase();
final String encodedValue = Base64.getEncoder().encodeToString(digest.toBytes());
return algorithm + "=:" + encodedValue + ":";
}

@HEAD
Expand Down Expand Up @@ -298,7 +299,7 @@ public Response getFileOrDirectoryMetadata() {
.lastModified(Date.from(fileAttributes.lastModifiedTime().toInstant()))
.header(HttpHeaders.CONTENT_LENGTH, fileAttributes.size())
.tag(getETag(fileAttributes))
.header("Repr-Digest", getDigestHeader(fileAttributes.digest().toHexString()))
.header("Repr-Digest", getDigestHeader(fileAttributes.digest()))
.build();
}

Expand Down Expand Up @@ -338,7 +339,7 @@ public Response getFileOrDirectory(@Context UriInfo uriInfo, @Context HttpHeader
content.setMimeType(context.getMimeType(mcrPath.getFileName().toString()));
try {
final List<Map.Entry<String, String>> responseHeader = List
.of(Map.entry("Repr-Digest", getDigestHeader(fileAttributes.digest().toHexString())));
.of(Map.entry("Repr-Digest", getDigestHeader(fileAttributes.digest())));
return MCRRestContentHelper.serveContent(content, uriInfo, requestHeader, responseHeader);
} catch (IOException e) {
throw MCRErrorResponse.fromStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())
Expand Down

0 comments on commit b243c5e

Please sign in to comment.