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;
}
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;
}
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);
}
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);
}
}
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);
}
}
Aggregations