Example usage for org.apache.http.client.config RequestConfig custom

List of usage examples for org.apache.http.client.config RequestConfig custom

Introduction

In this page you can find the example usage for org.apache.http.client.config RequestConfig custom.

Prototype

public static RequestConfig.Builder custom() 

Source Link

Usage

From source file:info.bonjean.beluga.connection.BelugaHTTPClient.java

private BelugaHTTPClient() {
    BelugaConfiguration configuration = BelugaConfiguration.getInstance();
    HttpClientBuilder clientBuilder = HttpClients.custom();

    // timeout/*  w w  w  .j a  v  a  2  s  .c o m*/
    RequestConfig config = RequestConfig.custom().setConnectTimeout(TIMEOUT).setSocketTimeout(TIMEOUT)
            .setConnectionRequestTimeout(TIMEOUT).build();
    clientBuilder.setDefaultRequestConfig(config);

    switch (configuration.getConnectionType()) {
    case PROXY_DNS:
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", SSLConnectionSocketFactory.getSocketFactory()).build();
        BelugaDNSResolver dnsOverrider = new BelugaDNSResolver(DNSProxy.PROXY_DNS);
        connectionManager = new PoolingHttpClientConnectionManager(registry, dnsOverrider);
        break;
    case HTTP_PROXY:
        HttpHost proxy = new HttpHost(configuration.getProxyHost(), configuration.getProxyPort(), "http");
        clientBuilder.setProxy(proxy);
        break;
    default:
    }

    // limit the pool size
    connectionManager.setDefaultMaxPerRoute(2);

    // add interceptor, currently for debugging only
    clientBuilder.addInterceptorFirst(new HttpResponseInterceptor() {
        @Override
        public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
            HttpInetConnection connection = (HttpInetConnection) context
                    .getAttribute(HttpCoreContext.HTTP_CONNECTION);
            log.debug("Remote address: " + connection.getRemoteAddress());
            // TODO: reimplement blacklisting for DNS proxy by maintaining a
            // map [DNS IP,RESOLVED IP] in the DNS resolver for reverse
            // lookup
        }
    });

    // finally create the HTTP client
    clientBuilder.setConnectionManager(connectionManager);
    httpClient = clientBuilder.build();
}

From source file:net.siegmar.japtproxy.fetcher.HttpClientConfigurer.java

public CloseableHttpClient build() throws InitializationException {
    final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(MAX_CONNECTIONS);
    connectionManager.setDefaultMaxPerRoute(MAX_CONNECTIONS);

    final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connectTimeout)
            .setSocketTimeout(socketTimeout).build();

    final HttpClientBuilder httpClientBuilder = HttpClients.custom().setConnectionManager(connectionManager)
            .setDefaultRequestConfig(requestConfig);

    if (configuration.getHttpProxy() != null) {
        configureProxy(httpClientBuilder, configuration.getHttpProxy());
    }/*ww  w . j  a v  a 2s.  c  o  m*/

    return httpClientBuilder.build();
}

From source file:ld.ldhomework.crawler.DownloadTask.java

/**
 * Execute the actual download as separate thread. Best used together with
 * {@link Executors}. Handles mime types text/turtle and
 * application/rdf+xml. Avoids other content types and files larger than 1
 * MB./*from w  w  w. j av a 2  s. com*/
 */
public DownloadedFile call() throws Exception {
    DownloadedFile downloadedFile = null;
    HttpGet httpget = new HttpGet(currentURI);

    Builder configBuilder = RequestConfig.custom();
    configBuilder.setConnectTimeout(CONNECT_TIMEOUT);
    configBuilder.setSocketTimeout(SOCKET_TIMEOUT);
    configBuilder.setCircularRedirectsAllowed(false);
    httpget.setConfig(configBuilder.build());
    httpget.addHeader("Accept", "text/turtle,application/rdf+xml");

    CloseableHttpResponse response = null;
    try {
        LOG.info("Executing GET for " + currentURI);
        response = httpClient.execute(httpget);

        /*
         * Check the status code. 200 OK means we got the document.
         * Everything else needs to be handled, though the standard
         * HTTPClient Strategy includes automatic redirect handling
         * (probably following infinite redirects).
         */
        StatusLine statusLine = response.getStatusLine();
        HttpEntity entity = response.getEntity();

        LOG.info(currentURI + " cnttype: " + entity.getContentType());
        int responseStatusCode = statusLine.getStatusCode();
        LOG.info("Got status code " + responseStatusCode);

        if (responseStatusCode < 200 || responseStatusCode > 299) {
            LOG.info("Skipping document because got no 2xx (maybe even after being redirected)");
        } else if (entity.getContentLength() > ONE_MB
                || entity.getContentType().getValue().contains("application/rdf+xml")
                || entity.getContentType().getValue().contains("text/n3")
                || entity.getContentType().getValue().contains("text/turtle")) {

            downloadedFile = new DownloadedFile(entity.getContent(), entity.getContentType().getValue());

        }

    } catch (ClientProtocolException e) {

        LOG.severe("ClientProtocolException");
    } catch (IOException e) {
        LOG.severe("IOException");

    } catch (Exception e) {
        LOG.log(Level.SEVERE, "can't fetch document", e);
    } finally {
        try {
            if (response != null) {
                response.close();
            }
        } catch (IOException e) {
            LOG.warning("could not close response");
        }
    }

    return downloadedFile;

}

From source file:com.xx_dev.apn.proxy.test.TestProxyWithHttpClient.java

private void test(String uri, int exceptCode, String exceptHeaderName, String exceptHeaderValue) {
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Consts.UTF_8).build();

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(2000);//from   w  ww . j  a  v a2s.  c o  m
    cm.setDefaultMaxPerRoute(40);
    cm.setDefaultConnectionConfig(connectionConfig);

    CloseableHttpClient httpClient = HttpClients.custom()
            .setUserAgent("Mozilla/5.0 xx-dev-web-common httpclient/4.x").setConnectionManager(cm)
            .disableContentCompression().disableCookieManagement().build();

    HttpHost proxy = new HttpHost("127.0.0.1", ApnProxyConfig.getConfig().getPort());

    RequestConfig config = RequestConfig.custom().setProxy(proxy).setExpectContinueEnabled(true)
            .setConnectionRequestTimeout(5000).setConnectTimeout(10000).setSocketTimeout(10000)
            .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
    HttpGet request = new HttpGet(uri);
    request.setConfig(config);

    try {
        CloseableHttpResponse httpResponse = httpClient.execute(request);

        Assert.assertEquals(exceptCode, httpResponse.getStatusLine().getStatusCode());
        if (StringUtils.isNotBlank(exceptHeaderName) && StringUtils.isNotBlank(exceptHeaderValue)) {
            Assert.assertEquals(exceptHeaderValue, httpResponse.getFirstHeader(exceptHeaderName).getValue());
        }

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        responseHandler.handleResponse(httpResponse);

        httpResponse.close();
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }

}

From source file:com.ericsson.gerrit.plugins.highavailability.forwarder.rest.HttpClientProvider.java

private RequestConfig customRequestConfig() {
    return RequestConfig.custom().setConnectTimeout(cfg.http().connectionTimeout())
            .setSocketTimeout(cfg.http().socketTimeout())
            .setConnectionRequestTimeout(cfg.http().connectionTimeout()).build();
}

From source file:org.scassandra.http.client.CurrentClient.java

private CurrentClient(String host, int port, int socketTimeout) {
    RequestConfig.Builder requestBuilder = RequestConfig.custom();
    requestBuilder = requestBuilder.setConnectTimeout(socketTimeout);
    requestBuilder = requestBuilder.setConnectionRequestTimeout(socketTimeout);
    requestBuilder = requestBuilder.setSocketTimeout(socketTimeout);
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setDefaultRequestConfig(requestBuilder.build());
    httpClient = builder.build();/*from   w w w .j a v  a 2 s.c  o  m*/
    this.currentUrl = "http://" + host + ":" + port + "/current";
    this.connectionsUrl = this.currentUrl + "/connections";
    this.listenerUrl = this.currentUrl + "/listener";
}

From source file:nl.mineleni.cbsviewer.servlet.gazetteer.lusclient.OpenLSClient.java

/**
 * Maakt een nieuwe instance van de LUS client. Stelt de http proxy in mits
 * deze in de omgevingsvariabelen is gedefinieerd middels
 * {@code http.proxyHost} en {@code http.proxyPort}.
 *///from   w ww. j  a va  2 s  .  com
public OpenLSClient() {
    this.client = HttpClients.createSystem();
    this.requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();

    final String pHost = System.getProperty("http.proxyHost");
    int pPort = -1;
    try {
        pPort = Integer.valueOf(System.getProperty("http.proxyPort"));
    } catch (final NumberFormatException e) {
        LOGGER.debug("Geen proxy poort gedefinieerd.");
    }
    if ((null != pHost) && (pPort > 0)) {
        LOGGER.info("Instellen van proxy config: " + pHost + ":" + pPort);
        final HttpHost proxy = new HttpHost(pHost, pPort, "http");
        this.requestConfig = RequestConfig.copy(this.requestConfig).setProxy(proxy).build();
    } else {
        LOGGER.info("Er wordt geen proxy ingesteld.");
    }
    this.openLSResponseParser = new OpenLSGeocodeResponseParser();
    this.openLSReverseResponseParser = new OpenLSReverseGeocodeResponseParser();
}

From source file:com.linkedin.multitenant.db.ProxyDatabase.java

public ProxyDatabase() {
    RequestConfig rq = RequestConfig.custom().setStaleConnectionCheckEnabled(false).build();

    m_client = HttpClients.custom().setDefaultRequestConfig(rq).setMaxConnTotal(200).build();
    m_handler = new MyResponseHandler();
}