Search in sources :

Example 11 with RemoteIndexFeature

use of org.apache.archiva.repository.features.RemoteIndexFeature in project archiva by apache.

the class ArchivaIndexManagerMock method createRemoteContext.

private IndexingContext createRemoteContext(RemoteRepository remoteRepository) throws IOException {
    Path appServerBase = archivaConfiguration.getAppServerBaseDir();
    String contextKey = "remote-" + remoteRepository.getId();
    // create remote repository path
    Path repoDir = remoteRepository.getLocalPath();
    if (!Files.exists(repoDir)) {
        Files.createDirectories(repoDir);
    }
    Path indexDirectory = null;
    // is there configured indexDirectory ?
    if (remoteRepository.supportsFeature(RemoteIndexFeature.class)) {
        RemoteIndexFeature rif = remoteRepository.getFeature(RemoteIndexFeature.class).get();
        indexDirectory = getIndexPath(remoteRepository);
        String remoteIndexUrl = calculateIndexRemoteUrl(remoteRepository.getLocation(), rif);
        try {
            return getIndexingContext(remoteRepository, contextKey, repoDir, indexDirectory, remoteIndexUrl);
        } catch (IndexFormatTooOldException e) {
            // existing index with an old lucene format so we need to delete it!!!
            // delete it first then recreate it.
            // 
            log.warn(// 
            "the index of repository {} is too old we have to delete and recreate it", remoteRepository.getId());
            org.apache.archiva.common.utils.FileUtils.deleteDirectory(indexDirectory);
            return getIndexingContext(remoteRepository, contextKey, repoDir, indexDirectory, remoteIndexUrl);
        }
    } else {
        throw new IOException("No remote index defined");
    }
}
Also used : Path(java.nio.file.Path) IndexFormatTooOldException(org.apache.maven.index_shaded.lucene.index.IndexFormatTooOldException) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) IOException(java.io.IOException)

Example 12 with RemoteIndexFeature

use of org.apache.archiva.repository.features.RemoteIndexFeature 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 RemoteIndexFeature

use of org.apache.archiva.repository.features.RemoteIndexFeature 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)

Example 14 with RemoteIndexFeature

use of org.apache.archiva.repository.features.RemoteIndexFeature in project archiva by apache.

the class DefaultDownloadRemoteIndexScheduler method scheduleDownloadRemote.

@Override
public void scheduleDownloadRemote(String repositoryId, boolean now, boolean fullDownload) throws DownloadRemoteIndexException {
    try {
        org.apache.archiva.repository.RemoteRepository remoteRepo = repositoryRegistry.getRemoteRepository(repositoryId);
        if (remoteRepo == null) {
            log.warn("ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId);
            return;
        }
        if (!remoteRepo.supportsFeature(RemoteIndexFeature.class)) {
            log.warn("ignore scheduleDownloadRemote for repo with id {}. Does not support remote index.", repositoryId);
            return;
        }
        RemoteIndexFeature rif = remoteRepo.getFeature(RemoteIndexFeature.class).get();
        NetworkProxy networkProxy = null;
        if (StringUtils.isNotBlank(rif.getProxyId())) {
            networkProxy = networkProxyAdmin.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());
            }
        }
        DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest = // 
        new DownloadRemoteIndexTaskRequest().setRemoteRepository(// 
        remoteRepo).setNetworkProxy(// 
        networkProxy).setFullDownload(// 
        fullDownload).setWagonFactory(// 
        wagonFactory).setIndexUpdater(// 
        indexUpdater).setIndexPacker(this.indexPacker);
        if (now) {
            log.info("schedule download remote index for repository {}", remoteRepo.getId());
            // do it now
            taskScheduler.schedule(new DownloadRemoteIndexTask(downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds), new Date());
        } else {
            log.info("schedule download remote index for repository {} with cron expression {}", remoteRepo.getId(), remoteRepo.getSchedulingDefinition());
            try {
                CronTrigger cronTrigger = new CronTrigger(remoteRepo.getSchedulingDefinition());
                taskScheduler.schedule(new DownloadRemoteIndexTask(downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds), cronTrigger);
            } catch (IllegalArgumentException e) {
                log.warn("Unable to schedule remote index download: {}", e.getLocalizedMessage());
            }
            if (rif.isDownloadRemoteIndexOnStartup()) {
                log.info("remote repository {} configured with downloadRemoteIndexOnStartup schedule now a download", remoteRepo.getId());
                taskScheduler.schedule(new DownloadRemoteIndexTask(downloadRemoteIndexTaskRequest, this.runningRemoteDownloadIds), new Date());
            }
        }
    } catch (RepositoryAdminException e) {
        log.error(e.getMessage(), e);
        throw new DownloadRemoteIndexException(e.getMessage(), e);
    }
}
Also used : CronTrigger(org.springframework.scheduling.support.CronTrigger) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy) Date(java.util.Date)

Example 15 with RemoteIndexFeature

use of org.apache.archiva.repository.features.RemoteIndexFeature 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).get();
        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.PasswordCredentials) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) RepositoryException(org.apache.archiva.repository.RepositoryException) URI(java.net.URI) RepositoryException(org.apache.archiva.repository.RepositoryException)

Aggregations

RemoteIndexFeature (org.apache.archiva.repository.features.RemoteIndexFeature)20 PasswordCredentials (org.apache.archiva.repository.PasswordCredentials)12 URI (java.net.URI)8 Path (java.nio.file.Path)8 IOException (java.io.IOException)7 RemoteRepository (org.apache.archiva.repository.RemoteRepository)7 RepositoryException (org.apache.archiva.repository.RepositoryException)6 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)5 NetworkProxy (org.apache.archiva.admin.model.beans.NetworkProxy)5 RemoteRepositoryConfiguration (org.apache.archiva.configuration.RemoteRepositoryConfiguration)5 IndexCreationFeature (org.apache.archiva.repository.features.IndexCreationFeature)5 WagonFactoryRequest (org.apache.archiva.proxy.common.WagonFactoryRequest)4 RepositoryCredentials (org.apache.archiva.repository.RepositoryCredentials)4 IndexUpdateRequest (org.apache.maven.index.updater.IndexUpdateRequest)4 ResourceFetcher (org.apache.maven.index.updater.ResourceFetcher)4 StreamWagon (org.apache.maven.wagon.StreamWagon)4 AuthenticationInfo (org.apache.maven.wagon.authentication.AuthenticationInfo)4 ProxyInfo (org.apache.maven.wagon.proxy.ProxyInfo)4 AbstractHttpClientWagon (org.apache.maven.wagon.shared.http.AbstractHttpClientWagon)4 HttpConfiguration (org.apache.maven.wagon.shared.http.HttpConfiguration)4