Example usage for org.apache.http.impl.client HttpClientBuilder setRoutePlanner

List of usage examples for org.apache.http.impl.client HttpClientBuilder setRoutePlanner

Introduction

In this page you can find the example usage for org.apache.http.impl.client HttpClientBuilder setRoutePlanner.

Prototype

public final HttpClientBuilder setRoutePlanner(final HttpRoutePlanner routePlanner) 

Source Link

Document

Assigns HttpRoutePlanner instance.

Usage

From source file:org.georchestra.security.Proxy.java

private void handleRequest(HttpServletRequest request, HttpServletResponse finalResponse,
        RequestType requestType, String sURL, boolean localProxy) {
    HttpClientBuilder htb = HttpClients.custom().disableRedirectHandling();

    RequestConfig config = RequestConfig.custom().setSocketTimeout(this.httpClientTimeout).build();
    htb.setDefaultRequestConfig(config);

    //// w ww . jav a 2 s  .c  o m
    // Handle http proxy for external request.
    // Proxy must be configured by system variables (e.g.: -Dhttp.proxyHost=proxy -Dhttp.proxyPort=3128)
    htb.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()));
    HttpClient httpclient = htb.build();

    HttpResponse proxiedResponse = null;
    int statusCode = 500;

    try {
        URL url = null;
        try {
            url = new URL(sURL);
        } catch (MalformedURLException e) { // not an url
            finalResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
            return;
        }

        // HTTP protocol is required
        if (!"http".equalsIgnoreCase(url.getProtocol()) && !"https".equalsIgnoreCase(url.getProtocol())) {
            finalResponse.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    "HTTP protocol expected. \"" + url.getProtocol() + "\" used.");
            return;
        }

        // check if proxy must filter on final host
        if (!strategyForFilteringRequests.allowRequest(url)) {
            finalResponse.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    "Host \"" + url.getHost() + "\" is not allowed to be requested");
            return;
        }

        logger.debug("Final request -- " + sURL);

        HttpRequestBase proxyingRequest = makeRequest(request, requestType, sURL);
        headerManagement.configureRequestHeaders(request, proxyingRequest);

        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            Header[] originalHeaders = proxyingRequest.getHeaders("sec-org");
            String org = "";
            for (Header originalHeader : originalHeaders) {
                org = originalHeader.getValue();
            }
            // no OGC SERVICE log if request going through /proxy/?url=
            if (!request.getRequestURI().startsWith("/sec/proxy/")) {
                String[] roles = new String[] { "" };
                try {
                    Header[] rolesHeaders = proxyingRequest.getHeaders("sec-roles");
                    if (rolesHeaders.length > 0) {
                        roles = rolesHeaders[0].getValue().split(";");
                    }
                } catch (Exception e) {
                    logger.error("Unable to compute roles");
                }
                statsLogger.info(OGCServiceMessageFormatter.format(authentication.getName(), sURL, org, roles));

            }

        } catch (Exception e) {
            logger.error("Unable to log the request into the statistics logger", e);
        }

        if (localProxy) {
            //
            // Hack for geoserver
            // Should not be here. We must use a ProxyTarget class and
            // define
            // if Host header should be forwarded or not.
            //
            request.getHeader("Host");
            proxyingRequest.setHeader("Host", request.getHeader("Host"));

            if (logger.isDebugEnabled()) {
                logger.debug("Host header set to: " + proxyingRequest.getFirstHeader("Host").getValue()
                        + " for proxy request.");
            }
        }
        proxiedResponse = executeHttpRequest(httpclient, proxyingRequest);
        StatusLine statusLine = proxiedResponse.getStatusLine();
        statusCode = statusLine.getStatusCode();
        String reasonPhrase = statusLine.getReasonPhrase();

        if (reasonPhrase != null && statusCode > 399) {
            if (logger.isWarnEnabled()) {
                logger.warn("Error occurred. statuscode: " + statusCode + ", reason: " + reasonPhrase);
            }

            if (statusCode == 401) {
                //
                // Handle case of basic authentication.
                //
                Header authHeader = proxiedResponse.getFirstHeader("WWW-Authenticate");
                finalResponse.setHeader("WWW-Authenticate",
                        (authHeader == null) ? "Basic realm=\"Authentication required\""
                                : authHeader.getValue());
            }

            // 403 and 404 are handled by specific JSP files provided by the
            // security-proxy webapp
            if ((statusCode == 404) || (statusCode == 403)) {
                finalResponse.sendError(statusCode);
                return;
            }
        }

        headerManagement.copyResponseHeaders(request, request.getRequestURI(), proxiedResponse, finalResponse,
                this.targets);

        if (statusCode == 302 || statusCode == 301) {
            adjustLocation(request, proxiedResponse, finalResponse);
        }
        // get content type

        String contentType = null;
        if (proxiedResponse.getEntity() != null && proxiedResponse.getEntity().getContentType() != null) {
            contentType = proxiedResponse.getEntity().getContentType().getValue();
            logger.debug("content-type detected: " + contentType);
        }

        // content type has to be valid
        if (isCharsetRequiredForContentType(contentType)) {
            doHandleRequestCharsetRequired(request, finalResponse, requestType, proxiedResponse, contentType);
        } else {
            logger.debug("charset not required for contentType: " + contentType);
            doHandleRequest(request, finalResponse, requestType, proxiedResponse);
        }
    } catch (IOException e) {
        // connection problem with the host
        logger.error("Exception occured when trying to connect to the remote host: ", e);
        try {
            finalResponse.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        } catch (IOException e2) {
            // error occured while trying to return the
            // "service unavailable status"
            finalResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

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);
    }/*  w  w  w  . j  ava2 s . co m*/

    HttpClient httpClient = httpClientBulder.build();

    return httpClient;
}

From source file:com.arangodb.http.HttpManager.java

public void init() {
    // socket factory for HTTP
    ConnectionSocketFactory plainsf = new PlainConnectionSocketFactory();

    // socket factory for HTTPS
    SSLConnectionSocketFactory sslsf = null;
    if (configure.getSslContext() != null) {
        sslsf = new SSLConnectionSocketFactory(configure.getSslContext());
    } else {//from   w  w w.j a v a  2 s  .c  o m
        sslsf = new SSLConnectionSocketFactory(SSLContexts.createSystemDefault());
    }

    // register socket factories
    Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", plainsf).register("https", sslsf).build();

    // ConnectionManager
    cm = new PoolingHttpClientConnectionManager(r);
    cm.setDefaultMaxPerRoute(configure.getMaxPerConnection());
    cm.setMaxTotal(configure.getMaxTotalConnection());

    Builder custom = RequestConfig.custom();

    // RequestConfig
    if (configure.getConnectionTimeout() >= 0) {
        custom.setConnectTimeout(configure.getConnectionTimeout());
    }
    if (configure.getTimeout() >= 0) {
        custom.setConnectionRequestTimeout(configure.getTimeout());
        custom.setSocketTimeout(configure.getTimeout());
    }
    custom.setStaleConnectionCheckEnabled(configure.isStaleConnectionCheck());

    RequestConfig requestConfig = custom.build();

    HttpClientBuilder builder = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig);
    builder.setConnectionManager(cm);

    // KeepAlive Strategy
    ConnectionKeepAliveStrategy keepAliveStrategy = new ConnectionKeepAliveStrategy() {

        @Override
        public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
            // Honor 'keep-alive' header
            HeaderElementIterator it = new BasicHeaderElementIterator(
                    response.headerIterator(HTTP.CONN_KEEP_ALIVE));
            while (it.hasNext()) {
                HeaderElement he = it.nextElement();
                String param = he.getName();
                String value = he.getValue();
                if (value != null && param.equalsIgnoreCase("timeout")) {
                    try {
                        return Long.parseLong(value) * 1000;
                    } catch (NumberFormatException ignore) {
                    }
                }
            }
            // otherwise keep alive for 30 seconds
            return 30 * 1000;
        }

    };
    builder.setKeepAliveStrategy(keepAliveStrategy);

    // Retry Handler
    builder.setRetryHandler(new DefaultHttpRequestRetryHandler(configure.getRetryCount(), false));

    // Proxy
    if (configure.getProxyHost() != null && configure.getProxyPort() != 0) {
        HttpHost proxy = new HttpHost(configure.getProxyHost(), configure.getProxyPort(), "http");

        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
        builder.setRoutePlanner(routePlanner);
    }

    // Client
    client = builder.build();

    // Basic Auth
    // if (configure.getUser() != null && configure.getPassword() != null) {
    // AuthScope scope = AuthScope.ANY; // TODO
    // this.credentials = new
    // UsernamePasswordCredentials(configure.getUser(),
    // configure.getPassword());
    // client.getCredentialsProvider().setCredentials(scope, credentials);
    // }

}

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

public synchronized void init() {
    if (httpClient != null) {
        return;//from  ww w  .  j  a  v a2s.  c  o m
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Initializing " + HcHttpClient.class.getName());
    }

    super.init();

    // robots.txt parser
    final Boolean robotsTxtEnabled = getInitParameter(ROBOTS_TXT_ENABLED_PROPERTY, Boolean.TRUE, Boolean.class);
    if (robotsTxtHelper != null) {
        robotsTxtHelper.setEnabled(robotsTxtEnabled.booleanValue());
    }

    // httpclient
    final org.apache.http.client.config.RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

    final Integer connectionTimeoutParam = getInitParameter(CONNECTION_TIMEOUT_PROPERTY, connectionTimeout,
            Integer.class);
    if (connectionTimeoutParam != null) {
        requestConfigBuilder.setConnectTimeout(connectionTimeoutParam);

    }
    final Integer soTimeoutParam = getInitParameter(SO_TIMEOUT_PROPERTY, soTimeout, Integer.class);
    if (soTimeoutParam != null) {
        requestConfigBuilder.setSocketTimeout(soTimeoutParam);
    }

    // AuthSchemeFactory
    final RegistryBuilder<AuthSchemeProvider> authSchemeProviderBuilder = RegistryBuilder.create();
    @SuppressWarnings("unchecked")
    final Map<String, AuthSchemeProvider> factoryMap = getInitParameter(AUTH_SCHEME_PROVIDERS_PROPERTY,
            authSchemeProviderMap, Map.class);
    if (factoryMap != null) {
        for (final Map.Entry<String, AuthSchemeProvider> entry : factoryMap.entrySet()) {
            authSchemeProviderBuilder.register(entry.getKey(), entry.getValue());
        }
    }

    // user agent
    userAgent = getInitParameter(USER_AGENT_PROPERTY, userAgent, String.class);
    if (StringUtil.isNotBlank(userAgent)) {
        httpClientBuilder.setUserAgent(userAgent);
    }

    final HttpRoutePlanner planner = buildRoutePlanner();
    if (planner != null) {
        httpClientBuilder.setRoutePlanner(planner);
    }

    // Authentication
    final Authentication[] siteCredentialList = getInitParameter(BASIC_AUTHENTICATIONS_PROPERTY,
            new Authentication[0], Authentication[].class);
    for (final Authentication authentication : siteCredentialList) {
        final AuthScope authScope = authentication.getAuthScope();
        credentialsProvider.setCredentials(authScope, authentication.getCredentials());
        final AuthScheme authScheme = authentication.getAuthScheme();
        if (authScope.getHost() != null && authScheme != null) {
            final HttpHost targetHost = new HttpHost(authScope.getHost(), authScope.getPort());
            authCache.put(targetHost, authScheme);
        }
    }

    httpClientContext.setAuthCache(authCache);
    httpClientContext.setCredentialsProvider(credentialsProvider);

    // Request Header
    final RequestHeader[] requestHeaders = getInitParameter(REQUERT_HEADERS_PROPERTY, new RequestHeader[0],
            RequestHeader[].class);
    for (final RequestHeader requestHeader : requestHeaders) {
        if (requestHeader.isValid()) {
            requestHeaderList.add(new BasicHeader(requestHeader.getName(), requestHeader.getValue()));
        }
    }

    // do not redirect
    requestConfigBuilder
            .setRedirectsEnabled(getInitParameter(REDIRECTS_ENABLED, redirectsEnabled, Boolean.class));

    // cookie
    if (cookieSpec != null) {
        requestConfigBuilder.setCookieSpec(cookieSpec);
    }

    // cookie store
    httpClientBuilder.setDefaultCookieStore(cookieStore);
    if (cookieStore != null) {
        final Cookie[] cookies = getInitParameter(COOKIES_PROPERTY, new Cookie[0], Cookie[].class);
        for (final Cookie cookie : cookies) {
            cookieStore.addCookie(cookie);
        }
    }

    connectionMonitorTask = TimeoutManager.getInstance().addTimeoutTarget(
            new HcConnectionMonitorTarget(clientConnectionManager, idleConnectionTimeout),
            connectionCheckInterval, true);

    final CloseableHttpClient closeableHttpClient = httpClientBuilder
            .setConnectionManager(clientConnectionManager).setDefaultRequestConfig(requestConfigBuilder.build())
            .build();
    if (!httpClientPropertyMap.isEmpty()) {
        final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(closeableHttpClient.getClass());
        for (final Map.Entry<String, Object> entry : httpClientPropertyMap.entrySet()) {
            final String propertyName = entry.getKey();
            if (beanDesc.hasPropertyDesc(propertyName)) {
                final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(propertyName);
                propertyDesc.setValue(closeableHttpClient, entry.getValue());
            } else {
                logger.warn("DefaultHttpClient does not have " + propertyName + ".");
            }
        }
    }

    httpClient = closeableHttpClient;
}

From source file:io.github.thred.climatetray.ClimateTrayProxySettings.java

public CloseableHttpClient createHttpClient(String... additionalProxyExcludes) {
    if (proxyType == ProxyType.NONE) {
        return HttpClients.createDefault();
    }//from  w ww.j  a  v a  2  s.co  m

    if (proxyType == ProxyType.SYSTEM_DEFAULT) {
        return HttpClients.createSystem();
    }

    HttpHost proxy = new HttpHost(getProxyHost(), getProxyPort());
    HttpClientBuilder builder = HttpClientBuilder.create().setProxy(proxy);

    if (isProxyAuthorizationNeeded()) {
        Credentials credentials = new UsernamePasswordCredentials(getProxyUser(), getProxyPassword());
        AuthScope authScope = new AuthScope(getProxyHost(), getProxyPort());
        CredentialsProvider credsProvider = new BasicCredentialsProvider();

        credsProvider.setCredentials(authScope, credentials);

        builder.setDefaultCredentialsProvider(credsProvider);
    }

    String excludes = proxyExcludes;

    if (Utils.isBlank(excludes)) {
        excludes = "";
    }

    for (String additionalProxyExclude : additionalProxyExcludes) {
        if (excludes.length() > 0) {
            excludes += ", ";
        }

        excludes += additionalProxyExclude;
    }

    if (!Utils.isBlank(excludes)) {
        WildcardPattern pattern = new WildcardPattern(excludes.split("\\s*,\\s*"));
        HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy) {
            @Override
            public HttpRoute determineRoute(HttpHost host, HttpRequest request, HttpContext context)
                    throws HttpException {
                InetAddress address = host.getAddress();

                if (address == null) {
                    try {
                        address = InetAddress.getByName(host.getHostName());
                    } catch (UnknownHostException e) {
                        ClimateTray.LOG.info("Failed to determine address of host \"%s\"", host.getHostName());
                    }
                }

                if (address != null) {
                    String hostAddress = address.getHostAddress();

                    if (pattern.matches(hostAddress)) {
                        return new HttpRoute(host);
                    }
                }

                String hostName = host.getHostName();

                if (pattern.matches(hostName)) {
                    return new HttpRoute(host);
                }

                return super.determineRoute(host, request, context);
            }
        };

        builder.setRoutePlanner(routePlanner);
    }

    return builder.build();
}

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

public synchronized void init() {
    if (httpClient != null) {
        return;//w  w w .  java  2 s. co m
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Initializing " + HcHttpClient.class.getName());
    }

    // access timeout
    final Integer accessTimeoutParam = getInitParameter(ACCESS_TIMEOUT_PROPERTY, accessTimeout);
    if (accessTimeoutParam != null) {
        accessTimeout = accessTimeoutParam;
    }

    // robots.txt parser
    final Boolean robotsTxtEnabled = getInitParameter(ROBOTS_TXT_ENABLED_PROPERTY, Boolean.TRUE);
    if (robotsTxtHelper != null) {
        robotsTxtHelper.setEnabled(robotsTxtEnabled.booleanValue());
    }

    // httpclient
    final org.apache.http.client.config.RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

    final Integer connectionTimeoutParam = getInitParameter(CONNECTION_TIMEOUT_PROPERTY, connectionTimeout);
    if (connectionTimeoutParam != null) {
        requestConfigBuilder.setConnectTimeout(connectionTimeoutParam);

    }
    final Boolean staleCheckingEnabledParam = getInitParameter(STALE_CHECKING_ENABLED_PROPERTY,
            staleCheckingEnabled);
    if (staleCheckingEnabledParam != null) {
        requestConfigBuilder.setStaleConnectionCheckEnabled(staleCheckingEnabledParam);
    }
    final Integer soTimeoutParam = getInitParameter(SO_TIMEOUT_PROPERTY, soTimeout);
    if (soTimeoutParam != null) {
        requestConfigBuilder.setSocketTimeout(soTimeoutParam);
    }

    // AuthSchemeFactory
    final RegistryBuilder<AuthSchemeProvider> authSchemeProviderBuilder = RegistryBuilder.create();
    final Map<String, AuthSchemeProvider> factoryMap = getInitParameter(AUTH_SCHEME_PROVIDERS_PROPERTY,
            authSchemeProviderMap);
    if (factoryMap != null) {
        for (final Map.Entry<String, AuthSchemeProvider> entry : factoryMap.entrySet()) {
            authSchemeProviderBuilder.register(entry.getKey(), entry.getValue());
        }
    }

    // user agent
    userAgent = getInitParameter(USER_AGENT_PROPERTY, userAgent);
    if (StringUtil.isNotBlank(userAgent)) {
        httpClientBuilder.setUserAgent(userAgent);
    }

    CredentialsProvider credsProvider = null;
    AuthCache authCache = null;

    // 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 routePlanner = new DefaultProxyRoutePlanner(proxy);
        httpClientBuilder.setRoutePlanner(routePlanner);

        final Credentials credentials = getInitParameter(PROXY_CREDENTIALS_PROPERTY, proxyCredentials);
        if (credentials != null) {
            authCache = new BasicAuthCache();
            credsProvider = new BasicCredentialsProvider();

            credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), credentials);
            final AuthScheme authScheme = getInitParameter(PROXY_AUTH_SCHEME_PROPERTY, proxyAuthScheme);
            if (authScheme != null) {
                authCache.put(proxy, authScheme);
            }
        }
    }

    // Authentication
    final Authentication[] siteCredentialList = getInitParameter(BASIC_AUTHENTICATIONS_PROPERTY,
            new Authentication[0]);
    if (siteCredentialList != null && siteCredentialList.length > 0 && authCache == null) {
        authCache = new BasicAuthCache();
        credsProvider = new BasicCredentialsProvider();
    }
    for (final Authentication authentication : siteCredentialList) {
        final AuthScope authScope = authentication.getAuthScope();
        credsProvider.setCredentials(authScope, authentication.getCredentials());
        final AuthScheme authScheme = authentication.getAuthScheme();
        if (authScope.getHost() != null && authScheme != null) {
            final HttpHost targetHost = new HttpHost(authScope.getHost(), authScope.getPort());
            authCache.put(targetHost, authScheme);
        }
    }
    if (authCache != null) {
        httpClientContext.setAuthCache(authCache);
        httpClientContext.setCredentialsProvider(credsProvider);
    }

    // Request Header
    final RequestHeader[] requestHeaders = getInitParameter(REQUERT_HEADERS_PROPERTY, new RequestHeader[0]);
    for (final RequestHeader requestHeader : requestHeaders) {
        if (requestHeader.isValid()) {
            requestHeaderList.add(new BasicHeader(requestHeader.getName(), requestHeader.getValue()));
        }
    }

    // do not redirect
    requestConfigBuilder.setRedirectsEnabled(false);

    // cookie
    if (cookieSpec != null) {
        requestConfigBuilder.setCookieSpec(cookieSpec);
    }

    // cookie store
    httpClientBuilder.setDefaultCookieStore(cookieStore);
    if (cookieStore != null) {
        final Cookie[] cookies = getInitParameter(COOKIES_PROPERTY, new Cookie[0]);
        for (final Cookie cookie : cookies) {
            cookieStore.addCookie(cookie);
        }
    }

    connectionMonitorTask = TimeoutManager.getInstance().addTimeoutTarget(
            new HcConnectionMonitorTarget(clientConnectionManager, idleConnectionTimeout),
            connectionCheckInterval, true);

    final CloseableHttpClient closeableHttpClient = httpClientBuilder
            .setConnectionManager(clientConnectionManager).setDefaultRequestConfig(requestConfigBuilder.build())
            .build();
    if (!httpClientPropertyMap.isEmpty()) {
        final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(closeableHttpClient.getClass());
        for (final Map.Entry<String, Object> entry : httpClientPropertyMap.entrySet()) {
            final String propertyName = entry.getKey();
            if (beanDesc.hasPropertyDesc(propertyName)) {
                final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(propertyName);
                propertyDesc.setValue(closeableHttpClient, entry.getValue());
            } else {
                logger.warn("DefaultHttpClient does not have " + propertyName + ".");
            }
        }
    }

    httpClient = closeableHttpClient;
}

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(// w  w w. j  a  va2s.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:eu.europa.esig.dss.client.http.commons.CommonsDataLoader.java

/**
 * Configure the proxy with the required credential if needed
 *
 * @param httpClientBuilder/*from  w w w  .jav  a 2  s  .  co m*/
 * @param credentialsProvider
 * @param url
 * @return
 */
private HttpClientBuilder configureProxy(HttpClientBuilder httpClientBuilder,
        CredentialsProvider credentialsProvider, String url) throws DSSException {

    if (proxyPreferenceManager == null) {
        return httpClientBuilder;
    }
    try {

        final String protocol = new URL(url).getProtocol();
        final boolean proxyHTTPS = Protocol.isHttps(protocol) && proxyPreferenceManager.isHttpsEnabled();
        final boolean proxyHTTP = Protocol.isHttp(protocol) && proxyPreferenceManager.isHttpEnabled();

        if (!proxyHTTPS && !proxyHTTP) {
            return httpClientBuilder;
        }

        String proxyHost = null;
        int proxyPort = 0;
        String proxyUser = null;
        String proxyPassword = null;
        String proxyExcludedHosts = null;

        if (proxyHTTPS) {

            LOG.debug("Use proxy https parameters");
            final Long port = proxyPreferenceManager.getHttpsPort();
            proxyPort = port != null ? port.intValue() : 0;
            proxyHost = proxyPreferenceManager.getHttpsHost();
            proxyUser = proxyPreferenceManager.getHttpsUser();
            proxyPassword = proxyPreferenceManager.getHttpsPassword();
            proxyExcludedHosts = proxyPreferenceManager.getHttpsExcludedHosts();
        } else if (proxyHTTP) { // noinspection ConstantConditions

            LOG.debug("Use proxy http parameters");
            final Long port = proxyPreferenceManager.getHttpPort();
            proxyPort = port != null ? port.intValue() : 0;
            proxyHost = proxyPreferenceManager.getHttpHost();
            proxyUser = proxyPreferenceManager.getHttpUser();
            proxyPassword = proxyPreferenceManager.getHttpPassword();
            proxyExcludedHosts = proxyPreferenceManager.getHttpExcludedHosts();
        }
        if (StringUtils.isNotEmpty(proxyUser) && StringUtils.isNotEmpty(proxyPassword)) {

            AuthScope proxyAuth = new AuthScope(proxyHost, proxyPort);
            UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(proxyUser,
                    proxyPassword);
            credentialsProvider.setCredentials(proxyAuth, proxyCredentials);
        }

        LOG.debug("proxy host/port: " + proxyHost + ":" + proxyPort);
        // TODO SSL peer shut down incorrectly when protocol is https
        final HttpHost proxy = new HttpHost(proxyHost, proxyPort, Protocol.HTTP.getName());

        if (StringUtils.isNotEmpty(proxyExcludedHosts)) {
            final String[] hosts = proxyExcludedHosts.split("[,; ]");

            HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy) {
                @Override
                public HttpRoute determineRoute(final HttpHost host, final HttpRequest request,
                        final HttpContext context) throws HttpException {

                    String hostname = (host != null ? host.getHostName() : null);

                    if ((hosts != null) && (hostname != null)) {
                        for (String h : hosts) {
                            if (hostname.equalsIgnoreCase(h)) {
                                // bypass proxy for that hostname
                                return new HttpRoute(host);
                            }
                        }
                    }
                    return super.determineRoute(host, request, context);
                }
            };

            httpClientBuilder.setRoutePlanner(routePlanner);
        }

        final HttpClientBuilder httpClientBuilder1 = httpClientBuilder.setProxy(proxy);
        updated = false;
        return httpClientBuilder1;
    } catch (MalformedURLException e) {
        throw new DSSException(e);
    }
}

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

public synchronized void init() {
    if (httpClient != null) {
        return;//from w w  w.  j a  va 2s.c o  m
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Initializing " + HcHttpClient.class.getName());
    }

    // access timeout
    final Integer accessTimeoutParam = getInitParameter(ACCESS_TIMEOUT_PROPERTY, accessTimeout);
    if (accessTimeoutParam != null) {
        accessTimeout = accessTimeoutParam;
    }

    // robots.txt parser
    final Boolean robotsTxtEnabled = getInitParameter(ROBOTS_TXT_ENABLED_PROPERTY, Boolean.TRUE);
    if (robotsTxtHelper != null) {
        robotsTxtHelper.setEnabled(robotsTxtEnabled.booleanValue());
    }

    // httpclient
    final org.apache.http.client.config.RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

    final Integer connectionTimeoutParam = getInitParameter(CONNECTION_TIMEOUT_PROPERTY, connectionTimeout);
    if (connectionTimeoutParam != null) {
        requestConfigBuilder.setConnectTimeout(connectionTimeoutParam);

    }
    final Boolean staleCheckingEnabledParam = getInitParameter(STALE_CHECKING_ENABLED_PROPERTY,
            staleCheckingEnabled);
    if (staleCheckingEnabledParam != null) {
        requestConfigBuilder.setStaleConnectionCheckEnabled(staleCheckingEnabledParam);
    }
    final Integer soTimeoutParam = getInitParameter(SO_TIMEOUT_PROPERTY, soTimeout);
    if (soTimeoutParam != null) {
        requestConfigBuilder.setSocketTimeout(soTimeoutParam);
    }

    // AuthSchemeFactory
    final RegistryBuilder<AuthSchemeProvider> authSchemeProviderBuilder = RegistryBuilder.create();
    final Map<String, AuthSchemeProvider> factoryMap = getInitParameter(AUTH_SCHEME_PROVIDERS_PROPERTY,
            authSchemeProviderMap);
    if (factoryMap != null) {
        for (final Map.Entry<String, AuthSchemeProvider> entry : factoryMap.entrySet()) {
            authSchemeProviderBuilder.register(entry.getKey(), entry.getValue());
        }
    }

    // user agent
    userAgent = getInitParameter(USER_AGENT_PROPERTY, userAgent);
    if (StringUtil.isNotBlank(userAgent)) {
        httpClientBuilder.setUserAgent(userAgent);
    }

    final HttpRoutePlanner planner = buildRoutePlanner();
    if (planner != null) {
        httpClientBuilder.setRoutePlanner(planner);
    }

    // Authentication
    final Authentication[] siteCredentialList = getInitParameter(BASIC_AUTHENTICATIONS_PROPERTY,
            new Authentication[0]);
    for (final Authentication authentication : siteCredentialList) {
        final AuthScope authScope = authentication.getAuthScope();
        credentialsProvider.setCredentials(authScope, authentication.getCredentials());
        final AuthScheme authScheme = authentication.getAuthScheme();
        if (authScope.getHost() != null && authScheme != null) {
            final HttpHost targetHost = new HttpHost(authScope.getHost(), authScope.getPort());
            authCache.put(targetHost, authScheme);
        }
    }

    httpClientContext.setAuthCache(authCache);
    httpClientContext.setCredentialsProvider(credentialsProvider);

    // Request Header
    final RequestHeader[] requestHeaders = getInitParameter(REQUERT_HEADERS_PROPERTY, new RequestHeader[0]);
    for (final RequestHeader requestHeader : requestHeaders) {
        if (requestHeader.isValid()) {
            requestHeaderList.add(new BasicHeader(requestHeader.getName(), requestHeader.getValue()));
        }
    }

    // do not redirect
    requestConfigBuilder.setRedirectsEnabled(false);

    // cookie
    if (cookieSpec != null) {
        requestConfigBuilder.setCookieSpec(cookieSpec);
    }

    // cookie store
    httpClientBuilder.setDefaultCookieStore(cookieStore);
    if (cookieStore != null) {
        final Cookie[] cookies = getInitParameter(COOKIES_PROPERTY, new Cookie[0]);
        for (final Cookie cookie : cookies) {
            cookieStore.addCookie(cookie);
        }
    }

    connectionMonitorTask = TimeoutManager.getInstance().addTimeoutTarget(
            new HcConnectionMonitorTarget(clientConnectionManager, idleConnectionTimeout),
            connectionCheckInterval, true);

    final CloseableHttpClient closeableHttpClient = httpClientBuilder
            .setConnectionManager(clientConnectionManager).setDefaultRequestConfig(requestConfigBuilder.build())
            .build();
    if (!httpClientPropertyMap.isEmpty()) {
        final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(closeableHttpClient.getClass());
        for (final Map.Entry<String, Object> entry : httpClientPropertyMap.entrySet()) {
            final String propertyName = entry.getKey();
            if (beanDesc.hasPropertyDesc(propertyName)) {
                final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(propertyName);
                propertyDesc.setValue(closeableHttpClient, entry.getValue());
            } else {
                logger.warn("DefaultHttpClient does not have " + propertyName + ".");
            }
        }
    }

    httpClient = closeableHttpClient;
}

From source file:org.opennms.core.web.HttpClientWrapper.java

public CloseableHttpClient getClient() {
    if (m_httpClient == null) {
        final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();

        if (!m_reuseConnections) {
            httpClientBuilder.setConnectionReuseStrategy(new NoConnectionReuseStrategy());
        }/*from w w  w  . j av a2 s  .  c  om*/
        if (m_usePreemptiveAuth) {
            enablePreemptiveAuth(httpClientBuilder);
        }
        if (m_useSystemProxySettings) {
            httpClientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()));
        }
        if (!isEmpty(m_cookieSpec)) {
            requestConfigBuilder.setCookieSpec(m_cookieSpec);
        }
        if (m_cookieStore != null) {
            httpClientBuilder.setDefaultCookieStore(m_cookieStore);
        }
        if (m_username != null) {
            setCredentials(httpClientBuilder, m_username, m_password);
        }
        if (m_socketTimeout != null) {
            requestConfigBuilder.setSocketTimeout(m_socketTimeout);
        }
        if (m_connectionTimeout != null) {
            requestConfigBuilder.setConnectTimeout(m_connectionTimeout);
        }
        if (m_retries != null) {
            httpClientBuilder.setRetryHandler(new HttpRequestRetryOnExceptionHandler(m_retries, false));
        }
        if (m_sslContext.size() != 0) {
            configureSSLContext(httpClientBuilder);
        }
        for (final HttpRequestInterceptor interceptor : m_requestInterceptors) {
            httpClientBuilder.addInterceptorLast(interceptor);
        }
        for (final HttpResponseInterceptor interceptor : m_responseInterceptors) {
            httpClientBuilder.addInterceptorLast(interceptor);
        }
        if (m_useLaxRedirect) {
            httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy());
        }
        httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
        m_httpClient = httpClientBuilder.build();
    }
    return m_httpClient;
}