Search in sources :

Example 1 with SocketConfig

use of org.apache.http.config.SocketConfig in project webmagic by code4craft.

the class HttpClientGenerator method generateClient.

private CloseableHttpClient generateClient(Site site, Proxy proxy) {
    CredentialsProvider credsProvider = null;
    HttpClientBuilder httpClientBuilder = HttpClients.custom();
    if (proxy != null && StringUtils.isNotBlank(proxy.getUser()) && StringUtils.isNotBlank(proxy.getPassword())) {
        credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(proxy.getHttpHost().getAddress().getHostAddress(), proxy.getHttpHost().getPort()), new UsernamePasswordCredentials(proxy.getUser(), proxy.getPassword()));
        httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
    }
    if (site != null && site.getHttpProxy() != null && site.getUsernamePasswordCredentials() != null) {
        credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(//可以访问的范围
        new AuthScope(site.getHttpProxy()), //用户名和密码
        site.getUsernamePasswordCredentials());
        httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
    }
    httpClientBuilder.setConnectionManager(connectionManager);
    if (site != null && site.getUserAgent() != null) {
        httpClientBuilder.setUserAgent(site.getUserAgent());
    } else {
        httpClientBuilder.setUserAgent("");
    }
    if (site == null || site.isUseGzip()) {
        httpClientBuilder.addInterceptorFirst(new HttpRequestInterceptor() {

            public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
                if (!request.containsHeader("Accept-Encoding")) {
                    request.addHeader("Accept-Encoding", "gzip");
                }
            }
        });
    }
    //解决post/redirect/post 302跳转问题
    httpClientBuilder.setRedirectStrategy(new CustomRedirectStrategy());
    SocketConfig.Builder socketConfigBuilder = SocketConfig.custom();
    socketConfigBuilder.setSoKeepAlive(true).setTcpNoDelay(true);
    if (site != null) {
        socketConfigBuilder.setSoTimeout(site.getTimeOut());
    }
    SocketConfig socketConfig = socketConfigBuilder.build();
    httpClientBuilder.setDefaultSocketConfig(socketConfig);
    connectionManager.setDefaultSocketConfig(socketConfig);
    if (site != null) {
        httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(site.getRetryTimes(), true));
        generateCookie(httpClientBuilder, site);
    }
    return httpClientBuilder.build();
}
Also used : HttpRequest(org.apache.http.HttpRequest) SocketConfig(org.apache.http.config.SocketConfig) HttpContext(org.apache.http.protocol.HttpContext) CredentialsProvider(org.apache.http.client.CredentialsProvider) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) AuthScope(org.apache.http.auth.AuthScope) HttpException(org.apache.http.HttpException)

Example 2 with SocketConfig

use of org.apache.http.config.SocketConfig in project sling by apache.

the class HttpServerRule method before.

@Override
protected void before() throws Throwable {
    final SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(5000).build();
    serverBootstrap = ServerBootstrap.bootstrap().setSocketConfig(socketConfig).setServerInfo(ORIGIN);
    if (ProtocolScheme.https.equals(protocolScheme)) {
        serverBootstrap.setSslContext(SSLTestContexts.createServerSSLContext());
    }
    registerHandlers();
    server = serverBootstrap.create();
    server.start();
    host = new HttpHost("127.0.0.1", server.getLocalPort(), protocolScheme.name());
    uri = URIUtils.rewriteURI(new URI("/"), host);
}
Also used : SocketConfig(org.apache.http.config.SocketConfig) HttpHost(org.apache.http.HttpHost) URI(java.net.URI)

Example 3 with SocketConfig

use of org.apache.http.config.SocketConfig in project sling by apache.

the class HttpServerRule method before.

@Override
protected void before() throws Throwable {
    final SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(5000).build();
    serverBootstrap = ServerBootstrap.bootstrap().setSocketConfig(socketConfig).setServerInfo(ORIGIN);
    if (ProtocolScheme.https.equals(protocolScheme)) {
        serverBootstrap.setSslContext(SSLTestContexts.createServerSSLContext());
    }
    registerHandlers();
    server = serverBootstrap.create();
    server.start();
    host = new HttpHost("127.0.0.1", server.getLocalPort(), protocolScheme.name());
    uri = URIUtils.rewriteURI(new URI("/"), host);
}
Also used : SocketConfig(org.apache.http.config.SocketConfig) HttpHost(org.apache.http.HttpHost) URI(java.net.URI)

Example 4 with SocketConfig

use of org.apache.http.config.SocketConfig in project stanbol by apache.

the class MultiThreadedTestBase method initialiseHttpClient.

@Before
public void initialiseHttpClient() {
    if (this.pooledHttpClient == null) {
        //init for the first test
        RequestConfig requestConfig = RequestConfig.custom().setRedirectsEnabled(true).setMaxRedirects(3).build();
        SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).build();
        connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setDefaultSocketConfig(socketConfig);
        connectionManager.setMaxTotal(20);
        connectionManager.setDefaultMaxPerRoute(20);
        pooledHttpClient = HttpClientBuilder.create().setUserAgent("Stanbol Integration Test").setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build();
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) SocketConfig(org.apache.http.config.SocketConfig) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) Before(org.junit.Before)

Example 5 with SocketConfig

use of org.apache.http.config.SocketConfig in project dropwizard by dropwizard.

the class HttpClientBuilder method createClient.

/**
     * Map the parameters in {@link HttpClientConfiguration} to configuration on a
     * {@link org.apache.http.impl.client.HttpClientBuilder} instance
     *
     * @param builder
     * @param manager
     * @param name
     * @return the configured {@link CloseableHttpClient}
     */
protected ConfiguredCloseableHttpClient createClient(final org.apache.http.impl.client.HttpClientBuilder builder, final InstrumentedHttpClientConnectionManager manager, final String name) {
    final String cookiePolicy = configuration.isCookiesEnabled() ? CookieSpecs.DEFAULT : CookieSpecs.IGNORE_COOKIES;
    final Integer timeout = (int) configuration.getTimeout().toMilliseconds();
    final Integer connectionTimeout = (int) configuration.getConnectionTimeout().toMilliseconds();
    final Integer connectionRequestTimeout = (int) configuration.getConnectionRequestTimeout().toMilliseconds();
    final long keepAlive = configuration.getKeepAlive().toMilliseconds();
    final ConnectionReuseStrategy reuseStrategy = keepAlive == 0 ? new NoConnectionReuseStrategy() : new DefaultConnectionReuseStrategy();
    final HttpRequestRetryHandler retryHandler = configuration.getRetries() == 0 ? NO_RETRIES : (httpRequestRetryHandler == null ? new DefaultHttpRequestRetryHandler(configuration.getRetries(), false) : httpRequestRetryHandler);
    final RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(cookiePolicy).setSocketTimeout(timeout).setConnectTimeout(connectionTimeout).setConnectionRequestTimeout(connectionRequestTimeout).build();
    final SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).setSoTimeout(timeout).build();
    customizeBuilder(builder).setRequestExecutor(new InstrumentedHttpRequestExecutor(metricRegistry, metricNameStrategy, name)).setConnectionManager(manager).setDefaultRequestConfig(requestConfig).setDefaultSocketConfig(socketConfig).setConnectionReuseStrategy(reuseStrategy).setRetryHandler(retryHandler).setUserAgent(createUserAgent(name));
    if (keepAlive != 0) {
        // either keep alive based on response header Keep-Alive,
        // or if the server can keep a persistent connection (-1), then override based on client's configuration
        builder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {

            @Override
            public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                final long duration = super.getKeepAliveDuration(response, context);
                return (duration == -1) ? keepAlive : duration;
            }
        });
    }
    // create a tunnel through a proxy host if it's specified in the config
    final ProxyConfiguration proxy = configuration.getProxyConfiguration();
    if (proxy != null) {
        final HttpHost httpHost = new HttpHost(proxy.getHost(), proxy.getPort(), proxy.getScheme());
        builder.setRoutePlanner(new NonProxyListProxyRoutePlanner(httpHost, proxy.getNonProxyHosts()));
        // if the proxy host requires authentication then add the host credentials to the credentials provider
        final AuthConfiguration auth = proxy.getAuth();
        if (auth != null) {
            if (credentialsProvider == null) {
                credentialsProvider = new BasicCredentialsProvider();
            }
            credentialsProvider.setCredentials(new AuthScope(httpHost), new UsernamePasswordCredentials(auth.getUsername(), auth.getPassword()));
        }
    }
    if (credentialsProvider != null) {
        builder.setDefaultCredentialsProvider(credentialsProvider);
    }
    if (routePlanner != null) {
        builder.setRoutePlanner(routePlanner);
    }
    if (disableContentCompression) {
        builder.disableContentCompression();
    }
    if (redirectStrategy != null) {
        builder.setRedirectStrategy(redirectStrategy);
    }
    if (defaultHeaders != null) {
        builder.setDefaultHeaders(defaultHeaders);
    }
    if (verifier != null) {
        builder.setSSLHostnameVerifier(verifier);
    }
    if (httpProcessor != null) {
        builder.setHttpProcessor(httpProcessor);
    }
    return new ConfiguredCloseableHttpClient(builder.build(), requestConfig);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) SocketConfig(org.apache.http.config.SocketConfig) NonProxyListProxyRoutePlanner(io.dropwizard.client.proxy.NonProxyListProxyRoutePlanner) DefaultConnectionReuseStrategy(org.apache.http.impl.DefaultConnectionReuseStrategy) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) AuthConfiguration(io.dropwizard.client.proxy.AuthConfiguration) InstrumentedHttpRequestExecutor(com.codahale.metrics.httpclient.InstrumentedHttpRequestExecutor) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) NoConnectionReuseStrategy(org.apache.http.impl.NoConnectionReuseStrategy) ProxyConfiguration(io.dropwizard.client.proxy.ProxyConfiguration) DefaultConnectionKeepAliveStrategy(org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) NoConnectionReuseStrategy(org.apache.http.impl.NoConnectionReuseStrategy) DefaultConnectionReuseStrategy(org.apache.http.impl.DefaultConnectionReuseStrategy) ConnectionReuseStrategy(org.apache.http.ConnectionReuseStrategy) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) HttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler)

Aggregations

SocketConfig (org.apache.http.config.SocketConfig)5 HttpHost (org.apache.http.HttpHost)3 URI (java.net.URI)2 AuthScope (org.apache.http.auth.AuthScope)2 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)2 RequestConfig (org.apache.http.client.config.RequestConfig)2 HttpContext (org.apache.http.protocol.HttpContext)2 InstrumentedHttpRequestExecutor (com.codahale.metrics.httpclient.InstrumentedHttpRequestExecutor)1 AuthConfiguration (io.dropwizard.client.proxy.AuthConfiguration)1 NonProxyListProxyRoutePlanner (io.dropwizard.client.proxy.NonProxyListProxyRoutePlanner)1 ProxyConfiguration (io.dropwizard.client.proxy.ProxyConfiguration)1 IOException (java.io.IOException)1 ConnectionReuseStrategy (org.apache.http.ConnectionReuseStrategy)1 HttpException (org.apache.http.HttpException)1 HttpRequest (org.apache.http.HttpRequest)1 HttpRequestInterceptor (org.apache.http.HttpRequestInterceptor)1 HttpResponse (org.apache.http.HttpResponse)1 CredentialsProvider (org.apache.http.client.CredentialsProvider)1 HttpRequestRetryHandler (org.apache.http.client.HttpRequestRetryHandler)1 DefaultConnectionReuseStrategy (org.apache.http.impl.DefaultConnectionReuseStrategy)1