|
14 | 14 | import gov.loc.repository.bagit.util.PathUtils; |
15 | 15 |
|
16 | 16 | /** |
17 | | - * responsible for all things related to quick verification. Quick verification does not |
18 | | - * mean that a Bag is valid, only that a cursory check has been made. For a full verification |
19 | | - * see {@link BagVerifier} |
| 17 | + * responsible for all things related to quick verification. Quick verification |
| 18 | + * does not mean that a Bag is valid, only that a cursory check has been made. |
| 19 | + * For a full verification see {@link BagVerifier} |
20 | 20 | */ |
21 | 21 | public final class QuickVerifier { |
22 | 22 | private static final Logger logger = LoggerFactory.getLogger(QuickVerifier.class); |
23 | 23 | private static final String PAYLOAD_OXUM_REGEX = "\\d+\\.\\d+"; |
24 | | - |
25 | | - private QuickVerifier(){ |
26 | | - //intentionally left empty |
| 24 | + |
| 25 | + private QuickVerifier() { |
| 26 | + // intentionally left empty |
27 | 27 | } |
28 | | - |
| 28 | + |
29 | 29 | /** |
30 | | - * Determine if we can quickly verify by comparing the number of files and the total number of bytes expected |
| 30 | + * Determine if we can quickly verify by comparing the number of files and the |
| 31 | + * total number of bytes expected |
31 | 32 | * |
32 | | - * @param bag the {@link Bag} object you wish to check |
| 33 | + * @param bag |
| 34 | + * the {@link Bag} object you wish to check |
33 | 35 | * @return true if the bag can be quickly verified |
34 | 36 | */ |
35 | | - public static boolean canQuickVerify(final Bag bag){ |
| 37 | + public static boolean canQuickVerify(final Bag bag) { |
36 | 38 | boolean payloadInfoExists = false; |
37 | | - |
38 | | - if(bag.getPayloadByteCount() != null && bag.getPayloadFileCount() != null){ |
| 39 | + final String payloadOxum = getPayloadOxum(bag); |
| 40 | + |
| 41 | + if (bag.getPayloadByteCount() != null && bag.getPayloadFileCount() != null) { |
39 | 42 | logger.debug("Found payload byte and file count, using that instead of payload-oxum"); |
40 | | - //TODO check if it matches payload-oxum, and if not issue warning? |
| 43 | + if(payloadOxum != null){ |
| 44 | + comparePayloadOxumWithByteAndFileCount(payloadOxum, bag.getPayloadByteCount(), bag.getPayloadFileCount()); |
| 45 | + } |
41 | 46 | payloadInfoExists = true; |
42 | 47 | } |
43 | | - |
44 | | - final String payloadOxum = getPayloadOxum(bag); |
45 | | - if(payloadOxum != null && payloadOxum.matches(PAYLOAD_OXUM_REGEX)){ |
| 48 | + |
| 49 | + if (payloadOxum != null && payloadOxum.matches(PAYLOAD_OXUM_REGEX)) { |
46 | 50 | logger.debug("Found payload-oxum [{}] for bag [{}]", payloadOxum, bag.getRootDir()); |
47 | 51 | payloadInfoExists = true; |
48 | 52 | } |
49 | | - |
| 53 | + |
50 | 54 | return payloadInfoExists && bag.getItemsToFetch().size() == 0; |
51 | 55 | } |
52 | | - |
| 56 | + |
53 | 57 | /* |
54 | 58 | * Get the Payload-Oxum value from the key value pairs |
55 | 59 | */ |
56 | | - private static String getPayloadOxum(final Bag bag){ |
57 | | - for(final SimpleImmutableEntry<String,String> keyValue : bag.getMetadata()){ |
58 | | - if("Payload-Oxum".equals(keyValue.getKey())){ |
| 60 | + private static String getPayloadOxum(final Bag bag) { |
| 61 | + for (final SimpleImmutableEntry<String, String> keyValue : bag.getMetadata()) { |
| 62 | + if ("Payload-Oxum".equals(keyValue.getKey())) { |
59 | 63 | return keyValue.getValue(); |
60 | 64 | } |
61 | 65 | } |
62 | 66 | return null; |
63 | 67 | } |
64 | | - |
| 68 | + |
| 69 | + private static void comparePayloadOxumWithByteAndFileCount(final String payloadOxum, final Long payloadByteCount, |
| 70 | + final Long payloadFileCount) { |
| 71 | + final SimpleImmutableEntry<Long, Long> payloadOxumValues = parsePayloadOxum(payloadOxum); |
| 72 | + |
| 73 | + if(!payloadOxumValues.getKey().equals(payloadByteCount)){ |
| 74 | + logger.warn("Payload-Oxum byte count [{}] does not match Payload-Byte-Count [{}]!", payloadOxumValues.getKey(), payloadByteCount); |
| 75 | + } |
| 76 | + |
| 77 | + if(!payloadOxumValues.getValue().equals(payloadFileCount)){ |
| 78 | + logger.warn("Payload-Oxum file count [{}] does not match Payload-File-Count [{}]!", payloadOxumValues.getValue(), payloadFileCount); |
| 79 | + } |
| 80 | + } |
| 81 | + |
65 | 82 | /** |
66 | | - * Quickly verify by comparing the number of files and the total number of bytes expected |
| 83 | + * Quickly verify by comparing the number of files and the total number of |
| 84 | + * bytes expected |
67 | 85 | * |
68 | | - * @param bag the bag to quickly verify |
69 | | - * @param ignoreHiddenFiles ignore hidden files found in payload directory |
| 86 | + * @param bag |
| 87 | + * the bag to quickly verify |
| 88 | + * @param ignoreHiddenFiles |
| 89 | + * ignore hidden files found in payload directory |
70 | 90 | * |
71 | | - * @throws IOException if there is an error reading a file |
72 | | - * @throws InvalidPayloadOxumException if either the total bytes or the number of files |
73 | | - * calculated for the payload directory of the bag is different than the supplied values |
74 | | - * @throws PayloadOxumDoesNotExistException if the bag does not contain a payload-oxum. |
75 | | - * To check, run {@link BagVerifier#canQuickVerify} |
| 91 | + * @throws IOException |
| 92 | + * if there is an error reading a file |
| 93 | + * @throws InvalidPayloadOxumException |
| 94 | + * if either the total bytes or the number of files calculated for |
| 95 | + * the payload directory of the bag is different than the supplied |
| 96 | + * values |
| 97 | + * @throws PayloadOxumDoesNotExistException |
| 98 | + * if the bag does not contain a payload-oxum. To check, run |
| 99 | + * {@link BagVerifier#canQuickVerify} |
76 | 100 | */ |
77 | | - public static void quicklyVerify(final Bag bag, final boolean ignoreHiddenFiles) throws IOException, InvalidPayloadOxumException{ |
| 101 | + public static void quicklyVerify(final Bag bag, final boolean ignoreHiddenFiles) |
| 102 | + throws IOException, InvalidPayloadOxumException { |
78 | 103 | final SimpleImmutableEntry<Long, Long> byteAndFileCount = getByteAndFileCount(bag); |
79 | | - |
| 104 | + |
80 | 105 | final Path payloadDir = PathUtils.getDataDir(bag); |
81 | 106 | final FileCountAndTotalSizeVistor vistor = new FileCountAndTotalSizeVistor(ignoreHiddenFiles); |
82 | 107 | Files.walkFileTree(payloadDir, vistor); |
83 | | - logger.info("supplied payload-oxum: [{}.{}], Calculated payload-oxum: [{}.{}], for payload directory [{}]", |
| 108 | + logger.info("supplied payload-oxum: [{}.{}], Calculated payload-oxum: [{}.{}], for payload directory [{}]", |
84 | 109 | byteAndFileCount.getKey(), byteAndFileCount.getValue(), vistor.getTotalSize(), vistor.getCount(), payloadDir); |
85 | | - |
86 | | - if(byteAndFileCount.getKey() != vistor.getTotalSize()){ |
87 | | - throw new InvalidPayloadOxumException("Invalid total size. Expected " + byteAndFileCount.getKey() + " but calculated " + vistor.getTotalSize()); |
| 110 | + |
| 111 | + if (byteAndFileCount.getKey() != vistor.getTotalSize()) { |
| 112 | + throw new InvalidPayloadOxumException( |
| 113 | + "Invalid total size. Expected " + byteAndFileCount.getKey() + " but calculated " + vistor.getTotalSize()); |
88 | 114 | } |
89 | | - if(byteAndFileCount.getValue() != vistor.getCount()){ |
90 | | - throw new InvalidPayloadOxumException("Invalid file count. Expected " + byteAndFileCount.getValue() + " but found " + vistor.getCount() + " files"); |
| 115 | + if (byteAndFileCount.getValue() != vistor.getCount()) { |
| 116 | + throw new InvalidPayloadOxumException( |
| 117 | + "Invalid file count. Expected " + byteAndFileCount.getValue() + " but found " + vistor.getCount() + " files"); |
91 | 118 | } |
92 | 119 | } |
93 | | - |
| 120 | + |
94 | 121 | /** |
95 | | - * get either the payload-oxum values or the payload-byte-count and payload-file-count |
| 122 | + * get either the payload-oxum values or the payload-byte-count and |
| 123 | + * payload-file-count |
96 | 124 | * |
97 | | - * @param bag the bag to get the payload info from |
| 125 | + * @param bag |
| 126 | + * the bag to get the payload info from |
98 | 127 | * @return the byte count, the file count |
99 | 128 | */ |
100 | | - private static SimpleImmutableEntry<Long, Long> getByteAndFileCount(final Bag bag){ |
101 | | - if(bag.getPayloadByteCount() != null && bag.getPayloadFileCount() != null){ |
| 129 | + private static SimpleImmutableEntry<Long, Long> getByteAndFileCount(final Bag bag) { |
| 130 | + if (bag.getPayloadByteCount() != null && bag.getPayloadFileCount() != null) { |
102 | 131 | return new SimpleImmutableEntry<Long, Long>(bag.getPayloadByteCount(), bag.getPayloadFileCount()); |
103 | 132 | } |
104 | | - |
| 133 | + |
105 | 134 | final String payloadOxum = getPayloadOxum(bag); |
106 | | - if(payloadOxum == null || !payloadOxum.matches(PAYLOAD_OXUM_REGEX)){ |
107 | | - throw new PayloadOxumDoesNotExistException("Payload-Oxum or payload-byte-count and payload-file-count does not exist in bag."); |
| 135 | + return parsePayloadOxum(payloadOxum); |
| 136 | + } |
| 137 | + |
| 138 | + private static SimpleImmutableEntry<Long, Long> parsePayloadOxum(final String payloadOxum){ |
| 139 | + if (payloadOxum == null || !payloadOxum.matches(PAYLOAD_OXUM_REGEX)) { |
| 140 | + throw new PayloadOxumDoesNotExistException( |
| 141 | + "Payload-Oxum or payload-byte-count and payload-file-count does not exist in bag."); |
108 | 142 | } |
109 | 143 |
|
110 | 144 | final String[] parts = payloadOxum.split("\\."); |
|
0 commit comments