Search in sources :

Example 21 with Repository

use of org.apache.maven.wagon.repository.Repository in project archiva by apache.

the class DownloadRemoteIndexTask method run.

@Override
public void run() {
    // so short lock : not sure we need it
    synchronized (this.runningRemoteDownloadIds) {
        if (this.runningRemoteDownloadIds.contains(this.remoteRepository.getId())) {
            // skip it as it's running
            log.info("skip download index remote for repo {} it's already running", this.remoteRepository.getId());
            return;
        }
        this.runningRemoteDownloadIds.add(this.remoteRepository.getId());
    }
    Path tempIndexDirectory = null;
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    try {
        log.info("start download remote index for remote repository {}", this.remoteRepository.getId());
        if (this.remoteRepository.getIndexingContext() == null) {
            throw new IndexNotFoundException("No index context set for repository " + remoteRepository.getId());
        }
        if (this.remoteRepository.getType() != RepositoryType.MAVEN) {
            throw new RepositoryException("Bad repository type");
        }
        if (!this.remoteRepository.supportsFeature(RemoteIndexFeature.class)) {
            throw new RepositoryException("Repository does not support RemotIndexFeature " + remoteRepository.getId());
        }
        RemoteIndexFeature rif = this.remoteRepository.getFeature(RemoteIndexFeature.class);
        IndexingContext indexingContext = this.remoteRepository.getIndexingContext().getBaseContext(IndexingContext.class);
        // create a temp directory to download files
        tempIndexDirectory = Paths.get(indexingContext.getIndexDirectoryFile().getParent(), ".tmpIndex");
        Path indexCacheDirectory = Paths.get(indexingContext.getIndexDirectoryFile().getParent(), ".indexCache");
        Files.createDirectories(indexCacheDirectory);
        if (Files.exists(tempIndexDirectory)) {
            org.apache.archiva.common.utils.FileUtils.deleteDirectory(tempIndexDirectory);
        }
        Files.createDirectories(tempIndexDirectory);
        tempIndexDirectory.toFile().deleteOnExit();
        String baseIndexUrl = indexingContext.getIndexUpdateUrl();
        String wagonProtocol = this.remoteRepository.getLocation().getScheme();
        final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(new WagonFactoryRequest(wagonProtocol, this.remoteRepository.getExtraHeaders()).networkProxy(this.networkProxy));
        // FIXME olamy having 2 config values
        wagon.setReadTimeout((int) rif.getDownloadTimeout().toMillis());
        wagon.setTimeout((int) remoteRepository.getTimeout().toMillis());
        if (wagon instanceof AbstractHttpClientWagon) {
            HttpConfiguration httpConfiguration = new HttpConfiguration();
            HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration();
            httpMethodConfiguration.setUsePreemptive(true);
            httpMethodConfiguration.setReadTimeout((int) rif.getDownloadTimeout().toMillis());
            httpConfiguration.setGet(httpMethodConfiguration);
            AbstractHttpClientWagon.class.cast(wagon).setHttpConfiguration(httpConfiguration);
        }
        wagon.addTransferListener(new DownloadListener());
        ProxyInfo proxyInfo = null;
        if (this.networkProxy != null) {
            proxyInfo = new ProxyInfo();
            proxyInfo.setType(this.networkProxy.getProtocol());
            proxyInfo.setHost(this.networkProxy.getHost());
            proxyInfo.setPort(this.networkProxy.getPort());
            proxyInfo.setUserName(this.networkProxy.getUsername());
            proxyInfo.setPassword(new String(this.networkProxy.getPassword()));
        }
        AuthenticationInfo authenticationInfo = null;
        if (this.remoteRepository.getLoginCredentials() != null && this.remoteRepository.getLoginCredentials() instanceof PasswordCredentials) {
            PasswordCredentials creds = (PasswordCredentials) this.remoteRepository.getLoginCredentials();
            authenticationInfo = new AuthenticationInfo();
            authenticationInfo.setUserName(creds.getUsername());
            authenticationInfo.setPassword(new String(creds.getPassword()));
        }
        log.debug("Connection to {}, authInfo={}", this.remoteRepository.getId(), authenticationInfo);
        wagon.connect(new Repository(this.remoteRepository.getId(), baseIndexUrl), authenticationInfo, proxyInfo);
        Path indexDirectory = indexingContext.getIndexDirectoryFile().toPath();
        if (!Files.exists(indexDirectory)) {
            Files.createDirectories(indexDirectory);
        }
        log.debug("Downloading index file to {}", indexDirectory);
        log.debug("Index cache dir {}", indexCacheDirectory);
        ResourceFetcher resourceFetcher = new WagonResourceFetcher(log, tempIndexDirectory, wagon, remoteRepository);
        IndexUpdateRequest request = new IndexUpdateRequest(indexingContext, resourceFetcher);
        request.setForceFullUpdate(this.fullDownload);
        request.setLocalIndexCacheDir(indexCacheDirectory.toFile());
        IndexUpdateResult result = this.indexUpdater.fetchAndUpdateIndex(request);
        log.debug("Update result success: {}", result.isSuccessful());
        stopWatch.stop();
        log.info("time update index from remote for repository {}: {}ms", this.remoteRepository.getId(), (stopWatch.getTime()));
        // index packing optionnal ??
        // IndexPackingRequest indexPackingRequest =
        // new IndexPackingRequest( indexingContext, indexingContext.getIndexDirectoryFile() );
        // indexPacker.packIndex( indexPackingRequest );
        indexingContext.updateTimestamp(true);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        deleteDirectoryQuiet(tempIndexDirectory);
        this.runningRemoteDownloadIds.remove(this.remoteRepository.getId());
    }
    log.info("end download remote index for remote repository {}", this.remoteRepository.getId());
}
Also used : Path(java.nio.file.Path) AbstractHttpClientWagon(org.apache.maven.wagon.shared.http.AbstractHttpClientWagon) PasswordCredentials(org.apache.archiva.repository.base.PasswordCredentials) HttpMethodConfiguration(org.apache.maven.wagon.shared.http.HttpMethodConfiguration) ResourceFetcher(org.apache.maven.index.updater.ResourceFetcher) IndexUpdateRequest(org.apache.maven.index.updater.IndexUpdateRequest) RepositoryException(org.apache.archiva.repository.RepositoryException) HttpConfiguration(org.apache.maven.wagon.shared.http.HttpConfiguration) StreamWagon(org.apache.maven.wagon.StreamWagon) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo) TransferFailedException(org.apache.maven.wagon.TransferFailedException) IndexNotFoundException(org.apache.maven.index_shaded.lucene.index.IndexNotFoundException) RepositoryException(org.apache.archiva.repository.RepositoryException) AuthorizationException(org.apache.maven.wagon.authorization.AuthorizationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ResourceDoesNotExistException(org.apache.maven.wagon.ResourceDoesNotExistException) StopWatch(org.apache.commons.lang3.time.StopWatch) IndexUpdateResult(org.apache.maven.index.updater.IndexUpdateResult) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Repository(org.apache.maven.wagon.repository.Repository) RemoteRepository(org.apache.archiva.repository.RemoteRepository) WagonFactoryRequest(org.apache.archiva.maven.common.proxy.WagonFactoryRequest) IndexNotFoundException(org.apache.maven.index_shaded.lucene.index.IndexNotFoundException) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) IndexingContext(org.apache.maven.index.context.IndexingContext)

Example 22 with Repository

use of org.apache.maven.wagon.repository.Repository in project here-artifact-maven-wagon by heremaps.

the class ArtifactWagonTest method setup.

@Before
public void setup() throws IllegalAccessException {
    responses = new HashMap<>();
    repository = new Repository("example-repo", "here+https://example.com/artifact");
    artifactWagon = new ArtifactWagon() {

        @Override
        public Repository getRepository() {
            return ArtifactWagonTest.this.repository;
        }

        @Override
        protected Properties loadHereProperties() {
            Properties properties = new Properties();
            properties.setProperty("here.user.id", "test-user-id");
            properties.setProperty("here.client.id", "test-client-id");
            properties.setProperty("here.access.key.id", "test-access-key-id");
            properties.setProperty("here.access.key.secret", "test-access-key-secret");
            properties.setProperty("here.token.endpoint.url", "https://account.api.here.com/oauth2/token");
            return properties;
        }

        @Override
        protected CloseableHttpResponse execute(HttpUriRequest httpMethod) {
            HttpResponse response;
            String key = httpMethod.getMethod() + ":" + httpMethod.getURI().toString();
            if (responses.containsKey(key)) {
                response = responses.get(key);
            } else {
                response = mock(CloseableHttpResponse.class);
                when(response.getStatusLine()).thenReturn(new BasicStatusLine(protocolVersion, 404, "Not Found"));
            }
            return (CloseableHttpResponse) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { CloseableHttpResponse.class }, (proxy, method, args) -> {
                final String mname = method.getName();
                if (mname.equals("close")) {
                    EntityUtils.consume(((HttpResponse) proxy).getEntity());
                    return null;
                } else {
                    try {
                        return method.invoke(response, args);
                    } catch (final InvocationTargetException ex) {
                        final Throwable cause = ex.getCause();
                        if (cause != null) {
                            throw cause;
                        } else {
                            throw ex;
                        }
                    }
                }
            });
        }
    };
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Repository(org.apache.maven.wagon.repository.Repository) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) Properties(java.util.Properties) InvocationTargetException(java.lang.reflect.InvocationTargetException) BasicStatusLine(org.apache.http.message.BasicStatusLine) Before(org.junit.Before)

Example 23 with Repository

use of org.apache.maven.wagon.repository.Repository in project m2e-core by eclipse-m2e.

the class WagonTransferListenerAdapter method transferStarted.

@Override
public void transferStarted(TransferEvent e) {
    StringBuilder sb = new StringBuilder();
    if (e.getWagon() != null && e.getWagon().getRepository() != null) {
        Wagon wagon = e.getWagon();
        Repository repository = wagon.getRepository();
        String repositoryId = repository.getId();
        // $NON-NLS-1$
        sb.append(repositoryId).append(" : ");
    }
    sb.append(e.getResource().getName());
    transferStarted(sb.toString());
}
Also used : Repository(org.apache.maven.wagon.repository.Repository) Wagon(org.apache.maven.wagon.Wagon)

Example 24 with Repository

use of org.apache.maven.wagon.repository.Repository in project cumulocity-clients-java by SoftwareAG.

the class MicroserviceDeployMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        getLog().info("skipping microservice package deployment");
        return;
    }
    final Optional<String> serverUrl = getServerUrl();
    if (!serverUrl.isPresent()) {
        getLog().warn(format("URL parameter was not configured, microservice package cannot be deployed. please check your '%s' server configuration in settings.xml", SERVER_ID));
        return;
    }
    final String targetFilename = format(TARGET_FILENAME_PATTERN, name, project.getVersion());
    final File file = new File(build, targetFilename);
    try {
        final Repository repository = new Repository(serviceId, serverUrl.get());
        final Wagon wagon = wagonManager.getWagon(repository);
        wagon.connect(repository, wagonManager.getAuthenticationInfo(serviceId));
        wagon.put(file, targetFilename);
        wagon.disconnect();
    } catch (Exception e) {
        propagate(e);
    }
}
Also used : Repository(org.apache.maven.wagon.repository.Repository) Wagon(org.apache.maven.wagon.Wagon) File(java.io.File) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 25 with Repository

use of org.apache.maven.wagon.repository.Repository in project maven by apache.

the class DefaultWagonManager method connectWagon.

/**
 * Deal with connecting to a wagon repository taking into account authentication and proxies.
 *
 * @param wagon
 * @param repository
 *
 * @throws ConnectionException
 * @throws AuthenticationException
 */
private void connectWagon(Wagon wagon, ArtifactRepository repository) throws ConnectionException, AuthenticationException {
    // See org.eclipse.aether.connector.wagon.WagonRepositoryConnector.connectWagon(Wagon)
    if (legacySupport.getRepositorySession() != null) {
        String userAgent = ConfigUtils.getString(legacySupport.getRepositorySession(), null, ConfigurationProperties.USER_AGENT);
        if (userAgent == null) {
            Properties headers = new Properties();
            headers.put("User-Agent", ConfigUtils.getString(legacySupport.getRepositorySession(), "Maven", ConfigurationProperties.USER_AGENT));
            try {
                Method setHttpHeaders = wagon.getClass().getMethod("setHttpHeaders", Properties.class);
                setHttpHeaders.invoke(wagon, headers);
            } catch (NoSuchMethodException e) {
            // normal for non-http wagons
            } catch (Exception e) {
                logger.debug("Could not set user agent for wagon " + wagon.getClass().getName() + ": " + e);
            }
        }
    }
    if (repository.getProxy() != null && logger.isDebugEnabled()) {
        logger.debug("Using proxy " + repository.getProxy().getHost() + ":" + repository.getProxy().getPort() + " for " + repository.getUrl());
    }
    if (repository.getAuthentication() != null && repository.getProxy() != null) {
        wagon.connect(new Repository(repository.getId(), repository.getUrl()), authenticationInfo(repository), proxyInfo(repository));
    } else if (repository.getAuthentication() != null) {
        wagon.connect(new Repository(repository.getId(), repository.getUrl()), authenticationInfo(repository));
    } else if (repository.getProxy() != null) {
        wagon.connect(new Repository(repository.getId(), repository.getUrl()), proxyInfo(repository));
    } else {
        wagon.connect(new Repository(repository.getId(), repository.getUrl()));
    }
}
Also used : Repository(org.apache.maven.wagon.repository.Repository) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) Method(java.lang.reflect.Method) Properties(java.util.Properties) ConfigurationProperties(org.eclipse.aether.ConfigurationProperties) AuthenticationException(org.apache.maven.wagon.authentication.AuthenticationException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) ComponentLifecycleException(org.codehaus.plexus.component.repository.exception.ComponentLifecycleException) TransferFailedException(org.apache.maven.wagon.TransferFailedException) UnsupportedProtocolException(org.apache.maven.wagon.UnsupportedProtocolException) ConnectionException(org.apache.maven.wagon.ConnectionException) AuthorizationException(org.apache.maven.wagon.authorization.AuthorizationException) IOException(java.io.IOException) ResourceDoesNotExistException(org.apache.maven.wagon.ResourceDoesNotExistException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

Repository (org.apache.maven.wagon.repository.Repository)31 Test (org.junit.Test)13 IOException (java.io.IOException)11 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)9 File (java.io.File)9 AuthenticationException (org.apache.maven.wagon.authentication.AuthenticationException)8 Wagon (org.apache.maven.wagon.Wagon)7 ProxyInfo (org.apache.maven.wagon.proxy.ProxyInfo)7 Path (java.nio.file.Path)6 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)6 ConnectionException (org.apache.maven.wagon.ConnectionException)6 TransferFailedException (org.apache.maven.wagon.TransferFailedException)6 AuthenticationInfo (org.apache.maven.wagon.authentication.AuthenticationInfo)5 AuthorizationException (org.apache.maven.wagon.authorization.AuthorizationException)5 AccessToken (com.google.auth.oauth2.AccessToken)4 ZipEntry (java.util.zip.ZipEntry)4 ZipFile (java.util.zip.ZipFile)4 RoleManagementService (org.apache.archiva.redback.rest.api.services.RoleManagementService)4 RemoteRepository (org.apache.archiva.repository.RemoteRepository)4 UnsupportedProtocolException (org.apache.maven.wagon.UnsupportedProtocolException)4