{
failIfNotOnline();
String protocol = repository.getProtocol();
Wagon wagon;
try
{
wagon = getWagon( protocol );
configureWagon( wagon, repository );
}
catch ( UnsupportedProtocolException e )
{
throw new TransferFailedException( "Unsupported Protocol: '" + protocol + "': " + e.getMessage(), e );
}
if ( downloadMonitor != null )
{
wagon.addTransferListener( downloadMonitor );
}
Map checksums = new HashMap( 2 );
Map sums = new HashMap( 2 );
// TODO: configure these on the repository
try
{
ChecksumObserver checksumObserver = new ChecksumObserver( "MD5" );
wagon.addTransferListener( checksumObserver );
checksums.put( "md5", checksumObserver );
checksumObserver = new ChecksumObserver( "SHA-1" );
wagon.addTransferListener( checksumObserver );
checksums.put( "sha1", checksumObserver );
}
catch ( NoSuchAlgorithmException e )
{
throw new TransferFailedException( "Unable to add checksum methods: " + e.getMessage(), e );
}
try
{
Repository artifactRepository = new Repository( repository.getId(), repository.getUrl() );
if ( serverPermissionsMap.containsKey( repository.getId() ) )
{
RepositoryPermissions perms = (RepositoryPermissions) serverPermissionsMap.get( repository.getId() );
getLogger().debug(
"adding permissions to wagon connection: " + perms.getFileMode() + " " + perms.getDirectoryMode() );
artifactRepository.setPermissions( perms );
}
else
{
if ( defaultRepositoryPermissions != null )
{
artifactRepository.setPermissions( defaultRepositoryPermissions );
}
else
{
getLogger().debug( "not adding permissions to wagon connection" );
}
}
wagon.connect( artifactRepository, getAuthenticationInfo( repository.getId() ), new ProxyInfoProvider()
{
public ProxyInfo getProxyInfo( String protocol )
{
return getProxy( protocol );
}
});
wagon.put( source, remotePath );
wagon.removeTransferListener( downloadMonitor );
// Pre-store the checksums as any future puts will overwrite them
for ( Iterator i = checksums.keySet().iterator(); i.hasNext(); )
{
String extension = (String) i.next();
ChecksumObserver observer = (ChecksumObserver) checksums.get( extension );
sums.put( extension, observer.getActualChecksum() );
}
// We do this in here so we can checksum the artifact metadata too, otherwise it could be metadata itself
for ( Iterator i = checksums.keySet().iterator(); i.hasNext(); )
{
String extension = (String) i.next();
// TODO: shouldn't need a file intermediatary - improve wagon to take a stream
File temp = File.createTempFile( "maven-artifact", null );
temp.deleteOnExit();
FileUtils.fileWrite( temp.getAbsolutePath(), "UTF-8", (String) sums.get( extension ) );
wagon.put( temp, remotePath + "." + extension );
}
}
catch ( ConnectionException e )
{
throw new TransferFailedException( "Connection failed: " + e.getMessage(), e );