private String handleWantDigestHeader(final FedoraBinary binary, final String wantDigest) throws UnsupportedAlgorithmException { // handle the Want-Digest header with fixity check final Collection<String> preferredDigests = parseWantDigestHeader(wantDigest); if (preferredDigests.isEmpty()) { throw new UnsupportedAlgorithmException( "Unsupported digest algorithm provided in 'Want-Digest' header: " + wantDigest); } final Collection<URI> checksumResults = binary.checkFixity(idTranslator, preferredDigests); return checksumResults.stream().map(uri -> uri.toString().replaceFirst("urn:", "") .replaceFirst(":", "=").replaceFirst("sha1=", "sha=")).collect(Collectors.joining(",")); }
@Override public Response toResponse(final UnsupportedAlgorithmException e) { debugException(this, e, LOGGER); return status(BAD_REQUEST).entity(e.getMessage()).type(TEXT_PLAIN_WITH_CHARSET).build(); }
/** * Parse the RFC-3230 Digest response header value. Look for a sha1 checksum and return it as a urn, if missing or * malformed an empty string is returned. * * @param digest The Digest header value * @return the sha1 checksum value * @throws UnsupportedAlgorithmException if an unsupported digest is used */ protected static Collection<String> parseDigestHeader(final String digest) throws UnsupportedAlgorithmException { try { final Map<String, String> digestPairs = RFC3230_SPLITTER.split(nullToEmpty(digest)); final boolean allSupportedAlgorithms = digestPairs.keySet().stream().allMatch( ContentDigest.DIGEST_ALGORITHM::isSupportedAlgorithm); // If you have one or more digests that are all valid or no digests. if (digestPairs.isEmpty() || allSupportedAlgorithms) { return digestPairs.entrySet().stream() .filter(entry -> ContentDigest.DIGEST_ALGORITHM.isSupportedAlgorithm(entry.getKey())) .map(entry -> ContentDigest.asURI(entry.getKey(), entry.getValue()).toString()) .collect(Collectors.toSet()); } else { throw new UnsupportedAlgorithmException(String.format("Unsupported Digest Algorithim: %1$s", digest)); } } catch (final RuntimeException e) { if (e instanceof IllegalArgumentException) { throw new ClientErrorException("Invalid Digest header: " + digest + "\n", BAD_REQUEST); } throw e; } }
/** * Parse the RFC-3230 Digest response header value. Look for a * sha1 checksum and return it as a urn, if missing or malformed * an empty string is returned. * @param digest The Digest header value * @return the sha1 checksum value * @throws UnsupportedAlgorithmException if an unsupported digest is used */ protected static Collection<String> parseDigestHeader(final String digest) throws UnsupportedAlgorithmException { try { final Map<String,String> digestPairs = RFC3230_SPLITTER.split(nullToEmpty(digest)); final boolean allSupportedAlgorithms = digestPairs.keySet().stream().allMatch( ContentDigest.DIGEST_ALGORITHM::isSupportedAlgorithm); // If you have one or more digests that are all valid or no digests. if (digestPairs.isEmpty() || allSupportedAlgorithms) { return digestPairs.entrySet().stream() .filter(entry -> ContentDigest.DIGEST_ALGORITHM.isSupportedAlgorithm(entry.getKey())) .map(entry -> ContentDigest.asURI(entry.getKey(), entry.getValue()).toString()) .collect(Collectors.toSet()); } else { throw new UnsupportedAlgorithmException(String.format("Unsupported Digest Algorithm: %1$s", digest)); } } catch (final RuntimeException e) { if (e instanceof IllegalArgumentException) { throw new ClientErrorException("Invalid Digest header: " + digest + "\n", BAD_REQUEST); } throw e; } }
digestInputStreams.put(digestAlg, (DigestInputStream)digestStream); } catch (NoSuchAlgorithmException e) { throw new UnsupportedAlgorithmException("Unsupported digest algorithm: " + digestAlg);