Search in sources :

Example 6 with NetworkProxy

use of org.apache.archiva.proxy.model.NetworkProxy in project archiva by apache.

the class DefaultRemoteRepositoriesService method checkRemoteConnectivity.

@Override
public ActionStatus checkRemoteConnectivity(String repositoryId) throws ArchivaRestServiceException {
    try {
        RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository(repositoryId);
        if (remoteRepository == null) {
            log.warn("Remote repository {} does not exist. Connectivity check returns false.", repositoryId);
            return ActionStatus.FAIL;
        }
        NetworkProxy networkProxy = null;
        if (StringUtils.isNotBlank(remoteRepository.getRemoteDownloadNetworkProxyId())) {
            networkProxy = proxyRegistry.getNetworkProxy(remoteRepository.getRemoteDownloadNetworkProxyId());
            if (networkProxy == null) {
                log.warn("A network proxy {} was configured for repository {}. But the proxy with the given id does not exist.", remoteRepository.getRemoteDownloadNetworkProxyId(), repositoryId);
            }
        }
        String wagonProtocol = new URL(remoteRepository.getUrl()).getProtocol();
        final Wagon wagon = wagonFactory.getWagon(// 
        new WagonFactoryRequest(wagonProtocol, remoteRepository.getExtraHeaders()).networkProxy(networkProxy));
        // hardcoded value as it's a check of the remote repo connectivity
        wagon.setReadTimeout(checkReadTimeout);
        wagon.setTimeout(checkTimeout);
        if (wagon instanceof AbstractHttpClientWagon) {
            HttpMethodConfiguration httpMethodConfiguration = // 
            new HttpMethodConfiguration().setUsePreemptive(// 
            true).setReadTimeout(checkReadTimeout);
            HttpConfiguration httpConfiguration = new HttpConfiguration().setGet(httpMethodConfiguration);
            AbstractHttpClientWagon.class.cast(wagon).setHttpConfiguration(httpConfiguration);
        }
        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()));
        }
        String url = StringUtils.stripEnd(remoteRepository.getUrl(), "/");
        wagon.connect(new Repository(remoteRepository.getId(), url), proxyInfo);
        // MRM-1933, there are certain servers that do not allow browsing
        if (!(StringUtils.isEmpty(remoteRepository.getCheckPath()) || "/".equals(remoteRepository.getCheckPath()))) {
            return new ActionStatus(wagon.resourceExists(remoteRepository.getCheckPath()));
        } else {
            // we only check connectivity as remote repo can be empty
            // MRM-1909: Wagon implementation appends a slash already
            wagon.getFileList("");
        }
        return ActionStatus.SUCCESS;
    } catch (TransferFailedException e) {
        log.info("TransferFailedException :{}", e.getMessage());
        return ActionStatus.FAIL;
    } catch (Exception e) {
        // This service returns either true or false, Exception cannot be handled by the clients
        log.debug("Exception occured on connectivity test.", e);
        log.info("Connection exception: {}", e.getMessage());
        return ActionStatus.FAIL;
    }
}
Also used : AbstractHttpClientWagon(org.apache.maven.wagon.shared.http.AbstractHttpClientWagon) HttpMethodConfiguration(org.apache.maven.wagon.shared.http.HttpMethodConfiguration) RemoteRepository(org.apache.archiva.admin.model.beans.RemoteRepository) AbstractHttpClientWagon(org.apache.maven.wagon.shared.http.AbstractHttpClientWagon) Wagon(org.apache.maven.wagon.Wagon) HttpConfiguration(org.apache.maven.wagon.shared.http.HttpConfiguration) NetworkProxy(org.apache.archiva.proxy.model.NetworkProxy) URL(java.net.URL) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) TransferFailedException(org.apache.maven.wagon.TransferFailedException) ActionStatus(org.apache.archiva.rest.api.model.ActionStatus) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Repository(org.apache.maven.wagon.repository.Repository) RemoteRepository(org.apache.archiva.admin.model.beans.RemoteRepository) WagonFactoryRequest(org.apache.archiva.maven.common.proxy.WagonFactoryRequest) TransferFailedException(org.apache.maven.wagon.TransferFailedException)

Example 7 with NetworkProxy

use of org.apache.archiva.proxy.model.NetworkProxy in project archiva by apache.

the class Maven2RepositoryStorage method readProjectVersionMetadata.

@Override
public ProjectVersionMetadata readProjectVersionMetadata(ReadMetadataRequest readMetadataRequest) throws RepositoryStorageMetadataNotFoundException, RepositoryStorageMetadataInvalidException, RepositoryStorageRuntimeException {
    ManagedRepository managedRepository = repositoryRegistry.getManagedRepository(readMetadataRequest.getRepositoryId());
    boolean isReleases = managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE);
    boolean isSnapshots = managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT);
    String artifactVersion = readMetadataRequest.getProjectVersion();
    // olamy: in case of browsing via the ui we can mix repos (parent of a SNAPSHOT can come from release repo)
    if (!readMetadataRequest.isBrowsingRequest()) {
        if (VersionUtil.isSnapshot(artifactVersion)) {
            // skygo trying to improve speed by honoring managed configuration MRM-1658
            if (isReleases && !isSnapshots) {
                throw new RepositoryStorageRuntimeException("lookforsnaponreleaseonly", "managed repo is configured for release only");
            }
        } else {
            if (!isReleases && isSnapshots) {
                throw new RepositoryStorageRuntimeException("lookforsreleaseonsneponly", "managed repo is configured for snapshot only");
            }
        }
    }
    StorageAsset basedir = managedRepository.getRoot();
    if (VersionUtil.isSnapshot(artifactVersion)) {
        StorageAsset metadataFile = pathTranslator.toFile(basedir, readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(), artifactVersion, METADATA_FILENAME);
        try {
            ArchivaRepositoryMetadata metadata = metadataReader.read(metadataFile);
            // re-adjust to timestamp if present, otherwise retain the original -SNAPSHOT filename
            SnapshotVersion snapshotVersion = metadata.getSnapshotVersion();
            if (snapshotVersion != null) {
                artifactVersion = // remove SNAPSHOT from end
                artifactVersion.substring(0, artifactVersion.length() - 8);
                artifactVersion = artifactVersion + snapshotVersion.getTimestamp() + "-" + snapshotVersion.getBuildNumber();
            }
        } catch (RepositoryMetadataException e) {
            // unable to parse metadata - LOGGER it, and continue with the version as the original SNAPSHOT version
            log.warn("Invalid metadata: {} - {}", metadataFile, e.getMessage());
        }
    }
    // TODO: won't work well with some other layouts, might need to convert artifact parts to ID by path translator
    String id = readMetadataRequest.getProjectId() + "-" + artifactVersion + ".pom";
    StorageAsset file = pathTranslator.toFile(basedir, readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(), readMetadataRequest.getProjectVersion(), id);
    if (!file.exists()) {
        // metadata could not be resolved
        throw new RepositoryStorageMetadataNotFoundException("The artifact's POM file '" + file.getPath() + "' was missing");
    }
    // TODO: this is a workaround until we can properly resolve using proxies as well - this doesn't cache
    // anything locally!
    List<RemoteRepository> remoteRepositories = new ArrayList<>();
    Map<String, NetworkProxy> networkProxies = new HashMap<>();
    Map<String, List<ProxyConnector>> proxyConnectorsMap = proxyRegistry.getProxyConnectorAsMap();
    List<ProxyConnector> proxyConnectors = proxyConnectorsMap.get(readMetadataRequest.getRepositoryId());
    if (proxyConnectors != null) {
        for (ProxyConnector proxyConnector : proxyConnectors) {
            RemoteRepository remoteRepoConfig = repositoryRegistry.getRemoteRepository(proxyConnector.getTargetRepository().getId());
            if (remoteRepoConfig != null) {
                remoteRepositories.add(remoteRepoConfig);
                NetworkProxy networkProxyConfig = proxyRegistry.getNetworkProxy(proxyConnector.getProxyId());
                if (networkProxyConfig != null) {
                    // key/value: remote repo ID/proxy info
                    networkProxies.put(proxyConnector.getTargetRepository().getId(), networkProxyConfig);
                }
            }
        }
    }
    // can have released parent pom
    if (readMetadataRequest.isBrowsingRequest()) {
        remoteRepositories.addAll(repositoryRegistry.getRemoteRepositories());
    }
    ModelBuildingRequest req = new DefaultModelBuildingRequest().setProcessPlugins(false).setPomFile(file.getFilePath().toFile()).setTwoPhaseBuilding(false).setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
    // MRM-1607. olamy this will resolve jdk profiles on the current running archiva jvm
    req.setSystemProperties(System.getProperties());
    // MRM-1411
    req.setModelResolver(new RepositoryModelResolver(managedRepository, pathTranslator, wagonFactory, remoteRepositories, networkProxies, managedRepository, mavenSystemManager, metadataReader));
    Model model;
    try {
        model = builder.build(req).getEffectiveModel();
    } catch (ModelBuildingException e) {
        String msg = "The artifact's POM file '" + file + "' was invalid: " + e.getMessage();
        List<ModelProblem> modelProblems = e.getProblems();
        for (ModelProblem problem : modelProblems) {
            // but setTwoPhaseBuilding(true) fix that
            if (((problem.getException() instanceof FileNotFoundException || problem.getException() instanceof NoSuchFileException) && e.getModelId() != null && !e.getModelId().equals(problem.getModelId()))) {
                log.warn("The artifact's parent POM file '{}' cannot be resolved. " + "Using defaults for project version metadata..", file);
                ProjectVersionMetadata metadata = new ProjectVersionMetadata();
                metadata.setId(readMetadataRequest.getProjectVersion());
                MavenProjectFacet facet = new MavenProjectFacet();
                facet.setGroupId(readMetadataRequest.getNamespace());
                facet.setArtifactId(readMetadataRequest.getProjectId());
                facet.setPackaging("jar");
                metadata.addFacet(facet);
                String errMsg = "Error in resolving artifact's parent POM file. " + (problem.getException() == null ? problem.getMessage() : problem.getException().getMessage());
                RepositoryProblemFacet repoProblemFacet = new RepositoryProblemFacet();
                repoProblemFacet.setRepositoryId(readMetadataRequest.getRepositoryId());
                repoProblemFacet.setId(readMetadataRequest.getRepositoryId());
                repoProblemFacet.setMessage(errMsg);
                repoProblemFacet.setProblem(errMsg);
                repoProblemFacet.setProject(readMetadataRequest.getProjectId());
                repoProblemFacet.setVersion(readMetadataRequest.getProjectVersion());
                repoProblemFacet.setNamespace(readMetadataRequest.getNamespace());
                metadata.addFacet(repoProblemFacet);
                return metadata;
            }
        }
        throw new RepositoryStorageMetadataInvalidException("invalid-pom", msg, e);
    }
    // Check if the POM is in the correct location
    boolean correctGroupId = readMetadataRequest.getNamespace().equals(model.getGroupId());
    boolean correctArtifactId = readMetadataRequest.getProjectId().equals(model.getArtifactId());
    boolean correctVersion = readMetadataRequest.getProjectVersion().equals(model.getVersion());
    if (!correctGroupId || !correctArtifactId || !correctVersion) {
        StringBuilder message = new StringBuilder("Incorrect POM coordinates in '" + file + "':");
        if (!correctGroupId) {
            message.append("\nIncorrect group ID: ").append(model.getGroupId());
        }
        if (!correctArtifactId) {
            message.append("\nIncorrect artifact ID: ").append(model.getArtifactId());
        }
        if (!correctVersion) {
            message.append("\nIncorrect version: ").append(model.getVersion());
        }
        throw new RepositoryStorageMetadataInvalidException("mislocated-pom", message.toString());
    }
    ProjectVersionMetadata metadata = new ProjectVersionMetadata();
    metadata.setCiManagement(convertCiManagement(model.getCiManagement()));
    metadata.setDescription(model.getDescription());
    metadata.setId(readMetadataRequest.getProjectVersion());
    metadata.setIssueManagement(convertIssueManagement(model.getIssueManagement()));
    metadata.setLicenses(convertLicenses(model.getLicenses()));
    metadata.setMailingLists(convertMailingLists(model.getMailingLists()));
    metadata.setDependencies(convertDependencies(model.getDependencies()));
    metadata.setName(model.getName());
    metadata.setOrganization(convertOrganization(model.getOrganization()));
    metadata.setScm(convertScm(model.getScm()));
    metadata.setUrl(model.getUrl());
    metadata.setProperties(new HashMap<String, String>((Map) model.getProperties()));
    MavenProjectFacet facet = new MavenProjectFacet();
    facet.setGroupId(model.getGroupId() != null ? model.getGroupId() : model.getParent().getGroupId());
    facet.setArtifactId(model.getArtifactId());
    facet.setPackaging(model.getPackaging());
    if (model.getParent() != null) {
        MavenProjectParent parent = new MavenProjectParent();
        parent.setGroupId(model.getParent().getGroupId());
        parent.setArtifactId(model.getParent().getArtifactId());
        parent.setVersion(model.getParent().getVersion());
        facet.setParent(parent);
    }
    metadata.addFacet(facet);
    return metadata;
}
Also used : ModelBuildingRequest(org.apache.maven.model.building.ModelBuildingRequest) DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) RepositoryStorageRuntimeException(org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException) ManagedRepository(org.apache.archiva.repository.ManagedRepository) HashMap(java.util.HashMap) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) RemoteRepository(org.apache.archiva.repository.RemoteRepository) NetworkProxy(org.apache.archiva.proxy.model.NetworkProxy) RepositoryStorageMetadataInvalidException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException) RepositoryStorageMetadataNotFoundException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException) RepositoryMetadataException(org.apache.archiva.repository.metadata.RepositoryMetadataException) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException) List(java.util.List) ArrayList(java.util.ArrayList) MailingList(org.apache.maven.model.MailingList) ModelProblem(org.apache.maven.model.building.ModelProblem) ProxyConnector(org.apache.archiva.proxy.model.ProxyConnector) RepositoryProblemFacet(org.apache.archiva.metadata.model.facets.RepositoryProblemFacet) DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) Model(org.apache.maven.model.Model) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with NetworkProxy

use of org.apache.archiva.proxy.model.NetworkProxy in project archiva by apache.

the class RepositoryModelResolver method connectToRepository.

/**
 * Using wagon, connect to the remote repository.
 *
 * @param wagon the wagon instance to establish the connection on.
 * @return true if the connection was successful. false if not connected.
 */
private boolean connectToRepository(Wagon wagon, RemoteRepository remoteRepository) {
    boolean connected;
    final NetworkProxy proxyConnector = this.networkProxyMap.get(remoteRepository.getId());
    ProxyInfo networkProxy = null;
    if (proxyConnector != null) {
        networkProxy = new ProxyInfo();
        networkProxy.setType(proxyConnector.getProtocol());
        networkProxy.setHost(proxyConnector.getHost());
        networkProxy.setPort(proxyConnector.getPort());
        networkProxy.setUserName(proxyConnector.getUsername());
        networkProxy.setPassword(new String(proxyConnector.getPassword()));
        String msg = "Using network proxy " + networkProxy.getHost() + ":" + networkProxy.getPort() + " to connect to remote repository " + remoteRepository.getLocation();
        if (networkProxy.getNonProxyHosts() != null) {
            msg += "; excluding hosts: " + networkProxy.getNonProxyHosts();
        }
        if (StringUtils.isNotBlank(networkProxy.getUserName())) {
            msg += "; as user: " + networkProxy.getUserName();
        }
        log.debug(msg);
    }
    AuthenticationInfo authInfo = null;
    RepositoryCredentials creds = remoteRepository.getLoginCredentials();
    String username = "";
    String password = "";
    if (creds instanceof UsernamePasswordCredentials) {
        UsernamePasswordCredentials c = (UsernamePasswordCredentials) creds;
        username = c.getUserName();
        password = c.getPassword();
    }
    if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
        log.debug("Using username {} to connect to remote repository {}", username, remoteRepository.getLocation());
        authInfo = new AuthenticationInfo();
        authInfo.setUserName(username);
        authInfo.setPassword(password);
    }
    int timeoutInMilliseconds = ((int) remoteRepository.getTimeout().getSeconds()) * 1000;
    // FIXME olamy having 2 config values
    // Set timeout
    wagon.setReadTimeout(timeoutInMilliseconds);
    wagon.setTimeout(timeoutInMilliseconds);
    try {
        org.apache.maven.wagon.repository.Repository wagonRepository = new org.apache.maven.wagon.repository.Repository(remoteRepository.getId(), remoteRepository.getLocation().toString());
        if (networkProxy != null) {
            wagon.connect(wagonRepository, authInfo, networkProxy);
        } else {
            wagon.connect(wagonRepository, authInfo);
        }
        connected = true;
    } catch (ConnectionException | AuthenticationException e) {
        log.error("Could not connect to {}:{} ", remoteRepository.getName(), e.getMessage());
        connected = false;
    }
    return connected;
}
Also used : RepositoryCredentials(org.apache.archiva.repository.RepositoryCredentials) AuthenticationException(org.apache.maven.wagon.authentication.AuthenticationException) NetworkProxy(org.apache.archiva.proxy.model.NetworkProxy) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Repository(org.apache.maven.model.Repository) RemoteRepository(org.apache.archiva.repository.RemoteRepository) ManagedRepository(org.apache.archiva.repository.ManagedRepository) ConnectionException(org.apache.maven.wagon.ConnectionException)

Example 9 with NetworkProxy

use of org.apache.archiva.proxy.model.NetworkProxy in project archiva by apache.

the class RepositoryModelResolver method getModelFromProxy.

// FIXME: we need to do some refactoring, we cannot re-use the proxy components of archiva-proxy in maven2-repository
// because it's causing a cyclic dependency
private boolean getModelFromProxy(RemoteRepository remoteRepository, String groupId, String artifactId, String version, String filename) throws AuthorizationException, TransferFailedException, ResourceDoesNotExistException, WagonFactoryException, IOException, RepositoryMetadataException {
    boolean success = false;
    Path tmpMd5 = null;
    Path tmpSha1 = null;
    Path tmpResource = null;
    String artifactPath = pathTranslator.toPath(groupId, artifactId, version, filename);
    Path resource = targetRepository.getRoot().getFilePath().resolve(artifactPath);
    Path workingDirectory = createWorkingDirectory(targetRepository.getLocation().toString());
    try {
        Wagon wagon = null;
        try {
            String protocol = getProtocol(remoteRepository.getLocation().toString());
            final NetworkProxy networkProxy = this.networkProxyMap.get(remoteRepository.getId());
            wagon = wagonFactory.getWagon(new WagonFactoryRequest("wagon#" + protocol, remoteRepository.getExtraHeaders()).networkProxy(networkProxy));
            if (wagon == null) {
                throw new RuntimeException("Unsupported remote repository protocol: " + protocol);
            }
            boolean connected = connectToRepository(wagon, remoteRepository);
            if (connected) {
                tmpResource = workingDirectory.resolve(filename);
                if (VersionUtil.isSnapshot(version)) {
                    // get the metadata first!
                    Path tmpMetadataResource = workingDirectory.resolve(METADATA_FILENAME);
                    String metadataPath = StringUtils.substringBeforeLast(artifactPath, "/") + "/" + METADATA_FILENAME;
                    wagon.get(addParameters(metadataPath, remoteRepository), tmpMetadataResource.toFile());
                    log.debug("Successfully downloaded metadata.");
                    ArchivaRepositoryMetadata metadata = metadataReader.read(tmpMetadataResource);
                    // re-adjust to timestamp if present, otherwise retain the original -SNAPSHOT filename
                    SnapshotVersion snapshotVersion = metadata.getSnapshotVersion();
                    String timestampVersion = version;
                    if (snapshotVersion != null) {
                        timestampVersion = timestampVersion.substring(0, timestampVersion.length() - // remove SNAPSHOT from end
                        8);
                        timestampVersion = timestampVersion + snapshotVersion.getTimestamp() + "-" + snapshotVersion.getBuildNumber();
                        filename = artifactId + "-" + timestampVersion + ".pom";
                        artifactPath = pathTranslator.toPath(groupId, artifactId, version, filename);
                        log.debug("New artifactPath :{}", artifactPath);
                    }
                }
                log.info("Retrieving {} from {}", artifactPath, remoteRepository.getName());
                wagon.get(addParameters(artifactPath, remoteRepository), tmpResource.toFile());
                log.debug("Downloaded successfully.");
                tmpSha1 = transferChecksum(wagon, remoteRepository, artifactPath, tmpResource, workingDirectory, ".sha1");
                tmpMd5 = transferChecksum(wagon, remoteRepository, artifactPath, tmpResource, workingDirectory, ".md5");
            }
        } finally {
            if (wagon != null) {
                try {
                    wagon.disconnect();
                } catch (ConnectionException e) {
                    log.warn("Unable to disconnect wagon.", e);
                }
            }
        }
        if (resource != null) {
            synchronized (resource.toAbsolutePath().toString().intern()) {
                Path directory = resource.getParent();
                moveFileIfExists(tmpMd5, directory);
                moveFileIfExists(tmpSha1, directory);
                moveFileIfExists(tmpResource, directory);
                success = true;
            }
        }
    } finally {
        org.apache.archiva.common.utils.FileUtils.deleteQuietly(workingDirectory);
    }
    return success;
}
Also used : Path(java.nio.file.Path) WagonFactoryRequest(org.apache.archiva.maven.common.proxy.WagonFactoryRequest) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) Wagon(org.apache.maven.wagon.Wagon) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) NetworkProxy(org.apache.archiva.proxy.model.NetworkProxy) ConnectionException(org.apache.maven.wagon.ConnectionException)

Example 10 with NetworkProxy

use of org.apache.archiva.proxy.model.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);
        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)

Aggregations

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