Search in sources :

Example 11 with ProxyInfo

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

the class JavadocUtil method createHttpClient.

/**
 * Creates a new {@code HttpClient} instance.
 *
 * @param settings The settings to use for setting up the client or {@code null}.
 * @param url The {@code URL} to use for setting up the client or {@code null}.
 *
 * @return A new {@code HttpClient} instance.
 *
 * @see #DEFAULT_TIMEOUT
 * @since 2.8
 */
private static HttpClient createHttpClient(Settings settings, URL url) {
    DefaultHttpClient httpClient = new DefaultHttpClient(new PoolingClientConnectionManager());
    httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, DEFAULT_TIMEOUT);
    httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, DEFAULT_TIMEOUT);
    httpClient.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
    // Some web servers don't allow the default user-agent sent by httpClient
    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
    if (settings != null && settings.getActiveProxy() != null) {
        Proxy activeProxy = settings.getActiveProxy();
        ProxyInfo proxyInfo = new ProxyInfo();
        proxyInfo.setNonProxyHosts(activeProxy.getNonProxyHosts());
        if (StringUtils.isNotEmpty(activeProxy.getHost()) && (url == null || !ProxyUtils.validateNonProxyHosts(proxyInfo, url.getHost()))) {
            HttpHost proxy = new HttpHost(activeProxy.getHost(), activeProxy.getPort());
            httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
            if (StringUtils.isNotEmpty(activeProxy.getUsername()) && activeProxy.getPassword() != null) {
                Credentials credentials = new UsernamePasswordCredentials(activeProxy.getUsername(), activeProxy.getPassword());
                httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);
            }
        }
    }
    return httpClient;
}
Also used : ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) PoolingClientConnectionManager(org.apache.http.impl.conn.PoolingClientConnectionManager) Proxy(org.apache.maven.settings.Proxy) HttpHost(org.apache.http.HttpHost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 12 with ProxyInfo

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

the class DoapUtil method fetchURL.

/**
 * Fetch an URL
 *
 * @param settings the user settings used to fetch the url with an active proxy, if defined.
 * @param url the url to fetch
 * @throws IOException if any
 * @see #DEFAULT_TIMEOUT
 * @since 1.1
 */
public static void fetchURL(Settings settings, URL url) throws IOException {
    if (url == null) {
        throw new IllegalArgumentException("The url is null");
    }
    if ("file".equals(url.getProtocol())) {
        InputStream in = null;
        try {
            in = url.openStream();
            in.close();
            in = null;
        } finally {
            IOUtil.close(in);
        }
        return;
    }
    // http, https...
    HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(DEFAULT_TIMEOUT);
    httpClient.getHttpConnectionManager().getParams().setSoTimeout(DEFAULT_TIMEOUT);
    httpClient.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
    // Some web servers don't allow the default user-agent sent by httpClient
    httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
    if (settings != null && settings.getActiveProxy() != null) {
        Proxy activeProxy = settings.getActiveProxy();
        ProxyInfo proxyInfo = new ProxyInfo();
        proxyInfo.setNonProxyHosts(activeProxy.getNonProxyHosts());
        if (StringUtils.isNotEmpty(activeProxy.getHost()) && !ProxyUtils.validateNonProxyHosts(proxyInfo, url.getHost())) {
            httpClient.getHostConfiguration().setProxy(activeProxy.getHost(), activeProxy.getPort());
            if (StringUtils.isNotEmpty(activeProxy.getUsername()) && activeProxy.getPassword() != null) {
                Credentials credentials = new UsernamePasswordCredentials(activeProxy.getUsername(), activeProxy.getPassword());
                httpClient.getState().setProxyCredentials(AuthScope.ANY, credentials);
            }
        }
    }
    GetMethod getMethod = new GetMethod(url.toString());
    try {
        int status;
        try {
            status = httpClient.executeMethod(getMethod);
        } catch (SocketTimeoutException e) {
            // could be a sporadic failure, one more retry before we give up
            status = httpClient.executeMethod(getMethod);
        }
        if (status != HttpStatus.SC_OK) {
            throw new FileNotFoundException(url.toString());
        }
    } finally {
        getMethod.releaseConnection();
    }
}
Also used : ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Proxy(org.apache.maven.settings.Proxy) SocketTimeoutException(java.net.SocketTimeoutException) InputStream(java.io.InputStream) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) GetMethod(org.apache.commons.httpclient.methods.GetMethod) FileNotFoundException(java.io.FileNotFoundException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 13 with ProxyInfo

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

the class RepositoryUtils method dependencyExistsInRepo.

/**
 * @param repo not null
 * @param artifact not null
 * @return <code>true</code> if the artifact exists in the given repo, <code>false</code> otherwise or if
 * the repo is blacklisted.
 */
public boolean dependencyExistsInRepo(ArtifactRepository repo, Artifact artifact) {
    if (repo.isBlacklisted()) {
        if (log.isDebugEnabled()) {
            log.debug("The repo '" + repo.getId() + "' is black listed - Ignored it");
        }
        return false;
    }
    if (UNKNOWN_HOSTS.contains(repo.getUrl())) {
        if (log.isDebugEnabled()) {
            log.debug("The repo url '" + repo.getUrl() + "' is unknown - Ignored it");
        }
        return false;
    }
    repo = wagonManager.getMirrorRepository(repo);
    String id = repo.getId();
    Repository repository = new Repository(id, repo.getUrl());
    Wagon wagon;
    try {
        wagon = wagonManager.getWagon(repository);
    } catch (UnsupportedProtocolException e) {
        logError("Unsupported protocol: '" + repo.getProtocol() + "'", e);
        return false;
    } catch (WagonConfigurationException e) {
        logError("Unsupported protocol: '" + repo.getProtocol() + "'", e);
        return false;
    }
    wagon.setTimeout(1000);
    if (log.isDebugEnabled()) {
        Debug debug = new Debug();
        wagon.addSessionListener(debug);
        wagon.addTransferListener(debug);
    }
    try {
        // FIXME when upgrading to maven 3.x : this must be changed.
        AuthenticationInfo auth = wagonManager.getAuthenticationInfo(repo.getId());
        ProxyInfo proxyInfo = getProxyInfo();
        if (proxyInfo != null) {
            wagon.connect(repository, auth, proxyInfo);
        } else {
            wagon.connect(repository, auth);
        }
        return wagon.resourceExists(StringUtils.replace(getDependencyUrlFromRepository(artifact, repo), repo.getUrl(), ""));
    } catch (ConnectionException e) {
        logError("Unable to connect to: " + repo.getUrl(), e);
        return false;
    } catch (AuthenticationException e) {
        logError("Unable to connect to: " + repo.getUrl(), e);
        return false;
    } catch (TransferFailedException e) {
        if (e.getCause() instanceof UnknownHostException) {
            log.error("Unknown host " + e.getCause().getMessage() + " - ignored it");
            UNKNOWN_HOSTS.add(repo.getUrl());
        } else {
            logError("Unable to determine if resource " + artifact + " exists in " + repo.getUrl(), e);
        }
        return false;
    } catch (AuthorizationException e) {
        logError("Unable to connect to: " + repo.getUrl(), e);
        return false;
    } catch (AbstractMethodError e) {
        log.error("Wagon " + wagon.getClass().getName() + " does not support the resourceExists method");
        return false;
    } finally {
        try {
            wagon.disconnect();
        } catch (ConnectionException e) {
            logError("Error disconnecting wagon - ignored", e);
        }
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) AuthenticationException(org.apache.maven.wagon.authentication.AuthenticationException) AuthorizationException(org.apache.maven.wagon.authorization.AuthorizationException) WagonConfigurationException(org.apache.maven.artifact.manager.WagonConfigurationException) UnsupportedProtocolException(org.apache.maven.wagon.UnsupportedProtocolException) Wagon(org.apache.maven.wagon.Wagon) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Repository(org.apache.maven.wagon.repository.Repository) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) TransferFailedException(org.apache.maven.wagon.TransferFailedException) Debug(org.apache.maven.wagon.observers.Debug) ConnectionException(org.apache.maven.wagon.ConnectionException)

Example 14 with ProxyInfo

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

the class RepositoryUtils method getProxyInfo.

// ----------------------------------------------------------------------
// Private methods
// ----------------------------------------------------------------------
/**
 * Convenience method to map a <code>Proxy</code> object from the user system settings to a <code>ProxyInfo</code>
 * object.
 *
 * @return a proxyInfo object instanced or null if no active proxy is define in the settings.xml
 */
private ProxyInfo getProxyInfo() {
    if (settings == null || settings.getActiveProxy() == null) {
        return null;
    }
    Proxy settingsProxy = settings.getActiveProxy();
    ProxyInfo 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 15 with ProxyInfo

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

the class JavadocUtil method createHttpClient.

/**
     * Creates a new {@code HttpClient} instance.
     *
     * @param settings The settings to use for setting up the client or {@code null}.
     * @param url The {@code URL} to use for setting up the client or {@code null}.
     *
     * @return A new {@code HttpClient} instance.
     *
     * @see #DEFAULT_TIMEOUT
     * @since 2.8
     */
private static HttpClient createHttpClient(Settings settings, URL url) {
    DefaultHttpClient httpClient = new DefaultHttpClient(new PoolingClientConnectionManager());
    httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, DEFAULT_TIMEOUT);
    httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, DEFAULT_TIMEOUT);
    httpClient.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
    // Some web servers don't allow the default user-agent sent by httpClient
    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
    if (settings != null && settings.getActiveProxy() != null) {
        Proxy activeProxy = settings.getActiveProxy();
        ProxyInfo proxyInfo = new ProxyInfo();
        proxyInfo.setNonProxyHosts(activeProxy.getNonProxyHosts());
        if (StringUtils.isNotEmpty(activeProxy.getHost()) && (url == null || !ProxyUtils.validateNonProxyHosts(proxyInfo, url.getHost()))) {
            HttpHost proxy = new HttpHost(activeProxy.getHost(), activeProxy.getPort());
            httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
            if (StringUtils.isNotEmpty(activeProxy.getUsername()) && activeProxy.getPassword() != null) {
                Credentials credentials = new UsernamePasswordCredentials(activeProxy.getUsername(), activeProxy.getPassword());
                httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);
            }
        }
    }
    return httpClient;
}
Also used : ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) PoolingClientConnectionManager(org.apache.http.impl.conn.PoolingClientConnectionManager) Proxy(org.apache.maven.settings.Proxy) HttpHost(org.apache.http.HttpHost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

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