From f8a9bc1640ddbe2748eb7bc8e68b4dd3dd84fa51 Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Sun, 1 Apr 2018 09:54:30 +0200 Subject: [PATCH] Changing the event system to use the checksum module --- .../checksum/ChecksumValidationException.java | 2 +- .../archiva/checksum/ChecksummedFile.java | 29 ++++++++--- .../archiva/checksum/ChecksummedFileTest.java | 10 ++-- .../ArtifactMissingChecksumsConsumer.java | 2 +- .../ArtifactMissingChecksumsConsumerTest.java | 5 +- .../archiva-base/archiva-converter/pom.xml | 4 ++ .../artifact/LegacyToDefaultConverter.java | 48 +++++++++---------- .../LegacyToDefaultConverterTest.java | 4 -- .../archiva/policies/ChecksumPolicy.java | 3 +- .../repository/metadata/MetadataTools.java | 3 +- .../archiva-base/archiva-transaction/pom.xml | 4 ++ .../transaction/AbstractTransactionEvent.java | 36 ++++++-------- .../archiva/transaction/CopyFileEvent.java | 12 ++--- .../archiva/transaction/CreateFileEvent.java | 7 +-- .../archiva/transaction/FileTransaction.java | 13 ++--- .../transaction/AbstractFileEventTest.java | 8 ++-- .../transaction/CopyFileEventTest.java | 6 +-- .../transaction/CreateFileEventTest.java | 6 +-- .../services/DefaultRepositoriesService.java | 6 +-- .../web/api/DefaultFileUploadService.java | 3 +- 20 files changed, 113 insertions(+), 98 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 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 algorithms, boolean throwExceptions) throws ChecksumValidationException { - List checksums = new ArrayList<>( algorithms.length ); + List 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 algorithms ) { - List checksums = new ArrayList<>( algorithms.length ); + List 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 ) ) ); } diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumer.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumer.java index ca45bb617..f90fb10c6 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumer.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumer.java @@ -162,7 +162,7 @@ public class ArtifactMissingChecksumsConsumer checksum = new ChecksummedFile( artifactFile); if ( !checksum.isValidChecksum( checksumAlgorithm ) ) { - checksum.fixChecksums( new ChecksumAlgorithm[]{checksumAlgorithm} ); + checksum.fixChecksum( checksumAlgorithm ); log.info( "Fixed checksum file {}", checksumFile.toAbsolutePath( ) ); triggerConsumerInfo( "Fixed checksum file " + checksumFile.toAbsolutePath( ) ); } diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumerTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumerTest.java index 5851e6770..0cd2488ae 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumerTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/ArtifactMissingChecksumsConsumerTest.java @@ -14,6 +14,7 @@ import org.junit.Test; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; import java.util.Calendar; /* @@ -112,7 +113,7 @@ public class ArtifactMissingChecksumsConsumerTest Assertions.assertThat( sha1Path.toFile() ).exists(); Assertions.assertThat( md5Path.toFile() ).exists(); Assertions.assertThat( - checksum.isValidChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) ) // + checksum.isValidChecksums( Arrays.asList(ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 ) ) ) // .isFalse(); consumer.beginScan( repoConfig, Calendar.getInstance().getTime() ); @@ -122,7 +123,7 @@ public class ArtifactMissingChecksumsConsumerTest Assertions.assertThat( sha1Path.toFile() ).exists(); Assertions.assertThat( md5Path.toFile() ).exists(); Assertions.assertThat( - checksum.isValidChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) ) // + checksum.isValidChecksums( Arrays.asList(ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 )) ) // .isTrue(); } } diff --git a/archiva-modules/archiva-base/archiva-converter/pom.xml b/archiva-modules/archiva-base/archiva-converter/pom.xml index 8d729fbc5..98a7abe04 100644 --- a/archiva-modules/archiva-base/archiva-converter/pom.xml +++ b/archiva-modules/archiva-base/archiva-converter/pom.xml @@ -41,6 +41,10 @@ org.apache.maven.shared maven-model-converter + + org.apache.archiva + archiva-checksum + org.apache.archiva archiva-repository-layer diff --git a/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverter.java b/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverter.java index f6a17a661..1404b0ca4 100644 --- a/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverter.java +++ b/archiva-modules/archiva-base/archiva-converter/src/main/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverter.java @@ -19,6 +19,9 @@ package org.apache.archiva.converter.artifact; * under the License. */ +import org.apache.archiva.checksum.ChecksumAlgorithm; +import org.apache.archiva.checksum.ChecksumValidationException; +import org.apache.archiva.checksum.ChecksummedFile; import org.apache.archiva.common.plexusbridge.DigesterUtils; import org.apache.archiva.common.plexusbridge.PlexusSisuBridge; import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException; @@ -59,6 +62,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -75,14 +79,11 @@ public class LegacyToDefaultConverter /** * {@link List}<{@link Digester} */ - private List digesters; + private List digesters; @Inject private PlexusSisuBridge plexusSisuBridge; - @Inject - private DigesterUtils digesterUtils; - private ModelConverter translator; private ArtifactFactory artifactFactory; @@ -99,7 +100,8 @@ public class LegacyToDefaultConverter public void initialize() throws PlexusSisuBridgeException { - this.digesters = digesterUtils.getAllDigesters(); + // TODO: Should be configurable! + this.digesters = Arrays.asList(ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5); translator = plexusSisuBridge.lookup( ModelConverter.class ); artifactFactory = plexusSisuBridge.lookup( ArtifactFactory.class ); artifactHandlerManager = plexusSisuBridge.lookup( ArtifactHandlerManager.class ); @@ -287,7 +289,7 @@ public class LegacyToDefaultConverter throws IOException { boolean result = true; - for ( Digester digester : digesters ) + for ( ChecksumAlgorithm digester : digesters ) { result &= verifyChecksum( file, file.getFileName() + "." + getDigesterFileExtension( digester ), digester, //$NON-NLS-1$ @@ -297,24 +299,22 @@ public class LegacyToDefaultConverter return result; } - private boolean verifyChecksum( Path file, String fileName, Digester digester, Artifact artifact, String key ) + private boolean verifyChecksum( Path file, String fileName, ChecksumAlgorithm digester, Artifact artifact, String key ) throws IOException { - boolean result = true; - + boolean result; Path checksumFile = file.resolveSibling( fileName ); - if ( Files.exists(checksumFile) ) + // We ignore the check, if the checksum file does not exist + if (!Files.exists(checksumFile)) { + return true; + } + ChecksummedFile csFile = new ChecksummedFile( file ); + try { - String checksum = org.apache.archiva.common.utils.FileUtils.readFileToString( checksumFile, Charset.defaultCharset() ); - try - { - digester.verify( file.toFile(), checksum ); - } - catch ( DigesterException e ) - { - addWarning( artifact, Messages.getString( key ) ); - result = false; - } + result = csFile.isValidChecksum( digester, true ); + } catch (ChecksumValidationException e ) { + addWarning( artifact, Messages.getString( key ) ); + result = false; } return result; } @@ -323,9 +323,9 @@ public class LegacyToDefaultConverter * File extension for checksums * TODO should be moved to plexus-digester ? */ - private String getDigesterFileExtension( Digester digester ) + private String getDigesterFileExtension( ChecksumAlgorithm checksumAlgorithm ) { - return digester.getAlgorithm().toLowerCase().replaceAll( "-", "" ); //$NON-NLS-1$ //$NON-NLS-2$ + return checksumAlgorithm.getExt().get(0); } private boolean copyArtifact( Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction ) @@ -672,12 +672,12 @@ public class LegacyToDefaultConverter } - public List getDigesters() + public List getDigesters() { return digesters; } - public void setDigesters( List digesters ) + public void setDigesters( List digesters ) { this.digesters = digesters; } diff --git a/archiva-modules/archiva-base/archiva-converter/src/test/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverterTest.java b/archiva-modules/archiva-base/archiva-converter/src/test/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverterTest.java index 5084bffa9..b6fdbf140 100644 --- a/archiva-modules/archiva-base/archiva-converter/src/test/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverterTest.java +++ b/archiva-modules/archiva-base/archiva-converter/src/test/java/org/apache/archiva/converter/artifact/LegacyToDefaultConverterTest.java @@ -84,10 +84,6 @@ public class LegacyToDefaultConverterTest ArtifactRepositoryFactory factory = plexusSisuBridge.lookup( ArtifactRepositoryFactory.class ); - Map layoutsMap = plexusSisuBridge.lookupMap( ArtifactRepositoryLayout.class ); - - System.out.println( "hints " + layoutsMap.keySet().toString() ); - ArtifactRepositoryLayout layout = plexusSisuBridge.lookup( ArtifactRepositoryLayout.class, "legacy" ); Path sourceBase = getTestFile( "src/test/source-repository" ); diff --git a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/ChecksumPolicy.java b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/ChecksumPolicy.java index 146ea8809..c2004d139 100644 --- a/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/ChecksumPolicy.java +++ b/archiva-modules/archiva-base/archiva-policies/src/main/java/org/apache/archiva/policies/ChecksumPolicy.java @@ -30,6 +30,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Properties; @@ -66,7 +67,7 @@ public class ChecksumPolicy */ public static final String FIX = "fix"; - private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 }; + private List algorithms = Arrays.asList( ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 ); private List options = new ArrayList<>( 3 ); diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/MetadataTools.java b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/MetadataTools.java index af5b4ea1a..9bfcc2676 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/MetadataTools.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/main/java/org/apache/archiva/repository/metadata/MetadataTools.java @@ -61,6 +61,7 @@ import java.nio.file.Paths; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.Collection; import java.util.Collections; @@ -108,7 +109,7 @@ public class MetadataTools @Named( value = "fileTypes" ) private FileTypes filetypes; - private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 }; + private List algorithms = Arrays.asList(ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 ); private List artifactPatterns; diff --git a/archiva-modules/archiva-base/archiva-transaction/pom.xml b/archiva-modules/archiva-base/archiva-transaction/pom.xml index f15b31f09..12143296f 100644 --- a/archiva-modules/archiva-base/archiva-transaction/pom.xml +++ b/archiva-modules/archiva-base/archiva-transaction/pom.xml @@ -33,6 +33,10 @@ org.apache.archiva archiva-common + + org.apache.archiva + archiva-checksum + commons-io commons-io diff --git a/archiva-modules/archiva-base/archiva-transaction/src/main/java/org/apache/archiva/transaction/AbstractTransactionEvent.java b/archiva-modules/archiva-base/archiva-transaction/src/main/java/org/apache/archiva/transaction/AbstractTransactionEvent.java index 2df01e668..0082517d8 100644 --- a/archiva-modules/archiva-base/archiva-transaction/src/main/java/org/apache/archiva/transaction/AbstractTransactionEvent.java +++ b/archiva-modules/archiva-base/archiva-transaction/src/main/java/org/apache/archiva/transaction/AbstractTransactionEvent.java @@ -19,9 +19,10 @@ package org.apache.archiva.transaction; * under the License. */ +import org.apache.archiva.checksum.ChecksumAlgorithm; +import org.apache.archiva.checksum.ChecksummedFile; import org.apache.commons.io.FileUtils; import org.codehaus.plexus.digest.Digester; -import org.codehaus.plexus.digest.DigesterException; import java.io.IOException; import java.nio.charset.Charset; @@ -53,21 +54,21 @@ public abstract class AbstractTransactionEvent /** * {@link List}<{@link Digester}> */ - private List digesters; + private List checksumAlgorithms; protected AbstractTransactionEvent() { - this( new ArrayList( 0 ) ); + this( new ArrayList( 0 ) ); } - protected AbstractTransactionEvent( List digesters ) + protected AbstractTransactionEvent( List checksumAlgorithms) { - this.digesters = digesters; + this.checksumAlgorithms = checksumAlgorithms; } - protected List getDigesters() + protected List getChecksumAlgorithms() { - return digesters; + return checksumAlgorithms; } /** @@ -181,10 +182,10 @@ public abstract class AbstractTransactionEvent protected void createChecksums( Path file, boolean force ) throws IOException { - for ( Digester digester : getDigesters() ) + for ( ChecksumAlgorithm checksumAlgorithm : getChecksumAlgorithms() ) { - Path checksumFile = Paths.get(file.toAbsolutePath() + "." + getDigesterFileExtension( digester ) ); - if ( Files.exists(checksumFile) ) + Path checksumFile = Paths.get( file.toAbsolutePath( ) + "." + getChecksumFileExtension( checksumAlgorithm ) ); + if ( Files.exists( checksumFile ) ) { if ( !force ) { @@ -196,16 +197,9 @@ public abstract class AbstractTransactionEvent { createdFiles.add( checksumFile ); } - - try - { - writeStringToFile( checksumFile, digester.calc( file.toFile() ) ); - } - catch ( DigesterException e ) - { - throw (IOException) e.getCause(); - } } + ChecksummedFile csFile = new ChecksummedFile( file ); + csFile.fixChecksums( getChecksumAlgorithms() ); } /** @@ -221,9 +215,9 @@ public abstract class AbstractTransactionEvent * File extension for checksums * TODO should be moved to plexus-digester ? */ - protected String getDigesterFileExtension( Digester digester ) + protected String getChecksumFileExtension( ChecksumAlgorithm algorithm ) { - return digester.getAlgorithm().toLowerCase().replaceAll( "-", "" ); + return algorithm.getExt().get(0); } } diff --git a/archiva-modules/archiva-base/archiva-transaction/src/main/java/org/apache/archiva/transaction/CopyFileEvent.java b/archiva-modules/archiva-base/archiva-transaction/src/main/java/org/apache/archiva/transaction/CopyFileEvent.java index 2ef4a9093..b168dfbcf 100644 --- a/archiva-modules/archiva-base/archiva-transaction/src/main/java/org/apache/archiva/transaction/CopyFileEvent.java +++ b/archiva-modules/archiva-base/archiva-transaction/src/main/java/org/apache/archiva/transaction/CopyFileEvent.java @@ -19,8 +19,8 @@ package org.apache.archiva.transaction; * under the License. */ +import org.apache.archiva.checksum.ChecksumAlgorithm; import org.apache.commons.io.FileUtils; -import org.codehaus.plexus.digest.Digester; import java.io.IOException; import java.nio.file.Files; @@ -44,11 +44,11 @@ public class CopyFileEvent * * @param source * @param destination - * @param digesters {@link List}<{@link Digester}> digesters to use for checksumming + * @param checksumAlgorithms The checksum algorithms */ - public CopyFileEvent( Path source, Path destination, List digesters ) + public CopyFileEvent( Path source, Path destination, List checksumAlgorithms ) { - super( digesters ); + super( checksumAlgorithms ); this.source = source; this.destination = destination; } @@ -77,9 +77,9 @@ public class CopyFileEvent private void copyChecksums() throws IOException { - for ( Digester digester : getDigesters() ) + for ( ChecksumAlgorithm checksumAlgorithm : getChecksumAlgorithms() ) { - copyChecksum( getDigesterFileExtension( digester ) ); + copyChecksum( getChecksumFileExtension( checksumAlgorithm ) ); } } diff --git a/archiva-modules/archiva-base/archiva-transaction/src/main/java/org/apache/archiva/transaction/CreateFileEvent.java b/archiva-modules/archiva-base/archiva-transaction/src/main/java/org/apache/archiva/transaction/CreateFileEvent.java index 6cb6126b7..2be970657 100644 --- a/archiva-modules/archiva-base/archiva-transaction/src/main/java/org/apache/archiva/transaction/CreateFileEvent.java +++ b/archiva-modules/archiva-base/archiva-transaction/src/main/java/org/apache/archiva/transaction/CreateFileEvent.java @@ -19,6 +19,7 @@ package org.apache.archiva.transaction; * under the License. */ +import org.apache.archiva.checksum.ChecksumAlgorithm; import org.codehaus.plexus.digest.Digester; import java.io.IOException; @@ -42,11 +43,11 @@ public class CreateFileEvent * * @param content * @param destination - * @param digesters {@link List}<{@link Digester}> digesters to use for checksumming + * @param checksumAlgorithms {@link List}<{@link Digester}> digesters to use for checksumming */ - public CreateFileEvent( String content, Path destination, List digesters ) + public CreateFileEvent( String content, Path destination, List checksumAlgorithms ) { - super( digesters ); + super( checksumAlgorithms ); this.content = content; this.destination = destination; } diff --git a/archiva-modules/archiva-base/archiva-transaction/src/main/java/org/apache/archiva/transaction/FileTransaction.java b/archiva-modules/archiva-base/archiva-transaction/src/main/java/org/apache/archiva/transaction/FileTransaction.java index 8b4a5dd3a..1c93667ac 100644 --- a/archiva-modules/archiva-base/archiva-transaction/src/main/java/org/apache/archiva/transaction/FileTransaction.java +++ b/archiva-modules/archiva-base/archiva-transaction/src/main/java/org/apache/archiva/transaction/FileTransaction.java @@ -19,6 +19,7 @@ package org.apache.archiva.transaction; * under the License. */ +import org.apache.archiva.checksum.ChecksumAlgorithm; import org.codehaus.plexus.digest.Digester; import java.io.IOException; @@ -77,20 +78,20 @@ public class FileTransaction /** * @param source * @param destination - * @param digesters {@link List}<{@link org.codehaus.plexus.digest.Digester}> digesters to use for checksumming + * @param checksumAlgorithms The checksum algorithms */ - public void copyFile(Path source, Path destination, List digesters ) + public void copyFile(Path source, Path destination, List checksumAlgorithms ) { - events.add( new CopyFileEvent( source, destination, digesters ) ); + events.add( new CopyFileEvent( source, destination, checksumAlgorithms ) ); } /** * @param content * @param destination - * @param digesters {@link List}<{@link org.codehaus.plexus.digest.Digester}> digesters to use for checksumming + * @param checksumAlgorithms Checksum algorithms */ - public void createFile( String content, Path destination, List digesters ) + public void createFile( String content, Path destination, List checksumAlgorithms ) { - events.add( new CreateFileEvent( content, destination, digesters ) ); + events.add( new CreateFileEvent( content, destination, checksumAlgorithms ) ); } } diff --git a/archiva-modules/archiva-base/archiva-transaction/src/test/java/org/apache/archiva/transaction/AbstractFileEventTest.java b/archiva-modules/archiva-base/archiva-transaction/src/test/java/org/apache/archiva/transaction/AbstractFileEventTest.java index 80b9ca924..0905c477c 100644 --- a/archiva-modules/archiva-base/archiva-transaction/src/test/java/org/apache/archiva/transaction/AbstractFileEventTest.java +++ b/archiva-modules/archiva-base/archiva-transaction/src/test/java/org/apache/archiva/transaction/AbstractFileEventTest.java @@ -20,11 +20,9 @@ package org.apache.archiva.transaction; */ import junit.framework.TestCase; +import org.apache.archiva.checksum.ChecksumAlgorithm; import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; import org.apache.commons.io.FileUtils; -import org.codehaus.plexus.digest.Digester; -import org.codehaus.plexus.digest.Md5Digester; -import org.codehaus.plexus.digest.Sha1Digester; import org.junit.Before; import org.junit.runner.RunWith; @@ -43,7 +41,7 @@ import java.util.List; public abstract class AbstractFileEventTest extends TestCase { - protected List digesters; + protected List checksumAlgorithms; @SuppressWarnings( "unchecked" ) @Before @@ -53,7 +51,7 @@ public abstract class AbstractFileEventTest { super.setUp(); - digesters = Arrays.asList( (Digester) new Md5Digester(), (Digester) new Sha1Digester() ); + checksumAlgorithms = Arrays.asList( ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 ); } protected void assertChecksumExists(Path file, String algorithm ) diff --git a/archiva-modules/archiva-base/archiva-transaction/src/test/java/org/apache/archiva/transaction/CopyFileEventTest.java b/archiva-modules/archiva-base/archiva-transaction/src/test/java/org/apache/archiva/transaction/CopyFileEventTest.java index 164b5e6db..4ecf44377 100644 --- a/archiva-modules/archiva-base/archiva-transaction/src/test/java/org/apache/archiva/transaction/CopyFileEventTest.java +++ b/archiva-modules/archiva-base/archiva-transaction/src/test/java/org/apache/archiva/transaction/CopyFileEventTest.java @@ -77,7 +77,7 @@ public class CopyFileEventTest public void testCopyCommitRollback() throws Exception { - CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters ); + CopyFileEvent event = new CopyFileEvent( testSource, testDest, checksumAlgorithms ); assertFalse( "Test that the destination is not yet created", Files.exists(testDest) ); @@ -110,7 +110,7 @@ public class CopyFileEventTest assertTrue( "Test that the destination exists", Files.exists(testDest) ); - CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters ); + CopyFileEvent event = new CopyFileEvent( testSource, testDest, checksumAlgorithms ); String target = readFile( testDest ); @@ -137,7 +137,7 @@ public class CopyFileEventTest public void testCreateRollbackCommit() throws Exception { - CopyFileEvent event = new CopyFileEvent( testSource, testDest, digesters ); + CopyFileEvent event = new CopyFileEvent( testSource, testDest, checksumAlgorithms ); assertFalse( "Test that the destination is not yet created", Files.exists(testDest) ); diff --git a/archiva-modules/archiva-base/archiva-transaction/src/test/java/org/apache/archiva/transaction/CreateFileEventTest.java b/archiva-modules/archiva-base/archiva-transaction/src/test/java/org/apache/archiva/transaction/CreateFileEventTest.java index 30164eb77..36924574d 100644 --- a/archiva-modules/archiva-base/archiva-transaction/src/test/java/org/apache/archiva/transaction/CreateFileEventTest.java +++ b/archiva-modules/archiva-base/archiva-transaction/src/test/java/org/apache/archiva/transaction/CreateFileEventTest.java @@ -40,7 +40,7 @@ public class CreateFileEventTest { Path testFile = testDir.resolve("test-file.txt" ); - CreateFileEvent event = new CreateFileEvent( "file contents", testFile, digesters ); + CreateFileEvent event = new CreateFileEvent( "file contents", testFile, checksumAlgorithms ); assertFalse( "Test file is not yet created", Files.exists(testFile) ); @@ -72,7 +72,7 @@ public class CreateFileEventTest writeFile( testFile, "original contents" ); - CreateFileEvent event = new CreateFileEvent( "modified contents", testFile, digesters ); + CreateFileEvent event = new CreateFileEvent( "modified contents", testFile, checksumAlgorithms ); String contents = readFile( testFile ); @@ -101,7 +101,7 @@ public class CreateFileEventTest { Path testFile = testDir.resolve( "test-file.txt" ); - CreateFileEvent event = new CreateFileEvent( "file contents", testFile, digesters ); + CreateFileEvent event = new CreateFileEvent( "file contents", testFile, checksumAlgorithms ); assertFalse( "Test file is not yet created", Files.exists(testFile) ); diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java index d3da9bb62..c8ae783f1 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java @@ -25,7 +25,6 @@ import org.apache.archiva.admin.model.beans.ManagedRepository; import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin; import org.apache.archiva.checksum.ChecksumAlgorithm; import org.apache.archiva.checksum.ChecksummedFile; -import org.apache.archiva.common.plexusbridge.PlexusSisuBridge; import org.apache.archiva.common.utils.VersionComparator; import org.apache.archiva.common.utils.VersionUtil; import org.apache.archiva.maven2.metadata.MavenMetadataReader; @@ -96,6 +95,7 @@ import java.nio.file.StandardCopyOption; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.Collection; import java.util.Collections; @@ -125,8 +125,6 @@ public class DefaultRepositoriesService @Inject private ManagedRepositoryAdmin managedRepositoryAdmin; - @Inject - private PlexusSisuBridge plexusSisuBridge; @Inject private SecuritySystem securitySystem; @@ -156,7 +154,7 @@ public class DefaultRepositoriesService @Named(value = "cache#namespaces") private Cache> namespacesCache; - private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 }; + private List algorithms = Arrays.asList(ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 ); @Override public Boolean scanRepository( String repositoryId, boolean fullScan ) diff --git a/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/DefaultFileUploadService.java b/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/DefaultFileUploadService.java index 937470af2..5e1c69ac9 100644 --- a/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/DefaultFileUploadService.java +++ b/archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/api/DefaultFileUploadService.java @@ -75,6 +75,7 @@ import java.nio.file.StandardCopyOption; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Date; @@ -105,7 +106,7 @@ public class DefaultFileUploadService @Inject private ArchivaAdministration archivaAdministration; - private ChecksumAlgorithm[] algorithms = new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 }; + private List algorithms = Arrays.asList( ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 ); @Inject @Named(value = "archivaTaskScheduler#repository") -- 2.39.5