Search in sources :

Example 1 with ProxyDownloadException

use of org.apache.archiva.policies.ProxyDownloadException in project archiva by apache.

the class DefaultRepositoryProxyConnectors method fetchFromProxies.

@Override
public Path fetchFromProxies(ManagedRepositoryContent repository, ArtifactReference artifact) throws ProxyDownloadException {
    Path localFile = toLocalFile(repository, artifact);
    Properties requestProperties = new Properties();
    requestProperties.setProperty("filetype", "artifact");
    requestProperties.setProperty("version", artifact.getVersion());
    requestProperties.setProperty("managedRepositoryId", repository.getId());
    List<ProxyConnector> connectors = getProxyConnectors(repository);
    Map<String, Exception> previousExceptions = new LinkedHashMap<>();
    for (ProxyConnector connector : connectors) {
        if (connector.isDisabled()) {
            continue;
        }
        RemoteRepositoryContent targetRepository = connector.getTargetRepository();
        requestProperties.setProperty("remoteRepositoryId", targetRepository.getId());
        String targetPath = targetRepository.toPath(artifact);
        if (SystemUtils.IS_OS_WINDOWS) {
            // toPath use system PATH_SEPARATOR so on windows url are \ which doesn't work very well :-)
            targetPath = FilenameUtils.separatorsToUnix(targetPath);
        }
        try {
            Path downloadedFile = transferFile(connector, targetRepository, targetPath, repository, localFile, requestProperties, true);
            if (fileExists(downloadedFile)) {
                log.debug("Successfully transferred: {}", downloadedFile.toAbsolutePath());
                return downloadedFile;
            }
        } catch (NotFoundException e) {
            log.debug("Artifact {} not found on repository \"{}\".", Keys.toKey(artifact), targetRepository.getRepository().getId());
        } catch (NotModifiedException e) {
            log.debug("Artifact {} not updated on repository \"{}\".", Keys.toKey(artifact), targetRepository.getRepository().getId());
        } catch (ProxyException | RepositoryAdminException e) {
            validatePolicies(this.downloadErrorPolicies, connector.getPolicies(), requestProperties, artifact, targetRepository, 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.", Keys.toKey(artifact));
    return null;
}
Also used : Path(java.nio.file.Path) Properties(java.util.Properties) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) ProxyDownloadException(org.apache.archiva.policies.ProxyDownloadException) AuthenticationException(org.apache.maven.wagon.authentication.AuthenticationException) TaskQueueException(org.apache.archiva.redback.components.taskqueue.TaskQueueException) PolicyConfigurationException(org.apache.archiva.policies.PolicyConfigurationException) WagonFactoryException(org.apache.archiva.proxy.common.WagonFactoryException) FileLockException(org.apache.archiva.common.filelock.FileLockException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) ConnectionException(org.apache.maven.wagon.ConnectionException) ProxyDownloadException(org.apache.archiva.policies.ProxyDownloadException) FileLockTimeoutException(org.apache.archiva.common.filelock.FileLockTimeoutException) RepositoryMetadataException(org.apache.archiva.repository.metadata.RepositoryMetadataException) IOException(java.io.IOException) PolicyViolationException(org.apache.archiva.policies.PolicyViolationException) WagonException(org.apache.maven.wagon.WagonException) ResourceDoesNotExistException(org.apache.maven.wagon.ResourceDoesNotExistException) LinkedHashMap(java.util.LinkedHashMap) RemoteRepositoryContent(org.apache.archiva.repository.RemoteRepositoryContent) ProxyConnector(org.apache.archiva.proxy.model.ProxyConnector)

Example 2 with ProxyDownloadException

use of org.apache.archiva.policies.ProxyDownloadException in project archiva by apache.

the class DefaultRepositoryProxyConnectors method validatePolicies.

private void validatePolicies(Map<String, DownloadErrorPolicy> policies, Map<String, String> settings, Properties request, ArtifactReference artifact, RemoteRepositoryContent content, Path localFile, Exception exception, Map<String, Exception> previousExceptions) throws ProxyDownloadException {
    boolean process = true;
    for (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 = StringUtils.substringAfterLast(entry.getKey(), "#");
        DownloadErrorPolicy policy = entry.getValue();
        String defaultSetting = policy.getDefaultOption();
        String setting = StringUtils.defaultString(settings.get(key), defaultSetting);
        log.debug("Applying [{}] policy with [{}]", key, setting);
        try {
            // all policies must approve the exception, any can cancel
            process = policy.applyPolicy(setting, 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(), Keys.toKey(artifact), exception.getMessage());
    log.debug("Full stack trace", exception);
}
Also used : PolicyConfigurationException(org.apache.archiva.policies.PolicyConfigurationException) DownloadErrorPolicy(org.apache.archiva.policies.DownloadErrorPolicy) ProxyDownloadException(org.apache.archiva.policies.ProxyDownloadException)

Example 3 with ProxyDownloadException

use of org.apache.archiva.policies.ProxyDownloadException in project archiva by apache.

the class ErrorHandlingTest method confirmFailures.

private void confirmFailures(String path, String[] ids) throws LayoutException {
    wagonMockControl.replay();
    // Attempt the proxy fetch.
    Path downloadedFile = null;
    try {
        downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository, managedDefaultRepository.toArtifactReference(path));
        fail("Proxy should not have succeeded");
    } catch (ProxyDownloadException e) {
        assertEquals(ids.length, e.getFailures().size());
        for (String id : ids) {
            assertTrue(e.getFailures().keySet().contains(id));
        }
    }
    wagonMockControl.verify();
    assertNotDownloaded(downloadedFile);
}
Also used : Path(java.nio.file.Path) ProxyDownloadException(org.apache.archiva.policies.ProxyDownloadException)

Example 4 with ProxyDownloadException

use of org.apache.archiva.policies.ProxyDownloadException in project archiva by apache.

the class ArchivaDavResourceFactory method fetchContentFromProxies.

private boolean fetchContentFromProxies(ManagedRepositoryContent managedRepository, DavServletRequest request, LogicalResource resource) throws DavException {
    String path = resource.getPath();
    if (repositoryRequest.isSupportFile(path)) {
        Path proxiedFile = connectors.fetchFromProxies(managedRepository, path);
        return (proxiedFile != null);
    }
    // Is it a Metadata resource?
    if (repositoryRequest.isDefault(path) && repositoryRequest.isMetadata(path)) {
        return connectors.fetchMetadataFromProxies(managedRepository, path).isModified();
    }
    // Is it an Archetype Catalog?
    if (repositoryRequest.isArchetypeCatalog(path)) {
        // FIXME we must implement a merge of remote archetype catalog from remote servers.
        Path proxiedFile = connectors.fetchFromProxies(managedRepository, path);
        return (proxiedFile != null);
    }
    // Not any of the above? Then it's gotta be an artifact reference.
    try {
        // Get the artifact reference in a layout neutral way.
        ArtifactReference artifact = repositoryRequest.toArtifactReference(path);
        if (artifact != null) {
            String repositoryLayout = managedRepository.getRepository().getLayout();
            RepositoryStorage repositoryStorage = this.applicationContext.getBean("repositoryStorage#" + repositoryLayout, RepositoryStorage.class);
            repositoryStorage.applyServerSideRelocation(managedRepository, artifact);
            Path proxiedFile = connectors.fetchFromProxies(managedRepository, artifact);
            resource.setPath(managedRepository.toPath(artifact));
            log.debug("Proxied artifact '{}:{}:{}'", artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
            return (proxiedFile != null);
        }
    } catch (LayoutException e) {
    /* eat it */
    } catch (ProxyDownloadException e) {
        log.error(e.getMessage(), e);
        throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unable to fetch artifact resource.");
    }
    return false;
}
Also used : Path(java.nio.file.Path) RepositoryStorage(org.apache.archiva.metadata.repository.storage.RepositoryStorage) LayoutException(org.apache.archiva.repository.LayoutException) DavException(org.apache.jackrabbit.webdav.DavException) ProxyDownloadException(org.apache.archiva.policies.ProxyDownloadException) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Aggregations

ProxyDownloadException (org.apache.archiva.policies.ProxyDownloadException)4 Path (java.nio.file.Path)3 PolicyConfigurationException (org.apache.archiva.policies.PolicyConfigurationException)2 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1 Properties (java.util.Properties)1 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)1 FileLockException (org.apache.archiva.common.filelock.FileLockException)1 FileLockTimeoutException (org.apache.archiva.common.filelock.FileLockTimeoutException)1 RepositoryStorage (org.apache.archiva.metadata.repository.storage.RepositoryStorage)1 ArtifactReference (org.apache.archiva.model.ArtifactReference)1 DownloadErrorPolicy (org.apache.archiva.policies.DownloadErrorPolicy)1 PolicyViolationException (org.apache.archiva.policies.PolicyViolationException)1 WagonFactoryException (org.apache.archiva.proxy.common.WagonFactoryException)1 ProxyConnector (org.apache.archiva.proxy.model.ProxyConnector)1 TaskQueueException (org.apache.archiva.redback.components.taskqueue.TaskQueueException)1 LayoutException (org.apache.archiva.repository.LayoutException)1 RemoteRepositoryContent (org.apache.archiva.repository.RemoteRepositoryContent)1 RepositoryMetadataException (org.apache.archiva.repository.metadata.RepositoryMetadataException)1 DavException (org.apache.jackrabbit.webdav.DavException)1