Search in sources :

Example 61 with Bag

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

the class BagReader method read.

/**
 * Read the bag from the filesystem and create a bag object
 *
 * @param rootDir the root directory of the bag
 * @throws IOException if there is a problem reading a file
 * @return a {@link Bag} object representing a bag on the filesystem
 *
 * @throws UnparsableVersionException If there is a problem parsing the bagit version
 * @throws MaliciousPathException if there is path that is referenced in the manifest or fetch file that is outside the bag root directory
 * @throws InvalidBagMetadataException if the metadata or bagit.txt file does not conform to the bagit spec
 * @throws UnsupportedAlgorithmException if the manifest uses a algorithm that isn't supported
 * @throws InvalidBagitFileFormatException if the manifest or fetch file is not formatted properly
 */
public Bag read(final Path rootDir) throws IOException, UnparsableVersionException, MaliciousPathException, InvalidBagMetadataException, UnsupportedAlgorithmException, InvalidBagitFileFormatException {
    final Bag bag = new Bag();
    // @Incubating
    Path bagitDir = rootDir.resolve(".bagit");
    if (!Files.exists(bagitDir)) {
        bagitDir = rootDir;
    }
    bag.setRootDir(rootDir);
    final Path bagitFile = bagitDir.resolve("bagit.txt");
    final SimpleImmutableEntry<Version, Charset> bagitInfo = BagitTextFileReader.readBagitTextFile(bagitFile);
    bag.setVersion(bagitInfo.getKey());
    bag.setFileEncoding(bagitInfo.getValue());
    ManifestReader.readAllManifests(nameMapping, bagitDir, bag);
    bag.getMetadata().addAll(MetadataReader.readBagMetadata(bagitDir, bag.getFileEncoding()));
    final Path fetchFile = bagitDir.resolve("fetch.txt");
    if (Files.exists(fetchFile)) {
        bag.getItemsToFetch().addAll(FetchReader.readFetch(fetchFile, bag.getFileEncoding(), bag.getRootDir()));
    }
    return bag;
}
Also used : Path(java.nio.file.Path) Version(gov.loc.repository.bagit.domain.Version) Bag(gov.loc.repository.bagit.domain.Bag) Charset(java.nio.charset.Charset)

Example 62 with Bag

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

the class BagCreator method bagInPlace.

private static Bag bagInPlace(final Version version, final Path root, final Collection<SupportedAlgorithm> algorithms, final boolean includeHidden, final Metadata metadata) throws NoSuchAlgorithmException, IOException {
    final Bag bag = new Bag(version);
    logger.info(messages.getString("creating_bag"), bag.getVersion(), root);
    bag.setRootDir(root);
    moveDataFilesIfNeeded(bag, includeHidden);
    createBagitFile(bag);
    createPayloadManifests(bag, algorithms, includeHidden);
    createMetadataFile(bag, metadata);
    createTagManifests(bag, algorithms, includeHidden);
    return bag;
}
Also used : Bag(gov.loc.repository.bagit.domain.Bag)

Example 63 with Bag

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

the class BagitSuiteComplanceTest method testInvalidBags.

@Test
public void testInvalidBags() {
    int errorCount = 0;
    Bag bag;
    ConcurrentMap<Class<? extends Exception>, AtomicLong> map = new ConcurrentHashMap<>();
    for (Path invalidBagDir : visitor.getInvalidTestCases()) {
        try {
            bag = reader.read(invalidBagDir);
            verifier.isValid(bag, true);
            System.err.println(bag.getRootDir() + " should have failed but didn't!");
        } catch (InvalidBagitFileFormatException | IOException | UnparsableVersionException | MissingPayloadManifestException | MissingBagitFileException | MissingPayloadDirectoryException | FileNotInPayloadDirectoryException | InterruptedException | MaliciousPathException | CorruptChecksumException | VerificationException | UnsupportedAlgorithmException e) {
            logger.info("Found invalid os specific bag with message: {}", e.getMessage());
            map.putIfAbsent(e.getClass(), new AtomicLong(0));
            map.get(e.getClass()).incrementAndGet();
            errorCount++;
        }
    }
    assertEquals("every test case should throw an error", visitor.getInvalidTestCases().size(), errorCount);
    logger.debug("Count of all errors found in generic invalid cases: {}", map);
}
Also used : Path(java.nio.file.Path) MissingPayloadManifestException(gov.loc.repository.bagit.exceptions.MissingPayloadManifestException) MaliciousPathException(gov.loc.repository.bagit.exceptions.MaliciousPathException) CorruptChecksumException(gov.loc.repository.bagit.exceptions.CorruptChecksumException) Bag(gov.loc.repository.bagit.domain.Bag) IOException(java.io.IOException) FileNotInPayloadDirectoryException(gov.loc.repository.bagit.exceptions.FileNotInPayloadDirectoryException) CorruptChecksumException(gov.loc.repository.bagit.exceptions.CorruptChecksumException) UnparsableVersionException(gov.loc.repository.bagit.exceptions.UnparsableVersionException) MaliciousPathException(gov.loc.repository.bagit.exceptions.MaliciousPathException) MissingBagitFileException(gov.loc.repository.bagit.exceptions.MissingBagitFileException) UnsupportedAlgorithmException(gov.loc.repository.bagit.exceptions.UnsupportedAlgorithmException) IOException(java.io.IOException) MissingPayloadManifestException(gov.loc.repository.bagit.exceptions.MissingPayloadManifestException) MissingPayloadDirectoryException(gov.loc.repository.bagit.exceptions.MissingPayloadDirectoryException) VerificationException(gov.loc.repository.bagit.exceptions.VerificationException) FileNotInPayloadDirectoryException(gov.loc.repository.bagit.exceptions.FileNotInPayloadDirectoryException) InvalidBagitFileFormatException(gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException) MissingBagitFileException(gov.loc.repository.bagit.exceptions.MissingBagitFileException) AtomicLong(java.util.concurrent.atomic.AtomicLong) MissingPayloadDirectoryException(gov.loc.repository.bagit.exceptions.MissingPayloadDirectoryException) UnparsableVersionException(gov.loc.repository.bagit.exceptions.UnparsableVersionException) UnsupportedAlgorithmException(gov.loc.repository.bagit.exceptions.UnsupportedAlgorithmException) InvalidBagitFileFormatException(gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException) VerificationException(gov.loc.repository.bagit.exceptions.VerificationException) BeforeClass(org.junit.BeforeClass) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 64 with Bag

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

the class BagitSuiteComplanceTest method testReadWriteProducesSameBag.

@Test
public void testReadWriteProducesSameBag() throws Exception {
    Bag bag;
    Path newBagDir;
    for (final Path bagDir : visitor.getValidTestCases()) {
        newBagDir = folder.newFolder().toPath();
        bag = reader.read(bagDir);
        BagWriter.write(bag, newBagDir);
        testTagFileContents(bag, newBagDir);
        testBagsStructureAreEqual(bagDir, newBagDir);
    }
}
Also used : Path(java.nio.file.Path) Bag(gov.loc.repository.bagit.domain.Bag) Test(org.junit.Test)

Example 65 with Bag

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

the class ReaderWriterVerifierIntegrationTest method testReaderWriterVersion2_0.

@Test
public void testReaderWriterVersion2_0() throws Exception {
    BagReader reader = new BagReader();
    Path rootDir = Paths.get(this.getClass().getClassLoader().getResource("bags/v2_0/bag").toURI());
    Bag bag = reader.read(rootDir);
    Path outputDir = Paths.get(folder.newFolder().toURI());
    BagWriter.write(bag, outputDir);
    testBagsEqual(rootDir, outputDir);
    try (BagVerifier verifier = new BagVerifier()) {
        verifier.isValid(reader.read(outputDir), true);
    }
}
Also used : Path(java.nio.file.Path) BagVerifier(gov.loc.repository.bagit.verify.BagVerifier) BagReader(gov.loc.repository.bagit.reader.BagReader) Bag(gov.loc.repository.bagit.domain.Bag) Test(org.junit.Test)

Aggregations

Bag (gov.loc.repository.bagit.domain.Bag)71 Test (org.junit.Test)67 Path (java.nio.file.Path)55 File (java.io.File)38 PrivateConstructorTest (gov.loc.repository.bagit.PrivateConstructorTest)28 Version (gov.loc.repository.bagit.domain.Version)11 InputStream (java.io.InputStream)10 Manifest (gov.loc.repository.bagit.domain.Manifest)9 BagReader (gov.loc.repository.bagit.reader.BagReader)9 BagVerifier (gov.loc.repository.bagit.verify.BagVerifier)6 Metadata (gov.loc.repository.bagit.domain.Metadata)2 CorruptChecksumException (gov.loc.repository.bagit.exceptions.CorruptChecksumException)2 FileNotInPayloadDirectoryException (gov.loc.repository.bagit.exceptions.FileNotInPayloadDirectoryException)2 InvalidBagitFileFormatException (gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException)2 MaliciousPathException (gov.loc.repository.bagit.exceptions.MaliciousPathException)2 MissingBagitFileException (gov.loc.repository.bagit.exceptions.MissingBagitFileException)2 MissingPayloadDirectoryException (gov.loc.repository.bagit.exceptions.MissingPayloadDirectoryException)2 MissingPayloadManifestException (gov.loc.repository.bagit.exceptions.MissingPayloadManifestException)2 UnparsableVersionException (gov.loc.repository.bagit.exceptions.UnparsableVersionException)2 UnsupportedAlgorithmException (gov.loc.repository.bagit.exceptions.UnsupportedAlgorithmException)2