Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make BagVerifier extendable #126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class doesn't actually test anything. The src/test and src/integration are for junit tests. The integration is meant to test against the bagit-suite as well as the combination of the readers and writers. You should probably put this in the src/test directory following the package name and have junit test something. That way gradle will test it as well as it being tested during compile time.


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);
}
}
12 changes: 6 additions & 6 deletions src/main/java/gov/loc/repository/bagit/verify/BagVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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);

Expand All @@ -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...
Expand Down Expand Up @@ -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());
Expand All @@ -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;
}
}