aboutsummaryrefslogtreecommitdiffstats
path: root/archiva-modules/archiva-base/archiva-checksum
diff options
context:
space:
mode:
authorMartin Stockhammer <martin_s@apache.org>2018-04-01 09:54:30 +0200
committerMartin Stockhammer <martin_s@apache.org>2018-04-01 09:54:30 +0200
commitf8a9bc1640ddbe2748eb7bc8e68b4dd3dd84fa51 (patch)
tree1721e44ab24c19e5ebb4a02982942a3e5e28f4b5 /archiva-modules/archiva-base/archiva-checksum
parentfbf5e991b3cbe86b6a60aae4f127e07b30bb374a (diff)
downloadarchiva-f8a9bc1640ddbe2748eb7bc8e68b4dd3dd84fa51.tar.gz
archiva-f8a9bc1640ddbe2748eb7bc8e68b4dd3dd84fa51.zip
Changing the event system to use the checksum module
Diffstat (limited to 'archiva-modules/archiva-base/archiva-checksum')
-rw-r--r--archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksumValidationException.java2
-rw-r--r--archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksummedFile.java29
-rw-r--r--archiva-modules/archiva-base/archiva-checksum/src/test/java/org/apache/archiva/checksum/ChecksummedFileTest.java10
3 files changed, 28 insertions, 13 deletions
diff --git a/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksumValidationException.java b/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksumValidationException.java
index 1123e6afa..280dc8b2d 100644
--- a/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksumValidationException.java
+++ b/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksumValidationException.java
@@ -30,7 +30,7 @@ public class ChecksumValidationException extends RuntimeException
{
public enum ValidationError {
- INVALID_FORMAT, DIGEST_ERROR, READ_ERROR, FILE_NOT_FOUND, BAD_CHECKSUM_FILE_REF
+ INVALID_FORMAT, DIGEST_ERROR, READ_ERROR, FILE_NOT_FOUND, BAD_CHECKSUM_FILE_REF, BAD_CHECKSUM_FILE
};
final private ValidationError errorType;
diff --git a/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksummedFile.java b/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksummedFile.java
index 53a3d4658..b8ff4e338 100644
--- a/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksummedFile.java
+++ b/archiva-modules/archiva-base/archiva-checksum/src/main/java/org/apache/archiva/checksum/ChecksummedFile.java
@@ -30,10 +30,12 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import static org.apache.archiva.checksum.ChecksumValidationException.ValidationError.BAD_CHECKSUM_FILE;
import static org.apache.archiva.checksum.ChecksumValidationException.ValidationError.BAD_CHECKSUM_FILE_REF;
/**
@@ -157,7 +159,7 @@ public class ChecksummedFile
public boolean isValidChecksum( ChecksumAlgorithm algorithm, boolean throwExceptions )
throws ChecksumValidationException
{
- return isValidChecksums( new ChecksumAlgorithm[]{algorithm} );
+ return isValidChecksums( Arrays.asList( algorithm ), throwExceptions );
}
/**
@@ -167,7 +169,7 @@ public class ChecksummedFile
* @param algorithms the algorithms to check for.
* @return true if the checksums report that the the reference file is valid, false if invalid.
*/
- public boolean isValidChecksums( ChecksumAlgorithm algorithms[]) throws ChecksumValidationException
+ public boolean isValidChecksums( List<ChecksumAlgorithm> algorithms) throws ChecksumValidationException
{
return isValidChecksums( algorithms, false );
}
@@ -180,10 +182,10 @@ public class ChecksummedFile
* @return True, if it is valid, otherwise false.
* @throws ChecksumValidationException
*/
- public boolean isValidChecksums( ChecksumAlgorithm algorithms[], boolean throwExceptions) throws ChecksumValidationException
+ public boolean isValidChecksums( List<ChecksumAlgorithm> algorithms, boolean throwExceptions) throws ChecksumValidationException
{
- List<Checksum> checksums = new ArrayList<>( algorithms.length );
+ List<Checksum> checksums = new ArrayList<>( algorithms.size() );
// Create checksum object for each algorithm.
for ( ChecksumAlgorithm checksumAlgorithm : algorithms )
{
@@ -255,15 +257,20 @@ public class ChecksummedFile
return referenceFile;
}
+
+
+ public boolean fixChecksum(ChecksumAlgorithm algorithm) {
+ return fixChecksums( Arrays.asList(algorithm) );
+ }
/**
* Fix or create checksum files for the reference file.
*
* @param algorithms the hashes to check for.
* @return true if checksums were created successfully.
*/
- public boolean fixChecksums( ChecksumAlgorithm[] algorithms )
+ public boolean fixChecksums( List<ChecksumAlgorithm> algorithms )
{
- List<Checksum> checksums = new ArrayList<>( algorithms.length );
+ List<Checksum> checksums = new ArrayList<>( algorithms.size() );
// Create checksum object for each algorithm.
for ( ChecksumAlgorithm checksumAlgorithm : algorithms )
{
@@ -299,7 +306,13 @@ public class ChecksummedFile
Path checksumFile = getChecksumFile( checksumAlgorithm );
if ( Files.exists( checksumFile ) )
{
- String expectedChecksum = parseChecksum( checksumFile, checksumAlgorithm, referenceFile.getFileName( ).toString( ), FILE_ENCODING );
+ String expectedChecksum;
+ try
+ {
+ expectedChecksum = parseChecksum( checksumFile, checksumAlgorithm, referenceFile.getFileName( ).toString( ), FILE_ENCODING );
+ } catch (ChecksumValidationException ex) {
+ expectedChecksum = "";
+ }
if ( !checksum.compare( expectedChecksum ) )
{
@@ -362,6 +375,8 @@ public class ChecksummedFile
{
throw new ChecksumValidationException(BAD_CHECKSUM_FILE_REF,
"The file reference '" + fc.getFileReference( ) + "' in the checksum file does not match expected file: '" + expectedPath + "'" );
+ } else if (!fc.isFormatMatch()) {
+ throw new ChecksumValidationException( BAD_CHECKSUM_FILE, "The checksum file content could not be parsed: "+checksumFile );
}
return fc.getChecksum( );
}
diff --git a/archiva-modules/archiva-base/archiva-checksum/src/test/java/org/apache/archiva/checksum/ChecksummedFileTest.java b/archiva-modules/archiva-base/archiva-checksum/src/test/java/org/apache/archiva/checksum/ChecksummedFileTest.java
index d458d82a7..963cd87bc 100644
--- a/archiva-modules/archiva-base/archiva-checksum/src/test/java/org/apache/archiva/checksum/ChecksummedFileTest.java
+++ b/archiva-modules/archiva-base/archiva-checksum/src/test/java/org/apache/archiva/checksum/ChecksummedFileTest.java
@@ -25,13 +25,13 @@ import org.junit.Before;
import org.junit.Test;
import org.slf4j.LoggerFactory;
-import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
+import java.util.Arrays;
/**
* ChecksummedFileTest
@@ -152,7 +152,7 @@ public class ChecksummedFileTest
assertFalse( "ChecksummedFile.isValid(SHA1) == false",
checksummedFile.isValidChecksum( ChecksumAlgorithm.SHA1 ) );
- boolean fixed = checksummedFile.fixChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1 } );
+ boolean fixed = checksummedFile.fixChecksums( Arrays.asList( ChecksumAlgorithm.SHA1 ) );
assertTrue( "ChecksummedFile.fixChecksums() == true", fixed );
assertTrue( "ChecksummedFile.isValid(SHA1) == true",
@@ -199,7 +199,7 @@ public class ChecksummedFileTest
ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
assertFalse( "ChecksummedFile.isValid(SHA1,MD5)", checksummedFile.isValidChecksums(
- new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 } ) );
+ Arrays.asList(ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 ) ) );
}
@@ -211,7 +211,7 @@ public class ChecksummedFileTest
ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
assertTrue( "ChecksummedFile.isValid(SHA1,MD5)", checksummedFile.isValidChecksums(
- new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 } ) );
+ Arrays.asList(ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 ) ) );
}
@Test
@@ -222,7 +222,7 @@ public class ChecksummedFileTest
ChecksummedFile checksummedFile = new ChecksummedFile( jarFile );
assertTrue( "ChecksummedFile.isValid(SHA1)", checksummedFile.isValidChecksums(
- new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 } ) );
+ Arrays.asList(ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 ) ) );
}