import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.repository.ArtifactUtils;
-import org.apache.maven.repository.digest.DefaultDigester;
-import org.apache.maven.repository.digest.Digester;
import org.apache.maven.repository.proxy.configuration.ProxyConfiguration;
import org.apache.maven.repository.proxy.repository.ProxyRepository;
import org.apache.maven.wagon.ConnectionException;
import org.apache.maven.wagon.authorization.AuthorizationException;
import org.apache.maven.wagon.observers.ChecksumObserver;
import org.codehaus.plexus.logging.AbstractLogEnabled;
+import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.IOUtil;
import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Iterator;
ArtifactRepository repoCache = config.getRepositoryCache();
File artifactFile = new File( repoCache.getBasedir(), repoCache.pathOf( artifact ) );
+ artifact.setFile( artifactFile );
if ( !artifactFile.exists() )
{
if ( connected )
{
File temp = new File( target.getAbsolutePath() + ".tmp" );
- temp.deleteOnExit();
int tries = 0;
boolean success = false;
{
tries++;
+ getLogger().info( "trying " + path + " from " + repository.getId() );
+
wagon.get( path, temp );
if ( useChecksum )
{
- releaseChecksums( wagon, checksums );
success = doChecksumCheck( checksums, path, wagon );
}
else
}
disconnectWagon( wagon );
+ if ( !temp.renameTo( target ) )
+ {
+ getLogger().warn( "Unable to rename tmp file to its final name... resorting to copy command." );
+
+ try
+ {
+ FileUtils.copyFile( temp, target );
+ }
+ catch ( IOException e )
+ {
+ throw new ProxyException( "Cannot copy tmp file to its final location", e );
+ }
+ finally
+ {
+ temp.delete();
+ }
+ }
+
return target;
}
//try next repository
}
catch ( UnsupportedProtocolException e )
{
- getLogger().info( "Skipping repository " + repository.getUrl() + ": no wagonManager configured for protocol " +
- repository.getProtocol() );
+ getLogger().info( "Skipping repository " + repository.getUrl() + ": no wagonManager configured " +
+ "for protocol " + repository.getProtocol() );
}
finally
{
*/
private boolean doChecksumCheck( Map checksumMap, String path, Wagon wagon )
{
+ releaseChecksums( wagon, checksumMap );
for ( Iterator checksums = checksumMap.keySet().iterator(); checksums.hasNext(); )
{
String checksumExt = (String) checksums.next();
ChecksumObserver checksum = (ChecksumObserver) checksumMap.get( checksumExt );
- String remotePath = path + "." + checksumExt;
- File checksumFile = new File( config.getRepositoryCache().getBasedir(), remotePath );
+ String checksumPath = path + "." + checksumExt;
+ File checksumFile = new File( config.getRepositoryCache().getBasedir(), checksumPath );
try
{
File tempChecksumFile = new File( checksumFile.getAbsolutePath() + "." + checksumExt );
- wagon.get( remotePath + "." + checksumExt, tempChecksumFile );
+ wagon.get( checksumPath, tempChecksumFile );
- String algorithm;
- if ( "md5".equals( checksumExt ) )
- {
- algorithm = "MD5";
- }
- else
- {
- algorithm = "SHA-1";
- }
-
- Digester digester = new DefaultDigester();
- try
- {
- return digester.verifyChecksum( tempChecksumFile, checksum.getActualChecksum(), algorithm );
- }
- catch ( NoSuchAlgorithmException e )
- {
- getLogger().info( "Failed to initialize checksum: " + algorithm + "\n " + e.getMessage() );
- return false;
- }
- catch ( IOException e )
+ String remoteChecksum = readTextFile( tempChecksumFile ).trim();
+ if ( remoteChecksum.indexOf( ' ' ) > 0 )
{
- getLogger().info( "Failed to verify checksum: " + algorithm + "\n " + e.getMessage() );
- return false;
+ remoteChecksum = remoteChecksum.substring( 0, remoteChecksum.indexOf( ' ' ) );
}
-
+ return remoteChecksum.toUpperCase().equals( checksum.getActualChecksum().toUpperCase() );
}
catch ( ChecksumFailedException e )
{
}
catch ( TransferFailedException e )
{
- getLogger().warn( "An error occurred during the download of " + remotePath + ": " + e.getMessage() );
+ getLogger().warn( "An error occurred during the download of " + checksumPath + ": " + e.getMessage() );
// do nothing try the next checksum
}
catch ( ResourceDoesNotExistException e )
{
- getLogger().warn( "An error occurred during the download of " + remotePath + ": " + e.getMessage() );
+ getLogger().warn( "An error occurred during the download of " + checksumPath + ": " + e.getMessage() );
// do nothing try the next checksum
}
catch ( AuthorizationException e )
{
- getLogger().warn( "An error occurred during the download of " + remotePath + ": " + e.getMessage() );
+ getLogger().warn( "An error occurred during the download of " + checksumPath + ": " + e.getMessage() );
// do nothing try the next checksum
}
+ catch ( IOException e )
+ {
+ getLogger().info( "An error occurred while reading the temporary checksum file." );
+ return false;
+ }
}
getLogger().info( "Skipping checksum validation for " + path + ": No remote checksums available." );
}
}
+ private String readTextFile( File file )
+ throws IOException
+ {
+ String text = "";
+
+ InputStream fis = new FileInputStream( file );
+ try
+ {
+ byte[] buffer = new byte[ 64 ];
+ int numRead;
+ do
+ {
+ numRead = fis.read( buffer );
+ if ( numRead > 0 )
+ {
+ text += new String( buffer );
+ }
+ }
+ while ( numRead != -1 );
+ }
+ finally
+ {
+ IOUtil.close( fis );
+ }
+
+ return text;
+ }
+
/**
* Used to disconnect the wagonManager from its repository
*
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.apache.maven.repository.proxy.configuration.ProxyConfiguration;
+import org.apache.maven.repository.proxy.repository.ProxyRepository;
import org.apache.maven.wagon.ResourceDoesNotExistException;
+import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
+
+import java.io.File;
/*
* Copyright 2005-2006 The Apache Software Foundation.
{
fail( "Expected Exception not thrown." );
}
-
- try
- {
- proxy.getRemoteFile( "/invalid" );
- fail( "Expected empty configuration error." );
- }
- catch ( ProxyException e )
- {
- assertEquals( "Expected Exception not thrown.", "No proxy configuration defined.", e.getMessage() );
- }
- catch ( ResourceDoesNotExistException e )
- {
- fail( "Expected Exception not thrown." );
- }
}
public void testCache()
+ throws Exception
{
-
+ File file = proxy.get( "/commons-logging/commons-logging/1.0/commons-logging-1.0.jar" );
+ assertTrue( "File must be downloaded.", file.exists() );
+ assertTrue( "Downloaded file should be present in the cache.",
+ file.getAbsolutePath().startsWith( proxy.getConfiguration().getRepositoryCachePath() ) );
+
+ file = proxy.get( "/commons-logging/commons-logging/1.0/commons-logging-1.0.jar" );
+
+ file = proxy.get( "/not-standard/repository/file.txt" );
+ assertTrue( "File must be downloaded.", file.exists() );
+ assertTrue( "Downloaded file should be present in the cache.",
+ file.getAbsolutePath().startsWith( proxy.getConfiguration().getRepositoryCachePath() ) );
+
+ file = proxy.get( "/checksumed-md5/repository/file.txt" );
+ assertTrue( "File must be downloaded.", file.exists() );
+ assertTrue( "Downloaded file should be present in the cache.",
+ file.getAbsolutePath().startsWith( proxy.getConfiguration().getRepositoryCachePath() ) );
}
protected void tearDown()
config.setRepositoryCachePath( "target/proxy-cache" );
+ ArtifactRepositoryLayout defLayout = new DefaultRepositoryLayout();
+
+ File repo1File = new File( "src/test/remote-repo1" );
+
+ ProxyRepository repo1 = new ProxyRepository( "test-repo", "file://" + repo1File.getAbsolutePath(), defLayout );
+
+ config.addRepository( repo1 );
+
return config;
}
}