import org.apache.archiva.policies.ProxyDownloadException;
import org.apache.archiva.repository.ManagedRepository;
import org.apache.archiva.repository.RepositoryType;
+import org.apache.archiva.repository.content.Artifact;
import org.apache.archiva.repository.storage.StorageAsset;
import java.util.List;
* If the artifact is found, it is downloaded and placed into the source repository
* filesystem.
*
+ * @deprecated Replaced by {@link #fetchFromProxies(ManagedRepository, Artifact)}
* @param repository the source repository to use. (must be a managed repository)
* @param artifact the artifact to fetch.
* @return the file that was obtained, or null if no content was obtained
StorageAsset fetchFromProxies( ManagedRepository repository, ArtifactReference artifact )
throws ProxyDownloadException;
+ /**
+ * Performs the artifact fetch operation against the target repositories
+ * of the provided source repository.
+ * <p>
+ * If the artifact is found, it is downloaded and placed into the source repository
+ * filesystem.
+ *
+ * @param repository the source repository to use. (must be a managed repository)
+ * @param artifact the artifact to fetch.
+ * @return the file that was obtained, or null if no content was obtained
+ * @throws ProxyDownloadException if there was a problem fetching the content from the target repositories.
+ */
+ StorageAsset fetchFromProxies( ManagedRepository repository, Artifact artifact )
+ throws ProxyDownloadException;
+
/**
* Performs the metadata fetch operation against the target repositories
* of the provided source repository.
return proxyConnectorRuleConfigurations;
}
+ @Override
+ public StorageAsset fetchFromProxies( ManagedRepository repository, Artifact artifact )
+ throws ProxyDownloadException
+ {
+ Map<String, Exception> previousExceptions = new LinkedHashMap<>();
+ StorageAsset localFile = artifact.getAsset( );
+
+ Properties requestProperties = new Properties();
+ requestProperties.setProperty( "filetype", "artifact" );
+ requestProperties.setProperty( "version", artifact.getVersion().getId() );
+ requestProperties.setProperty( "managedRepositoryId", repository.getId() );
+
+ List<ProxyConnector> connectors = getProxyConnectors( repository );
+ for ( ProxyConnector connector : connectors )
+ {
+ if ( !connector.isEnabled() )
+ {
+ continue;
+ }
+ RemoteRepository targetRepository = connector.getTargetRepository();
+ requestProperties.setProperty( "remoteRepositoryId", targetRepository.getId() );
+
+ StorageAsset targetFile = targetRepository.getAsset( localFile.getPath( ) );
+ // Removing the leading '/' from the path
+ String targetPath = targetFile.getPath( ).substring( 1 );
+ try
+ {
+ StorageAsset downloadedFile =
+ transferFile( connector, targetRepository, targetPath, repository, localFile, requestProperties,
+ true );
+
+ if ( fileExists(downloadedFile) )
+ {
+ log.debug( "Successfully transferred: {}", downloadedFile.getPath() );
+ return downloadedFile;
+ }
+ }
+ catch ( NotFoundException e )
+ {
+ log.debug( "Artifact {} not found on repository \"{}\".", artifact.getId(),
+ targetRepository.getId() );
+ }
+ catch ( NotModifiedException e )
+ {
+ log.debug( "Artifact {} not updated on repository \"{}\".", artifact.getId(),
+ targetRepository.getId() );
+ }
+ catch ( ProxyException e )
+ {
+ validatePolicies( this.downloadErrorPolicies, connector.getPolicies(), requestProperties, artifact,
+ targetRepository.getContent(), localFile, e, previousExceptions );
+ }
+ }
+
+ if ( !previousExceptions.isEmpty() )
+ {
+ throw new ProxyDownloadException( "Failures occurred downloading from some remote repositories",
+ previousExceptions );
+ }
+
+ log.debug( "Exhausted all target repositories, artifact {} not found.", artifact.getId() );
+
+ return null;
+ }
@Override
public StorageAsset fetchFromProxies( ManagedRepository repository, ArtifactReference artifact )
}
}
+ private void validatePolicies( Map<String, DownloadErrorPolicy> policies, Map<Policy, PolicyOption> settings,
+ Properties request, Artifact artifact, RemoteRepositoryContent content,
+ StorageAsset localFile, Exception exception, Map<String, Exception> previousExceptions )
+ throws ProxyDownloadException
+ {
+ boolean process = true;
+ for ( Map.Entry<String, ? extends DownloadErrorPolicy> entry : policies.entrySet() )
+ {
+
+ // olamy with spring rolehint is now downloadPolicy#hint
+ // so substring after last # to get the hint as with plexus
+ String key = entry.getValue( ).getId( );
+ DownloadErrorPolicy policy = entry.getValue();
+ PolicyOption option = settings.containsKey( policy ) ? settings.get(policy) : policy.getDefaultOption();
+
+ log.debug( "Applying [{}] policy with [{}]", key, option );
+ try
+ {
+ // all policies must approve the exception, any can cancel
+ process = policy.applyPolicy( option, request, localFile, exception, previousExceptions );
+ if ( !process )
+ {
+ break;
+ }
+ }
+ catch ( PolicyConfigurationException e )
+ {
+ log.error( e.getMessage(), e );
+ }
+ }
+
+ if ( process )
+ {
+ // if the exception was queued, don't throw it
+ if ( !previousExceptions.containsKey( content.getId() ) )
+ {
+ throw new ProxyDownloadException(
+ "An error occurred in downloading from the remote repository, and the policy is to fail immediately",
+ content.getId(), exception );
+ }
+ }
+ else
+ {
+ // if the exception was queued, but cancelled, remove it
+ previousExceptions.remove( content.getId() );
+ }
+
+ log.warn(
+ "Transfer error from repository {} for artifact {} , continuing to next repository. Error message: {}",
+ content.getRepository().getId(), artifact.getId(), exception.getMessage() );
+ log.debug( "Full stack trace", exception );
+ }
+
private void validatePolicies( Map<String, DownloadErrorPolicy> policies, Map<Policy, PolicyOption> settings,
Properties request, ArtifactReference artifact, RemoteRepositoryContent content,
StorageAsset localFile, Exception exception, Map<String, Exception> previousExceptions )
}
}
- /**
- * Get the first Artifact found in the provided VersionedReference location.
- *
- * @param managedRepository the repository to search within.
- * @param reference the reference to the versioned reference to search within
- * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
- * no artifact was found within the versioned reference.
- * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
- * @throws LayoutException
- */
- public ArtifactReference getFirstArtifact( BaseRepositoryContentLayout managedRepository,
- VersionedReference reference )
- throws LayoutException, IOException
- {
- String path = toPath( reference );
-
- int idx = path.lastIndexOf( '/' );
- if ( idx > 0 )
- {
- path = path.substring( 0, idx );
- }
-
- StorageAsset repoDir = managedRepository.getGenericContent( ).getRepository( ).getRoot();
-
- if ( !repoDir.exists())
- {
- throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
- + repoDir.toString() );
- }
-
- if ( !repoDir.isContainer())
- {
- throw new IOException(
- "Unable to gather the list of snapshot versions on a non-directory: " + repoDir.toString() );
- }
-
- Path repoRoot = repoDir.getFilePath( );
- try(Stream<Path> stream = Files.list(repoRoot)) {
- String result = stream.filter( Files::isRegularFile ).map( path1 ->
- repoRoot.relativize( path1 ).toString()
- ).filter( filetypes::matchesArtifactPattern ).findFirst().orElse( null );
- if (result!=null) {
- return managedRepository.getGenericContent().toArtifactReference( result );
- }
- }
- // No artifact was found.
- return null;
- }
-
public ArchivaConfiguration getConfiguration()
{
return configuration;
import org.apache.archiva.policies.SnapshotsPolicy;
import org.apache.archiva.policies.urlcache.UrlFailureCache;
import org.apache.archiva.repository.BaseRepositoryContentLayout;
+import org.apache.archiva.repository.content.Artifact;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.maven.wagon.ResourceDoesNotExistException;
import org.easymock.EasyMock;
assertNotExistsInManagedDefaultRepo( expectedFile );
BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
- ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
+ Artifact artifact = layout.getArtifact( path );
+ // ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
// Configure Repository (usually done within archiva.xml configuration)
saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "http://bad.machine.com/repo/", "default" );
assertNotExistsInManagedDefaultRepo( expectedFile );
BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout( BaseRepositoryContentLayout.class );
- ArtifactReference artifact = managedDefaultRepository.toArtifactReference( path );
+ Artifact artifact = layout.getArtifact( path );
// Configure Repository (usually done within archiva.xml configuration)
saveRemoteRepositoryConfig( "badproxied1", "Bad Proxied 1", "http://bad.machine.com/repo/", "default" );