turn on md5 checksum tests on download

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@164054 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-04-21 13:19:29 +00:00
parent 26c968d770
commit 3aea101d3c
2 changed files with 77 additions and 20 deletions

View File

@ -0,0 +1,33 @@
package org.apache.maven.artifact;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.maven.wagon.TransferFailedException;
/**
* Occurs when a download checksum fails.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @version $Id$
*/
public class ChecksumFailedException extends TransferFailedException
{
public ChecksumFailedException( String s )
{
super( s );
}
}

View File

@ -17,6 +17,7 @@ package org.apache.maven.artifact.manager;
*/
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ChecksumFailedException;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.artifact.metadata.ArtifactMetadata;
import org.apache.maven.artifact.repository.ArtifactRepository;
@ -48,7 +49,6 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
public class DefaultWagonManager
extends AbstractLogEnabled
@ -260,7 +260,7 @@ public class DefaultWagonManager
private void getRemoteFile( ArtifactRepository repository, File destination, String remotePath,
TransferListener downloadMonitor )
throws TransferFailedException, ResourceDoesNotExistException
throws TransferFailedException, ResourceDoesNotExistException, ChecksumFailedException
{
Wagon wagon;
@ -273,16 +273,6 @@ public class DefaultWagonManager
throw new TransferFailedException( "Unsupported Protocol: ", e );
}
// ----------------------------------------------------------------------
// These can certainly be configurable ... registering listeners
// ...
//ChecksumObserver md5SumObserver = new ChecksumObserver();
// ----------------------------------------------------------------------
//wagon.addTransferListener( md5SumObserver );
if ( downloadMonitor != null )
{
wagon.addTransferListener( downloadMonitor );
@ -294,6 +284,18 @@ public class DefaultWagonManager
destination.getParentFile().mkdirs();
}
// TODO: configure on repository
ChecksumObserver checksumObserver;
try
{
checksumObserver = new ChecksumObserver( "MD5" );
wagon.addTransferListener( checksumObserver );
}
catch ( NoSuchAlgorithmException e )
{
throw new TransferFailedException( "Unable to add checksum methods", e );
}
File temp = new File( destination + ".tmp" );
temp.deleteOnExit();
@ -302,6 +304,32 @@ public class DefaultWagonManager
wagon.connect( repository, getProxy( repository.getProtocol() ) );
wagon.get( remotePath, temp );
try
{
// grab it first, because it's about to change...
String actualChecksum = checksumObserver.getActualChecksum();
File checksumFile = new File( destination + ".md5" );
wagon.get( remotePath + ".md5", checksumFile );
String expectedChecksum = FileUtils.fileRead( checksumFile );
if ( !expectedChecksum.equals( actualChecksum ) )
{
// TODO: optionally retry?
throw new ChecksumFailedException(
"Checksum failed on download: local = " + actualChecksum + "; remote = " +
expectedChecksum );
}
}
catch ( ResourceDoesNotExistException e )
{
getLogger().warn( "No checksum exists - assuming a valid download" );
}
catch ( IOException e )
{
getLogger().error( "Unable to read checksum - assuming a valid download", e );
}
}
catch ( ConnectionException e )
{
@ -327,15 +355,11 @@ public class DefaultWagonManager
throw new ResourceDoesNotExistException( "Downloaded file does not exist: " + temp );
}
// The temporary file is named destination + ".tmp" and is done this
// way to ensure
// that the temporary file is in the same file system as the
// destination because the
// The temporary file is named destination + ".tmp" and is done this way to ensure
// that the temporary file is in the same file system as the destination because the
// File.renameTo operation doesn't really work across file systems.
// So we will attempt
// to do a File.renameTo for efficiency and atomicity, if this fails
// then we will use
// a brute force copy and delete the temporary file.
// So we will attempt to do a File.renameTo for efficiency and atomicity, if this fails
// then we will use a brute force copy and delete the temporary file.
if ( !temp.renameTo( destination ) )
{