Code cleanup

This commit is contained in:
Martin Stockhammer 2020-06-17 20:41:34 +02:00
parent 735a03f2b0
commit 720ebf3874
4 changed files with 31 additions and 50 deletions

View File

@ -23,8 +23,6 @@
import org.apache.archiva.consumers.AbstractMonitoredConsumer;
import org.apache.archiva.consumers.ConsumerException;
import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
import org.apache.archiva.model.ProjectReference;
import org.apache.archiva.model.VersionedReference;
import org.apache.archiva.repository.BaseRepositoryContentLayout;
import org.apache.archiva.repository.LayoutException;
import org.apache.archiva.repository.ManagedRepository;
@ -33,7 +31,6 @@
import org.apache.archiva.repository.RepositoryNotFoundException;
import org.apache.archiva.repository.RepositoryRegistry;
import org.apache.archiva.repository.content.Artifact;
import org.apache.archiva.repository.content.ItemSelector;
import org.apache.archiva.repository.content.Project;
import org.apache.archiva.repository.metadata.RepositoryMetadataException;
import org.apache.archiva.repository.metadata.base.MetadataTools;

View File

@ -25,8 +25,6 @@
import org.apache.archiva.metadata.repository.MetadataRepository;
import org.apache.archiva.metadata.repository.MetadataRepositoryException;
import org.apache.archiva.metadata.repository.RepositorySession;
import org.apache.archiva.model.ProjectReference;
import org.apache.archiva.model.VersionedReference;
import org.apache.archiva.repository.BaseRepositoryContentLayout;
import org.apache.archiva.repository.ContentNotFoundException;
import org.apache.archiva.repository.LayoutException;
@ -142,10 +140,6 @@ public void process( String path )
// Now clean out any version that is earlier than the highest released version.
boolean needsMetadataUpdate = false;
VersionedReference versionRef = new VersionedReference( );
versionRef.setGroupId( artifactRef.getNamespace( ).getId() );
versionRef.setArtifactId( artifactRef.getId( ) );
ArchivaItemSelector.Builder versionSelectorBuilder = ArchivaItemSelector.builder( )
.withNamespace( artifactRef.getNamespace().getId() )
.withProjectId( artifactRef.getId( ) )

View File

@ -148,34 +148,31 @@ protected void transferResources( ProxyConnector connector, RemoteRepository rem
throw new ProxyException("Unsupported target repository protocol: " + protocol);
}
if (wagon == null) {
throw new ProxyException("Unsupported target repository protocol: " + protocol);
}
boolean connected = connectToRepository(connector, wagon, remoteRepository);
if (connected) {
transferArtifact(wagon, remoteRepository, remotePath, repository, resource.getFilePath(), workingDirectory,
tmpResource);
transferArtifact(wagon, remoteRepository, remotePath, resource.getFilePath(),
tmpResource);
// TODO: these should be used to validate the download based on the policies, not always downloaded
// to
// save on connections since md5 is rarely used
for (int i=0; i<checksumFiles.length; i++) {
String ext = "."+StringUtils.substringAfterLast(checksumFiles[i].getName( ), "." );
transferChecksum(wagon, remoteRepository, remotePath, repository, resource.getFilePath(), ext,
checksumFiles[i].getFilePath());
for ( StorageAsset checksumFile : checksumFiles )
{
String ext = "." + StringUtils.substringAfterLast( checksumFile.getName( ), "." );
transferChecksum( wagon, remoteRepository, remotePath, resource.getFilePath( ), ext,
checksumFile.getFilePath( ) );
}
}
} catch (NotFoundException e) {
urlFailureCache.cacheFailure(url);
throw e;
} catch (NotModifiedException e) {
}
catch (NotModifiedException e) {
// Do not cache url here.
throw e;
} catch (ProxyException e) {
}
catch ( ProxyException e) {
urlFailureCache.cacheFailure(url);
throw e;
} catch (WagonFactoryException e) {
}
catch (WagonFactoryException e) {
throw new ProxyException(e.getMessage(), e);
} finally {
if (wagon != null) {
@ -188,11 +185,11 @@ protected void transferResources( ProxyConnector connector, RemoteRepository rem
}
}
protected void transferArtifact(Wagon wagon, RemoteRepository remoteRepository, String remotePath,
ManagedRepository repository, Path resource, Path tmpDirectory,
StorageAsset destFile)
protected void transferArtifact( Wagon wagon, RemoteRepository remoteRepository, String remotePath,
Path resource,
StorageAsset destFile )
throws ProxyException {
transferSimpleFile(wagon, remoteRepository, remotePath, repository, resource, destFile.getFilePath());
transferSimpleFile(wagon, remoteRepository, remotePath, resource, destFile.getFilePath());
}
/**
@ -203,13 +200,12 @@ protected void transferArtifact(Wagon wagon, RemoteRepository remoteRepository,
* @param wagon the wagon instance (should already be connected) to use.
* @param remoteRepository the remote repository to transfer from.
* @param remotePath the remote path to the resource to get.
* @param repository the managed repository that will hold the file
* @param resource the local file that should contain the downloaded contents
* @param ext the type of checksum to transfer (example: ".md5" or ".sha1")
* @throws ProxyException if copying the downloaded file into place did not succeed.
*/
protected void transferChecksum( Wagon wagon, RemoteRepository remoteRepository, String remotePath,
ManagedRepository repository, Path resource, String ext,
Path resource, String ext,
Path destFile )
throws ProxyException {
String url = remoteRepository.getLocation().toString() + remotePath + ext;
@ -220,7 +216,7 @@ protected void transferChecksum( Wagon wagon, RemoteRepository remoteRepository,
}
try {
transferSimpleFile(wagon, remoteRepository, remotePath + ext, repository, resource, destFile);
transferSimpleFile(wagon, remoteRepository, remotePath + ext, resource, destFile);
log.debug("Checksum {} Downloaded: {} to move to {}", url, destFile, resource);
} catch (NotFoundException e) {
urlFailureCache.cacheFailure(url);
@ -243,27 +239,26 @@ protected void transferChecksum( Wagon wagon, RemoteRepository remoteRepository,
* @param wagon the wagon instance to use.
* @param remoteRepository the remote repository to use
* @param remotePath the remote path to attempt to get
* @param repository the managed repository that will hold the file
* @param origFile the local file to save to
* @throws ProxyException if there was a problem moving the downloaded file into place.
*/
protected void transferSimpleFile(Wagon wagon, RemoteRepository remoteRepository, String remotePath,
ManagedRepository repository, Path origFile, Path destFile)
protected void transferSimpleFile( Wagon wagon, RemoteRepository remoteRepository, String remotePath,
Path origFile, Path destFile )
throws ProxyException {
assert (remotePath != null);
// Transfer the file.
try {
boolean success = false;
if (!Files.exists(origFile)) {
log.debug("Retrieving {} from {}", remotePath, remoteRepository.getId());
wagon.get(addParameters(remotePath, remoteRepository), destFile.toFile());
success = true;
// You wouldn't get here on failure, a WagonException would have been thrown.
log.debug("Downloaded successfully.");
} else {
boolean success;
log.debug("Retrieving {} from {} if updated", remotePath, remoteRepository.getId());
try {
success = wagon.getIfNewer(addParameters(remotePath, remoteRepository), destFile.toFile(),
@ -306,8 +301,6 @@ protected void transferSimpleFile(Wagon wagon, RemoteRepository remoteRepository
*/
protected boolean connectToRepository(ProxyConnector connector, Wagon wagon,
RemoteRepository remoteRepository) {
boolean connected = false;
final ProxyInfo networkProxy =
connector.getProxyId() == null ? null : this.networkProxyMap.get(connector.getProxyId());
@ -356,13 +349,12 @@ protected boolean connectToRepository(ProxyConnector connector, Wagon wagon,
Repository wagonRepository =
new Repository(remoteRepository.getId(), remoteRepository.getLocation().toString());
wagon.connect(wagonRepository, authInfo, networkProxy);
connected = true;
return true;
} catch (ConnectionException | AuthenticationException e) {
log.warn("Could not connect to {}: {}", remoteRepository.getId(), e.getMessage());
connected = false;
return false;
}
return connected;
}

View File

@ -20,23 +20,21 @@
import org.apache.archiva.common.utils.VersionComparator;
import org.apache.archiva.configuration.ProxyConnectorConfiguration;
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.content.ItemSelector;
import org.apache.archiva.repository.content.base.ArchivaItemSelector;
import org.apache.archiva.repository.maven.metadata.storage.mock.MockConfiguration;
import org.apache.archiva.model.ProjectReference;
import org.apache.archiva.model.VersionedReference;
import org.apache.archiva.policies.CachedFailuresPolicy;
import org.apache.archiva.policies.ChecksumPolicy;
import org.apache.archiva.policies.ReleasesPolicy;
import org.apache.archiva.policies.SnapshotsPolicy;
import org.apache.archiva.repository.maven.AbstractRepositoryLayerTestCase;
import org.apache.archiva.repository.LayoutException;
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.RemoteRepositoryContent;
import org.apache.archiva.repository.RepositoryContentProvider;
import org.apache.archiva.repository.content.ItemSelector;
import org.apache.archiva.repository.content.base.ArchivaItemSelector;
import org.apache.archiva.repository.maven.AbstractRepositoryLayerTestCase;
import org.apache.archiva.repository.maven.MavenManagedRepository;
import org.apache.archiva.repository.maven.metadata.storage.mock.MockConfiguration;
import org.apache.archiva.repository.metadata.RepositoryMetadataException;
import org.apache.archiva.repository.metadata.base.MetadataTools;
import org.apache.archiva.repository.maven.MavenManagedRepository;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;