Search in sources :

Example 66 with Bag

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

the class ReaderWriterVerifierIntegrationTest method testReaderWriterVersion97.

@Test
public void testReaderWriterVersion97() throws Exception {
    BagReader reader = new BagReader();
    Path rootDir = Paths.get(this.getClass().getClassLoader().getResource("bags/v0_97/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)

Example 67 with Bag

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

the class ReaderWriterVerifierIntegrationTest method testReaderWriterVersion93.

@Test
public void testReaderWriterVersion93() throws Exception {
    try (BagVerifier verifier = new BagVerifier()) {
        BagReader reader = new BagReader();
        Path rootDir = Paths.get(this.getClass().getClassLoader().getResource("bags/v0_93/bag").toURI());
        Path outputDir = Paths.get(folder.newFolder().toURI());
        Bag bag = reader.read(rootDir);
        verifier.isValid(bag, true);
        BagWriter.write(bag, outputDir);
        testBagsEqual(rootDir, outputDir);
        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)

Example 68 with Bag

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

the class ReaderWriterVerifierIntegrationTest method testReaderWriterVersion96.

@Test
public void testReaderWriterVersion96() throws Exception {
    BagReader reader = new BagReader();
    Path rootDir = Paths.get(this.getClass().getClassLoader().getResource("bags/v0_96/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)

Example 69 with Bag

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

the class ReaderWriterVerifierIntegrationTest method testReaderWriterVersion94.

@Test
public void testReaderWriterVersion94() throws Exception {
    BagReader reader = new BagReader();
    Path rootDir = Paths.get(this.getClass().getClassLoader().getResource("bags/v0_94/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)

Example 70 with Bag

use of gov.loc.repository.bagit.domain.Bag in project epadd by ePADD.

the class ArchiveReaderWriter method saveArchive.

/**
 * saves the archive in the current session to the cachedir. note: no blobs saved.
 */
/* mode attributes select if this archive was already part of a bag or is a first time creation. Based on this flag the ouptput directory
    changes. In case of incremental bag update, the files will be in basedir/data/ subfolder whereas in case of fresh creation the files will be in
    basedir.
     */
public static boolean saveArchive(String baseDir, String name, Archive archive, Archive.Save_Archive_Mode mode) throws IOException {
    /*log.info("Before saving the archive checking if it is still in good shape");
        archive.Verify();*/
    String dir = baseDir + File.separatorChar + Archive.BAG_DATA_FOLDER + File.separatorChar + Archive.SESSIONS_SUBDIR;
    // just to be safe
    new File(dir).mkdirs();
    String filename = dir + File.separatorChar + name + SESSION_SUFFIX;
    log.info("Saving archive to (session) file " + filename);
    /*//file path names of addressbook, entitybook and correspondentAuthorityMapper data.
        String addressBookPath = dir + File.separatorChar + Archive.ADDRESSBOOK_SUFFIX;
        String entityBookPath = dir + File.separatorChar + Archive.ENTITYBOOK_SUFFIX;
        String cAuthorityPath =  dir + File.separatorChar + Archive.CAUTHORITYMAPPER_SUFFIX;
        */
    recalculateCollectionMetadata(archive);
    try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(filename))))) {
        oos.writeObject("archive");
        oos.writeObject(archive);
    } catch (Exception e1) {
        Util.print_exception("Failed to write archive: ", e1, log);
    }
    if (mode == Archive.Save_Archive_Mode.INCREMENTAL_UPDATE) {
    // archive object doesn't get modified so no point in saving it for incremental one.
    // archive.updateFileInBag(filename,archive.baseDir);
    }
    // ///////////////AddressBook Writing -- In human readable form ///////////////////////////////////
    if (mode != Archive.Save_Archive_Mode.INCREMENTAL_UPDATE)
        // no need to save addressbook while saving an archive in incrremental mode because address book is saved after every explicit modification.
        saveAddressBook(archive, mode);
    // //////////////EntityBook Writing -- In human readable form/////////////////////////////////////
    saveEntityBookManager(archive, mode);
    // /////////////CAuthorityMapper Writing-- Serialized///////////////////////////////
    saveCorrespondentAuthorityMapper(archive, mode);
    // ////////////LabelManager Writing -- Serialized//////////////////////////////////
    saveLabelManager(archive, mode);
    // ////////////AnnotationManager writing-- In human readable form/////////////////////////////////////
    saveAnnotations(archive, mode);
    saveCollectionMetadata(archive, mode);
    // if archivesave mode is freshcreation then create a bag around basedir and set bag as this one..
    if (mode == Archive.Save_Archive_Mode.FRESH_CREATION) {
        StandardSupportedAlgorithms algorithm = StandardSupportedAlgorithms.MD5;
        boolean includeHiddenFiles = false;
        try {
            archive.close();
            // First copy the content of archive.baseDir + "/data" to archive.baseDir and then create an in place bag.
            // Why so complicated? Because we wanted to have a uniform directory structure of archive irrespective of the fact whether it is
            // in a bag or not. That structure is 'archive.baseDir + "/data" folder'
            File tmp = Util.createTempDirectory();
            tmp.delete();
            // It seems that if indexer kept the file handles open then move directory failed on windows because of the lock held on those file
            // therefore call archive.close() before moving stuff around
            // archive.close();
            FileUtils.moveDirectory(Paths.get(archive.baseDir + File.separatorChar + Archive.BAG_DATA_FOLDER).toFile(), tmp.toPath().toFile());
            // Files.copy(Paths.get(userDir+File.separatorChar+Archive.BAG_DATA_FOLDER),tmp.toPath(),StandardCopyOption.REPLACE_EXISTING);
            File wheretocopy = Paths.get(archive.baseDir).toFile();
            Util.deleteDir(wheretocopy.getPath(), log);
            FileUtils.moveDirectory(tmp.toPath().toFile(), wheretocopy);
            Bag bag = BagCreator.bagInPlace(Paths.get(archive.baseDir), Arrays.asList(algorithm), includeHiddenFiles);
            archive.openForRead();
            archive.setArchiveBag(bag);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    } else {
        archive.close();
        // re-open for reading
        archive.openForRead();
    }
    return true;
}
Also used : StandardSupportedAlgorithms(gov.loc.repository.bagit.hash.StandardSupportedAlgorithms) GZIPOutputStream(java.util.zip.GZIPOutputStream) Bag(gov.loc.repository.bagit.domain.Bag) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ParseException(org.apache.lucene.queryparser.classic.ParseException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

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