Search in sources :

Example 11 with WagonFactoryRequest

use of org.apache.archiva.maven.common.proxy.WagonFactoryRequest 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 12 with WagonFactoryRequest

use of org.apache.archiva.maven.common.proxy.WagonFactoryRequest in project archiva by apache.

the class Maven2RepositoryMetadataResolverMRM1411Test method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    c = new Configuration();
    testRepo = new ManagedRepositoryConfiguration();
    testRepo.setId(TEST_REPO_ID);
    testRepo.setLocation(Paths.get("target/test-repository").toAbsolutePath().toString());
    testRepo.setReleases(true);
    testRepo.setSnapshots(true);
    c.addManagedRepository(testRepo);
    RemoteRepositoryConfiguration testRemoteRepo = new RemoteRepositoryConfiguration();
    testRemoteRepo.setId(TEST_REMOTE_REPO_ID);
    testRemoteRepo.setLayout("default");
    testRemoteRepo.setName("Central Repository");
    testRemoteRepo.setUrl("http://central.repo.com/maven2");
    testRemoteRepo.setTimeout(10);
    c.addRemoteRepository(testRemoteRepo);
    ProxyConnectorConfiguration proxyConnector = new ProxyConnectorConfiguration();
    proxyConnector.setSourceRepoId(TEST_REPO_ID);
    proxyConnector.setTargetRepoId(TEST_REMOTE_REPO_ID);
    proxyConnector.setDisabled(false);
    c.addProxyConnector(proxyConnector);
    configuration.save(c);
    repositoryRegistry.reload();
    assertTrue(c.getManagedRepositories().get(0).isSnapshots());
    assertTrue(c.getManagedRepositories().get(0).isReleases());
    wagonFactory = mock(WagonFactory.class);
    assertNotNull(storage);
    storage.setWagonFactory(wagonFactory);
    Wagon wagon = new MockWagon();
    when(wagonFactory.getWagon(new WagonFactoryRequest("wagon#http", new HashMap<String, String>()))).thenReturn(wagon);
}
Also used : WagonFactoryRequest(org.apache.archiva.maven.common.proxy.WagonFactoryRequest) ArchivaConfiguration(org.apache.archiva.configuration.provider.ArchivaConfiguration) ManagedRepositoryConfiguration(org.apache.archiva.configuration.model.ManagedRepositoryConfiguration) ProxyConnectorConfiguration(org.apache.archiva.configuration.model.ProxyConnectorConfiguration) Configuration(org.apache.archiva.configuration.model.Configuration) RemoteRepositoryConfiguration(org.apache.archiva.configuration.model.RemoteRepositoryConfiguration) ContextConfiguration(org.springframework.test.context.ContextConfiguration) ManagedRepositoryConfiguration(org.apache.archiva.configuration.model.ManagedRepositoryConfiguration) HashMap(java.util.HashMap) RemoteRepositoryConfiguration(org.apache.archiva.configuration.model.RemoteRepositoryConfiguration) ProxyConnectorConfiguration(org.apache.archiva.configuration.model.ProxyConnectorConfiguration) WagonFactory(org.apache.archiva.maven.common.proxy.WagonFactory) Wagon(org.apache.maven.wagon.Wagon) Before(org.junit.Before)

Aggregations

WagonFactoryRequest (org.apache.archiva.maven.common.proxy.WagonFactoryRequest)12 NetworkProxy (org.apache.archiva.proxy.model.NetworkProxy)7 Wagon (org.apache.maven.wagon.Wagon)7 Path (java.nio.file.Path)6 ConnectionException (org.apache.maven.wagon.ConnectionException)6 ProxyInfo (org.apache.maven.wagon.proxy.ProxyInfo)6 AbstractHttpClientWagon (org.apache.maven.wagon.shared.http.AbstractHttpClientWagon)6 HttpConfiguration (org.apache.maven.wagon.shared.http.HttpConfiguration)6 HttpMethodConfiguration (org.apache.maven.wagon.shared.http.HttpMethodConfiguration)6 IOException (java.io.IOException)5 URI (java.net.URI)5 WagonFactoryException (org.apache.archiva.maven.common.proxy.WagonFactoryException)5 RemoteRepository (org.apache.archiva.repository.RemoteRepository)5 PasswordCredentials (org.apache.archiva.repository.base.PasswordCredentials)5 RemoteIndexFeature (org.apache.archiva.repository.features.RemoteIndexFeature)5 IndexUpdateRequest (org.apache.maven.index.updater.IndexUpdateRequest)5 ResourceFetcher (org.apache.maven.index.updater.ResourceFetcher)5 StreamWagon (org.apache.maven.wagon.StreamWagon)5 AuthenticationInfo (org.apache.maven.wagon.authentication.AuthenticationInfo)5 MalformedURLException (java.net.MalformedURLException)4