diff --git a/src/integration/java/gov/loc/repository/bagit/ExtendedBagVerifier.java b/src/integration/java/gov/loc/repository/bagit/ExtendedBagVerifier.java new file mode 100644 index 000000000..d2da5bfe5 --- /dev/null +++ b/src/integration/java/gov/loc/repository/bagit/ExtendedBagVerifier.java @@ -0,0 +1,32 @@ +package gov.loc.repository.bagit; + +import gov.loc.repository.bagit.domain.Bag; +import gov.loc.repository.bagit.domain.Manifest; +import gov.loc.repository.bagit.exceptions.CorruptChecksumException; +import gov.loc.repository.bagit.exceptions.FileNotInPayloadDirectoryException; +import gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException; +import gov.loc.repository.bagit.exceptions.MaliciousPathException; +import gov.loc.repository.bagit.exceptions.UnsupportedAlgorithmException; +import gov.loc.repository.bagit.exceptions.VerificationException; +import gov.loc.repository.bagit.verify.BagVerifier; + +import java.io.IOException; + +// an example of extending the BagVerifier in order to add your own checks that suffice the specific +// verification rules for a bag, as defined by your project +// NOTE: this cannot override the BagVerifier.isComplete or BagVerifier.isValid, as they're defined +// by "The BagIt File Packaging Format" (https://tools.ietf.org/html/draft-kunze-bagit). +public class ExtendedBagVerifier extends BagVerifier { + + public void isValidAccordingToMyValidation(final Bag bag, final boolean ignoreHiddenFiles) + throws InterruptedException, CorruptChecksumException, VerificationException, + MaliciousPathException, FileNotInPayloadDirectoryException, UnsupportedAlgorithmException, + InvalidBagitFileFormatException, IOException { + + for (Manifest manifest : bag.getPayLoadManifests()) { + this.checkHashes(manifest); + } + + getManifestVerifier().verifyPayload(bag, ignoreHiddenFiles); + } +} diff --git a/src/main/java/gov/loc/repository/bagit/verify/BagVerifier.java b/src/main/java/gov/loc/repository/bagit/verify/BagVerifier.java index 835e06beb..c8e8c20f7 100644 --- a/src/main/java/gov/loc/repository/bagit/verify/BagVerifier.java +++ b/src/main/java/gov/loc/repository/bagit/verify/BagVerifier.java @@ -33,7 +33,7 @@ /** * Responsible for verifying if a bag is valid, complete */ -public final class BagVerifier implements AutoCloseable{ +public class BagVerifier implements AutoCloseable{ private static final Logger logger = LoggerFactory.getLogger(BagVerifier.class); private static final ResourceBundle messages = ResourceBundle.getBundle("MessageBundle"); @@ -130,7 +130,7 @@ public static void quicklyVerify(final Bag bag) throws IOException, InvalidPaylo * @throws UnsupportedAlgorithmException if the manifest uses a algorithm that isn't supported * @throws InvalidBagitFileFormatException if the manifest is not formatted properly */ - public void isValid(final Bag bag, final boolean ignoreHiddenFiles) throws IOException, MissingPayloadManifestException, MissingBagitFileException, MissingPayloadDirectoryException, FileNotInPayloadDirectoryException, InterruptedException, MaliciousPathException, CorruptChecksumException, VerificationException, UnsupportedAlgorithmException, InvalidBagitFileFormatException{ + public final void isValid(final Bag bag, final boolean ignoreHiddenFiles) throws IOException, MissingPayloadManifestException, MissingBagitFileException, MissingPayloadDirectoryException, FileNotInPayloadDirectoryException, InterruptedException, MaliciousPathException, CorruptChecksumException, VerificationException, UnsupportedAlgorithmException, InvalidBagitFileFormatException{ logger.info(messages.getString("checking_bag_is_valid"), bag.getRootDir()); isComplete(bag, ignoreHiddenFiles); @@ -149,7 +149,7 @@ public void isValid(final Bag bag, final boolean ignoreHiddenFiles) throws IOExc * Check the supplied checksum hashes against the generated checksum hashes */ @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") - void checkHashes(final Manifest manifest) throws CorruptChecksumException, InterruptedException, VerificationException{ + protected final void checkHashes(final Manifest manifest) throws CorruptChecksumException, InterruptedException, VerificationException{ final CountDownLatch latch = new CountDownLatch( manifest.getFileToChecksumMap().size()); //TODO maybe return all of these at some point... @@ -196,7 +196,7 @@ void checkHashes(final Manifest manifest) throws CorruptChecksumException, Inter * @throws UnsupportedAlgorithmException if the manifest uses a algorithm that isn't supported * @throws InvalidBagitFileFormatException if the manifest is not formatted properly */ - public void isComplete(final Bag bag, final boolean ignoreHiddenFiles) throws + public final void isComplete(final Bag bag, final boolean ignoreHiddenFiles) throws IOException, MissingPayloadManifestException, MissingBagitFileException, MissingPayloadDirectoryException, FileNotInPayloadDirectoryException, InterruptedException, MaliciousPathException, UnsupportedAlgorithmException, InvalidBagitFileFormatException{ logger.info(messages.getString("checking_bag_is_complete"), bag.getRootDir()); @@ -212,11 +212,11 @@ public void isComplete(final Bag bag, final boolean ignoreHiddenFiles) throws manifestVerifier.verifyPayload(bag, ignoreHiddenFiles); } - public ExecutorService getExecutor() { + public final ExecutorService getExecutor() { return executor; } - public PayloadVerifier getManifestVerifier() { + public final PayloadVerifier getManifestVerifier() { return manifestVerifier; } }