Search in sources :

Example 1 with ProxyInfo

use of org.apache.maven.wagon.proxy.ProxyInfo in project felix by apache.

the class RemoteFileManager method getProxyInfo.

/**
 * Convenience method to map a Proxy object from the user system settings to a ProxyInfo object.
 * @param settings project settings given by maven
 * @return a proxyInfo object instancied or null if no active proxy is define in the settings.xml
 */
public static ProxyInfo getProxyInfo(Settings settings) {
    ProxyInfo proxyInfo = null;
    if (settings != null && settings.getActiveProxy() != null) {
        Proxy settingsProxy = settings.getActiveProxy();
        proxyInfo = new ProxyInfo();
        proxyInfo.setHost(settingsProxy.getHost());
        proxyInfo.setType(settingsProxy.getProtocol());
        proxyInfo.setPort(settingsProxy.getPort());
        proxyInfo.setNonProxyHosts(settingsProxy.getNonProxyHosts());
        proxyInfo.setUserName(settingsProxy.getUsername());
        proxyInfo.setPassword(settingsProxy.getPassword());
    }
    return proxyInfo;
}
Also used : ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Proxy(org.apache.maven.settings.Proxy)

Example 2 with ProxyInfo

use of org.apache.maven.wagon.proxy.ProxyInfo in project fabric8 by jboss-fuse.

the class ConfigurableHttpWagon method execute.

@Override
protected CloseableHttpResponse execute(HttpUriRequest httpMethod) throws HttpException, IOException {
    setHeaders(httpMethod);
    String userAgent = getUserAgent(httpMethod);
    if (userAgent != null) {
        httpMethod.setHeader(HTTP.USER_AGENT, userAgent);
    }
    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    // WAGON-273: default the cookie-policy to browser compatible
    requestConfigBuilder.setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY);
    Repository repo = getRepository();
    ProxyInfo proxyInfo = getProxyInfo(repo.getProtocol(), repo.getHost());
    if (proxyInfo != null) {
        HttpHost proxy = new HttpHost(proxyInfo.getHost(), proxyInfo.getPort());
        requestConfigBuilder.setProxy(proxy);
    }
    HttpMethodConfiguration config = getHttpConfiguration() == null ? null : getHttpConfiguration().getMethodConfiguration(httpMethod);
    if (config != null) {
        copyConfig(config, requestConfigBuilder);
    } else {
        requestConfigBuilder.setSocketTimeout(getReadTimeout());
        requestConfigBuilder.setConnectTimeout(getTimeout());
    }
    getLocalContext().setRequestConfig(requestConfigBuilder.build());
    if (config != null && config.isUsePreemptive()) {
        HttpHost targetHost = new HttpHost(repo.getHost(), repo.getPort(), repo.getProtocol());
        AuthScope targetScope = getBasicAuthScope().getScope(targetHost);
        if (getCredentialsProvider().getCredentials(targetScope) != null) {
            BasicScheme targetAuth = new BasicScheme();
            targetAuth.processChallenge(new BasicHeader(AUTH.WWW_AUTH, "BASIC preemptive"));
            getAuthCache().put(targetHost, targetAuth);
        }
    }
    if (proxyInfo != null) {
        if (proxyInfo.getHost() != null) {
            HttpHost proxyHost = new HttpHost(proxyInfo.getHost(), proxyInfo.getPort());
            AuthScope proxyScope = getProxyBasicAuthScope().getScope(proxyHost);
            String proxyUsername = proxyInfo.getUserName();
            String proxyPassword = proxyInfo.getPassword();
            String proxyNtlmHost = proxyInfo.getNtlmHost();
            String proxyNtlmDomain = proxyInfo.getNtlmDomain();
            if (proxyUsername != null && proxyPassword != null) {
                Credentials creds;
                if (proxyNtlmHost != null || proxyNtlmDomain != null) {
                    creds = new NTCredentials(proxyUsername, proxyPassword, proxyNtlmHost, proxyNtlmDomain);
                } else {
                    creds = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
                }
                getCredentialsProvider().setCredentials(proxyScope, creds);
                BasicScheme proxyAuth = new BasicScheme();
                proxyAuth.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC preemptive"));
                getAuthCache().put(proxyHost, proxyAuth);
            }
        }
    }
    return client.execute(httpMethod, getLocalContext());
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicScheme(org.apache.http.impl.auth.BasicScheme) HttpMethodConfiguration(org.apache.maven.wagon.providers.http.HttpMethodConfiguration) NTCredentials(org.apache.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Repository(org.apache.maven.wagon.repository.Repository) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) BasicHeader(org.apache.http.message.BasicHeader) NTCredentials(org.apache.http.auth.NTCredentials) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 3 with ProxyInfo

use of org.apache.maven.wagon.proxy.ProxyInfo 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(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.admin.model.beans.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 4 with ProxyInfo

use of org.apache.maven.wagon.proxy.ProxyInfo in project archiva by apache.

the class DefaultRepositoryProxyConnectors method connectToRepository.

/**
 * Using wagon, connect to the remote repository.
 *
 * @param connector        the connector configuration to utilize (for obtaining network proxy configuration from)
 * @param wagon            the wagon instance to establish the connection on.
 * @param remoteRepository the remote repository to connect to.
 * @return true if the connection was successful. false if not connected.
 */
private boolean connectToRepository(ProxyConnector connector, Wagon wagon, RemoteRepositoryContent remoteRepository) {
    boolean connected = false;
    final ProxyInfo networkProxy = connector.getProxyId() == null ? null : this.networkProxyMap.get(connector.getProxyId());
    if (log.isDebugEnabled()) {
        if (networkProxy != null) {
            // TODO: move to proxyInfo.toString()
            String msg = "Using network proxy " + networkProxy.getHost() + ":" + networkProxy.getPort() + " to connect to remote repository " + remoteRepository.getURL();
            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;
    String username = "";
    String password = "";
    RepositoryCredentials repCred = remoteRepository.getRepository().getLoginCredentials();
    if (repCred != null && repCred instanceof PasswordCredentials) {
        PasswordCredentials pwdCred = (PasswordCredentials) repCred;
        username = pwdCred.getUsername();
        password = pwdCred.getPassword() == null ? "" : new String(pwdCred.getPassword());
    }
    if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
        log.debug("Using username {} to connect to remote repository {}", username, remoteRepository.getURL());
        authInfo = new AuthenticationInfo();
        authInfo.setUserName(username);
        authInfo.setPassword(password);
    }
    // Convert seconds to milliseconds
    long timeoutInMilliseconds = remoteRepository.getRepository().getTimeout().toMillis();
    // Set timeout  read and connect
    // FIXME olamy having 2 config values
    wagon.setReadTimeout((int) timeoutInMilliseconds);
    wagon.setTimeout((int) timeoutInMilliseconds);
    try {
        Repository wagonRepository = new Repository(remoteRepository.getId(), remoteRepository.getURL().toString());
        wagon.connect(wagonRepository, authInfo, networkProxy);
        connected = true;
    } catch (ConnectionException | AuthenticationException e) {
        log.warn("Could not connect to {}: {}", remoteRepository.getRepository().getName(), e.getMessage());
        connected = false;
    }
    return connected;
}
Also used : ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) RepositoryCredentials(org.apache.archiva.repository.RepositoryCredentials) PasswordCredentials(org.apache.archiva.repository.PasswordCredentials) Repository(org.apache.maven.wagon.repository.Repository) ManagedRepository(org.apache.archiva.repository.ManagedRepository) RemoteRepository(org.apache.archiva.repository.RemoteRepository) AuthenticationException(org.apache.maven.wagon.authentication.AuthenticationException) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo) ConnectionException(org.apache.maven.wagon.ConnectionException)

Example 5 with ProxyInfo

use of org.apache.maven.wagon.proxy.ProxyInfo 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

ProxyInfo (org.apache.maven.wagon.proxy.ProxyInfo)22 Proxy (org.apache.maven.settings.Proxy)8 AuthenticationInfo (org.apache.maven.wagon.authentication.AuthenticationInfo)8 RemoteRepository (org.apache.archiva.repository.RemoteRepository)7 ConnectionException (org.apache.maven.wagon.ConnectionException)7 AuthenticationException (org.apache.maven.wagon.authentication.AuthenticationException)6 Repository (org.apache.maven.wagon.repository.Repository)6 MalformedURLException (java.net.MalformedURLException)5 NetworkProxy (org.apache.archiva.admin.model.beans.NetworkProxy)5 WagonFactoryRequest (org.apache.archiva.proxy.common.WagonFactoryRequest)5 PasswordCredentials (org.apache.archiva.repository.PasswordCredentials)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 Path (java.nio.file.Path)4 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)4 RemoteIndexFeature (org.apache.archiva.repository.features.RemoteIndexFeature)4 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)4 TransferFailedException (org.apache.maven.wagon.TransferFailedException)4