Search in sources :

Example 11 with NetworkProxy

use of org.apache.archiva.admin.model.beans.NetworkProxy in project archiva by apache.

the class Maven3DependencyTreeBuilder method buildDependencyTree.

@Override
public void buildDependencyTree(List<String> repositoryIds, String groupId, String artifactId, String version, DependencyVisitor dependencyVisitor) throws DependencyTreeBuilderException {
    Artifact projectArtifact = factory.createProjectArtifact(groupId, artifactId, version);
    ManagedRepository repository = null;
    try {
        repository = findArtifactInRepositories(repositoryIds, projectArtifact);
    } catch (RepositoryAdminException e) {
        // FIXME better exception
        throw new DependencyTreeBuilderException("Cannot build project dependency tree " + e.getMessage(), e);
    }
    if (repository == null) {
        // metadata could not be resolved
        return;
    }
    List<RemoteRepository> remoteRepositories = new ArrayList<>();
    Map<String, NetworkProxy> networkProxies = new HashMap<>();
    try {
        // MRM-1411
        // TODO: this is a workaround for a lack of proxy capability in the resolvers - replace when it can all be
        // handled there. It doesn't cache anything locally!
        Map<String, List<ProxyConnector>> proxyConnectorsMap = proxyConnectorAdmin.getProxyConnectorAsMap();
        List<ProxyConnector> proxyConnectors = proxyConnectorsMap.get(repository.getId());
        if (proxyConnectors != null) {
            for (ProxyConnector proxyConnector : proxyConnectors) {
                remoteRepositories.add(remoteRepositoryAdmin.getRemoteRepository(proxyConnector.getTargetRepoId()));
                NetworkProxy networkProxyConfig = networkProxyAdmin.getNetworkProxy(proxyConnector.getProxyId());
                if (networkProxyConfig != null) {
                    // key/value: remote repo ID/proxy info
                    networkProxies.put(proxyConnector.getTargetRepoId(), networkProxyConfig);
                }
            }
        }
    } catch (RepositoryAdminException e) {
        throw new DependencyTreeBuilderException(e.getMessage(), e);
    }
    // FIXME take care of relative path
    ResolveRequest resolveRequest = new ResolveRequest();
    resolveRequest.dependencyVisitor = dependencyVisitor;
    resolveRequest.localRepoDir = repository.getLocation();
    resolveRequest.groupId = groupId;
    resolveRequest.artifactId = artifactId;
    resolveRequest.version = version;
    resolveRequest.remoteRepositories = remoteRepositories;
    resolveRequest.networkProxies = networkProxies;
    resolve(resolveRequest);
}
Also used : ManagedRepository(org.apache.archiva.admin.model.beans.ManagedRepository) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RemoteRepository(org.apache.archiva.admin.model.beans.RemoteRepository) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.sonatype.aether.util.artifact.DefaultArtifact) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy) List(java.util.List) ArrayList(java.util.ArrayList) ProxyConnector(org.apache.archiva.admin.model.beans.ProxyConnector)

Example 12 with NetworkProxy

use of org.apache.archiva.admin.model.beans.NetworkProxy in project archiva by apache.

the class ArchivaIndexManagerMock method update.

@Override
public void update(final ArchivaIndexingContext context, final boolean fullUpdate) throws IndexUpdateFailedException {
    log.info("start download remote index for remote repository {}", context.getRepository().getId());
    URI remoteUpdateUri;
    if (!(context.getRepository() instanceof RemoteRepository) || !(context.getRepository().supportsFeature(RemoteIndexFeature.class))) {
        throw new IndexUpdateFailedException("The context is not associated to a remote repository with remote index " + context.getId());
    } else {
        RemoteIndexFeature rif = context.getRepository().getFeature(RemoteIndexFeature.class).get();
        remoteUpdateUri = context.getRepository().getLocation().resolve(rif.getIndexUri());
    }
    final RemoteRepository remoteRepository = (RemoteRepository) context.getRepository();
    executeUpdateFunction(context, indexingContext -> {
        try {
            // create a temp directory to download files
            Path tempIndexDirectory = Paths.get(indexingContext.getIndexDirectoryFile().getParent(), ".tmpIndex");
            Path indexCacheDirectory = Paths.get(indexingContext.getIndexDirectoryFile().getParent(), ".indexCache");
            Files.createDirectories(indexCacheDirectory);
            if (Files.exists(tempIndexDirectory)) {
                org.apache.archiva.common.utils.FileUtils.deleteDirectory(tempIndexDirectory);
            }
            Files.createDirectories(tempIndexDirectory);
            tempIndexDirectory.toFile().deleteOnExit();
            String baseIndexUrl = indexingContext.getIndexUpdateUrl();
            String wagonProtocol = remoteUpdateUri.toURL().getProtocol();
            NetworkProxy networkProxy = null;
            if (remoteRepository.supportsFeature(RemoteIndexFeature.class)) {
                RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class).get();
                if (StringUtils.isNotBlank(rif.getProxyId())) {
                    try {
                        networkProxy = networkProxyAdmin.getNetworkProxy(rif.getProxyId());
                    } catch (RepositoryAdminException e) {
                        log.error("Error occured while retrieving proxy {}", e.getMessage());
                    }
                    if (networkProxy == null) {
                        log.warn("your remote repository is configured to download remote index trought a proxy we cannot find id:{}", rif.getProxyId());
                    }
                }
                final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(new WagonFactoryRequest(wagonProtocol, remoteRepository.getExtraHeaders()).networkProxy(networkProxy));
                int readTimeout = (int) rif.getDownloadTimeout().toMillis() * 1000;
                wagon.setReadTimeout(readTimeout);
                wagon.setTimeout((int) remoteRepository.getTimeout().toMillis() * 1000);
                if (wagon instanceof AbstractHttpClientWagon) {
                    HttpConfiguration httpConfiguration = new HttpConfiguration();
                    HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration();
                    httpMethodConfiguration.setUsePreemptive(true);
                    httpMethodConfiguration.setReadTimeout(readTimeout);
                    httpConfiguration.setGet(httpMethodConfiguration);
                    AbstractHttpClientWagon.class.cast(wagon).setHttpConfiguration(httpConfiguration);
                }
                wagon.addTransferListener(new DownloadListener());
                ProxyInfo proxyInfo = null;
                if (networkProxy != null) {
                    proxyInfo = new ProxyInfo();
                    proxyInfo.setType(networkProxy.getProtocol());
                    proxyInfo.setHost(networkProxy.getHost());
                    proxyInfo.setPort(networkProxy.getPort());
                    proxyInfo.setUserName(networkProxy.getUsername());
                    proxyInfo.setPassword(networkProxy.getPassword());
                }
                AuthenticationInfo authenticationInfo = null;
                if (remoteRepository.getLoginCredentials() != null && (remoteRepository.getLoginCredentials() instanceof PasswordCredentials)) {
                    PasswordCredentials creds = (PasswordCredentials) remoteRepository.getLoginCredentials();
                    authenticationInfo = new AuthenticationInfo();
                    authenticationInfo.setUserName(creds.getUsername());
                    authenticationInfo.setPassword(new String(creds.getPassword()));
                }
                wagon.connect(new org.apache.maven.wagon.repository.Repository(remoteRepository.getId(), baseIndexUrl), authenticationInfo, proxyInfo);
                Path indexDirectory = indexingContext.getIndexDirectoryFile().toPath();
                if (!Files.exists(indexDirectory)) {
                    Files.createDirectories(indexDirectory);
                }
                ResourceFetcher resourceFetcher = new WagonResourceFetcher(log, tempIndexDirectory, wagon, remoteRepository);
                IndexUpdateRequest request = new IndexUpdateRequest(indexingContext, resourceFetcher);
                request.setForceFullUpdate(fullUpdate);
                request.setLocalIndexCacheDir(indexCacheDirectory.toFile());
                // indexUpdater.fetchAndUpdateIndex( request );
                indexingContext.updateTimestamp(true);
            }
        } catch (AuthenticationException e) {
            log.error("Could not login to the remote proxy for updating index of {}", remoteRepository.getId(), e);
            throw new IndexUpdateFailedException("Login in to proxy failed while updating remote repository " + remoteRepository.getId(), e);
        } catch (ConnectionException e) {
            log.error("Connection error during index update for remote repository {}", remoteRepository.getId(), e);
            throw new IndexUpdateFailedException("Connection error during index update for remote repository " + remoteRepository.getId(), e);
        } catch (MalformedURLException e) {
            log.error("URL for remote index update of remote repository {} is not correct {}", remoteRepository.getId(), remoteUpdateUri, e);
            throw new IndexUpdateFailedException("URL for remote index update of repository is not correct " + remoteUpdateUri, e);
        } catch (IOException e) {
            log.error("IOException during index update of remote repository {}: {}", remoteRepository.getId(), e.getMessage(), e);
            throw new IndexUpdateFailedException("IOException during index update of remote repository " + remoteRepository.getId() + (StringUtils.isNotEmpty(e.getMessage()) ? ": " + e.getMessage() : ""), e);
        } catch (WagonFactoryException e) {
            log.error("Wagon for remote index download of {} could not be created: {}", remoteRepository.getId(), e.getMessage(), e);
            throw new IndexUpdateFailedException("Error while updating the remote index of " + remoteRepository.getId(), e);
        }
    });
}
Also used : AbstractHttpClientWagon(org.apache.maven.wagon.shared.http.AbstractHttpClientWagon) MalformedURLException(java.net.MalformedURLException) HttpMethodConfiguration(org.apache.maven.wagon.shared.http.HttpMethodConfiguration) AuthenticationException(org.apache.maven.wagon.authentication.AuthenticationException) ResourceFetcher(org.apache.maven.index.updater.ResourceFetcher) RemoteRepository(org.apache.archiva.repository.RemoteRepository) HttpConfiguration(org.apache.maven.wagon.shared.http.HttpConfiguration) URI(java.net.URI) StreamWagon(org.apache.maven.wagon.StreamWagon) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) WagonFactoryRequest(org.apache.archiva.proxy.common.WagonFactoryRequest) IndexUpdateFailedException(org.apache.archiva.indexer.IndexUpdateFailedException) Path(java.nio.file.Path) PasswordCredentials(org.apache.archiva.repository.PasswordCredentials) IndexUpdateRequest(org.apache.maven.index.updater.IndexUpdateRequest) IOException(java.io.IOException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) WagonFactoryException(org.apache.archiva.proxy.common.WagonFactoryException) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) ConnectionException(org.apache.maven.wagon.ConnectionException)

Example 13 with NetworkProxy

use of org.apache.archiva.admin.model.beans.NetworkProxy in project archiva by apache.

the class NetworkProxyAdminTest method addAndDeleteWithRemoteRepoLinked.

/**
 * ensure we cleanup remote repos linked to a network proxy
 */
@Test
public void addAndDeleteWithRemoteRepoLinked() throws Exception {
    mockAuditListener.clearEvents();
    int initialSize = networkProxyAdmin.getNetworkProxies().size();
    NetworkProxy networkProxy = getNetworkProxyTest("foo");
    networkProxyAdmin.addNetworkProxy(networkProxy, getFakeAuditInformation());
    assertEquals(initialSize + 1, networkProxyAdmin.getNetworkProxies().size());
    networkProxy = networkProxyAdmin.getNetworkProxy("foo");
    assertNotNull(networkProxy);
    RemoteRepository remoteRepository = getRemoteRepository();
    remoteRepository.setRemoteDownloadNetworkProxyId(networkProxy.getId());
    remoteRepositoryAdmin.addRemoteRepository(remoteRepository, getFakeAuditInformation());
    networkProxyAdmin.deleteNetworkProxy("foo", getFakeAuditInformation());
    remoteRepository = remoteRepositoryAdmin.getRemoteRepository(getRemoteRepository().getId());
    assertNull(remoteRepository.getRemoteDownloadNetworkProxyId());
    remoteRepositoryAdmin.deleteRemoteRepository(getRemoteRepository().getId(), getFakeAuditInformation());
}
Also used : RemoteRepository(org.apache.archiva.admin.model.beans.RemoteRepository) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy) AbstractRepositoryAdminTest(org.apache.archiva.admin.repository.AbstractRepositoryAdminTest) Test(org.junit.Test)

Example 14 with NetworkProxy

use of org.apache.archiva.admin.model.beans.NetworkProxy in project archiva by apache.

the class NetworkProxyAdminTest method addAndUpdateAndDelete.

@Test
public void addAndUpdateAndDelete() throws Exception {
    mockAuditListener.clearEvents();
    int initialSize = networkProxyAdmin.getNetworkProxies().size();
    NetworkProxy networkProxy = getNetworkProxyTest("foo");
    networkProxyAdmin.addNetworkProxy(networkProxy, getFakeAuditInformation());
    assertEquals(initialSize + 1, networkProxyAdmin.getNetworkProxies().size());
    networkProxy = networkProxyAdmin.getNetworkProxy("foo");
    assertNotNull(networkProxy);
    assertEquals(getNetworkProxyTest("foo").getId(), networkProxy.getId());
    assertEquals(getNetworkProxyTest("foo").getHost(), networkProxy.getHost());
    assertEquals(getNetworkProxyTest("foo").getPassword(), networkProxy.getPassword());
    assertEquals(getNetworkProxyTest("foo").getPort(), networkProxy.getPort());
    assertEquals(getNetworkProxyTest("foo").getUsername(), networkProxy.getUsername());
    assertEquals(getNetworkProxyTest("foo").getProtocol(), networkProxy.getProtocol());
    networkProxy.setHost("https://toto.com");
    networkProxy.setPassword("newpasswd");
    networkProxy.setPort(9191);
    networkProxy.setProtocol("http");
    networkProxy.setUsername("newusername");
    networkProxyAdmin.updateNetworkProxy(networkProxy, getFakeAuditInformation());
    NetworkProxy updatedNetworkProxy = networkProxyAdmin.getNetworkProxy("foo");
    assertNotNull(updatedNetworkProxy);
    assertEquals(networkProxy.getId(), updatedNetworkProxy.getId());
    assertEquals(networkProxy.getHost(), updatedNetworkProxy.getHost());
    assertEquals(networkProxy.getPassword(), updatedNetworkProxy.getPassword());
    assertEquals(networkProxy.getPort(), updatedNetworkProxy.getPort());
    assertEquals(networkProxy.getUsername(), updatedNetworkProxy.getUsername());
    assertEquals(networkProxy.getProtocol(), updatedNetworkProxy.getProtocol());
    networkProxyAdmin.deleteNetworkProxy("foo", getFakeAuditInformation());
    assertEquals(3, mockAuditListener.getAuditEvents().size());
    assertEquals(AuditEvent.ADD_NETWORK_PROXY, mockAuditListener.getAuditEvents().get(0).getAction());
    assertEquals(AuditEvent.MODIFY_NETWORK_PROXY, mockAuditListener.getAuditEvents().get(1).getAction());
    assertEquals(AuditEvent.DELETE_NETWORK_PROXY, mockAuditListener.getAuditEvents().get(2).getAction());
    mockAuditListener.clearEvents();
}
Also used : NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy) AbstractRepositoryAdminTest(org.apache.archiva.admin.repository.AbstractRepositoryAdminTest) Test(org.junit.Test)

Example 15 with NetworkProxy

use of org.apache.archiva.admin.model.beans.NetworkProxy in project archiva by apache.

the class DefaultNetworkProxyAdmin method deleteNetworkProxy.

@Override
public void deleteNetworkProxy(String networkProxyId, AuditInformation auditInformation) throws RepositoryAdminException {
    NetworkProxy networkProxy = getNetworkProxy(networkProxyId);
    if (networkProxy == null) {
        throw new RepositoryAdminException("cannot delete NetworkProxy with id " + networkProxyId + " as not exist");
    }
    Configuration configuration = getArchivaConfiguration().getConfiguration();
    NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration(networkProxy);
    configuration.removeNetworkProxy(networkProxyConfiguration);
    for (RemoteRepository repo : repositoryRegistry.getRemoteRepositories()) {
        if (repo.supportsFeature(RemoteIndexFeature.class)) {
            RemoteIndexFeature rif = repo.getFeature(RemoteIndexFeature.class).get();
            if (networkProxyId.equals(rif.getProxyId())) {
                rif.setProxyId(null);
                try {
                    repositoryRegistry.putRepository(repo, configuration);
                } catch (RepositoryException e) {
                    log.error("Could not update repository {}", repo.getId(), e);
                }
            }
        }
    }
    triggerAuditEvent(networkProxy.getId(), null, AuditEvent.DELETE_NETWORK_PROXY, auditInformation);
    saveConfiguration(configuration);
}
Also used : Configuration(org.apache.archiva.configuration.Configuration) NetworkProxyConfiguration(org.apache.archiva.configuration.NetworkProxyConfiguration) NetworkProxyConfiguration(org.apache.archiva.configuration.NetworkProxyConfiguration) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) RemoteRepository(org.apache.archiva.repository.RemoteRepository) RepositoryException(org.apache.archiva.repository.RepositoryException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy)

Aggregations

NetworkProxy (org.apache.archiva.admin.model.beans.NetworkProxy)17 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)8 WagonFactoryRequest (org.apache.archiva.proxy.common.WagonFactoryRequest)6 RemoteRepository (org.apache.archiva.repository.RemoteRepository)6 ConnectionException (org.apache.maven.wagon.ConnectionException)6 Path (java.nio.file.Path)5 RemoteIndexFeature (org.apache.archiva.repository.features.RemoteIndexFeature)5 ProxyInfo (org.apache.maven.wagon.proxy.ProxyInfo)5 WagonFactoryException (org.apache.archiva.proxy.common.WagonFactoryException)4 AuthenticationException (org.apache.maven.wagon.authentication.AuthenticationException)4 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)3 URI (java.net.URI)3 RemoteRepository (org.apache.archiva.admin.model.beans.RemoteRepository)3 AbstractRepositoryAdminTest (org.apache.archiva.admin.repository.AbstractRepositoryAdminTest)3 IndexUpdateFailedException (org.apache.archiva.indexer.IndexUpdateFailedException)3 PasswordCredentials (org.apache.archiva.repository.PasswordCredentials)3 IndexUpdateRequest (org.apache.maven.index.updater.IndexUpdateRequest)3 ResourceFetcher (org.apache.maven.index.updater.ResourceFetcher)3 StreamWagon (org.apache.maven.wagon.StreamWagon)3