Search in sources :

Example 16 with PasswordCredentials

use of org.apache.archiva.repository.base.PasswordCredentials 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);
        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)) {
                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);
                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(new String(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.proxy.model.NetworkProxy) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) WagonFactoryRequest(org.apache.archiva.maven.common.proxy.WagonFactoryRequest) IndexUpdateFailedException(org.apache.archiva.indexer.IndexUpdateFailedException) Path(java.nio.file.Path) PasswordCredentials(org.apache.archiva.repository.base.PasswordCredentials) IndexUpdateRequest(org.apache.maven.index.updater.IndexUpdateRequest) IOException(java.io.IOException) WagonFactoryException(org.apache.archiva.maven.common.proxy.WagonFactoryException) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) ConnectionException(org.apache.maven.wagon.ConnectionException)

Example 17 with PasswordCredentials

use of org.apache.archiva.repository.base.PasswordCredentials in project archiva by apache.

the class DownloadRemoteIndexTask method run.

@Override
public void run() {
    // so short lock : not sure we need it
    synchronized (this.runningRemoteDownloadIds) {
        if (this.runningRemoteDownloadIds.contains(this.remoteRepository.getId())) {
            // skip it as it's running
            log.info("skip download index remote for repo {} it's already running", this.remoteRepository.getId());
            return;
        }
        this.runningRemoteDownloadIds.add(this.remoteRepository.getId());
    }
    Path tempIndexDirectory = null;
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    try {
        log.info("start download remote index for remote repository {}", this.remoteRepository.getId());
        if (this.remoteRepository.getIndexingContext() == null) {
            throw new IndexNotFoundException("No index context set for repository " + remoteRepository.getId());
        }
        if (this.remoteRepository.getType() != RepositoryType.MAVEN) {
            throw new RepositoryException("Bad repository type");
        }
        if (!this.remoteRepository.supportsFeature(RemoteIndexFeature.class)) {
            throw new RepositoryException("Repository does not support RemotIndexFeature " + remoteRepository.getId());
        }
        RemoteIndexFeature rif = this.remoteRepository.getFeature(RemoteIndexFeature.class);
        IndexingContext indexingContext = this.remoteRepository.getIndexingContext().getBaseContext(IndexingContext.class);
        // create a temp directory to download files
        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 = this.remoteRepository.getLocation().getScheme();
        final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(new WagonFactoryRequest(wagonProtocol, this.remoteRepository.getExtraHeaders()).networkProxy(this.networkProxy));
        // FIXME olamy having 2 config values
        wagon.setReadTimeout((int) rif.getDownloadTimeout().toMillis());
        wagon.setTimeout((int) remoteRepository.getTimeout().toMillis());
        if (wagon instanceof AbstractHttpClientWagon) {
            HttpConfiguration httpConfiguration = new HttpConfiguration();
            HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration();
            httpMethodConfiguration.setUsePreemptive(true);
            httpMethodConfiguration.setReadTimeout((int) rif.getDownloadTimeout().toMillis());
            httpConfiguration.setGet(httpMethodConfiguration);
            AbstractHttpClientWagon.class.cast(wagon).setHttpConfiguration(httpConfiguration);
        }
        wagon.addTransferListener(new DownloadListener());
        ProxyInfo proxyInfo = null;
        if (this.networkProxy != null) {
            proxyInfo = new ProxyInfo();
            proxyInfo.setType(this.networkProxy.getProtocol());
            proxyInfo.setHost(this.networkProxy.getHost());
            proxyInfo.setPort(this.networkProxy.getPort());
            proxyInfo.setUserName(this.networkProxy.getUsername());
            proxyInfo.setPassword(new String(this.networkProxy.getPassword()));
        }
        AuthenticationInfo authenticationInfo = null;
        if (this.remoteRepository.getLoginCredentials() != null && this.remoteRepository.getLoginCredentials() instanceof PasswordCredentials) {
            PasswordCredentials creds = (PasswordCredentials) this.remoteRepository.getLoginCredentials();
            authenticationInfo = new AuthenticationInfo();
            authenticationInfo.setUserName(creds.getUsername());
            authenticationInfo.setPassword(new String(creds.getPassword()));
        }
        log.debug("Connection to {}, authInfo={}", this.remoteRepository.getId(), authenticationInfo);
        wagon.connect(new Repository(this.remoteRepository.getId(), baseIndexUrl), authenticationInfo, proxyInfo);
        Path indexDirectory = indexingContext.getIndexDirectoryFile().toPath();
        if (!Files.exists(indexDirectory)) {
            Files.createDirectories(indexDirectory);
        }
        log.debug("Downloading index file to {}", indexDirectory);
        log.debug("Index cache dir {}", indexCacheDirectory);
        ResourceFetcher resourceFetcher = new WagonResourceFetcher(log, tempIndexDirectory, wagon, remoteRepository);
        IndexUpdateRequest request = new IndexUpdateRequest(indexingContext, resourceFetcher);
        request.setForceFullUpdate(this.fullDownload);
        request.setLocalIndexCacheDir(indexCacheDirectory.toFile());
        IndexUpdateResult result = this.indexUpdater.fetchAndUpdateIndex(request);
        log.debug("Update result success: {}", result.isSuccessful());
        stopWatch.stop();
        log.info("time update index from remote for repository {}: {}ms", this.remoteRepository.getId(), (stopWatch.getTime()));
        // index packing optionnal ??
        // IndexPackingRequest indexPackingRequest =
        // new IndexPackingRequest( indexingContext, indexingContext.getIndexDirectoryFile() );
        // indexPacker.packIndex( indexPackingRequest );
        indexingContext.updateTimestamp(true);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        deleteDirectoryQuiet(tempIndexDirectory);
        this.runningRemoteDownloadIds.remove(this.remoteRepository.getId());
    }
    log.info("end download remote index for remote repository {}", this.remoteRepository.getId());
}
Also used : Path(java.nio.file.Path) AbstractHttpClientWagon(org.apache.maven.wagon.shared.http.AbstractHttpClientWagon) PasswordCredentials(org.apache.archiva.repository.base.PasswordCredentials) HttpMethodConfiguration(org.apache.maven.wagon.shared.http.HttpMethodConfiguration) ResourceFetcher(org.apache.maven.index.updater.ResourceFetcher) IndexUpdateRequest(org.apache.maven.index.updater.IndexUpdateRequest) RepositoryException(org.apache.archiva.repository.RepositoryException) HttpConfiguration(org.apache.maven.wagon.shared.http.HttpConfiguration) StreamWagon(org.apache.maven.wagon.StreamWagon) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo) TransferFailedException(org.apache.maven.wagon.TransferFailedException) IndexNotFoundException(org.apache.maven.index_shaded.lucene.index.IndexNotFoundException) RepositoryException(org.apache.archiva.repository.RepositoryException) AuthorizationException(org.apache.maven.wagon.authorization.AuthorizationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ResourceDoesNotExistException(org.apache.maven.wagon.ResourceDoesNotExistException) StopWatch(org.apache.commons.lang3.time.StopWatch) IndexUpdateResult(org.apache.maven.index.updater.IndexUpdateResult) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Repository(org.apache.maven.wagon.repository.Repository) RemoteRepository(org.apache.archiva.repository.RemoteRepository) WagonFactoryRequest(org.apache.archiva.maven.common.proxy.WagonFactoryRequest) IndexNotFoundException(org.apache.maven.index_shaded.lucene.index.IndexNotFoundException) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) IndexingContext(org.apache.maven.index.context.IndexingContext)

Example 18 with PasswordCredentials

use of org.apache.archiva.repository.base.PasswordCredentials in project archiva by apache.

the class MavenRepositoryProviderTest method createRemoteInstance.

@Test
public void createRemoteInstance() throws Exception {
    RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
    repo.setUsername("testuser001");
    repo.setPassword("pwd0000abc");
    repo.setCheckPath("test/check.html");
    repo.setTimeout(50);
    repo.setUrl("https://repo.maven.apache.org/maven2/test");
    repo.setDownloadRemoteIndex(true);
    repo.setDownloadRemoteIndexOnStartup(true);
    Map<String, String> header = new HashMap<>();
    header.put("header1", "value1");
    header.put("header2", "value2");
    repo.setExtraHeaders(header);
    Map<String, String> params = new HashMap<>();
    params.put("param1", "pval1");
    params.put("param2", "pval2");
    repo.setExtraParameters(params);
    repo.setRefreshCronExpression("0 1 07 ? * MON");
    repo.setRemoteDownloadTimeout(333);
    repo.setRemoteIndexUrl("testremote/.index");
    repo.setDescription("This is a test");
    repo.setId("test001");
    repo.setName("Remote Test Repo 001");
    repo.setIndexDir("testindex/.index");
    repo.setLayout("maven2");
    repo.setType(RepositoryType.MAVEN.toString());
    repo.setIndexDir("local/.index");
    RemoteRepository mr = provider.createRemoteInstance(repo);
    assertEquals("test001", mr.getId());
    assertEquals("This is a test", mr.getDescription());
    assertNotNull(mr.getLocation());
    assertEquals("https://repo.maven.apache.org/maven2/test", mr.getLocation().toString());
    assertEquals("Remote Test Repo 001", mr.getName());
    assertEquals("test001", mr.getId());
    assertEquals("0 1 07 ? * MON", mr.getSchedulingDefinition());
    assertEquals(50, mr.getTimeout().get(ChronoUnit.SECONDS));
    assertTrue(mr.isScanned());
    assertNotNull(mr.getLoginCredentials());
    assertTrue(mr.getLoginCredentials() instanceof PasswordCredentials);
    PasswordCredentials creds = (PasswordCredentials) mr.getLoginCredentials();
    assertEquals("testuser001", creds.getUsername());
    assertEquals("pwd0000abc", new String(creds.getPassword()));
    assertEquals("value1", mr.getExtraHeaders().get("header1"));
    assertEquals("pval2", mr.getExtraParameters().get("param2"));
    assertEquals("maven2", mr.getLayout());
    try {
        ArtifactCleanupFeature artifactCleanupFeature = mr.getFeature(ArtifactCleanupFeature.class);
        throw new Exception("artifactCleanupFeature should not be available");
    } catch (UnsupportedFeatureException e) {
    // correct
    }
    IndexCreationFeature indexCreationFeature = mr.getFeature(IndexCreationFeature.class);
    assertEquals("local/.index", indexCreationFeature.getIndexPath().toString());
    try {
        StagingRepositoryFeature stagingRepositoryFeature = mr.getFeature(StagingRepositoryFeature.class);
        throw new Exception("stagingRepositoryFeature should not be available");
    } catch (UnsupportedFeatureException e) {
    // correct
    }
    RemoteIndexFeature remoteIndexFeature = mr.getFeature(RemoteIndexFeature.class);
    assertNull(remoteIndexFeature.getProxyId());
}
Also used : PasswordCredentials(org.apache.archiva.repository.base.PasswordCredentials) IndexCreationFeature(org.apache.archiva.repository.features.IndexCreationFeature) UnsupportedFeatureException(org.apache.archiva.repository.UnsupportedFeatureException) HashMap(java.util.HashMap) RemoteRepositoryConfiguration(org.apache.archiva.configuration.model.RemoteRepositoryConfiguration) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) RemoteRepository(org.apache.archiva.repository.RemoteRepository) ArtifactCleanupFeature(org.apache.archiva.repository.features.ArtifactCleanupFeature) StagingRepositoryFeature(org.apache.archiva.repository.features.StagingRepositoryFeature) UnsupportedFeatureException(org.apache.archiva.repository.UnsupportedFeatureException) URISyntaxException(java.net.URISyntaxException) RepositoryException(org.apache.archiva.repository.RepositoryException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

PasswordCredentials (org.apache.archiva.repository.base.PasswordCredentials)18 RemoteIndexFeature (org.apache.archiva.repository.features.RemoteIndexFeature)17 IOException (java.io.IOException)10 URI (java.net.URI)9 RemoteRepository (org.apache.archiva.repository.RemoteRepository)8 RepositoryException (org.apache.archiva.repository.RepositoryException)7 RemoteRepositoryConfiguration (org.apache.archiva.configuration.model.RemoteRepositoryConfiguration)6 RepositoryCredentials (org.apache.archiva.repository.RepositoryCredentials)6 AuthenticationInfo (org.apache.maven.wagon.authentication.AuthenticationInfo)6 ProxyInfo (org.apache.maven.wagon.proxy.ProxyInfo)6 Path (java.nio.file.Path)5 WagonFactoryRequest (org.apache.archiva.maven.common.proxy.WagonFactoryRequest)5 IndexCreationFeature (org.apache.archiva.repository.features.IndexCreationFeature)5 IndexUpdateRequest (org.apache.maven.index.updater.IndexUpdateRequest)5 ResourceFetcher (org.apache.maven.index.updater.ResourceFetcher)5 ConnectionException (org.apache.maven.wagon.ConnectionException)5 StreamWagon (org.apache.maven.wagon.StreamWagon)5 AuthenticationException (org.apache.maven.wagon.authentication.AuthenticationException)5 AbstractHttpClientWagon (org.apache.maven.wagon.shared.http.AbstractHttpClientWagon)5 HttpConfiguration (org.apache.maven.wagon.shared.http.HttpConfiguration)5