Search in sources :

Example 1 with ProxyConnector

use of org.apache.archiva.proxy.model.ProxyConnector 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 ProxyConnector

use of org.apache.archiva.proxy.model.ProxyConnector in project archiva by apache.

the class DefaultRepositoryProxyConnectors method fetchMetadataFromProxies.

@Override
public ProxyFetchResult fetchMetadataFromProxies(ManagedRepositoryContent repository, String logicalPath) {
    Path localFile = Paths.get(repository.getRepoRoot(), logicalPath);
    Properties requestProperties = new Properties();
    requestProperties.setProperty("filetype", "metadata");
    boolean metadataNeedsUpdating = false;
    long originalTimestamp = getLastModified(localFile);
    List<ProxyConnector> connectors = new ArrayList<>(getProxyConnectors(repository));
    for (ProxyConnector connector : connectors) {
        if (connector.isDisabled()) {
            continue;
        }
        RemoteRepositoryContent targetRepository = connector.getTargetRepository();
        Path localRepoFile = toLocalRepoFile(repository, targetRepository, logicalPath);
        long originalMetadataTimestamp = getLastModified(localRepoFile);
        try {
            transferFile(connector, targetRepository, logicalPath, repository, localRepoFile, requestProperties, true);
            if (hasBeenUpdated(localRepoFile, originalMetadataTimestamp)) {
                metadataNeedsUpdating = true;
            }
        } catch (NotFoundException e) {
            log.debug("Metadata {} not found on remote repository '{}'.", logicalPath, targetRepository.getRepository().getId(), e);
        } catch (NotModifiedException e) {
            log.debug("Metadata {} not updated on remote repository '{}'.", logicalPath, targetRepository.getRepository().getId(), e);
        } catch (ProxyException | RepositoryAdminException e) {
            log.warn("Transfer error from repository {} for versioned Metadata {}, continuing to next repository. Error message: {}", targetRepository.getRepository().getId(), logicalPath, e.getMessage());
            log.debug("Full stack trace", e);
        }
    }
    if (hasBeenUpdated(localFile, originalTimestamp)) {
        metadataNeedsUpdating = true;
    }
    if (metadataNeedsUpdating || !Files.exists(localFile)) {
        try {
            metadataTools.updateMetadata(repository, logicalPath);
        } catch (RepositoryMetadataException e) {
            log.warn("Unable to update metadata {}:{}", localFile.toAbsolutePath(), e.getMessage(), e);
        }
    }
    if (fileExists(localFile)) {
        return new ProxyFetchResult(localFile, metadataNeedsUpdating);
    }
    return new ProxyFetchResult(null, false);
}
Also used : Path(java.nio.file.Path) ProxyFetchResult(org.apache.archiva.proxy.model.ProxyFetchResult) ArrayList(java.util.ArrayList) Properties(java.util.Properties) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) RepositoryMetadataException(org.apache.archiva.repository.metadata.RepositoryMetadataException) RemoteRepositoryContent(org.apache.archiva.repository.RemoteRepositoryContent) ProxyConnector(org.apache.archiva.proxy.model.ProxyConnector)

Example 3 with ProxyConnector

use of org.apache.archiva.proxy.model.ProxyConnector in project archiva by apache.

the class DefaultRepositoryProxyConnectors method initConnectorsAndNetworkProxies.

@SuppressWarnings("unchecked")
private void initConnectorsAndNetworkProxies() {
    ProxyConnectorOrderComparator proxyOrderSorter = new ProxyConnectorOrderComparator();
    this.proxyConnectorMap.clear();
    Configuration configuration = archivaConfiguration.getConfiguration();
    List<ProxyConnectorRuleConfiguration> allProxyConnectorRuleConfigurations = configuration.getProxyConnectorRuleConfigurations();
    List<ProxyConnectorConfiguration> proxyConfigs = configuration.getProxyConnectors();
    for (ProxyConnectorConfiguration proxyConfig : proxyConfigs) {
        String key = proxyConfig.getSourceRepoId();
        // Create connector object.
        ProxyConnector connector = new ProxyConnector();
        ManagedRepository repo = repositoryRegistry.getManagedRepository(proxyConfig.getSourceRepoId());
        if (repo == null) {
            log.error("Cannot find source repository after config change " + proxyConfig.getSourceRepoId());
            continue;
        }
        connector.setSourceRepository(repo.getContent());
        RemoteRepository rRepo = repositoryRegistry.getRemoteRepository(proxyConfig.getTargetRepoId());
        if (rRepo == null) {
            log.error("Cannot find target repository after config change " + proxyConfig.getSourceRepoId());
            continue;
        }
        connector.setTargetRepository(rRepo.getContent());
        connector.setProxyId(proxyConfig.getProxyId());
        connector.setPolicies(proxyConfig.getPolicies());
        connector.setOrder(proxyConfig.getOrder());
        connector.setDisabled(proxyConfig.isDisabled());
        // Copy any blacklist patterns.
        List<String> blacklist = new ArrayList<>(0);
        if (CollectionUtils.isNotEmpty(proxyConfig.getBlackListPatterns())) {
            blacklist.addAll(proxyConfig.getBlackListPatterns());
        }
        connector.setBlacklist(blacklist);
        // Copy any whitelist patterns.
        List<String> whitelist = new ArrayList<>(0);
        if (CollectionUtils.isNotEmpty(proxyConfig.getWhiteListPatterns())) {
            whitelist.addAll(proxyConfig.getWhiteListPatterns());
        }
        connector.setWhitelist(whitelist);
        List<ProxyConnectorRuleConfiguration> proxyConnectorRuleConfigurations = findProxyConnectorRules(connector.getSourceRepository().getId(), connector.getTargetRepository().getId(), allProxyConnectorRuleConfigurations);
        if (!proxyConnectorRuleConfigurations.isEmpty()) {
            for (ProxyConnectorRuleConfiguration proxyConnectorRuleConfiguration : proxyConnectorRuleConfigurations) {
                if (StringUtils.equals(proxyConnectorRuleConfiguration.getRuleType(), ProxyConnectorRuleType.BLACK_LIST.getRuleType())) {
                    connector.getBlacklist().add(proxyConnectorRuleConfiguration.getPattern());
                }
                if (StringUtils.equals(proxyConnectorRuleConfiguration.getRuleType(), ProxyConnectorRuleType.WHITE_LIST.getRuleType())) {
                    connector.getWhitelist().add(proxyConnectorRuleConfiguration.getPattern());
                }
            }
        }
        // Get other connectors
        List<ProxyConnector> connectors = this.proxyConnectorMap.get(key);
        if (connectors == null) {
            // Create if we are the first.
            connectors = new ArrayList<>(1);
        }
        // Add the connector.
        connectors.add(connector);
        // Ensure the list is sorted.
        Collections.sort(connectors, proxyOrderSorter);
        // Set the key to the list of connectors.
        this.proxyConnectorMap.put(key, connectors);
    }
    this.networkProxyMap.clear();
    List<NetworkProxyConfiguration> networkProxies = archivaConfiguration.getConfiguration().getNetworkProxies();
    for (NetworkProxyConfiguration networkProxyConfig : networkProxies) {
        String key = networkProxyConfig.getId();
        ProxyInfo proxy = new ProxyInfo();
        proxy.setType(networkProxyConfig.getProtocol());
        proxy.setHost(networkProxyConfig.getHost());
        proxy.setPort(networkProxyConfig.getPort());
        proxy.setUserName(networkProxyConfig.getUsername());
        proxy.setPassword(networkProxyConfig.getPassword());
        this.networkProxyMap.put(key, proxy);
    }
}
Also used : ManagedRepository(org.apache.archiva.repository.ManagedRepository) ArchivaConfiguration(org.apache.archiva.configuration.ArchivaConfiguration) ProxyConnectorConfiguration(org.apache.archiva.configuration.ProxyConnectorConfiguration) ProxyConnectorRuleConfiguration(org.apache.archiva.configuration.ProxyConnectorRuleConfiguration) Configuration(org.apache.archiva.configuration.Configuration) NetworkProxyConfiguration(org.apache.archiva.configuration.NetworkProxyConfiguration) ArrayList(java.util.ArrayList) ProxyConnectorConfiguration(org.apache.archiva.configuration.ProxyConnectorConfiguration) RemoteRepository(org.apache.archiva.repository.RemoteRepository) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) ProxyConnectorRuleConfiguration(org.apache.archiva.configuration.ProxyConnectorRuleConfiguration) NetworkProxyConfiguration(org.apache.archiva.configuration.NetworkProxyConfiguration) ProxyConnector(org.apache.archiva.proxy.model.ProxyConnector)

Example 4 with ProxyConnector

use of org.apache.archiva.proxy.model.ProxyConnector in project archiva by apache.

the class DefaultRepositoryProxyConnectors method fetchFromProxies.

@Override
public Path fetchFromProxies(ManagedRepositoryContent repository, String path) {
    Path localFile = Paths.get(repository.getRepoRoot(), path);
    // no update policies for these paths
    if (Files.exists(localFile)) {
        return null;
    }
    Properties requestProperties = new Properties();
    requestProperties.setProperty("filetype", "resource");
    requestProperties.setProperty("managedRepositoryId", repository.getId());
    List<ProxyConnector> connectors = getProxyConnectors(repository);
    for (ProxyConnector connector : connectors) {
        if (connector.isDisabled()) {
            continue;
        }
        RemoteRepositoryContent targetRepository = connector.getTargetRepository();
        requestProperties.setProperty("remoteRepositoryId", targetRepository.getId());
        String targetPath = path;
        try {
            Path downloadedFile = transferFile(connector, targetRepository, targetPath, repository, localFile, requestProperties, false);
            if (fileExists(downloadedFile)) {
                log.debug("Successfully transferred: {}", downloadedFile.toAbsolutePath());
                return downloadedFile;
            }
        } catch (NotFoundException e) {
            log.debug("Resource {} not found on repository \"{}\".", path, targetRepository.getRepository().getId());
        } catch (NotModifiedException e) {
            log.debug("Resource {} not updated on repository \"{}\".", path, targetRepository.getRepository().getId());
        } catch (ProxyException e) {
            log.warn("Transfer error from repository {} for resource {}, continuing to next repository. Error message: {}", targetRepository.getRepository().getId(), path, e.getMessage());
            log.debug(MarkerFactory.getDetachedMarker("transfer.error"), "Transfer error from repository \"{}" + "\" for resource {}, continuing to next repository. Error message: {}", targetRepository.getRepository().getId(), path, e.getMessage(), e);
        } catch (RepositoryAdminException e) {
            log.debug(MarkerFactory.getDetachedMarker("transfer.error"), "Transfer error from repository {} for resource {}, continuing to next repository. Error message: {}", targetRepository.getRepository().getId(), path, e.getMessage(), e);
            log.debug(MarkerFactory.getDetachedMarker("transfer.error"), "Full stack trace", e);
        }
    }
    log.debug("Exhausted all target repositories, resource {} not found.", path);
    return null;
}
Also used : Path(java.nio.file.Path) RemoteRepositoryContent(org.apache.archiva.repository.RemoteRepositoryContent) ProxyConnector(org.apache.archiva.proxy.model.ProxyConnector) Properties(java.util.Properties) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException)

Aggregations

ProxyConnector (org.apache.archiva.proxy.model.ProxyConnector)4 Path (java.nio.file.Path)3 Properties (java.util.Properties)3 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)3 RemoteRepositoryContent (org.apache.archiva.repository.RemoteRepositoryContent)3 ArrayList (java.util.ArrayList)2 RepositoryMetadataException (org.apache.archiva.repository.metadata.RepositoryMetadataException)2 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1 FileLockException (org.apache.archiva.common.filelock.FileLockException)1 FileLockTimeoutException (org.apache.archiva.common.filelock.FileLockTimeoutException)1 ArchivaConfiguration (org.apache.archiva.configuration.ArchivaConfiguration)1 Configuration (org.apache.archiva.configuration.Configuration)1 NetworkProxyConfiguration (org.apache.archiva.configuration.NetworkProxyConfiguration)1 ProxyConnectorConfiguration (org.apache.archiva.configuration.ProxyConnectorConfiguration)1 ProxyConnectorRuleConfiguration (org.apache.archiva.configuration.ProxyConnectorRuleConfiguration)1 PolicyConfigurationException (org.apache.archiva.policies.PolicyConfigurationException)1 PolicyViolationException (org.apache.archiva.policies.PolicyViolationException)1 ProxyDownloadException (org.apache.archiva.policies.ProxyDownloadException)1 WagonFactoryException (org.apache.archiva.proxy.common.WagonFactoryException)1