Search in sources :

Example 1 with SupportedAlgorithm

use of gov.loc.repository.bagit.hash.SupportedAlgorithm in project bagit-java by LibraryOfCongress.

the class BagVerifierTest method testVerificationExceptionIsThrownForNoSuchAlgorithmException.

@Test(expected = VerificationException.class)
public void testVerificationExceptionIsThrownForNoSuchAlgorithmException() throws Exception {
    Path unreadableFile = Paths.get(folder.newFile().toURI());
    Manifest manifest = new Manifest(new SupportedAlgorithm() {

        @Override
        public String getMessageDigestName() {
            return "FOO";
        }

        @Override
        public String getBagitName() {
            return "foo";
        }
    });
    manifest.getFileToChecksumMap().put(unreadableFile, "foo");
    sut.checkHashes(manifest);
}
Also used : Path(java.nio.file.Path) Manifest(gov.loc.repository.bagit.domain.Manifest) SupportedAlgorithm(gov.loc.repository.bagit.hash.SupportedAlgorithm) Test(org.junit.Test)

Example 2 with SupportedAlgorithm

use of gov.loc.repository.bagit.hash.SupportedAlgorithm in project bagit-java by LibraryOfCongress.

the class ManifestReader method readManifest.

/**
 * Reads a manifest file and converts it to a {@link Manifest} object.
 *
 * @param nameMapping a map between BagIt algorithm names and {@link MessageDigest} names
 * @param manifestFile a specific manifest file
 * @param bagRootDir the root directory of the bag
 * @param charset the encoding to use when reading the manifest file
 * @return the converted manifest object from the file
 *
 * @throws IOException if there is a problem reading a file
 * @throws MaliciousPathException if there is path that is referenced in the manifest that is outside the bag root directory
 * @throws UnsupportedAlgorithmException if the manifest uses a algorithm that isn't supported
 * @throws InvalidBagitFileFormatException if the manifest is not formatted properly
 */
public static Manifest readManifest(final BagitAlgorithmNameToSupportedAlgorithmMapping nameMapping, final Path manifestFile, final Path bagRootDir, final Charset charset) throws IOException, MaliciousPathException, UnsupportedAlgorithmException, InvalidBagitFileFormatException {
    logger.debug(messages.getString("reading_manifest"), manifestFile);
    final String alg = PathUtils.getFilename(manifestFile).split("[-\\.]")[1];
    final SupportedAlgorithm algorithm = nameMapping.getSupportedAlgorithm(alg);
    final Manifest manifest = new Manifest(algorithm);
    final Map<Path, String> filetToChecksumMap = readChecksumFileMap(manifestFile, bagRootDir, charset);
    manifest.setFileToChecksumMap(filetToChecksumMap);
    return manifest;
}
Also used : Path(java.nio.file.Path) Manifest(gov.loc.repository.bagit.domain.Manifest) SupportedAlgorithm(gov.loc.repository.bagit.hash.SupportedAlgorithm)

Aggregations

Manifest (gov.loc.repository.bagit.domain.Manifest)2 SupportedAlgorithm (gov.loc.repository.bagit.hash.SupportedAlgorithm)2 Path (java.nio.file.Path)2 Test (org.junit.Test)1