Search in sources :

Example 1 with Manifest

use of gov.loc.repository.bagit.domain.Manifest in project bagit-java by LibraryOfCongress.

the class BagProfileChecker method requiredManifestsExist.

@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static void requiredManifestsExist(final Set<Manifest> manifests, final List<String> requiredManifestTypes, final boolean isPayloadManifest) throws RequiredManifestNotPresentException {
    final Set<String> manifestTypesPresent = new HashSet<>();
    logger.debug(messages.getString("check_required_manifests_present"));
    for (final Manifest manifest : manifests) {
        manifestTypesPresent.add(manifest.getAlgorithm().getBagitName());
    }
    for (final String requiredManifestType : requiredManifestTypes) {
        if (!manifestTypesPresent.contains(requiredManifestType)) {
            final StringBuilder sb = new StringBuilder();
            if (isPayloadManifest) {
                sb.append("tag");
                sb.append(MessageFormatter.format(messages.getString("required_tag_manifest_type_not_present"), requiredManifestType).getMessage());
            } else {
                sb.append(MessageFormatter.format(messages.getString("required_manifest_type_not_present"), requiredManifestType).getMessage());
            }
            throw new RequiredManifestNotPresentException(sb.toString());
        }
    }
}
Also used : Manifest(gov.loc.repository.bagit.domain.Manifest) HashSet(java.util.HashSet) RequiredManifestNotPresentException(gov.loc.repository.bagit.exceptions.conformance.RequiredManifestNotPresentException)

Example 2 with Manifest

use of gov.loc.repository.bagit.domain.Manifest in project bagit-java by LibraryOfCongress.

the class BagCreator method calculatePayloadManifests.

private static Map<Manifest, MessageDigest> calculatePayloadManifests(final Bag bag, final Collection<SupportedAlgorithm> algorithms, final boolean includeHidden) throws NoSuchAlgorithmException, IOException {
    final Path dataDir = PathUtils.getDataDir(bag);
    logger.info(messages.getString("creating_payload_manifests"));
    final Map<Manifest, MessageDigest> payloadFilesMap = Hasher.createManifestToMessageDigestMap(algorithms);
    final CreatePayloadManifestsVistor payloadVisitor = new CreatePayloadManifestsVistor(payloadFilesMap, includeHidden);
    Files.walkFileTree(dataDir, payloadVisitor);
    return payloadFilesMap;
}
Also used : Path(java.nio.file.Path) Manifest(gov.loc.repository.bagit.domain.Manifest) MessageDigest(java.security.MessageDigest)

Example 3 with Manifest

use of gov.loc.repository.bagit.domain.Manifest in project bagit-java by LibraryOfCongress.

the class Hasher method createManifestToMessageDigestMap.

/**
 * create a mapping between {@link Manifest} and {@link MessageDigest} for each each supplied {@link SupportedAlgorithm}
 *
 * @param algorithms the {@link SupportedAlgorithm} that you which to map to {@link MessageDigest}
 * @return mapping between {@link Manifest} and {@link MessageDigest}
 * @throws NoSuchAlgorithmException if {@link MessageDigest} doesn't support the algorithm
 */
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public static Map<Manifest, MessageDigest> createManifestToMessageDigestMap(final Collection<SupportedAlgorithm> algorithms) throws NoSuchAlgorithmException {
    final Map<Manifest, MessageDigest> map = new HashMap<>();
    for (final SupportedAlgorithm algorithm : algorithms) {
        final MessageDigest messageDigest = MessageDigest.getInstance(algorithm.getMessageDigestName());
        final Manifest manifest = new Manifest(algorithm);
        map.put(manifest, messageDigest);
    }
    return map;
}
Also used : HashMap(java.util.HashMap) Manifest(gov.loc.repository.bagit.domain.Manifest) MessageDigest(java.security.MessageDigest)

Example 4 with Manifest

use of gov.loc.repository.bagit.domain.Manifest in project bagit-java by LibraryOfCongress.

the class BagReaderTest method testReadBagWithEncodedNames.

@Test
public void testReadBagWithEncodedNames() throws Exception {
    Path rootDir = Paths.get(getClass().getClassLoader().getResource("bags/v0_96/bag-with-encoded-names").toURI());
    Bag bag = sut.read(rootDir);
    assertNotNull(bag);
    for (Manifest payloadManifest : bag.getPayLoadManifests()) {
        for (Path file : payloadManifest.getFileToChecksumMap().keySet()) {
            assertTrue(file + " should exist but it doesn't!", Files.exists(file));
        }
    }
}
Also used : Path(java.nio.file.Path) Bag(gov.loc.repository.bagit.domain.Bag) Manifest(gov.loc.repository.bagit.domain.Manifest) Test(org.junit.Test)

Example 5 with Manifest

use of gov.loc.repository.bagit.domain.Manifest in project bagit-java by LibraryOfCongress.

the class BagReaderTest method testReadVersion0_97Bag.

@Test
public void testReadVersion0_97Bag() throws Exception {
    Path rootBag = Paths.get(new File("src/test/resources/bags/v0_97/bag").toURI());
    Path[] payloadFiles = new Path[] { rootBag.resolve("data/dir1/test3.txt"), rootBag.resolve("data/dir2/dir3/test5.txt"), rootBag.resolve("data/dir2/test4.txt"), rootBag.resolve("data/test1.txt"), rootBag.resolve("data/test2.txt") };
    Bag returnedBag = sut.read(rootBag);
    assertNotNull(returnedBag);
    assertEquals(new Version(0, 97), returnedBag.getVersion());
    Manifest payloadManifest = (Manifest) returnedBag.getPayLoadManifests().toArray()[0];
    for (Path payloadFile : payloadFiles) {
        assertTrue(payloadManifest.getFileToChecksumMap().containsKey(payloadFile));
    }
}
Also used : Path(java.nio.file.Path) Version(gov.loc.repository.bagit.domain.Version) Bag(gov.loc.repository.bagit.domain.Bag) Manifest(gov.loc.repository.bagit.domain.Manifest) File(java.io.File) Test(org.junit.Test)

Aggregations

Manifest (gov.loc.repository.bagit.domain.Manifest)27 Path (java.nio.file.Path)21 Test (org.junit.Test)16 Bag (gov.loc.repository.bagit.domain.Bag)9 File (java.io.File)8 HashSet (java.util.HashSet)7 PrivateConstructorTest (gov.loc.repository.bagit.PrivateConstructorTest)5 Version (gov.loc.repository.bagit.domain.Version)4 MessageDigest (java.security.MessageDigest)4 SupportedAlgorithm (gov.loc.repository.bagit.hash.SupportedAlgorithm)2 HashMap (java.util.HashMap)2 FetchItem (gov.loc.repository.bagit.domain.FetchItem)1 RequiredManifestNotPresentException (gov.loc.repository.bagit.exceptions.conformance.RequiredManifestNotPresentException)1 StandardSupportedAlgorithms (gov.loc.repository.bagit.hash.StandardSupportedAlgorithms)1