MRM-859 - Use File.createTempFile() when downloading files from a remote repository

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@673685 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James William Dumay 2008-07-03 14:16:56 +00:00
parent dee53b0c55
commit d07d790c54
7 changed files with 89 additions and 13 deletions

View File

@ -691,7 +691,8 @@ public class DefaultRepositoryProxyConnectors
try try
{ {
temp = new File( localFile.getAbsolutePath() + ".tmp" ); localFile.getParentFile().mkdirs();
temp = File.createTempFile(localFile.getName() + ".", null, localFile.getParentFile());
boolean success = false; boolean success = false;
@ -729,6 +730,10 @@ public class DefaultRepositoryProxyConnectors
return localFile; return localFile;
} }
catch (IOException e)
{
throw new ProxyException("Could not create temporary file at " + localFile.getAbsolutePath(), e);
}
catch ( ResourceDoesNotExistException e ) catch ( ResourceDoesNotExistException e )
{ {
throw new NotFoundException( throw new NotFoundException(
@ -743,10 +748,7 @@ public class DefaultRepositoryProxyConnectors
} }
finally finally
{ {
if ( temp != null ) FileUtils.deleteQuietly(temp);
{
temp.delete();
}
} }
} }
@ -857,11 +859,18 @@ public class DefaultRepositoryProxyConnectors
} }
catch ( IOException e ) catch ( IOException e )
{ {
throw new ProxyException( "Cannot copy tmp file to its final location", e ); if (target.exists())
{
log.debug("Tried to copy file " + temp.getName() + " to " + target.getAbsolutePath() + " but file with this name already exists.");
}
else
{
throw new ProxyException( "Cannot copy tmp file " + temp.getAbsolutePath() + " to its final location", e );
}
} }
finally finally
{ {
temp.delete(); FileUtils.deleteQuietly(temp);
} }
} }
} }

View File

@ -31,6 +31,7 @@ import java.util.Date;
import java.util.Locale; import java.util.Locale;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration; import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
@ -43,10 +44,9 @@ import org.apache.maven.archiva.policies.ReleasesPolicy;
import org.apache.maven.archiva.policies.SnapshotsPolicy; import org.apache.maven.archiva.policies.SnapshotsPolicy;
import org.apache.maven.archiva.repository.ManagedRepositoryContent; import org.apache.maven.archiva.repository.ManagedRepositoryContent;
import org.apache.maven.wagon.Wagon; import org.apache.maven.wagon.Wagon;
import org.codehaus.plexus.spring.PlexusClassPathXmlApplicationContext;
import org.codehaus.plexus.spring.PlexusInSpringTestCase; import org.codehaus.plexus.spring.PlexusInSpringTestCase;
import org.easymock.ArgumentsMatcher;
import org.easymock.MockControl; import org.easymock.MockControl;
import org.springframework.beans.factory.BeanFactory;
/** /**
* AbstractProxyTestCase * AbstractProxyTestCase
@ -89,6 +89,53 @@ public abstract class AbstractProxyTestCase
protected static final String REPOPATH_LEGACY_MANAGED_TARGET = "target/test-repository/legacy-managed"; protected static final String REPOPATH_LEGACY_MANAGED_TARGET = "target/test-repository/legacy-managed";
protected static final ArgumentsMatcher customWagonGetIfNewerMatcher = new ArgumentsMatcher() {
public boolean matches(Object[] expected, Object[] actual) {
if (expected.length < 1 || actual.length < 1)
{
return false;
}
return MockControl.ARRAY_MATCHER.matches(ArrayUtils.remove(expected, 1), ArrayUtils.remove(actual, 1));
}
public String toString(Object[] arguments) {
return ArrayUtils.toString(arguments);
}
};
protected static final ArgumentsMatcher customWagonGetMatcher = new ArgumentsMatcher() {
public boolean matches(Object[] expected, Object[] actual)
{
if (expected.length == 2 && actual.length == 2)
{
if (expected[0] == null && actual[0] == null)
{
return true;
}
if (expected[0] == null)
{
return actual[0] == null;
}
if (actual[0] == null)
{
return expected[0] == null;
}
return expected[0].equals(actual[0]);
}
return false;
}
public String toString(Object[] arguments)
{
return ArrayUtils.toString(arguments);
}
};
protected MockControl wagonMockControl; protected MockControl wagonMockControl;
protected Wagon wagonMock; protected Wagon wagonMock;

View File

@ -65,6 +65,9 @@ public class CacheFailuresTransferTest
SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES ); SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES );
wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) ); wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
wagonMockControl.setMatcher(customWagonGetMatcher);
wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 ); wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
wagonMockControl.replay(); wagonMockControl.replay();
@ -105,6 +108,8 @@ public class CacheFailuresTransferTest
SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO ); SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) ); wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
wagonMockControl.setMatcher(customWagonGetMatcher);
wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 ); wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
wagonMockControl.replay(); wagonMockControl.replay();
@ -116,10 +121,12 @@ public class CacheFailuresTransferTest
// Second attempt to download same artifact DOES NOT use cache // Second attempt to download same artifact DOES NOT use cache
wagonMockControl.reset(); wagonMockControl.reset();
wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) ); wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
wagonMockControl.setMatcher(customWagonGetMatcher);
wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 ); wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
wagonMockControl.replay(); wagonMockControl.replay();
downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact ); downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
wagonMockControl.verify(); wagonMockControl.verify();

View File

@ -397,10 +397,13 @@ public class ChecksumTransferTest
SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO ); SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
wagonMock.get( path, new File( expectedFile.getAbsolutePath() + ".tmp" ) ); wagonMock.get( path, new File( expectedFile.getAbsolutePath() + ".tmp" ) );
wagonMockControl.setMatcher(customWagonGetMatcher);
wagonMockControl.setVoidCallable(); wagonMockControl.setVoidCallable();
wagonMock.get( path + ".sha1", new File( expectedFile.getAbsolutePath() + ".sha1.tmp" ) ); wagonMock.get( path + ".sha1", new File( expectedFile.getAbsolutePath() + ".sha1.tmp" ) );
wagonMockControl.setMatcher(customWagonGetMatcher);
wagonMockControl.setVoidCallable(); wagonMockControl.setVoidCallable();
wagonMock.get( path + ".md5", new File( expectedFile.getAbsolutePath() + ".md5.tmp" ) ); wagonMock.get( path + ".md5", new File( expectedFile.getAbsolutePath() + ".md5.tmp" ) );
wagonMockControl.setMatcher(customWagonGetMatcher);
wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Resource does not exist." ) ); wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Resource does not exist." ) );
wagonMockControl.replay(); wagonMockControl.replay();

View File

@ -537,6 +537,7 @@ public class ErrorHandlingTest
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
{ {
wagonMock.get( path, createExpectedTempFile( expectedFile ) ); wagonMock.get( path, createExpectedTempFile( expectedFile ) );
wagonMockControl.setMatcher(customWagonGetMatcher);
wagonMockControl.setThrowable( throwable, 1 ); wagonMockControl.setThrowable( throwable, 1 );
} }
@ -544,6 +545,7 @@ public class ErrorHandlingTest
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
{ {
wagonMock.getIfNewer( path, createExpectedTempFile( expectedFile ), expectedFile.lastModified() ); wagonMock.getIfNewer( path, createExpectedTempFile( expectedFile ), expectedFile.lastModified() );
wagonMockControl.setMatcher(customWagonGetIfNewerMatcher);
wagonMockControl.setThrowable( exception, 1 ); wagonMockControl.setThrowable( exception, 1 );
} }

View File

@ -306,6 +306,7 @@ public class ManagedDefaultTransferTest
saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "test://bad.machine.com/repo/", "default" ); saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "test://bad.machine.com/repo/", "default" );
wagonMock.get( path, new File( expectedFile.getAbsolutePath() + ".tmp" ) ); wagonMock.get( path, new File( expectedFile.getAbsolutePath() + ".tmp" ) );
wagonMockControl.setMatcher(customWagonGetMatcher);
wagonMockControl.setThrowable( new ResourceDoesNotExistException( "transfer failed" ) ); wagonMockControl.setThrowable( new ResourceDoesNotExistException( "transfer failed" ) );
wagonMockControl.replay(); wagonMockControl.replay();
@ -344,9 +345,13 @@ public class ManagedDefaultTransferTest
File tmpFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ); File tmpFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" );
wagonMock.get( path, tmpFile ); wagonMock.get( path, tmpFile );
wagonMockControl.setMatcher(customWagonGetMatcher);
wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) ); wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
wagonMock.get( path, tmpFile ); wagonMock.get( path, tmpFile );
wagonMockControl.setMatcher(customWagonGetMatcher);
wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) ); wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
wagonMockControl.replay(); wagonMockControl.replay();

View File

@ -120,6 +120,9 @@ public class MetadataTransferTest
File expectedFile = new File( managedDefaultDir.getAbsoluteFile(), File expectedFile = new File( managedDefaultDir.getAbsoluteFile(),
metadataTools.getRepositorySpecificName( "badproxied1", requestedResource ) ); metadataTools.getRepositorySpecificName( "badproxied1", requestedResource ) );
wagonMock.get( requestedResource, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) ); wagonMock.get( requestedResource, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
wagonMockControl.setMatcher(customWagonGetMatcher);
wagonMockControl.setThrowable( new TransferFailedException( "can't connect" ) ); wagonMockControl.setThrowable( new TransferFailedException( "can't connect" ) );
wagonMockControl.replay(); wagonMockControl.replay();