Search in sources :

Example 1 with Wagon

use of org.apache.maven.wagon.Wagon 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);
    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.proxy.common.WagonFactoryRequest) ArchivaConfiguration(org.apache.archiva.configuration.ArchivaConfiguration) ProxyConnectorConfiguration(org.apache.archiva.configuration.ProxyConnectorConfiguration) ManagedRepositoryConfiguration(org.apache.archiva.configuration.ManagedRepositoryConfiguration) RemoteRepositoryConfiguration(org.apache.archiva.configuration.RemoteRepositoryConfiguration) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Configuration(org.apache.archiva.configuration.Configuration) ManagedRepositoryConfiguration(org.apache.archiva.configuration.ManagedRepositoryConfiguration) HashMap(java.util.HashMap) RemoteRepositoryConfiguration(org.apache.archiva.configuration.RemoteRepositoryConfiguration) ProxyConnectorConfiguration(org.apache.archiva.configuration.ProxyConnectorConfiguration) WagonFactory(org.apache.archiva.proxy.common.WagonFactory) Wagon(org.apache.maven.wagon.Wagon) Before(org.junit.Before)

Example 2 with Wagon

use of org.apache.maven.wagon.Wagon in project archiva by apache.

the class Maven2RepositoryMetadataResolverTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    c = new Configuration();
    c.setVersion("2.0");
    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);
    RepositoryScanningConfiguration scCfg = new RepositoryScanningConfiguration();
    c.setRepositoryScanning(scCfg);
    configuration.save(c);
    assertFalse(configuration.isDefaulted());
    repositoryRegistry.reload();
    assertTrue(c.getManagedRepositories().get(0).isSnapshots());
    assertTrue(c.getManagedRepositories().get(0).isReleases());
    wagonFactory = mock(WagonFactory.class);
    storage.setWagonFactory(wagonFactory);
    Wagon wagon = new MockWagon();
    when(wagonFactory.getWagon(new WagonFactoryRequest().protocol("wagon#http"))).thenReturn(wagon);
}
Also used : WagonFactoryRequest(org.apache.archiva.proxy.common.WagonFactoryRequest) RepositoryScanningConfiguration(org.apache.archiva.configuration.RepositoryScanningConfiguration) ArchivaConfiguration(org.apache.archiva.configuration.ArchivaConfiguration) ProxyConnectorConfiguration(org.apache.archiva.configuration.ProxyConnectorConfiguration) ManagedRepositoryConfiguration(org.apache.archiva.configuration.ManagedRepositoryConfiguration) RemoteRepositoryConfiguration(org.apache.archiva.configuration.RemoteRepositoryConfiguration) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Configuration(org.apache.archiva.configuration.Configuration) ManagedRepositoryConfiguration(org.apache.archiva.configuration.ManagedRepositoryConfiguration) RemoteRepositoryConfiguration(org.apache.archiva.configuration.RemoteRepositoryConfiguration) RepositoryScanningConfiguration(org.apache.archiva.configuration.RepositoryScanningConfiguration) ProxyConnectorConfiguration(org.apache.archiva.configuration.ProxyConnectorConfiguration) WagonFactory(org.apache.archiva.proxy.common.WagonFactory) Wagon(org.apache.maven.wagon.Wagon) Before(org.junit.Before)

Example 3 with Wagon

use of org.apache.maven.wagon.Wagon 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, XMLException, IOException {
    boolean success = false;
    Path tmpMd5 = null;
    Path tmpSha1 = null;
    Path tmpResource = null;
    String artifactPath = pathTranslator.toPath(groupId, artifactId, version, filename);
    Path resource = Paths.get(targetRepository.getLocation()).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 = MavenMetadataReader.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.proxy.common.WagonFactoryRequest) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) Wagon(org.apache.maven.wagon.Wagon) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy) ConnectionException(org.apache.maven.wagon.ConnectionException)

Example 4 with Wagon

use of org.apache.maven.wagon.Wagon in project archiva by apache.

the class DefaultRepositoryProxyConnectors method transferResources.

/**
 * @param connector
 * @param remoteRepository
 * @param tmpMd5
 * @param tmpSha1
 * @param tmpResource
 * @param url
 * @param remotePath
 * @param resource
 * @param workingDirectory
 * @param repository
 * @throws ProxyException
 * @throws NotModifiedException
 * @throws org.apache.archiva.admin.model.RepositoryAdminException
 */
protected void transferResources(ProxyConnector connector, RemoteRepositoryContent remoteRepository, Path tmpMd5, Path tmpSha1, Path tmpResource, String url, String remotePath, Path resource, Path workingDirectory, ManagedRepositoryContent repository) throws ProxyException, NotModifiedException, RepositoryAdminException {
    Wagon wagon = null;
    try {
        RepositoryURL repoUrl = remoteRepository.getURL();
        String protocol = repoUrl.getProtocol();
        NetworkProxy networkProxy = null;
        if (StringUtils.isNotBlank(connector.getProxyId())) {
            networkProxy = networkProxyAdmin.getNetworkProxy(connector.getProxyId());
        }
        WagonFactoryRequest wagonFactoryRequest = new WagonFactoryRequest("wagon#" + protocol, remoteRepository.getRepository().getExtraHeaders()).networkProxy(networkProxy);
        wagon = wagonFactory.getWagon(wagonFactoryRequest);
        if (wagon == null) {
            throw new ProxyException("Unsupported target repository protocol: " + protocol);
        }
        if (wagon == null) {
            throw new ProxyException("Unsupported target repository protocol: " + protocol);
        }
        boolean connected = connectToRepository(connector, wagon, remoteRepository);
        if (connected) {
            transferArtifact(wagon, remoteRepository, remotePath, repository, resource, workingDirectory, tmpResource);
            // TODO: these should be used to validate the download based on the policies, not always downloaded
            // to
            // save on connections since md5 is rarely used
            transferChecksum(wagon, remoteRepository, remotePath, repository, resource, workingDirectory, ".sha1", tmpSha1);
            transferChecksum(wagon, remoteRepository, remotePath, repository, resource, workingDirectory, ".md5", tmpMd5);
        }
    } catch (NotFoundException e) {
        urlFailureCache.cacheFailure(url);
        throw e;
    } catch (NotModifiedException e) {
        // Do not cache url here.
        throw e;
    } catch (ProxyException e) {
        urlFailureCache.cacheFailure(url);
        throw e;
    } catch (WagonFactoryException e) {
        throw new ProxyException(e.getMessage(), e);
    } finally {
        if (wagon != null) {
            try {
                wagon.disconnect();
            } catch (ConnectionException e) {
                log.warn("Unable to disconnect wagon.", e);
            }
        }
    }
}
Also used : WagonFactoryRequest(org.apache.archiva.proxy.common.WagonFactoryRequest) WagonFactoryException(org.apache.archiva.proxy.common.WagonFactoryException) Wagon(org.apache.maven.wagon.Wagon) RepositoryURL(org.apache.archiva.model.RepositoryURL) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy) ConnectionException(org.apache.maven.wagon.ConnectionException)

Example 5 with Wagon

use of org.apache.maven.wagon.Wagon in project archiva by apache.

the class DefaultRemoteRepositoriesService method checkRemoteConnectivity.

@Override
public Boolean checkRemoteConnectivity(String repositoryId) throws ArchivaRestServiceException {
    try {
        RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository(repositoryId);
        if (remoteRepository == null) {
            log.warn("ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId);
            return Boolean.FALSE;
        }
        NetworkProxy networkProxy = null;
        if (StringUtils.isNotBlank(remoteRepository.getRemoteDownloadNetworkProxyId())) {
            networkProxy = networkProxyAdmin.getNetworkProxy(remoteRepository.getRemoteDownloadNetworkProxyId());
            if (networkProxy == null) {
                log.warn("your remote repository is configured to download remote index trought a proxy we cannot find id:{}", remoteRepository.getRemoteDownloadNetworkProxyId());
            }
        }
        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(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 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 Boolean.TRUE;
    } catch (TransferFailedException e) {
        log.info("TransferFailedException :{}", e.getMessage());
        return Boolean.FALSE;
    } 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 Boolean.FALSE;
    }
}
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.admin.model.beans.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) 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.proxy.common.WagonFactoryRequest) TransferFailedException(org.apache.maven.wagon.TransferFailedException)

Aggregations

Wagon (org.apache.maven.wagon.Wagon)16 WagonFactoryRequest (org.apache.archiva.proxy.common.WagonFactoryRequest)6 ConnectionException (org.apache.maven.wagon.ConnectionException)5 ProxyInfo (org.apache.maven.wagon.proxy.ProxyInfo)4 Repository (org.apache.maven.wagon.repository.Repository)4 NetworkProxy (org.apache.archiva.admin.model.beans.NetworkProxy)3 ArchivaConfiguration (org.apache.archiva.configuration.ArchivaConfiguration)3 Configuration (org.apache.archiva.configuration.Configuration)3 ManagedRepositoryConfiguration (org.apache.archiva.configuration.ManagedRepositoryConfiguration)3 ProxyConnectorConfiguration (org.apache.archiva.configuration.ProxyConnectorConfiguration)3 RemoteRepositoryConfiguration (org.apache.archiva.configuration.RemoteRepositoryConfiguration)3 WagonFactory (org.apache.archiva.proxy.common.WagonFactory)3 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)3 TransferFailedException (org.apache.maven.wagon.TransferFailedException)3 UnsupportedProtocolException (org.apache.maven.wagon.UnsupportedProtocolException)3 AuthenticationInfo (org.apache.maven.wagon.authentication.AuthenticationInfo)3 Before (org.junit.Before)3 ContextConfiguration (org.springframework.test.context.ContextConfiguration)3 File (java.io.File)2 IOException (java.io.IOException)2