// FIXME: We need to be able to disable/retrieve/manipulate existing checksum observers on wagon instances.
// Otherwise, each time a checksum GET throws ResourceNotFoundException, all other checksum data in other
// observers for the main artifact have their actual checksums destroyed.
for ( Iterator it = checksumObservers.iterator(); it.hasNext(); )
{
ChecksumObserver observer = (ChecksumObserver) it.next();
wagon.removeTransferListener( observer );
}
try
{
// grab it first, because it's about to change...
String actualChecksum = checksumObserver.getActualChecksum();
File tempChecksumFile = new File( tempDestination + checksumFileExtension + ".tmp" );
tempChecksumFile.deleteOnExit();
wagon.get( remotePath + checksumFileExtension, tempChecksumFile );
String expectedChecksum = FileUtils.fileRead( tempChecksumFile, "UTF-8" );
// remove whitespaces at the end
expectedChecksum = expectedChecksum.trim();
// check for 'ALGO (name) = CHECKSUM' like used by openssl
if ( expectedChecksum.regionMatches( true, 0, "MD", 0, 2 )
|| expectedChecksum.regionMatches( true, 0, "SHA", 0, 3 ) )
{
int lastSpacePos = expectedChecksum.lastIndexOf( ' ' );
expectedChecksum = expectedChecksum.substring( lastSpacePos + 1 );
}
else
{
// remove everything after the first space (if available)
int spacePos = expectedChecksum.indexOf( ' ' );
if ( spacePos != -1 )
{
expectedChecksum = expectedChecksum.substring( 0, spacePos );
}
}
if ( expectedChecksum.equalsIgnoreCase( actualChecksum ) )
{
File checksumFile = new File( destination + checksumFileExtension );
if ( checksumFile.exists() )
{
checksumFile.delete();
}
FileUtils.copyFile( tempChecksumFile, checksumFile );
}
else
{
throw new ChecksumFailedException( "Checksum failed on download: local = '" + actualChecksum +
"'; remote = '" + expectedChecksum + "'" );
}
}
catch ( IOException e )
{
throw new ChecksumFailedException( "Invalid checksum file", e );
}
finally
{
for ( Iterator it = checksumObservers.iterator(); it.hasNext(); )
{
ChecksumObserver observer = (ChecksumObserver) it.next();
wagon.addTransferListener( observer );
}
}
}