Skip to content

Commit 8869467

Browse files
author
Richard van Heest
committed
make BagVerifier extendable and checkHashes available for subclasses
1 parent 2b7002e commit 8869467

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package gov.loc.repository.bagit;
2+
3+
import gov.loc.repository.bagit.domain.Bag;
4+
import gov.loc.repository.bagit.domain.Manifest;
5+
import gov.loc.repository.bagit.exceptions.CorruptChecksumException;
6+
import gov.loc.repository.bagit.exceptions.FileNotInPayloadDirectoryException;
7+
import gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException;
8+
import gov.loc.repository.bagit.exceptions.MaliciousPathException;
9+
import gov.loc.repository.bagit.exceptions.UnsupportedAlgorithmException;
10+
import gov.loc.repository.bagit.exceptions.VerificationException;
11+
import gov.loc.repository.bagit.verify.BagVerifier;
12+
13+
import java.io.IOException;
14+
15+
// an example of extending the BagVerifier in order to add your own checks that suffice the specific
16+
// verification rules for a bag, as defined by your project
17+
// NOTE: this cannot override the BagVerifier.isComplete or BagVerifier.isValid, as they're defined
18+
// by "The BagIt File Packaging Format" (https://tools.ietf.org/html/draft-kunze-bagit).
19+
public class ExtendedBagVerifier extends BagVerifier {
20+
21+
public void isValidAccordingToMyValidation(final Bag bag, final boolean ignoreHiddenFiles)
22+
throws InterruptedException, CorruptChecksumException, VerificationException,
23+
MaliciousPathException, FileNotInPayloadDirectoryException, UnsupportedAlgorithmException,
24+
InvalidBagitFileFormatException, IOException {
25+
26+
for (Manifest manifest : bag.getPayLoadManifests()) {
27+
this.checkHashes(manifest);
28+
}
29+
30+
getManifestVerifier().verifyPayload(bag, ignoreHiddenFiles);
31+
}
32+
}

src/main/java/gov/loc/repository/bagit/verify/BagVerifier.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
/**
3434
* Responsible for verifying if a bag is valid, complete
3535
*/
36-
public final class BagVerifier implements AutoCloseable{
36+
public class BagVerifier implements AutoCloseable{
3737
private static final Logger logger = LoggerFactory.getLogger(BagVerifier.class);
3838
private static final ResourceBundle messages = ResourceBundle.getBundle("MessageBundle");
3939

@@ -130,7 +130,7 @@ public static void quicklyVerify(final Bag bag) throws IOException, InvalidPaylo
130130
* @throws UnsupportedAlgorithmException if the manifest uses a algorithm that isn't supported
131131
* @throws InvalidBagitFileFormatException if the manifest is not formatted properly
132132
*/
133-
public void isValid(final Bag bag, final boolean ignoreHiddenFiles) throws IOException, MissingPayloadManifestException, MissingBagitFileException, MissingPayloadDirectoryException, FileNotInPayloadDirectoryException, InterruptedException, MaliciousPathException, CorruptChecksumException, VerificationException, UnsupportedAlgorithmException, InvalidBagitFileFormatException{
133+
public final void isValid(final Bag bag, final boolean ignoreHiddenFiles) throws IOException, MissingPayloadManifestException, MissingBagitFileException, MissingPayloadDirectoryException, FileNotInPayloadDirectoryException, InterruptedException, MaliciousPathException, CorruptChecksumException, VerificationException, UnsupportedAlgorithmException, InvalidBagitFileFormatException{
134134
logger.info(messages.getString("checking_bag_is_valid"), bag.getRootDir());
135135
isComplete(bag, ignoreHiddenFiles);
136136

@@ -149,7 +149,7 @@ public void isValid(final Bag bag, final boolean ignoreHiddenFiles) throws IOExc
149149
* Check the supplied checksum hashes against the generated checksum hashes
150150
*/
151151
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
152-
void checkHashes(final Manifest manifest) throws CorruptChecksumException, InterruptedException, VerificationException{
152+
protected void checkHashes(final Manifest manifest) throws CorruptChecksumException, InterruptedException, VerificationException{
153153
final CountDownLatch latch = new CountDownLatch( manifest.getFileToChecksumMap().size());
154154

155155
//TODO maybe return all of these at some point...
@@ -196,7 +196,7 @@ void checkHashes(final Manifest manifest) throws CorruptChecksumException, Inter
196196
* @throws UnsupportedAlgorithmException if the manifest uses a algorithm that isn't supported
197197
* @throws InvalidBagitFileFormatException if the manifest is not formatted properly
198198
*/
199-
public void isComplete(final Bag bag, final boolean ignoreHiddenFiles) throws
199+
public final void isComplete(final Bag bag, final boolean ignoreHiddenFiles) throws
200200
IOException, MissingPayloadManifestException, MissingBagitFileException, MissingPayloadDirectoryException,
201201
FileNotInPayloadDirectoryException, InterruptedException, MaliciousPathException, UnsupportedAlgorithmException, InvalidBagitFileFormatException{
202202
logger.info(messages.getString("checking_bag_is_complete"), bag.getRootDir());
@@ -212,11 +212,11 @@ public void isComplete(final Bag bag, final boolean ignoreHiddenFiles) throws
212212
manifestVerifier.verifyPayload(bag, ignoreHiddenFiles);
213213
}
214214

215-
public ExecutorService getExecutor() {
215+
public final ExecutorService getExecutor() {
216216
return executor;
217217
}
218218

219-
public PayloadVerifier getManifestVerifier() {
219+
public final PayloadVerifier getManifestVerifier() {
220220
return manifestVerifier;
221221
}
222222
}

0 commit comments

Comments
 (0)