Search in sources :

Example 11 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)) {
                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);
                if (StringUtils.isNotBlank(rif.getProxyId())) {
                    networkProxy = proxyRegistry.getNetworkProxy(rif.getProxyId());
                    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(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 12 with PasswordCredentials

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

the class RepositoryProviderMock method getRemoteConfiguration.

@Override
public RemoteRepositoryConfiguration getRemoteConfiguration(RemoteRepository remoteRepository) throws RepositoryException {
    RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration();
    configuration.setId(remoteRepository.getId());
    configuration.setName(remoteRepository.getName());
    configuration.setDescription(remoteRepository.getDescription());
    configuration.setLayout(remoteRepository.getLayout());
    configuration.setRefreshCronExpression(remoteRepository.getSchedulingDefinition());
    configuration.setCheckPath(remoteRepository.getCheckPath());
    configuration.setExtraHeaders(remoteRepository.getExtraHeaders());
    configuration.setExtraParameters(remoteRepository.getExtraParameters());
    configuration.setTimeout((int) remoteRepository.getTimeout().getSeconds());
    RepositoryCredentials creds = remoteRepository.getLoginCredentials();
    if (creds != null) {
        PasswordCredentials pwdCreds = (PasswordCredentials) creds;
        configuration.setUsername(pwdCreds.getUsername());
        configuration.setPassword(new String(pwdCreds.getPassword()));
    }
    configuration.setUrl(remoteRepository.getLocation() == null ? "" : remoteRepository.getLocation().toString());
    RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class);
    configuration.setDownloadRemoteIndex(rif.isDownloadRemoteIndex());
    configuration.setDownloadRemoteIndexOnStartup(rif.isDownloadRemoteIndexOnStartup());
    configuration.setIndexDir(rif.getIndexUri() == null ? "" : rif.getIndexUri().toString());
    configuration.setRemoteDownloadNetworkProxyId(rif.getProxyId());
    return configuration;
}
Also used : PasswordCredentials(org.apache.archiva.repository.base.PasswordCredentials) RemoteRepositoryConfiguration(org.apache.archiva.configuration.model.RemoteRepositoryConfiguration) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature)

Example 13 with PasswordCredentials

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

the class RepositoryProviderMock method updateRemoteInstance.

@SuppressWarnings("unchecked")
@Override
public void updateRemoteInstance(EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration) throws RepositoryException {
    try {
        remoteRepository.setName(remoteRepository.getPrimaryLocale(), configuration.getName());
        remoteRepository.setBaseUri(new URI(""));
        remoteRepository.setDescription(remoteRepository.getPrimaryLocale(), configuration.getDescription());
        remoteRepository.setLayout(configuration.getLayout());
        remoteRepository.setSchedulingDefinition(configuration.getRefreshCronExpression());
        remoteRepository.setCheckPath(configuration.getCheckPath());
        remoteRepository.setExtraHeaders(configuration.getExtraHeaders());
        remoteRepository.setExtraParameters(configuration.getExtraParameters());
        remoteRepository.setTimeout(Duration.ofSeconds(configuration.getTimeout()));
        char[] pwd = configuration.getPassword() == null ? "".toCharArray() : configuration.getPassword().toCharArray();
        remoteRepository.setCredentials(new PasswordCredentials(configuration.getUsername(), pwd));
        remoteRepository.setLocation(new URI(configuration.getUrl() == null ? "" : configuration.getUrl()));
        RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class);
        rif.setDownloadRemoteIndexOnStartup(configuration.isDownloadRemoteIndexOnStartup());
        rif.setDownloadRemoteIndex(configuration.isDownloadRemoteIndex());
        rif.setIndexUri(new URI(configuration.getIndexDir()));
        rif.setDownloadTimeout(Duration.ofSeconds(configuration.getRemoteDownloadTimeout()));
        rif.setProxyId(configuration.getRemoteDownloadNetworkProxyId());
    } catch (Exception e) {
        throw new RepositoryException("Error", e);
    }
}
Also used : PasswordCredentials(org.apache.archiva.repository.base.PasswordCredentials) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) URI(java.net.URI) IOException(java.io.IOException)

Example 14 with PasswordCredentials

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

the class MavenRepositoryProvider method updateRemoteInstance.

@Override
public void updateRemoteInstance(EditableRemoteRepository repo, RemoteRepositoryConfiguration cfg) throws RepositoryException {
    setBaseConfig(repo, cfg);
    repo.setCheckPath(cfg.getCheckPath());
    repo.setSchedulingDefinition(cfg.getRefreshCronExpression());
    try {
        repo.setLocation(new URI(cfg.getUrl()));
    } catch (UnsupportedURIException | URISyntaxException e) {
        log.error("Could not set remote url " + cfg.getUrl());
        throw new RepositoryException("The url config is not a valid uri: " + cfg.getUrl());
    }
    repo.setTimeout(Duration.ofSeconds(cfg.getTimeout()));
    RemoteIndexFeature remoteIndexFeature = repo.getFeature(RemoteIndexFeature.class);
    remoteIndexFeature.setDownloadRemoteIndex(cfg.isDownloadRemoteIndex());
    remoteIndexFeature.setDownloadRemoteIndexOnStartup(cfg.isDownloadRemoteIndexOnStartup());
    remoteIndexFeature.setDownloadTimeout(Duration.ofSeconds(cfg.getRemoteDownloadTimeout()));
    remoteIndexFeature.setProxyId(cfg.getRemoteDownloadNetworkProxyId());
    if (cfg.isDownloadRemoteIndex()) {
        try {
            remoteIndexFeature.setIndexUri(new URI(cfg.getRemoteIndexUrl()));
        } catch (URISyntaxException e) {
            log.error("Could not set remote index url " + cfg.getRemoteIndexUrl());
            remoteIndexFeature.setDownloadRemoteIndex(false);
            remoteIndexFeature.setDownloadRemoteIndexOnStartup(false);
        }
    }
    for (Object key : cfg.getExtraHeaders().keySet()) {
        repo.addExtraHeader(key.toString(), cfg.getExtraHeaders().get(key).toString());
    }
    for (Object key : cfg.getExtraParameters().keySet()) {
        repo.addExtraParameter(key.toString(), cfg.getExtraParameters().get(key).toString());
    }
    PasswordCredentials credentials = new PasswordCredentials("", new char[0]);
    if (cfg.getPassword() != null && cfg.getUsername() != null) {
        credentials.setPassword(cfg.getPassword().toCharArray());
        credentials.setUsername(cfg.getUsername());
        repo.setCredentials(credentials);
    } else {
        credentials.setPassword(new char[0]);
    }
    IndexCreationFeature indexCreationFeature = repo.getFeature(IndexCreationFeature.class);
    if (!StringUtils.isEmpty(cfg.getIndexDir())) {
        indexCreationFeature.setIndexPath(getURIFromString(cfg.getIndexDir()));
    }
    if (!StringUtils.isEmpty(cfg.getPackedIndexDir())) {
        indexCreationFeature.setPackedIndexPath(getURIFromString(cfg.getPackedIndexDir()));
    }
    log.debug("Updated remote instance {}", repo);
}
Also used : PasswordCredentials(org.apache.archiva.repository.base.PasswordCredentials) IndexCreationFeature(org.apache.archiva.repository.features.IndexCreationFeature) UnsupportedURIException(org.apache.archiva.repository.UnsupportedURIException) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) RepositoryException(org.apache.archiva.repository.RepositoryException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 15 with PasswordCredentials

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

the class RepositoryProviderMock method updateRemoteInstance.

@SuppressWarnings("unchecked")
@Override
public void updateRemoteInstance(EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration) throws RepositoryException {
    try {
        remoteRepository.setName(remoteRepository.getPrimaryLocale(), configuration.getName());
        remoteRepository.setBaseUri(new URI(""));
        remoteRepository.setDescription(remoteRepository.getPrimaryLocale(), configuration.getDescription());
        remoteRepository.setLayout(configuration.getLayout());
        remoteRepository.setSchedulingDefinition(configuration.getRefreshCronExpression());
        remoteRepository.setCheckPath(configuration.getCheckPath());
        remoteRepository.setExtraHeaders(configuration.getExtraHeaders());
        remoteRepository.setExtraParameters(configuration.getExtraParameters());
        remoteRepository.setTimeout(Duration.ofSeconds(configuration.getTimeout()));
        char[] pwd = configuration.getPassword() == null ? "".toCharArray() : configuration.getPassword().toCharArray();
        remoteRepository.setCredentials(new PasswordCredentials(configuration.getUsername(), pwd));
        remoteRepository.setLocation(new URI(configuration.getUrl() == null ? "" : configuration.getUrl()));
        RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class);
        rif.setDownloadRemoteIndexOnStartup(configuration.isDownloadRemoteIndexOnStartup());
        rif.setDownloadRemoteIndex(configuration.isDownloadRemoteIndex());
        rif.setIndexUri(new URI(configuration.getIndexDir()));
        rif.setDownloadTimeout(Duration.ofSeconds(configuration.getRemoteDownloadTimeout()));
        rif.setProxyId(configuration.getRemoteDownloadNetworkProxyId());
        IndexCreationFeature icf = remoteRepository.getFeature(IndexCreationFeature.class);
        icf.setIndexPath(new URI(".index"));
    } catch (Exception e) {
        throw new RepositoryException("Error while updating remote instance: " + e.getMessage(), e);
    }
}
Also used : PasswordCredentials(org.apache.archiva.repository.base.PasswordCredentials) IndexCreationFeature(org.apache.archiva.repository.features.IndexCreationFeature) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) RepositoryException(org.apache.archiva.repository.RepositoryException) URI(java.net.URI) RepositoryException(org.apache.archiva.repository.RepositoryException) IOException(java.io.IOException)

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