archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksummedFile.java
/**
     * Checks if the checksum files are valid for the referenced file.
     * It tries to find a checksum file for each algorithm in the same directory as the referenceFile.
     * The method returns true, if at least one checksum file exists for one of the given algorithms
     * and all existing checksum files are valid.
     *
     * This method throws only exceptions, if throwExceptions is true. Otherwise false will be returned instead.
     *
     * It verifies only the existing checksum files. If the checksum file for a particular algorithm does not exist,
     * but others exist and are valid, it will return true.
     *
     * @param algorithms The algorithms to verify
     * @param throwExceptions If true, exceptions will be thrown, otherwise false will be returned, if a exception occurred.
     * @return True, if it is valid for all existing checksum files, otherwise false.
     * @throws ChecksumValidationException
     */
Checks if the checksum files are valid for the referenced file.
It tries to find a checksum file for each algorithm in the same directory as the referenceFile.
The method returns true, if at least one checksum file exists for one of the given algorithms
and all existing checksum files are valid.

This method throws only exceptions, if throwExceptions is true. Otherwise false will be returned instead.

It verifies only the existing checksum files. If the checksum file for a particular algorithm does not exist,
but others exist and are valid, it will return true.

@param algorithms The algorithms to verify
@param throwExceptions If true, exceptions will be thrown, otherwise false will be returned, if a exception occurred.
@return True, if it is valid for all existing checksum files, otherwise false.
Checks if the checksum files are valid for the referenced file . It tries to find a checksum file for each algorithm in the same directory as the referenceFile . The method returns true if at least one checksum file exists for one of the given algorithms and all existing checksum files are valid . This method throws only exceptions if throwExceptions is true . Otherwise false will be returned instead . It verifies only the existing checksum files . If the checksum file for a particular algorithm does not exist but others exist and are valid it will return true . @param algorithms The algorithms to verify @param throwExceptions If true exceptions will be thrown otherwise false will be returned if a exception occurred . @return True if it is valid for all existing checksum files otherwise false .
public boolean isValidChecksums( List<ChecksumAlgorithm> algorithms, boolean throwExceptions) throws ChecksumValidationException
    {
        List<Checksum> checksums;
        try
        {
            checksums = ChecksumUtil.initializeChecksums( referenceFile, algorithms );
        }
        catch (IOException e )
        {
            log.warn( "Unable to update checksum:{}", e.getMessage( ) );
            if (throwExceptions) {
                if (e instanceof FileNotFoundException) {
                    throw new ChecksumValidationException(ChecksumValidationException.ValidationError.FILE_NOT_FOUND, e);
                } else {
                    throw new ChecksumValidationException(ChecksumValidationException.ValidationError.READ_ERROR, e);
                }
            } else {
                return false;
            }
        }
        boolean valid = true;
        boolean fileExists = false;
        try
        {
            for ( Checksum checksum : checksums )
            {
                ChecksumAlgorithm checksumAlgorithm = checksum.getAlgorithm( );
                Path checksumFile = getChecksumFile( checksumAlgorithm );
                if (Files.exists(checksumFile)) {
                    fileExists = true;
                    String expectedChecksum = parseChecksum(checksumFile, checksumAlgorithm, referenceFile.getFileName().toString(), FILE_ENCODING);
                    valid &= checksum.compare(expectedChecksum);
                }
            }
        }
        catch ( ChecksumValidationException e )
        {
            log.warn( "Unable to read / parse checksum: {}", e.getMessage( ) );
            if (throwExceptions) {
                throw e;
            } else
            {
                return false;
            }
        }
        return fileExists && valid;
    }
public boolean isValidChecksums ( List < ChecksumAlgorithm > algorithms , boolean throwExceptions ) throws ChecksumValidationException { List < Checksum > checksums ; try { checksums = ChecksumUtil . initializeChecksums ( referenceFile , algorithms ) ; } catch ( IOException e ) { log . warn ( "Unable to update checksum:{}" , e . getMessage ( ) ) ; if ( throwExceptions ) { if ( e instanceof FileNotFoundException ) { throw new ChecksumValidationException ( ChecksumValidationException . ValidationError . FILE_NOT_FOUND , e ) ; } else { throw new ChecksumValidationException ( ChecksumValidationException . ValidationError . READ_ERROR , e ) ; } } else { return false ; } } boolean valid = true ; boolean fileExists = false ; try { for ( Checksum checksum : checksums ) { ChecksumAlgorithm checksumAlgorithm = checksum . getAlgorithm ( ) ; Path checksumFile = getChecksumFile ( checksumAlgorithm ) ; if ( Files . exists ( checksumFile ) ) { fileExists = true ; String expectedChecksum = parseChecksum ( checksumFile , checksumAlgorithm , referenceFile . getFileName ( ) . toString ( ) , FILE_ENCODING ) ; valid &= checksum . compare ( expectedChecksum ) ; } } } catch ( ChecksumValidationException e ) { log . warn ( "Unable to read / parse checksum: {}" , e . getMessage ( ) ) ; if ( throwExceptions ) { throw e ; } else { return false ; } } return fileExists && valid ; }
Checks if the checksum files are valid for the referenced file.
Checks if the checksum files are valid for the referenced file .
// Parse file once, for all checksums. // No file exists -> return false // if at least one file exists: // -> all existing files must be valid // check the checksum files