Example usage for org.apache.http.impl.conn DefaultProxyRoutePlanner DefaultProxyRoutePlanner

List of usage examples for org.apache.http.impl.conn DefaultProxyRoutePlanner DefaultProxyRoutePlanner

Introduction

In this page you can find the example usage for org.apache.http.impl.conn DefaultProxyRoutePlanner DefaultProxyRoutePlanner.

Prototype

public DefaultProxyRoutePlanner(final HttpHost proxy) 

Source Link

Usage

From source file:com.igormaznitsa.mvngolang.AbstractGolangMojo.java

@Nonnull
private synchronized HttpClient getHttpClient(@Nullable final ProxySettings proxy) {
    if (this.httpClient == null) {
        final HttpClientBuilder builder = HttpClients.custom();

        if (proxy != null) {
            if (proxy.hasCredentials()) {
                final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                credentialsProvider.setCredentials(new AuthScope(proxy.host, proxy.port),
                        new UsernamePasswordCredentials(proxy.username, proxy.password));
                builder.setDefaultCredentialsProvider(credentialsProvider);
                getLog().debug(//from  w  w  w  .  j av  a  2  s .  c  o  m
                        String.format("Credentials provider has been created for proxy (username : %s): %s",
                                proxy.username, proxy.toString()));
            }

            final String[] ignoreForAddresses = proxy.nonProxyHosts == null ? new String[0]
                    : proxy.nonProxyHosts.split("\\|");
            if (ignoreForAddresses.length > 0) {
                final WildCardMatcher[] matchers = new WildCardMatcher[ignoreForAddresses.length];
                for (int i = 0; i < ignoreForAddresses.length; i++) {
                    matchers[i] = new WildCardMatcher(ignoreForAddresses[i]);
                }

                final HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(
                        new HttpHost(proxy.host, proxy.port, proxy.protocol)) {
                    @Override
                    @Nonnull
                    public HttpRoute determineRoute(@Nonnull final HttpHost host,
                            @Nonnull final HttpRequest request, @Nonnull final HttpContext context)
                            throws HttpException {
                        final String hostName = host.getHostName();
                        for (final WildCardMatcher m : matchers) {
                            if (m.match(hostName)) {
                                getLog().debug("Ignoring proxy for host : " + hostName);
                                return new HttpRoute(host);
                            }
                        }
                        return super.determineRoute(host, request, context);
                    }
                };
                builder.setRoutePlanner(routePlanner);
                getLog().debug(
                        "Route planner tuned to ignore proxy for addresses : " + Arrays.toString(matchers));
            }
        }

        builder.setUserAgent("mvn-golang-wrapper-agent/1.0");
        this.httpClient = builder.build();

    }
    return this.httpClient;
}

From source file:com.redhat.red.offliner.Main.java

/**
 * Sets up components needed for the download process, including the {@link ExecutorCompletionService},
 * {@link java.util.concurrent.Executor}, {@link org.apache.http.client.HttpClient}, and {@link ArtifactListReader}
 * instances. If baseUrls were provided on the command line, it will initialize the "global" baseUrls list to that.
 * Otherwise it will use {@link Options#DEFAULT_REPO_URL} and {@link Options#CENTRAL_REPO_URL} as the default
 * baseUrls. If specified, configures the HTTP proxy and username/password for authentication.
 * @throws MalformedURLException In case an invalid {@link URL} is given as a baseUrl.
 *//*from www  .  j  av a2s  . co m*/
protected void init() throws MalformedURLException {
    int threads = opts.getThreads();
    executorService = Executors.newFixedThreadPool(threads, (final Runnable r) -> {
        //        executorService = Executors.newCachedThreadPool( ( final Runnable r ) -> {
        final Thread t = new Thread(r);
        t.setDaemon(true);

        return t;
    });

    executor = new ExecutorCompletionService<>(executorService);

    errors = new ConcurrentHashMap<String, Throwable>();

    final PoolingHttpClientConnectionManager ccm = new PoolingHttpClientConnectionManager();
    ccm.setMaxTotal(opts.getConnections());

    final HttpClientBuilder builder = HttpClients.custom().setConnectionManager(ccm);

    final String proxy = opts.getProxy();
    String proxyHost = proxy;
    int proxyPort = 8080;
    if (proxy != null) {
        final int portSep = proxy.lastIndexOf(':');

        if (portSep > -1) {
            proxyHost = proxy.substring(0, portSep);
            proxyPort = Integer.parseInt(proxy.substring(portSep + 1));
        }
        final HttpRoutePlanner planner = new DefaultProxyRoutePlanner(new HttpHost(proxyHost, proxyPort));

        builder.setRoutePlanner(planner);
    }

    client = builder.build();

    final CredentialsProvider creds = new BasicCredentialsProvider();

    cookieStore = new BasicCookieStore();

    baseUrls = opts.getBaseUrls();
    if (baseUrls == null) {
        baseUrls = new ArrayList<>();
    }

    List<String> repoUrls = (baseUrls.isEmpty() ? DEFAULT_URLS : baseUrls);

    System.out.println("Planning download from:\n  " + StringUtils.join(repoUrls, "\n  "));

    for (String repoUrl : repoUrls) {
        if (repoUrl != null) {
            final String user = opts.getUser();
            if (user != null) {
                final URL u = new URL(repoUrl);
                final AuthScope as = new AuthScope(u.getHost(), UrlUtils.getPort(u));

                creds.setCredentials(as, new UsernamePasswordCredentials(user, opts.getPassword()));
            }
        }

        if (proxy != null) {
            final String proxyUser = opts.getProxyUser();
            if (proxyUser != null) {
                creds.setCredentials(new AuthScope(proxyHost, proxyPort),
                        new UsernamePasswordCredentials(proxyUser, opts.getProxyPassword()));
            }
        }
    }

    artifactListReaders = new ArrayList<>(3);
    artifactListReaders.add(new FoloReportArtifactListReader());
    artifactListReaders.add(new PlaintextArtifactListReader());
    artifactListReaders.add(new PomArtifactListReader(opts.getSettingsXml(), opts.getTypeMapping(), creds));
}

From source file:org.codelibs.fess.crawler.client.http.HcHttpClient.java

protected HttpRoutePlanner buildRoutePlanner() {
    if (routePlanner != null) {
        return routePlanner;
    }/*from  ww w  .j  a v a  2 s .co m*/

    // proxy
    final String proxyHost = getInitParameter(PROXY_HOST_PROPERTY, this.proxyHost, String.class);
    final Integer proxyPort = getInitParameter(PROXY_PORT_PROPERTY, this.proxyPort, Integer.class);
    if (proxyHost != null && proxyPort != null) {
        final HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        final DefaultProxyRoutePlanner defaultRoutePlanner = new DefaultProxyRoutePlanner(proxy);

        final Credentials credentials = getInitParameter(PROXY_CREDENTIALS_PROPERTY, proxyCredentials,
                Credentials.class);
        if (credentials != null) {
            credentialsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), credentials);
            final AuthScheme authScheme = getInitParameter(PROXY_AUTH_SCHEME_PROPERTY, proxyAuthScheme,
                    AuthScheme.class);
            if (authScheme != null) {
                authCache.put(proxy, authScheme);
            }
        }
        return defaultRoutePlanner;
    }
    return null;
}

From source file:org.springframework.extensions.webscripts.connector.RemoteClient.java

/**
 * Create and configure an HttpClient per thread based on Pooled connection manager.
 * Proxy route will be applied the client based on current settings.
 * /*w w w  .j ava2 s . c  o  m*/
 * @param url URL
 * @return HttpClient
 */
protected HttpClient createHttpClient(URL url) {
    // use the appropriate HTTP proxy host if required
    HttpRoutePlanner routePlanner = null;
    if (s_httpProxyHost != null && this.allowHttpProxy && url.getProtocol().equals("http")
            && requiresProxy(url.getHost())) {
        routePlanner = new DefaultProxyRoutePlanner(s_httpProxyHost);
        if (logger.isDebugEnabled())
            logger.debug(" - using HTTP proxy host for: " + url);
    } else if (s_httpsProxyHost != null && this.allowHttpsProxy && url.getProtocol().equals("https")
            && requiresProxy(url.getHost())) {
        routePlanner = new DefaultProxyRoutePlanner(s_httpsProxyHost);
        if (logger.isDebugEnabled())
            logger.debug(" - using HTTPS proxy host for: " + url);
    }

    return HttpClientBuilder.create().setConnectionManager(connectionManager).setRoutePlanner(routePlanner)
            .setRedirectStrategy(new RedirectStrategy() {
                // Switch off automatic redirect handling as we want to process them ourselves and maintain cookies
                public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)
                        throws ProtocolException {
                    return false;
                }

                public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response,
                        HttpContext context) throws ProtocolException {
                    return null;
                }
            })
            .setDefaultRequestConfig(
                    RequestConfig.custom().setStaleConnectionCheckEnabled(httpConnectionStalecheck)
                            .setConnectTimeout(connectTimeout).setSocketTimeout(readTimeout).build())
            .build();

    // TODO: this appears to have vanished from the config that can be set since httpclient 3.1->4.3
    //params.setBooleanParameter("http.tcp.nodelay", httpTcpNodelay);
}

From source file:com.evolveum.polygon.scim.StandardScimHandlingStrategy.java

protected HttpClient initHttpClient(ScimConnectorConfiguration conf) {
    HttpClientBuilder httpClientBulder = HttpClientBuilder.create();

    if (StringUtil.isNotEmpty(conf.getProxyUrl())) {
        HttpHost proxy = new HttpHost(conf.getProxyUrl(), conf.getProxyPortNumber());
        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
        httpClientBulder.setRoutePlanner(routePlanner);
    }/*from  w  w w  .  ja  v a 2  s  .c o m*/

    HttpClient httpClient = httpClientBulder.build();

    return httpClient;
}

From source file:org.codelibs.robot.client.http.HcHttpClient.java

protected HttpRoutePlanner buildRoutePlanner() {
    if (routePlanner != null) {
        return routePlanner;
    }/*from w w w.  ja v a2 s. com*/

    // proxy
    final String proxyHost = getInitParameter(PROXY_HOST_PROPERTY, this.proxyHost);
    final Integer proxyPort = getInitParameter(PROXY_PORT_PROPERTY, this.proxyPort);
    if (proxyHost != null && proxyPort != null) {
        final HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        final DefaultProxyRoutePlanner defaultRoutePlanner = new DefaultProxyRoutePlanner(proxy);

        final Credentials credentials = getInitParameter(PROXY_CREDENTIALS_PROPERTY, proxyCredentials);
        if (credentials != null) {
            credentialsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), credentials);
            final AuthScheme authScheme = getInitParameter(PROXY_AUTH_SCHEME_PROPERTY, proxyAuthScheme);
            if (authScheme != null) {
                authCache.put(proxy, authScheme);
            }
        }
        return defaultRoutePlanner;
    }
    return null;
}

From source file:org.whitesource.agent.client.WssServiceClientImpl.java

@Override
public void setProxy(String host, int port, String username, String password) {
    if (host == null || host.trim().length() == 0) {
        return;//from   ww w.j  a  v  a 2 s . co m
    }
    if (port < 0 || port > 65535) {
        return;
    }

    HttpHost proxy = new HttpHost(host, port);
    //      httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
    httpClient = HttpClients.custom().setRoutePlanner(routePlanner).build();
    logger.info("Using proxy: " + proxy.toHostString());

    if (username != null && username.trim().length() > 0) {
        logger.info("Proxy username: " + username);
        Credentials credentials;
        if (username.indexOf('/') >= 0) {
            credentials = new NTCredentials(username + ":" + password);
        } else if (username.indexOf('\\') >= 0) {
            username = username.replace('\\', '/');
            credentials = new NTCredentials(username + ":" + password);
        } else {
            credentials = new UsernamePasswordCredentials(username, password);
        }
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY, credentials);
        // TODO check
        httpClient = HttpClientBuilder.create().setProxy(proxy).setDefaultCredentialsProvider(credsProvider)
                .build();

        //            httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);
    }
}

From source file:uk.ac.ebi.phenotype.solr.indexer.AlleleIndexer.java

private void initializeSolrCores() {

    final String SANGER_ALLELE_URL = config.get("imits.solrserver");
    final String PHENODIGM_URL = config.get("phenodigm.solrserver");

    // Use system proxy if set for external solr servers
    if (System.getProperty("externalProxyHost") != null && System.getProperty("externalProxyPort") != null) {

        String PROXY_HOST = System.getProperty("externalProxyHost");
        Integer PROXY_PORT = Integer.parseInt(System.getProperty("externalProxyPort"));

        HttpHost proxy = new HttpHost(PROXY_HOST, PROXY_PORT);
        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
        CloseableHttpClient client = HttpClients.custom().setRoutePlanner(routePlanner).build();

        logger.info("Using Proxy Settings: " + PROXY_HOST + " on port: " + PROXY_PORT);

        this.sangerAlleleCore = new HttpSolrServer(SANGER_ALLELE_URL, client);
        this.phenodigmCore = new HttpSolrServer(PHENODIGM_URL, client);

    } else {//from w  w  w .jav  a 2  s  . c o  m

        this.sangerAlleleCore = new HttpSolrServer(SANGER_ALLELE_URL);
        this.phenodigmCore = new HttpSolrServer(PHENODIGM_URL);

    }
}