Search in sources :

Example 6 with Manifest

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

the class BagReaderTest method testReadBagWithEscapableCharacter.

@Test
public void testReadBagWithEscapableCharacter() throws Exception {
    Path rootDir = Paths.get(getClass().getClassLoader().getResource("bags/v0_96/bag-with-escapable-characters").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 7 with Manifest

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

the class BagVerifierTest method testStandardSupportedAlgorithms.

@Test
public void testStandardSupportedAlgorithms() throws Exception {
    List<String> algorithms = Arrays.asList("md5", "sha1", "sha256", "sha512");
    for (String alg : algorithms) {
        StandardSupportedAlgorithms algorithm = StandardSupportedAlgorithms.valueOf(alg.toUpperCase());
        Manifest manifest = new Manifest(algorithm);
        sut.checkHashes(manifest);
    }
}
Also used : StandardSupportedAlgorithms(gov.loc.repository.bagit.hash.StandardSupportedAlgorithms) Manifest(gov.loc.repository.bagit.domain.Manifest) Test(org.junit.Test)

Example 8 with Manifest

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

the class BagWriter method updateTagManifests.

/*
   * Update the tag manifest cause the checksum of the other tag files will have changed since we just wrote them out to disk
   */
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static Set<Manifest> updateTagManifests(final Bag bag, final Path newBagRootDir) throws NoSuchAlgorithmException, IOException {
    final Set<Manifest> newManifests = new HashSet<>();
    for (final Manifest tagManifest : bag.getTagManifests()) {
        final Manifest newManifest = new Manifest(tagManifest.getAlgorithm());
        for (final Path originalPath : tagManifest.getFileToChecksumMap().keySet()) {
            final Path relativePath = bag.getRootDir().relativize(originalPath);
            final Path pathToUpdate = newBagRootDir.resolve(relativePath);
            final MessageDigest messageDigest = MessageDigest.getInstance(tagManifest.getAlgorithm().getMessageDigestName());
            final String newChecksum = Hasher.hash(pathToUpdate, messageDigest);
            newManifest.getFileToChecksumMap().put(pathToUpdate, newChecksum);
        }
        newManifests.add(newManifest);
    }
    return newManifests;
}
Also used : Path(java.nio.file.Path) Manifest(gov.loc.repository.bagit.domain.Manifest) MessageDigest(java.security.MessageDigest) HashSet(java.util.HashSet)

Example 9 with Manifest

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

the class BagWriter method write.

/**
 * Write the bag out to the specified directory.
 * If an error occurs some of the files may have been written out to the filesystem.
 * tag manifest(s) are updated prior to writing to ensure bag is valid after completion,
 * it is therefore recommended if you are going to further interact with the bag to read it from specified outputDir path
 *
 * @param bag the {@link Bag} object to write out
 * @param outputDir the output directory that will become the root of the bag
 *
 * @throws IOException if there is a problem writing a file
 * @throws NoSuchAlgorithmException when trying to generate a {@link MessageDigest} which is used during update.
 */
public static void write(final Bag bag, final Path outputDir) throws IOException, NoSuchAlgorithmException {
    logger.debug(messages.getString("writing_payload_files"));
    final Path bagitDir = PayloadWriter.writeVersionDependentPayloadFiles(bag, outputDir);
    logger.debug(messages.getString("upsert_payload_oxum"));
    final String payloadOxum = PathUtils.generatePayloadOxum(PathUtils.getDataDir(bag.getVersion(), outputDir));
    bag.getMetadata().upsertPayloadOxum(payloadOxum);
    logger.debug(messages.getString("writing_bagit_file"));
    BagitFileWriter.writeBagitFile(bag.getVersion(), bag.getFileEncoding(), bagitDir);
    logger.debug(messages.getString("writing_payload_manifests"));
    ManifestWriter.writePayloadManifests(bag.getPayLoadManifests(), bagitDir, bag.getRootDir(), bag.getFileEncoding());
    if (!bag.getMetadata().isEmpty()) {
        logger.debug(messages.getString("writing_bag_metadata"));
        MetadataWriter.writeBagMetadata(bag.getMetadata(), bag.getVersion(), bagitDir, bag.getFileEncoding());
    }
    if (bag.getItemsToFetch().size() > 0) {
        logger.debug(messages.getString("writing_fetch_file"));
        FetchWriter.writeFetchFile(bag.getItemsToFetch(), bagitDir, bag.getRootDir(), bag.getFileEncoding());
    }
    if (bag.getTagManifests().size() > 0) {
        logger.debug(messages.getString("writing_tag_manifests"));
        writeTagManifestFiles(bag.getTagManifests(), bagitDir, bag.getRootDir());
        final Set<Manifest> updatedTagManifests = updateTagManifests(bag, outputDir);
        bag.setTagManifests(updatedTagManifests);
        ManifestWriter.writeTagManifests(updatedTagManifests, bagitDir, outputDir, bag.getFileEncoding());
    }
}
Also used : Path(java.nio.file.Path) Manifest(gov.loc.repository.bagit.domain.Manifest)

Example 10 with Manifest

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

the class BagCreatorTest method testBagInPlaceIncludingHidden.

@Test
public void testBagInPlaceIncludingHidden() throws IOException, NoSuchAlgorithmException {
    TestStructure structure = createTestStructure();
    Bag bag = BagCreator.bagInPlace(Paths.get(folder.getRoot().toURI()), Arrays.asList(StandardSupportedAlgorithms.MD5), true);
    assertEquals(new Version(0, 97), bag.getVersion());
    File expectedManifest = new File(folder.getRoot(), "manifest-md5.txt");
    assertTrue(expectedManifest.exists());
    File expectedTagManifest = new File(folder.getRoot(), "tagmanifest-md5.txt");
    assertTrue(expectedTagManifest.exists());
    File bagitFile = new File(folder.getRoot(), "bagit.txt");
    assertTrue(bagitFile.exists());
    for (Manifest manifest : bag.getPayLoadManifests()) {
        for (Path expectedPayloadFile : manifest.getFileToChecksumMap().keySet()) {
            assertTrue(expectedPayloadFile + " doesn't exist but it should!", structure.regularPayloadFiles.contains(expectedPayloadFile) || structure.hiddenPayloadFiles.contains(expectedPayloadFile));
        }
    }
}
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