private void verifyChecksums ( final Collection < URI > checksums , final Property dataProperty ) throws InvalidChecksumException { final Map < URI , URI > checksumErrors = new HashMap <> ( ) ; // Loop through provided checksums validating against computed values checksums . forEach ( checksum -> { final String algorithm = ContentDigest . getAlgorithm ( checksum ) ; try { // The case internally supported by ModeShape if ( algorithm . equals ( SHA1 . algorithm ) ) { final String dsSHA1 = ( ( Binary ) dataProperty . getBinary ( ) ) . getHexHash ( ) ; final URI dsSHA1Uri = ContentDigest . asURI ( SHA1 . algorithm , dsSHA1 ) ; if ( ! dsSHA1Uri . equals ( checksum ) ) { LOGGER . debug ( "Failed checksum test" ) ; checksumErrors . put ( checksum , dsSHA1Uri ) ; } // The case that requires re-computing the checksum } else { final CacheEntry cacheEntry = CacheEntryFactory . forProperty ( dataProperty ) ; cacheEntry . checkFixity ( algorithm ) . stream ( ) . findFirst ( ) . ifPresent ( fixityResult -> { if ( ! fixityResult . matches ( checksum ) ) { LOGGER . debug ( "Failed checksum test" ) ; checksumErrors . put ( checksum , fixityResult . getComputedChecksum ( ) ) ; } } ) ; } } catch ( final RepositoryException e ) { throw new RepositoryRuntimeException ( e ) ; } } ) ; // Throw an exception if any checksum errors occurred if ( ! checksumErrors . isEmpty ( ) ) { final String template = "Checksum Mismatch of %1$s and %2$s\n" ; final StringBuilder error = new StringBuilder ( ) ; checksumErrors . forEach ( ( key , value ) -> error . append ( String . format ( template , key , value ) ) ) ; throw new InvalidChecksumException ( error . toString ( ) ) ; } }
This method ensures that the arg checksums are valid against the binary associated with the arg dataProperty . If one or more of the checksums are invalid an InvalidChecksumException is thrown .