Search in sources :

Example 1 with PayloadOxumDoesNotExistException

use of gov.loc.repository.bagit.exceptions.PayloadOxumDoesNotExistException in project bagit-java by LibraryOfCongress.

the class QuickVerifier method quicklyVerify.

/**
 * Quickly verify by comparing the number of files and the total number of bytes expected
 *
 * @param bag the bag to verify by payload-oxum
 *
 * @throws IOException if there is an error reading a file
 * @throws InvalidPayloadOxumException if either the total bytes or the number of files
 * calculated for the payload directory of the bag is different than the supplied values
 * @throws PayloadOxumDoesNotExistException if the bag does not contain a payload-oxum.
 * To check, run {@link BagVerifier#canQuickVerify}
 */
public static void quicklyVerify(final Bag bag) throws IOException, InvalidPayloadOxumException {
    final String payloadOxum = getPayloadOxum(bag);
    if (payloadOxum == null || !payloadOxum.matches(PAYLOAD_OXUM_REGEX)) {
        throw new PayloadOxumDoesNotExistException(messages.getString("payload_oxum_missing_error"));
    }
    final String[] parts = payloadOxum.split("\\.");
    logger.debug(messages.getString("parse_size_in_bytes"), parts[0]);
    final long totalSize = Long.parseLong(parts[0]);
    logger.debug(messages.getString("parse_number_of_files"), parts[1]);
    final long numberOfFiles = Long.parseLong(parts[1]);
    final Path payloadDir = PathUtils.getDataDir(bag);
    final FileCountAndTotalSizeVistor vistor = new FileCountAndTotalSizeVistor();
    Files.walkFileTree(payloadDir, vistor);
    logger.debug(messages.getString("compare_payload_oxums"), payloadOxum, vistor.getTotalSize(), vistor.getCount(), payloadDir);
    if (totalSize != vistor.getTotalSize()) {
        final String formattedMessage = messages.getString("invalid_total_size_error");
        throw new InvalidPayloadOxumException(MessageFormatter.format(formattedMessage, totalSize, vistor.getTotalSize()).getMessage());
    }
    if (numberOfFiles != vistor.getCount()) {
        final String formattedMessage = messages.getString("invalid_file_cound_error");
        throw new InvalidPayloadOxumException(MessageFormatter.format(formattedMessage, numberOfFiles, vistor.getCount()).getMessage());
    }
}
Also used : Path(java.nio.file.Path) InvalidPayloadOxumException(gov.loc.repository.bagit.exceptions.InvalidPayloadOxumException) PayloadOxumDoesNotExistException(gov.loc.repository.bagit.exceptions.PayloadOxumDoesNotExistException)

Aggregations

InvalidPayloadOxumException (gov.loc.repository.bagit.exceptions.InvalidPayloadOxumException)1 PayloadOxumDoesNotExistException (gov.loc.repository.bagit.exceptions.PayloadOxumDoesNotExistException)1 Path (java.nio.file.Path)1