Search in sources :

Example 21 with Manifest

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

the class PayloadWriter method writePayloadFiles.

/**
 * Write the payload <b>file(s)</b> to the output directory
 *
 * @param payloadManifests the set of objects representing the payload manifests
 * @param fetchItems the list of items to exclude from writing in the output directory because they will be fetched
 * @param outputDir the data directory of the bag
 * @param bagDataDir the data directory of the bag
 *
 * @throws IOException if there was a problem writing a file
 */
public static void writePayloadFiles(final Set<Manifest> payloadManifests, final List<FetchItem> fetchItems, final Path outputDir, final Path bagDataDir) throws IOException {
    logger.info(messages.getString("writing_payload_files"));
    final Set<Path> fetchPaths = getFetchPaths(fetchItems, bagDataDir);
    for (final Manifest payloadManifest : payloadManifests) {
        for (final Path payloadFile : payloadManifest.getFileToChecksumMap().keySet()) {
            final Path relativePayloadPath = bagDataDir.relativize(payloadFile);
            if (fetchPaths.contains(relativePayloadPath.normalize())) {
                logger.info(messages.getString("skip_fetch_item_when_writing_payload"), payloadFile);
            } else {
                final Path writeToPath = outputDir.resolve(relativePayloadPath);
                logger.debug(messages.getString("writing_payload_file_to_path"), payloadFile, writeToPath);
                final Path parent = writeToPath.getParent();
                if (parent != null) {
                    Files.createDirectories(parent);
                }
                Files.copy(payloadFile, writeToPath, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) Manifest(gov.loc.repository.bagit.domain.Manifest)

Example 22 with Manifest

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

the class AddPayloadToBagManifestVistorTest method includeDotKeepFilesInManifest.

@Test
public void includeDotKeepFilesInManifest() throws Exception {
    Manifest manifest = new Manifest(StandardSupportedAlgorithms.MD5);
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    Map<Manifest, MessageDigest> map = new HashMap<>();
    map.put(manifest, messageDigest);
    boolean includeHiddenFiles = false;
    Path start = Paths.get(new File("src/test/resources/dotKeepExampleBag").toURI()).resolve("data");
    CreatePayloadManifestsVistor sut = new CreatePayloadManifestsVistor(map, includeHiddenFiles);
    Files.walkFileTree(start, sut);
    assertEquals(1, manifest.getFileToChecksumMap().size());
    assertTrue(manifest.getFileToChecksumMap().containsKey(start.resolve("fooDir/.keep")));
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) Manifest(gov.loc.repository.bagit.domain.Manifest) MessageDigest(java.security.MessageDigest) File(java.io.File) Test(org.junit.Test)

Example 23 with Manifest

use of gov.loc.repository.bagit.domain.Manifest 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)

Example 24 with Manifest

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

the class ManifestWriterTest method testManifestsDontContainWindowsFilePathSeparator.

@Test
public void testManifestsDontContainWindowsFilePathSeparator() throws IOException {
    Set<Manifest> tagManifests = new HashSet<>();
    Manifest manifest = new Manifest(StandardSupportedAlgorithms.MD5);
    manifest.getFileToChecksumMap().put(Paths.get("/foo/bar/ham/data/one/two/buckleMyShoe.txt"), "someHashValue");
    tagManifests.add(manifest);
    File outputDir = folder.newFolder();
    File tagManifest = new File(outputDir, "tagmanifest-md5.txt");
    assertFalse(tagManifest.exists());
    ManifestWriter.writeTagManifests(tagManifests, Paths.get(outputDir.toURI()), Paths.get("/foo/bar/ham"), StandardCharsets.UTF_8);
    List<String> lines = Files.readAllLines(Paths.get(tagManifest.toURI()));
    for (String line : lines) {
        assertFalse("Line [" + line + "] contains \\ which is not allowed by the bagit specification", line.contains("\\"));
    }
}
Also used : Manifest(gov.loc.repository.bagit.domain.Manifest) File(java.io.File) HashSet(java.util.HashSet) PrivateConstructorTest(gov.loc.repository.bagit.PrivateConstructorTest) Test(org.junit.Test)

Example 25 with Manifest

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

the class ManifestWriterTest method testWriteTagManifests.

@Test
public void testWriteTagManifests() throws IOException {
    Set<Manifest> tagManifests = new HashSet<>();
    Manifest manifest = new Manifest(StandardSupportedAlgorithms.MD5);
    manifest.getFileToChecksumMap().put(Paths.get("/foo/bar/ham/data/one/two/buckleMyShoe.txt"), "someHashValue");
    tagManifests.add(manifest);
    File outputDir = folder.newFolder();
    File tagManifest = new File(outputDir, "tagmanifest-md5.txt");
    assertFalse(tagManifest.exists());
    ManifestWriter.writeTagManifests(tagManifests, Paths.get(outputDir.toURI()), Paths.get("/foo/bar/ham"), StandardCharsets.UTF_8);
    assertTrue(tagManifest.exists());
}
Also used : Manifest(gov.loc.repository.bagit.domain.Manifest) File(java.io.File) HashSet(java.util.HashSet) PrivateConstructorTest(gov.loc.repository.bagit.PrivateConstructorTest) 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