mirror of https://github.com/apache/archiva.git
use 1.7 features for files
This commit is contained in:
parent
f766714024
commit
f5022a2721
|
@ -19,6 +19,9 @@ package org.apache.archiva.checksum;
|
|||
* under the License.
|
||||
*/
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.io.output.NullOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.DigestInputStream;
|
||||
|
@ -26,13 +29,8 @@ import java.security.MessageDigest;
|
|||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.io.output.NullOutputStream;
|
||||
|
||||
/**
|
||||
* Checksum - simple checksum hashing routines.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class Checksum
|
||||
{
|
||||
|
@ -67,8 +65,9 @@ public class Checksum
|
|||
catch ( NoSuchAlgorithmException e )
|
||||
{
|
||||
// Not really possible, but here none-the-less
|
||||
throw new IllegalStateException( "Unable to initialize MessageDigest algorithm " + checksumAlgorithm.getAlgorithm()
|
||||
+ " : " + e.getMessage(), e );
|
||||
throw new IllegalStateException(
|
||||
"Unable to initialize MessageDigest algorithm " + checksumAlgorithm.getAlgorithm() + " : "
|
||||
+ e.getMessage(), e );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -96,9 +95,10 @@ public class Checksum
|
|||
public Checksum update( InputStream stream )
|
||||
throws IOException
|
||||
{
|
||||
DigestInputStream dig = new DigestInputStream( stream, md );
|
||||
IOUtils.copy( dig, new NullOutputStream() );
|
||||
|
||||
try (DigestInputStream dig = new DigestInputStream( stream, md ))
|
||||
{
|
||||
IOUtils.copy( dig, new NullOutputStream() );
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ import java.util.regex.Pattern;
|
|||
|
||||
/**
|
||||
* ChecksummedFile
|
||||
*
|
||||
* <p/>
|
||||
* <p>Terminology:</p>
|
||||
* <dl>
|
||||
* <dt>Checksum File</dt>
|
||||
|
@ -46,13 +46,13 @@ import java.util.regex.Pattern;
|
|||
* <dt>Reference File</dt>
|
||||
* <dd>The file that is being referenced in the checksum file.</dd>
|
||||
* </dl>
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class ChecksummedFile
|
||||
{
|
||||
private final Logger log = LoggerFactory.getLogger( ChecksummedFile.class );
|
||||
|
||||
private static final Pattern METADATA_PATTERN = Pattern.compile( "maven-metadata-\\S*.xml" );
|
||||
|
||||
private final File referenceFile;
|
||||
|
||||
/**
|
||||
|
@ -76,7 +76,7 @@ public class ChecksummedFile
|
|||
throws IOException
|
||||
{
|
||||
|
||||
try (InputStream fis = Files.newInputStream( referenceFile.toPath() ) )
|
||||
try (InputStream fis = Files.newInputStream( referenceFile.toPath() ))
|
||||
{
|
||||
Checksum checksum = new Checksum( checksumAlgorithm );
|
||||
checksum.update( fis );
|
||||
|
@ -115,7 +115,7 @@ public class ChecksummedFile
|
|||
* <p>
|
||||
* Given a checksum file, check to see if the file it represents is valid according to the checksum.
|
||||
* </p>
|
||||
*
|
||||
* <p/>
|
||||
* <p>
|
||||
* NOTE: Only supports single file checksums of type MD5 or SHA1.
|
||||
* </p>
|
||||
|
@ -199,7 +199,8 @@ public class ChecksummedFile
|
|||
}
|
||||
|
||||
return valid;
|
||||
} catch ( IOException e )
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
log.warn( "Unable to read / parse checksum: {}", e.getMessage() );
|
||||
return false;
|
||||
|
@ -228,7 +229,6 @@ public class ChecksummedFile
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
try (InputStream fis = Files.newInputStream( referenceFile.toPath() ))
|
||||
{
|
||||
// Parse file once, for all checksums.
|
||||
|
@ -281,8 +281,8 @@ public class ChecksummedFile
|
|||
private boolean isValidChecksumPattern( String filename, String path )
|
||||
{
|
||||
// check if it is a remote metadata file
|
||||
Pattern pattern = Pattern.compile( "maven-metadata-\\S*.xml" );
|
||||
Matcher m = pattern.matcher( path );
|
||||
|
||||
Matcher m = METADATA_PATTERN.matcher( path );
|
||||
if ( m.matches() )
|
||||
{
|
||||
return filename.endsWith( path ) || ( "-".equals( filename ) ) || filename.endsWith( "maven-metadata.xml" );
|
||||
|
@ -297,6 +297,7 @@ public class ChecksummedFile
|
|||
* Validate the expected path, and expected checksum algorithm, then return
|
||||
* the trimmed checksum hex string.
|
||||
* </p>
|
||||
*
|
||||
* @param rawChecksumString
|
||||
* @param expectedHash
|
||||
* @param expectedPath
|
||||
|
|
|
@ -145,6 +145,11 @@
|
|||
<artifactId>xercesImpl</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<pluginManagement>
|
||||
|
|
|
@ -141,8 +141,8 @@ public class ArtifactMissingChecksumsConsumer
|
|||
public void processFile( String path )
|
||||
throws ConsumerException
|
||||
{
|
||||
createFixChecksum( path, new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1 } );
|
||||
createFixChecksum( path, new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5 } );
|
||||
createFixChecksum( path, ChecksumAlgorithm.SHA1 );
|
||||
createFixChecksum( path, ChecksumAlgorithm.MD5 );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -152,19 +152,19 @@ public class ArtifactMissingChecksumsConsumer
|
|||
processFile( path );
|
||||
}
|
||||
|
||||
private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm[] )
|
||||
private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm )
|
||||
{
|
||||
File artifactFile = new File( this.repositoryDir, path );
|
||||
File checksumFile = new File( this.repositoryDir, path + checksumAlgorithm[0].getExt() );
|
||||
File checksumFile = new File( this.repositoryDir, path + checksumAlgorithm.getExt() );
|
||||
|
||||
if ( checksumFile.exists() )
|
||||
{
|
||||
checksum = new ChecksummedFile( artifactFile );
|
||||
try
|
||||
{
|
||||
if ( !checksum.isValidChecksum( checksumAlgorithm[0] ) )
|
||||
if ( !checksum.isValidChecksum( checksumAlgorithm ) )
|
||||
{
|
||||
checksum.fixChecksums( checksumAlgorithm );
|
||||
checksum.fixChecksums( new ChecksumAlgorithm[]{ checksumAlgorithm } );
|
||||
log.info( "Fixed checksum file {}", checksumFile.getAbsolutePath() );
|
||||
triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath() );
|
||||
}
|
||||
|
@ -181,7 +181,7 @@ public class ArtifactMissingChecksumsConsumer
|
|||
checksum = new ChecksummedFile( artifactFile );
|
||||
try
|
||||
{
|
||||
checksum.createChecksum( checksumAlgorithm[0] );
|
||||
checksum.createChecksum( checksumAlgorithm );
|
||||
log.info( "Created missing checksum file {}", checksumFile.getAbsolutePath() );
|
||||
triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() );
|
||||
}
|
||||
|
|
|
@ -1,16 +1,20 @@
|
|||
package org.apache.archiva.consumers.core;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Calendar;
|
||||
import org.apache.archiva.admin.model.beans.ManagedRepository;
|
||||
import org.apache.archiva.checksum.ChecksumAlgorithm;
|
||||
import org.apache.archiva.checksum.ChecksummedFile;
|
||||
import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import static org.junit.Assert.*;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Calendar;
|
||||
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
|
@ -58,21 +62,26 @@ public class ArtifactMissingChecksumsConsumerTest
|
|||
{
|
||||
String path = "/no-checksums-artifact/1.0/no-checksums-artifact-1.0.jar";
|
||||
|
||||
File sha1File = new File( repoConfig.getLocation(), path + ".sha1" );
|
||||
File md5File = new File( repoConfig.getLocation(), path + ".md5" );
|
||||
Path sha1Path = Paths.get( repoConfig.getLocation(),
|
||||
path + ".sha1" );// new File( repoConfig.getLocation(), path + ".sha1" );
|
||||
Path md5FilePath =
|
||||
Paths.get( repoConfig.getLocation(), path + ".md5" );// new File( repoConfig.getLocation(), path + ".md5" );
|
||||
|
||||
sha1File.delete();
|
||||
md5File.delete();
|
||||
Files.deleteIfExists( sha1Path );
|
||||
Files.deleteIfExists( md5FilePath );
|
||||
|
||||
assertFalse( sha1File.exists() );
|
||||
assertFalse( md5File.exists() );
|
||||
//sha1File.delete();
|
||||
//md5File.delete();
|
||||
|
||||
Assertions.assertThat( sha1Path.toFile() ).doesNotExist();// assertFalse( sha1File.exists() );
|
||||
Assertions.assertThat( md5FilePath.toFile() ).doesNotExist();// assertFalse( md5File.exists() );
|
||||
|
||||
consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
|
||||
|
||||
consumer.processFile( path );
|
||||
|
||||
assertTrue( sha1File.exists() );
|
||||
assertTrue( md5File.exists() );
|
||||
Assertions.assertThat( sha1Path.toFile() ).exists();// assertTrue( sha1File.exists() );
|
||||
Assertions.assertThat( md5FilePath.toFile() ).exists();//assertTrue( md5File.exists() );
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -86,21 +95,28 @@ public class ArtifactMissingChecksumsConsumerTest
|
|||
|
||||
String path = "/incorrect-checksums/1.0/incorrect-checksums-1.0.jar";
|
||||
|
||||
File sha1File = new File( repoConfig.getLocation(), path + ".sha1" );
|
||||
File md5File = new File( repoConfig.getLocation(), path + ".md5" );
|
||||
// new File( repoConfig.getLocation(), path + ".sha1" );
|
||||
Path sha1Path = Paths.get( repoConfig.getLocation(), path + ".sha1" );
|
||||
|
||||
//new File( repoConfig.getLocation(), path + ".md5" );
|
||||
Path md5Path = Paths.get( repoConfig.getLocation(), path + ".md5" );
|
||||
|
||||
ChecksummedFile checksum = new ChecksummedFile( new File( repoConfig.getLocation(), path ) );
|
||||
|
||||
assertTrue( sha1File.exists() );
|
||||
assertTrue( md5File.exists() );
|
||||
assertFalse( checksum.isValidChecksums( new ChecksumAlgorithm[] { ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) );
|
||||
Assertions.assertThat( sha1Path.toFile() ).exists();
|
||||
Assertions.assertThat( md5Path.toFile() ).exists();
|
||||
Assertions.assertThat(
|
||||
checksum.isValidChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) ) //
|
||||
.isFalse();
|
||||
|
||||
consumer.beginScan( repoConfig, Calendar.getInstance().getTime() );
|
||||
|
||||
consumer.processFile( path );
|
||||
|
||||
assertTrue( sha1File.exists() );
|
||||
assertTrue( md5File.exists() );
|
||||
assertTrue( checksum.isValidChecksums( new ChecksumAlgorithm[] { ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) );
|
||||
Assertions.assertThat( sha1Path.toFile() ).exists();
|
||||
Assertions.assertThat( md5Path.toFile() ).exists();
|
||||
Assertions.assertThat(
|
||||
checksum.isValidChecksums( new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5, ChecksumAlgorithm.SHA1 } ) ) //
|
||||
.isTrue();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue