Skip to content

Commit e451843

Browse files
committed
refactor to remove payload oxum fields from Bag domain object
1 parent f3c1c81 commit e451843

File tree

12 files changed

+152
-105
lines changed

12 files changed

+152
-105
lines changed

src/main/java/gov/loc/repository/bagit/creator/BagCreator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static Bag bagInPlace(final Path root, final Collection<SupportedAlgorith
6464
Files.walkFileTree(dataDir, payloadVisitor);
6565

6666
bag.getPayLoadManifests().addAll(payloadFilesMap.keySet());
67-
BagitFileWriter.writeBagitFile(bag.getVersion(), bag.getFileEncoding(), root);
67+
BagitFileWriter.writeBagitFile(bag.getVersion(), bag.getFileEncoding(), null, null, root);
6868
ManifestWriter.writePayloadManifests(bag.getPayLoadManifests(), root, root, bag.getFileEncoding());
6969

7070
logger.info("Creating tag manifest(s)");
@@ -105,7 +105,7 @@ public static Bag createDotBagit(final Path root, final Collection<SupportedAlgo
105105
Files.walkFileTree(root, visitor);
106106

107107
bag.getPayLoadManifests().addAll(map.keySet());
108-
BagitFileWriter.writeBagitFile(bag.getVersion(), bag.getFileEncoding(), dotbagitDir);
108+
BagitFileWriter.writeBagitFile(bag.getVersion(), bag.getFileEncoding(), null, null, dotbagitDir);
109109
ManifestWriter.writePayloadManifests(bag.getPayLoadManifests(), dotbagitDir, root, bag.getFileEncoding());
110110

111111
logger.info("Creating tag manifest(s)");

src/main/java/gov/loc/repository/bagit/domain/Bag.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ public final class Bag {
3535
//the current location of the bag on the filesystem
3636
private Path rootDir;
3737

38-
private Long payloadFileCount;
39-
40-
private Long payloadByteCount;
41-
4238
/**
4339
* empty bag with an invalid version
4440
*/
@@ -172,19 +168,4 @@ public void setVersion(final Version version) {
172168
this.version = version;
173169
}
174170

175-
public Long getPayloadFileCount() {
176-
return payloadFileCount;
177-
}
178-
179-
public void setPayloadFileCount(final Long payloadFileCount) {
180-
this.payloadFileCount = payloadFileCount;
181-
}
182-
183-
public Long getPayloadByteCount() {
184-
return payloadByteCount;
185-
}
186-
187-
public void setPayloadByteCount(final Long payloadByteCount) {
188-
this.payloadByteCount = payloadByteCount;
189-
}
190171
}

src/main/java/gov/loc/repository/bagit/reader/BagReader.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package gov.loc.repository.bagit.reader;
22

33
import java.io.IOException;
4+
import java.nio.charset.Charset;
45
import java.nio.file.Files;
56
import java.nio.file.Path;
7+
import java.util.AbstractMap.SimpleImmutableEntry;
68

79
import gov.loc.repository.bagit.domain.Bag;
10+
import gov.loc.repository.bagit.domain.Version;
811
import gov.loc.repository.bagit.exceptions.InvalidBagMetadataException;
912
import gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException;
1013
import gov.loc.repository.bagit.exceptions.MaliciousPathException;
@@ -50,7 +53,10 @@ public Bag read(final Path rootDir) throws IOException, UnparsableVersionExcepti
5053
}
5154
bag.setRootDir(rootDir);
5255

53-
BagitTextFileReader.readBagitTextFile(bag);
56+
final Path bagitFile = bagitDir.resolve("bagit.txt");
57+
final SimpleImmutableEntry<Version, Charset> bagitInfo = BagitTextFileReader.readBagitTextFile(bagitFile);
58+
bag.setVersion(bagitInfo.getKey());
59+
bag.setFileEncoding(bagitInfo.getValue());
5460

5561
ManifestReader.readAllManifests(nameMapping, bagitDir, bag);
5662

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package gov.loc.repository.bagit.reader;
2+
3+
import java.nio.charset.Charset;
4+
5+
import gov.loc.repository.bagit.domain.Version;
6+
7+
/**
8+
* A simple data object for passing around all the bagit.txt file values
9+
*/
10+
public class BagitFileValues {
11+
private final Version version;
12+
private final Charset encoding;
13+
private final Long payloadByteCount;
14+
private final Long payloadFileCount;
15+
16+
public BagitFileValues(final Version version, final Charset encoding, final Long payloadByteCount, final Long payloadFileCount){
17+
this.version = version;
18+
this.encoding = encoding;
19+
this.payloadByteCount = payloadByteCount;
20+
this.payloadFileCount = payloadFileCount;
21+
}
22+
23+
public Version getVersion() {
24+
return version;
25+
}
26+
public Charset getEncoding() {
27+
return encoding;
28+
}
29+
public Long getPayloadByteCount() {
30+
return payloadByteCount;
31+
}
32+
public Long getPayloadFileCount() {
33+
return payloadFileCount;
34+
}
35+
}

src/main/java/gov/loc/repository/bagit/reader/BagitTextFileReader.java

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
import java.io.IOException;
44
import java.nio.charset.Charset;
55
import java.nio.charset.StandardCharsets;
6-
import java.nio.file.Files;
76
import java.nio.file.Path;
87
import java.util.AbstractMap.SimpleImmutableEntry;
98
import java.util.List;
109

1110
import org.slf4j.Logger;
1211
import org.slf4j.LoggerFactory;
1312

14-
import gov.loc.repository.bagit.domain.Bag;
1513
import gov.loc.repository.bagit.domain.Version;
1614
import gov.loc.repository.bagit.exceptions.InvalidBagMetadataException;
1715
import gov.loc.repository.bagit.exceptions.UnparsableVersionException;
@@ -36,58 +34,65 @@ private BagitTextFileReader(){
3634
public static SimpleImmutableEntry<Version, Charset> readBagitTextFile(final Path bagitFile) throws IOException, UnparsableVersionException, InvalidBagMetadataException{
3735
final BagitFileValues values = parseValues(bagitFile);
3836

39-
return new SimpleImmutableEntry<Version, Charset>(values.version, values.encoding);
37+
return new SimpleImmutableEntry<Version, Charset>(values.getVersion(), values.getEncoding());
4038
}
4139

4240
/**
43-
* Read the bagit.txt file and get the version and encoding. In version 1.0+ also check for
44-
* payload-byte-count and payload-file-count
41+
* Read the Payload-Byte-Count and Payload-File-Count from the bagit.txt file
42+
* @since bagic specification 1.0
4543
*
46-
* @param bag the to read that contains the bagit.txt file and set the values in the bag
44+
* @param bagitFile the bagit.txt file to read
45+
*
46+
* @return the payload byte count, payload file count (in that order)
4747
*
4848
* @throws IOException if there is a problem reading a file
4949
* @throws UnparsableVersionException if there is a problem parsing the bagit version number
5050
* @throws InvalidBagMetadataException if the bagit.txt file does not conform to the bagit spec
5151
*/
52-
public static void readBagitTextFile(final Bag bag) throws IOException, UnparsableVersionException, InvalidBagMetadataException{
53-
Path bagitDir = bag.getRootDir().resolve(".bagit");
54-
if(!Files.exists(bagitDir)){
55-
bagitDir = bag.getRootDir();
56-
}
57-
final BagitFileValues values = parseValues(bagitDir.resolve("bagit.txt"));
52+
public static SimpleImmutableEntry<Long, Long> readPayloadByteAndFileCount(final Path bagitFile) throws UnparsableVersionException, IOException, InvalidBagMetadataException{
53+
final BagitFileValues values = parseValues(bagitFile);
5854

59-
bag.setVersion(values.version);
60-
bag.setFileEncoding(values.encoding);
61-
bag.setPayloadByteCount(values.payloadByteCount);
62-
bag.setPayloadFileCount(values.payloadFileCount);
55+
return new SimpleImmutableEntry<Long, Long>(values.getPayloadByteCount(), values.getPayloadFileCount());
6356
}
6457

65-
private static BagitFileValues parseValues(final Path bagitFile) throws UnparsableVersionException, IOException, InvalidBagMetadataException{
58+
/**
59+
* Read version, file encoding, and (possibly) payload byte and file count
60+
*
61+
* @param bagitFile the bagit.txt file to read
62+
*
63+
* @return all the possible bagit.txt file field values
64+
*
65+
* @throws IOException if there is a problem reading a file
66+
* @throws UnparsableVersionException if there is a problem parsing the bagit version number
67+
* @throws InvalidBagMetadataException if the bagit.txt file does not conform to the bagit spec
68+
*/
69+
public static BagitFileValues parseValues(final Path bagitFile) throws UnparsableVersionException, IOException, InvalidBagMetadataException{
6670
logger.debug("Reading [{}] file", bagitFile);
6771
final List<SimpleImmutableEntry<String, String>> pairs = KeyValueReader.readKeyValuesFromFile(bagitFile, ":", StandardCharsets.UTF_8);
68-
final BagitFileValues values = new BagitFileValues();
6972

70-
String version = "";
73+
74+
Version version = null;
7175
Charset encoding = StandardCharsets.UTF_8;
76+
Long payloadByteCount = null;
77+
Long payloadFileCount = null;
78+
7279
for(final SimpleImmutableEntry<String, String> pair : pairs){
7380
if("BagIt-Version".equals(pair.getKey())){
74-
version = pair.getValue();
75-
values.version = parseVersion(version);
81+
version = parseVersion(pair.getValue());
7682
}
7783
if("Tag-File-Character-Encoding".equals(pair.getKey())){
7884
encoding = Charset.forName(pair.getValue());
79-
values.encoding = encoding;
8085
}
8186
if("Payload-Byte-Count".equals(pair.getKey())){ //assume version is 1.0+
82-
values.payloadByteCount = Long.valueOf(pair.getValue());
87+
payloadByteCount = Long.valueOf(pair.getValue());
8388
}
8489
if("Payload-File-Count".equals(pair.getKey())){ //assume version is 1.0+
85-
values.payloadFileCount = Long.valueOf(pair.getValue());
90+
payloadFileCount = Long.valueOf(pair.getValue());
8691
}
8792
logger.debug("[{}] is [{}]", pair.getKey(), pair.getValue());
8893
}
8994

90-
return values;
95+
return new BagitFileValues(version, encoding, payloadByteCount, payloadFileCount);
9196
}
9297

9398
/*
@@ -104,12 +109,4 @@ static Version parseVersion(final String version) throws UnparsableVersionExcept
104109

105110
return new Version(major, minor);
106111
}
107-
108-
@SuppressWarnings({"PMD.BeanMembersShouldSerialize"})
109-
private static class BagitFileValues{
110-
public Version version;
111-
public Charset encoding;
112-
public Long payloadByteCount;
113-
public Long payloadFileCount;
114-
}
115112
}

src/main/java/gov/loc/repository/bagit/util/PathUtils.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ public static boolean isHidden(final Path path) throws IOException{
6767
return Files.isHidden(path);
6868
}
6969

70-
/*
71-
* Get the directory that contains the payload files.
72-
*/
7370
/**
7471
* With bagit version 2.0 (.bagit)
7572
* payload files are no longer in the "data" directory. This method accounts for this

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
import gov.loc.repository.bagit.domain.Manifest;
1919
import gov.loc.repository.bagit.exceptions.CorruptChecksumException;
2020
import gov.loc.repository.bagit.exceptions.FileNotInPayloadDirectoryException;
21+
import gov.loc.repository.bagit.exceptions.InvalidBagMetadataException;
2122
import gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException;
2223
import gov.loc.repository.bagit.exceptions.InvalidPayloadOxumException;
2324
import gov.loc.repository.bagit.exceptions.MaliciousPathException;
2425
import gov.loc.repository.bagit.exceptions.MissingBagitFileException;
2526
import gov.loc.repository.bagit.exceptions.MissingPayloadDirectoryException;
2627
import gov.loc.repository.bagit.exceptions.MissingPayloadManifestException;
2728
import gov.loc.repository.bagit.exceptions.PayloadOxumDoesNotExistException;
29+
import gov.loc.repository.bagit.exceptions.UnparsableVersionException;
2830
import gov.loc.repository.bagit.exceptions.UnsupportedAlgorithmException;
2931
import gov.loc.repository.bagit.exceptions.VerificationException;
3032
import gov.loc.repository.bagit.hash.BagitAlgorithmNameToSupportedAlgorithmMapping;
@@ -61,8 +63,12 @@ public BagVerifier(final ExecutorService executor, final BagitAlgorithmNameToSup
6163
*
6264
* @param bag the {@link Bag} object you wish to check
6365
* @return true if the bag can be quickly verified
66+
*
67+
* @throws IOException if there is a problem reading a file
68+
* @throws UnparsableVersionException if there is a problem parsing the bagit version number
69+
* @throws InvalidBagMetadataException if the bagit.txt file does not conform to the bagit spec
6470
*/
65-
public boolean canQuickVerify(final Bag bag){
71+
public boolean canQuickVerify(final Bag bag) throws UnparsableVersionException, IOException, InvalidBagMetadataException{
6672
return QuickVerifier.canQuickVerify(bag);
6773
}
6874

@@ -72,13 +78,15 @@ public boolean canQuickVerify(final Bag bag){
7278
* @param bag the bag to verify by payload-oxum
7379
* @param ignoreHiddenFiles ignore hidden files found in payload directory
7480
*
75-
* @throws IOException if there is an error reading a file
7681
* @throws InvalidPayloadOxumException if either the total bytes or the number of files
7782
* calculated for the payload directory of the bag is different than the supplied values
83+
* @throws IOException if there is a problem reading a file
84+
* @throws UnparsableVersionException if there is a problem parsing the bagit version number
85+
* @throws InvalidBagMetadataException if the bagit.txt file does not conform to the bagit spec
7886
* @throws PayloadOxumDoesNotExistException if the bag does not contain a payload-oxum.
7987
* To check, run {@link BagVerifier#canQuickVerify}
8088
*/
81-
public void quicklyVerify(final Bag bag, final boolean ignoreHiddenFiles) throws IOException, InvalidPayloadOxumException{
89+
public void quicklyVerify(final Bag bag, final boolean ignoreHiddenFiles) throws IOException, InvalidPayloadOxumException, UnparsableVersionException, InvalidBagMetadataException{
8290
QuickVerifier.quicklyVerify(bag, ignoreHiddenFiles);
8391
}
8492

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

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
import org.slf4j.LoggerFactory;
1010

1111
import gov.loc.repository.bagit.domain.Bag;
12+
import gov.loc.repository.bagit.exceptions.InvalidBagMetadataException;
1213
import gov.loc.repository.bagit.exceptions.InvalidPayloadOxumException;
1314
import gov.loc.repository.bagit.exceptions.PayloadOxumDoesNotExistException;
15+
import gov.loc.repository.bagit.exceptions.UnparsableVersionException;
16+
import gov.loc.repository.bagit.reader.BagitFileValues;
17+
import gov.loc.repository.bagit.reader.BagitTextFileReader;
1418
import gov.loc.repository.bagit.util.PathUtils;
1519

1620
/**
@@ -32,16 +36,22 @@ private QuickVerifier() {
3236
*
3337
* @param bag
3438
* the {@link Bag} object you wish to check
39+
*
3540
* @return true if the bag can be quickly verified
41+
*
42+
* @throws IOException if there is a problem reading a file
43+
* @throws UnparsableVersionException if there is a problem parsing the bagit version number
44+
* @throws InvalidBagMetadataException if the bagit.txt file does not conform to the bagit spec
3645
*/
37-
public static boolean canQuickVerify(final Bag bag) {
46+
public static boolean canQuickVerify(final Bag bag) throws UnparsableVersionException, IOException, InvalidBagMetadataException {
3847
boolean payloadInfoExists = false;
3948
final String payloadOxum = getPayloadOxum(bag);
49+
final BagitFileValues bagitValues = BagitTextFileReader.parseValues(bag.getRootDir().resolve("bagit.txt"));
4050

41-
if (bag.getPayloadByteCount() != null && bag.getPayloadFileCount() != null) {
51+
if (bagitValues.getPayloadByteCount() != null && bagitValues.getPayloadFileCount() != null) {
4252
logger.debug("Found payload byte and file count, using that instead of payload-oxum");
4353
if(payloadOxum != null){
44-
comparePayloadOxumWithByteAndFileCount(payloadOxum, bag.getPayloadByteCount(), bag.getPayloadFileCount());
54+
comparePayloadOxumWithByteAndFileCount(payloadOxum, bagitValues.getPayloadByteCount(), bagitValues.getPayloadFileCount());
4555
}
4656
payloadInfoExists = true;
4757
}
@@ -83,23 +93,22 @@ private static void comparePayloadOxumWithByteAndFileCount(final String payloadO
8393
* Quickly verify by comparing the number of files and the total number of
8494
* bytes expected
8595
*
86-
* @param bag
87-
* the bag to quickly verify
88-
* @param ignoreHiddenFiles
89-
* ignore hidden files found in payload directory
96+
* @param bag the bag to quickly verify
97+
* @param ignoreHiddenFiles ignore hidden files found in payload directory
9098
*
91-
* @throws IOException
92-
* if there is an error reading a file
99+
* @throws IOException if there is an error reading a file
93100
* @throws InvalidPayloadOxumException
94101
* if either the total bytes or the number of files calculated for
95102
* the payload directory of the bag is different than the supplied
96103
* values
104+
* @throws UnparsableVersionException if there is a problem parsing the bagit version number
105+
* @throws InvalidBagMetadataException if the bagit.txt file does not conform to the bagit spec
97106
* @throws PayloadOxumDoesNotExistException
98107
* if the bag does not contain a payload-oxum. To check, run
99108
* {@link BagVerifier#canQuickVerify}
100109
*/
101110
public static void quicklyVerify(final Bag bag, final boolean ignoreHiddenFiles)
102-
throws IOException, InvalidPayloadOxumException {
111+
throws IOException, InvalidPayloadOxumException, UnparsableVersionException, InvalidBagMetadataException {
103112
final SimpleImmutableEntry<Long, Long> byteAndFileCount = getByteAndFileCount(bag);
104113

105114
final Path payloadDir = PathUtils.getDataDir(bag);
@@ -124,11 +133,18 @@ public static void quicklyVerify(final Bag bag, final boolean ignoreHiddenFiles)
124133
*
125134
* @param bag
126135
* the bag to get the payload info from
136+
*
127137
* @return the byte count, the file count
138+
*
139+
* @throws IOException if there is a problem reading a file
140+
* @throws UnparsableVersionException if there is a problem parsing the bagit version number
141+
* @throws InvalidBagMetadataException if the bagit.txt file does not conform to the bagit spec
128142
*/
129-
private static SimpleImmutableEntry<Long, Long> getByteAndFileCount(final Bag bag) {
130-
if (bag.getPayloadByteCount() != null && bag.getPayloadFileCount() != null) {
131-
return new SimpleImmutableEntry<Long, Long>(bag.getPayloadByteCount(), bag.getPayloadFileCount());
143+
private static SimpleImmutableEntry<Long, Long> getByteAndFileCount(final Bag bag) throws UnparsableVersionException, IOException, InvalidBagMetadataException {
144+
final BagitFileValues bagitValues = BagitTextFileReader.parseValues(bag.getRootDir().resolve("bagit.txt"));
145+
146+
if (bagitValues.getPayloadByteCount() != null && bagitValues.getPayloadFileCount() != null) {
147+
return new SimpleImmutableEntry<Long, Long>(bagitValues.getPayloadByteCount(), bagitValues.getPayloadFileCount());
132148
}
133149

134150
final String payloadOxum = getPayloadOxum(bag);

0 commit comments

Comments
 (0)