org.apache.http.config.ConnectionConfig Java Examples

The following examples show how to use org.apache.http.config.ConnectionConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpGetter.java    From commafeed with Apache License 2.0 6 votes vote down vote up
public static CloseableHttpClient newClient(int timeout) {
	HttpClientBuilder builder = HttpClients.custom();
	builder.useSystemProperties();
	builder.addInterceptorFirst(REMOVE_INCORRECT_CONTENT_ENCODING);
	builder.disableAutomaticRetries();

	builder.setSSLContext(SSL_CONTEXT);
	builder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);

	RequestConfig.Builder configBuilder = RequestConfig.custom();
	configBuilder.setCookieSpec(CookieSpecs.IGNORE_COOKIES);
	configBuilder.setSocketTimeout(timeout);
	configBuilder.setConnectTimeout(timeout);
	configBuilder.setConnectionRequestTimeout(timeout);
	builder.setDefaultRequestConfig(configBuilder.build());

	builder.setDefaultConnectionConfig(ConnectionConfig.custom().setCharset(Consts.ISO_8859_1).build());

	return builder.build();
}
 
Example #2
Source File: TracingManagedHttpClientConnectionFactory.java    From caravan with Apache License 2.0 6 votes vote down vote up
@Override
public ManagedHttpClientConnection create(final HttpRoute route, final ConnectionConfig config) {
    final ConnectionConfig cconfig = config != null ? config : ConnectionConfig.DEFAULT;
    CharsetDecoder chardecoder = null;
    CharsetEncoder charencoder = null;
    final Charset charset = cconfig.getCharset();
    final CodingErrorAction malformedInputAction = cconfig.getMalformedInputAction() != null ? cconfig.getMalformedInputAction() : CodingErrorAction.REPORT;
    final CodingErrorAction unmappableInputAction = cconfig.getUnmappableInputAction() != null ? cconfig.getUnmappableInputAction()
            : CodingErrorAction.REPORT;
    if (charset != null) {
        chardecoder = charset.newDecoder();
        chardecoder.onMalformedInput(malformedInputAction);
        chardecoder.onUnmappableCharacter(unmappableInputAction);
        charencoder = charset.newEncoder();
        charencoder.onMalformedInput(malformedInputAction);
        charencoder.onUnmappableCharacter(unmappableInputAction);
    }
    final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
    return new TracingManagedHttpClientConnection(id, cconfig.getBufferSize(), cconfig.getFragmentSizeHint(), chardecoder, charencoder,
            cconfig.getMessageConstraints(), incomingContentStrategy, outgoingContentStrategy, requestWriterFactory, responseParserFactory, logFunc);
}
 
Example #3
Source File: DefaultHttpClientGenerator.java    From vscrawler with Apache License 2.0 6 votes vote down vote up
public static CrawlerHttpClientBuilder setDefault(CrawlerHttpClientBuilder proxyFeedBackDecorateHttpClientBuilder) {
    SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setSoLinger(-1).setSoReuseAddress(false)
            .setSoTimeout(ProxyConstant.SOCKETSO_TIMEOUT).setTcpNoDelay(true).build();
    proxyFeedBackDecorateHttpClientBuilder.setDefaultSocketConfig(socketConfig)
            // .setSSLSocketFactory(sslConnectionSocketFactory)
            // dungproxy0.0.6之后的版本,默认忽略https证书检查
            .setRedirectStrategy(new LaxRedirectStrategy())
            // 注意,这里使用ua生产算法自动产生ua,如果是mobile,可以使用
            // com.virjar.vscrawler.core.net.useragent.UserAgentBuilder.randomAppUserAgent()
            .setUserAgent(UserAgentBuilder.randomUserAgent())
            // 对于爬虫来说,连接池没啥卵用,直接禁止掉(因为我们可能创建大量HttpClient,每个HttpClient一个连接池,会把系统socket资源撑爆)
            // 测试开80个httpClient抓数据大概一个小时系统就会宕机
            .setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE)
            // 现在有些网站的网络响应头部数据有非ASCII数据,httpclient默认只使用ANSI标准解析header,这可能导致带有中文的header数据无法解析
            .setDefaultConnectionConfig(ConnectionConfig.custom().setCharset(Charsets.UTF_8).build());
    return proxyFeedBackDecorateHttpClientBuilder;
}
 
Example #4
Source File: YunpianClient.java    From yunpian-java-sdk with MIT License 6 votes vote down vote up
private CloseableHttpAsyncClient createHttpAsyncClient(YunpianConf conf) throws IOReactorException {
    IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setIoThreadCount(Runtime.getRuntime().availableProcessors())
            .setConnectTimeout(conf.getConfInt(YunpianConf.HTTP_CONN_TIMEOUT, "10000"))
            .setSoTimeout(conf.getConfInt(YunpianConf.HTTP_SO_TIMEOUT, "30000")).build();
    ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);

    PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE)
            .setUnmappableInputAction(CodingErrorAction.IGNORE)
            .setCharset(Charset.forName(conf.getConf(YunpianConf.HTTP_CHARSET, YunpianConf.HTTP_CHARSET_DEFAULT))).build();
    connManager.setDefaultConnectionConfig(connectionConfig);
    connManager.setMaxTotal(conf.getConfInt(YunpianConf.HTTP_CONN_MAXTOTAL, "100"));
    connManager.setDefaultMaxPerRoute(conf.getConfInt(YunpianConf.HTTP_CONN_MAXPERROUTE, "10"));

    CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setConnectionManager(connManager).build();
    httpclient.start();
    return httpclient;
}
 
Example #5
Source File: HttpClientManagerImplTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testPrepareUserAgentHeaderSetOnBuilder() {
  // Setup
  String expectedUserAgentHeader = "Nexus/Agent my user agent";
  HttpClientPlan plan = mock(HttpClientPlan.class);
  doReturn(expectedUserAgentHeader).when(plan).getUserAgent();
  HttpClientBuilder builder = mock(HttpClientBuilder.class);
  doReturn(builder).when(plan).getClient();

  ConnectionConfig.Builder conn = mock(ConnectionConfig.Builder.class);
  SocketConfig.Builder sock = mock(SocketConfig.Builder.class);
  RequestConfig.Builder req = mock(RequestConfig.Builder.class);
  doReturn(null).when(conn).build();
  doReturn(null).when(sock).build();
  doReturn(null).when(req).build();
  doReturn(conn).when(plan).getConnection();
  doReturn(sock).when(plan).getSocket();
  doReturn(req).when(plan).getRequest();

  HttpClientManagerImpl spy = spy(underTest);
  doReturn(plan).when(spy).httpClientPlan();

  // Execute
  HttpClientBuilder returned = spy.prepare(null);

  // Verify
  assertNotNull("Returned builder must not be null.", returned);
  assertEquals("Returned builder must be expected builder.", builder, returned);
  verify(spy).setUserAgent(builder, expectedUserAgentHeader);
}
 
Example #6
Source File: JsonRpcHttpAsyncClient.java    From jsonrpc4j with MIT License 5 votes vote down vote up
private void initialize() {
	if (initialized.getAndSet(true)) {
		return;
	}
	IOReactorConfig.Builder config = createConfig();
	// params.setParameter(CoreProtocolPNames.USER_AGENT, "jsonrpc4j/1.0");
	final ConnectingIOReactor ioReactor = createIoReactor(config);
	createSslContext();
	int socketBufferSize = Integer.getInteger("com.googlecode.jsonrpc4j.async.socket.buffer", 8 * 1024);
	final ConnectionConfig connectionConfig = ConnectionConfig.custom().setBufferSize(socketBufferSize).build();
	BasicNIOConnFactory nioConnFactory = new BasicNIOConnFactory(sslContext, null, connectionConfig);
	pool = new BasicNIOConnPool(ioReactor, nioConnFactory, Integer.getInteger("com.googlecode.jsonrpc4j.async.connect.timeout", 30000));
	pool.setDefaultMaxPerRoute(Integer.getInteger("com.googlecode.jsonrpc4j.async.max.inflight.route", 500));
	pool.setMaxTotal(Integer.getInteger("com.googlecode.jsonrpc4j.async.max.inflight.total", 500));
	
	Thread t = new Thread(new Runnable() {
		@Override
		public void run() {
			try {
				HttpAsyncRequestExecutor protocolHandler = new HttpAsyncRequestExecutor();
				IOEventDispatch ioEventDispatch = new DefaultHttpClientIODispatch(protocolHandler, sslContext, connectionConfig);
				ioReactor.execute(ioEventDispatch);
			} catch (InterruptedIOException ex) {
				System.err.println("Interrupted");
			} catch (IOException e) {
				System.err.println("I/O error: " + e.getMessage());
			}
		}
	}, "jsonrpc4j HTTP IOReactor");
	
	t.setDaemon(true);
	t.start();
	
	HttpProcessor httpProcessor = new ImmutableHttpProcessor(new RequestContent(), new RequestTargetHost(), new RequestConnControl(), new RequestUserAgent(), new RequestExpectContinue(false));
	requester = new HttpAsyncRequester(httpProcessor, new DefaultConnectionReuseStrategy());
}
 
Example #7
Source File: EndPointClient.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
/**
 * Get client.
 *
 * @return the client
 */
private synchronized CloseableHttpClient getClient() {
    if (client == null) {
        RequestConfig.Builder requestBuilder = RequestConfig.custom();
        requestBuilder.setConnectTimeout(connectionTimeout);

        ConnectionConfig.Builder connBuilder = ConnectionConfig.custom();
        connBuilder.setCharset(Charset.forName(getContentCharset()));

        // Create and initialize scheme registry
        RegistryBuilder<ConnectionSocketFactory> builder = RegistryBuilder.create();
        builder.register("http", getPlainFactory());
        builder.register("https", getSslFactory());
        Registry<ConnectionSocketFactory> registry = builder.build();

        HttpClientConnectionManager hccm = createClientConnectionManager(registry);

        HttpClientBuilder clientBuilder = HttpClients.custom();
        clientBuilder.setDefaultRequestConfig(requestBuilder.build());
        clientBuilder.setDefaultConnectionConfig(connBuilder.build());
        clientBuilder.setConnectionManager(hccm);

        client = clientBuilder.build();
    }

    return client;
}
 
Example #8
Source File: BceHttpClient.java    From bce-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Create asynchronous http client based on connection manager.
 *
 * @param connectionManager Asynchronous http client connection manager.
 * @return Asynchronous http client based on connection manager.
 */
protected CloseableHttpAsyncClient createHttpAsyncClient(NHttpClientConnectionManager connectionManager) {
    HttpAsyncClientBuilder builder = HttpAsyncClients.custom().setConnectionManager(connectionManager);

    int socketBufferSizeInBytes = this.config.getSocketBufferSizeInBytes();
    if (socketBufferSizeInBytes > 0) {
        builder.setDefaultConnectionConfig(
                ConnectionConfig.custom().setBufferSize(socketBufferSizeInBytes).build());
    }
    return builder.build();
}
 
Example #9
Source File: BceHttpClient.java    From bce-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Create http client based on connection manager.
 *
 * @param connectionManager The connection manager setting http client.
 * @return Http client based on connection manager.
 */
private CloseableHttpClient createHttpClient(HttpClientConnectionManager connectionManager) {
    HttpClientBuilder builder =
            HttpClients.custom().setConnectionManager(connectionManager).disableAutomaticRetries();

    int socketBufferSizeInBytes = this.config.getSocketBufferSizeInBytes();
    if (socketBufferSizeInBytes > 0) {
        builder.setDefaultConnectionConfig(
                ConnectionConfig.custom().setBufferSize(socketBufferSizeInBytes).build());
    }

    return builder.build();
}
 
Example #10
Source File: SimpleUrlContentReader.java    From cosmo with Apache License 2.0 5 votes vote down vote up
private CloseableHttpClient buildClient(int timeoutInMillis, URL url) {
    RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(timeoutInMillis)
            .setConnectTimeout(timeoutInMillis).setRedirectsEnabled(true).setMaxRedirects(MAX_REDIRECTS)
            .setProxy(this.proxyFactory.getProxy(url)).build();
    return HttpClientBuilder.create().setDefaultRequestConfig(config)
            .setDefaultConnectionConfig(
                    ConnectionConfig
                            .custom().setMessageConstraints(MessageConstraints.custom()
                                    .setMaxHeaderCount(MAX_HEADER_COUNT).setMaxLineLength(MAX_LINE_LENGTH).build())
                            .build())
            .build();
}
 
Example #11
Source File: HttpUtils.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
/**
 * 创建 HttpClient 连接池.
 */
private PoolingHttpClientConnectionManager createHttpClientConnPool(ConnectionConfig connectionConfig) {
    PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager(
            RegistryBuilder.<ConnectionSocketFactory>create().
                    register("http", PlainConnectionSocketFactory.getSocketFactory()).
                    register("https", buildSSLConn()).build());
    httpClientConnectionManager.setMaxTotal(MAX_TOTAL); // 设置连接池线程最大数量
    httpClientConnectionManager.setDefaultMaxPerRoute(MAX_ROUTE_TOTAL); // 设置单个路由最大的连接线程数量
    httpClientConnectionManager.setDefaultConnectionConfig(connectionConfig);
    return httpClientConnectionManager;
}
 
Example #12
Source File: HttpUtils.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
private void initialize() {
    // Connection 配置
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE)
            .setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8).build();
    coverCA();
    httpClientContext();
    HttpClientBuilder httpClientBuilder = initHttpClient(createHttpClientConnPool(connectionConfig),
            requestConfig(), redirectStrategy(), retryPolicy());
    if (HttpUtils.userAgent != null) httpClientBuilder.setUserAgent(userAgent);
    httpClient = httpClientBuilder.build();
}
 
Example #13
Source File: ArchiveTask.java    From archivo with GNU General Public License v3.0 5 votes vote down vote up
private CloseableHttpClient buildHttpClient() {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials("tivo", mak));
    CookieStore cookieStore = new BasicCookieStore();
    ConnectionConfig connConfig = ConnectionConfig.custom().setBufferSize(BUFFER_SIZE).build();
    return HttpClients.custom()
            .useSystemProperties()
            .setDefaultConnectionConfig(connConfig)
            .setDefaultCredentialsProvider(credsProvider)
            .setDefaultCookieStore(cookieStore)
            .setUserAgent(Archivo.USER_AGENT)
            .build();
}
 
Example #14
Source File: HttpClientPlan.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
public HttpClientPlan() {
  this.client = HttpClientBuilder.create();
  this.connection = ConnectionConfig.copy(ConnectionConfig.DEFAULT);
  this.socket = SocketConfig.copy(SocketConfig.DEFAULT);
  this.request = RequestConfig.copy(RequestConfig.DEFAULT);
  this.headers = new HashMap<>();
  this.attributes = new HashMap<>();
}
 
Example #15
Source File: HttpClientPool.java    From message_interface with MIT License 5 votes vote down vote up
private static CloseableHttpClient client() {
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setDefaultMaxPerRoute(100);
    cm.setMaxTotal(400);

    MessageConstraints messageConstraints = MessageConstraints.custom()
            .setMaxHeaderCount(200)
            .setMaxLineLength(2000)
            .build();

    ConnectionConfig connectionConfig = ConnectionConfig.custom()
            .setMalformedInputAction(CodingErrorAction.IGNORE)
            .setUnmappableInputAction(CodingErrorAction.IGNORE)
            .setCharset(Consts.UTF_8)
            .setMessageConstraints(messageConstraints)
            .build();

    RequestConfig defaultRequestConfig = RequestConfig.custom()
            .setSocketTimeout(5000)
            .setConnectTimeout(5000)
            .setConnectionRequestTimeout(5000)
            .build();

    cm.setDefaultConnectionConfig(connectionConfig);

    return HttpClients.custom()
            .setConnectionManager(cm)
            .setDefaultRequestConfig(defaultRequestConfig)
            .build();
}
 
Example #16
Source File: Main.java    From fahrschein with Apache License 2.0 5 votes vote down vote up
private static void subscriptionListenHttpComponents(ObjectMapper objectMapper, Listener<SalesOrderPlaced> listener) throws IOException {
    final RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(60000)
            .setConnectTimeout(2000)
            .setConnectionRequestTimeout(8000)
            .setContentCompressionEnabled(false)
            .build();

    final ConnectionConfig connectionConfig = ConnectionConfig.custom()
            .setBufferSize(512)
            .build();

    final CloseableHttpClient httpClient = HttpClients.custom()
            .setDefaultRequestConfig(requestConfig)
            .setDefaultConnectionConfig(connectionConfig)
            .setConnectionTimeToLive(30, TimeUnit.SECONDS)
            .disableAutomaticRetries()
            .disableRedirectHandling()
            .setMaxConnTotal(8)
            .setMaxConnPerRoute(2)
            .build();

    final RequestFactory requestFactory = new HttpComponentsRequestFactory(httpClient);

    final NakadiClient nakadiClient = NakadiClient.builder(NAKADI_URI)
            .withRequestFactory(requestFactory)
            .withAccessTokenProvider(new ZignAccessTokenProvider())
            .build();

    final Subscription subscription = nakadiClient.subscription("fahrschein-demo", SALES_ORDER_SERVICE_ORDER_PLACED)
            .withConsumerGroup("fahrschein-demo")
            .readFromEnd()
            .subscribe();

    nakadiClient.stream(subscription)
            .withObjectMapper(objectMapper)
            .listen(SalesOrderPlaced.class, listener);
}
 
Example #17
Source File: MCRHttpUtils.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public static CloseableHttpClient getHttpClient(HttpClientConnectionManager connectionManager, int maxConnections) {

        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).build();

        ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(StandardCharsets.UTF_8).build();
        SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).setSoKeepAlive(true)
            .setSoReuseAddress(true).build();

        //setup http client
        return HttpClients.custom().setConnectionManager(connectionManager)
            .setUserAgent(getHttpUserAgent()).setRetryHandler(new MCRRetryHandler(maxConnections))
            .setDefaultRequestConfig(requestConfig).setDefaultConnectionConfig(connectionConfig)
            .setDefaultSocketConfig(socketConfig).build();
    }
 
Example #18
Source File: FetchingThread.java    From BUbiNG with Apache License 2.0 5 votes vote down vote up
/** Creates a new fetching thread.
 *
 * @param frontier a reference to the {@link Frontier}.
 * @param index  the index of this thread (only for logging purposes).
 */
public FetchingThread(final Frontier frontier, final int index) throws NoSuchAlgorithmException, IllegalArgumentException, IOException {
	setName(this.getClass().getSimpleName() + '-' + index);
	setPriority(Thread.MIN_PRIORITY); // Low priority; there will be thousands of this guys around.
	this.frontier = frontier;

	final BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManagerWithAlternateDNS(frontier.rc.dnsResolver);
	connManager.closeIdleConnections(0, TimeUnit.MILLISECONDS);
	connManager.setConnectionConfig(ConnectionConfig.custom().setBufferSize(8 * 1024).build()); // TODO: make this configurable

	cookieStore = new BasicCookieStore();

	final BasicHeader[] headers = {
		new BasicHeader("From", frontier.rc.userAgentFrom),
		new BasicHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.95,text/*;q=0.9,*/*;q=0.8")
	};

	httpClient = HttpClients.custom()
			.setSSLContext(frontier.rc.acceptAllCertificates ? TRUST_ALL_CERTIFICATES_SSL_CONTEXT : TRUST_SELF_SIGNED_SSL_CONTEXT)
			.setConnectionManager(connManager)
			.setConnectionReuseStrategy(frontier.rc.keepAliveTime == 0 ? NoConnectionReuseStrategy.INSTANCE : DefaultConnectionReuseStrategy.INSTANCE)
			.setUserAgent(frontier.rc.userAgent)
			.setDefaultCookieStore(cookieStore)
			.setDefaultHeaders(ObjectArrayList.wrap(headers))
			.build();
  		fetchData = new FetchData(frontier.rc);
}
 
Example #19
Source File: ApacheConnectionManagerFactory.java    From ibm-cos-sdk-java with Apache License 2.0 5 votes vote down vote up
private ConnectionConfig buildConnectionConfig(HttpClientSettings settings) {

        int socketBufferSize = Math.max(settings.getSocketBufferSize()[0],
                settings.getSocketBufferSize()[1]);

        return socketBufferSize <= 0
                ? null
                : ConnectionConfig.custom()
                .setBufferSize(socketBufferSize)
                .build();
    }
 
Example #20
Source File: AsyncHttpClientGenerator.java    From cetty with Apache License 2.0 5 votes vote down vote up
@Override
protected CloseableHttpAsyncClient build(Payload payload) {
    HttpAsyncClientBuilder asyncClientBuilder = HttpAsyncClients.custom();

    if (StringUtils.isNotBlank(payload.getUserAgent())) {
        asyncClientBuilder.setUserAgent(payload.getUserAgent());
    } else {
        asyncClientBuilder.setUserAgent("");
    }

    asyncClientBuilder.setRedirectStrategy(new CustomRedirectStrategy());

    asyncClientBuilder.setConnectionManagerShared(true);

    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(payload.getConnectTimeout())
            .setSocketTimeout(payload.getSocketTimeout()).build();

    ConnectionConfig connectionConfig = ConnectionConfig.custom()
            .setMalformedInputAction(CodingErrorAction.IGNORE)
            .setUnmappableInputAction(CodingErrorAction.IGNORE)
            .setCharset(Consts.UTF_8).build();

    poolingNHttpClientConnectionManager.setDefaultConnectionConfig(connectionConfig);
    asyncClientBuilder.setConnectionManager(poolingNHttpClientConnectionManager);
    asyncClientBuilder.setDefaultRequestConfig(requestConfig);
    if (payload.getProxy() != null) {
        Proxy proxy = payload.getProxy();
        HttpHost httpHost = new HttpHost(proxy.getHost(), proxy.getPort(), proxy.getScheme());
        asyncClientBuilder.setProxy(httpHost);
    }
    reduceCookie(asyncClientBuilder,payload);
    return asyncClientBuilder.build();
}
 
Example #21
Source File: HttpClientUtil.java    From feiqu-opensource with Apache License 2.0 4 votes vote down vote up
private static void init() {
    try {
        SSLContext sslContext =
                SSLContexts.custom()
                        .loadTrustMaterial(KeyStore.getInstance(KeyStore.getDefaultType()), new TrustStrategy() {
                            @Override
                            public boolean isTrusted(X509Certificate[] chain, String authType)
                                    throws CertificateException {
                                return true;
                            }
                        }).build();
        SSLConnectionSocketFactory sslSFactory =
                new SSLConnectionSocketFactory(sslContext);
        Registry<ConnectionSocketFactory> socketFactoryRegistry =
                RegistryBuilder.<ConnectionSocketFactory>create()
                        .register("http", PlainConnectionSocketFactory.INSTANCE).register("https", sslSFactory)
                        .build();

        PoolingHttpClientConnectionManager connManager =
                new PoolingHttpClientConnectionManager(socketFactoryRegistry);

        SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(CommonConstant.TIMEOUT).setTcpNoDelay(true).build();
        connManager.setDefaultSocketConfig(socketConfig);

        ConnectionConfig connectionConfig =
                ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE)
                        .setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8).build();
        connManager.setDefaultConnectionConfig(connectionConfig);
        connManager.setMaxTotal(500);
        connManager.setDefaultMaxPerRoute(300);
        HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {
            @Override
            public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
                if (executionCount > 2) {
                    return false;
                }
                if (exception instanceof InterruptedIOException) {
                    return true;
                }
                if (exception instanceof ConnectTimeoutException) {
                    return true;
                }
                if (exception instanceof UnknownHostException) {
                    return true;
                }
                if (exception instanceof SSLException) {
                    return true;
                }
                HttpRequest request = HttpClientContext.adapt(context).getRequest();
                if (!(request instanceof HttpEntityEnclosingRequest)) {
                    return true;
                }
                return false;
            }
        };
        HttpClientBuilder httpClientBuilder =
                HttpClients.custom().setConnectionManager(connManager)
                        .setRetryHandler(retryHandler)
                        .setDefaultCookieStore(new BasicCookieStore()).setUserAgent(userAgent);
        if (proxy != null) {
            httpClientBuilder.setRoutePlanner(new DefaultProxyRoutePlanner(proxy)).build();
        }
        httpClient = httpClientBuilder.build();

        requestConfig = RequestConfig.custom().setSocketTimeout(CommonConstant.TIMEOUT).
                setConnectTimeout(CommonConstant.TIMEOUT).
                setConnectionRequestTimeout(CommonConstant.TIMEOUT).
                setCookieSpec(CookieSpecs.STANDARD).
                build();
    } catch (Exception e) {
        logger.error("Exception:", e);
    }
}
 
Example #22
Source File: HttpClientPlan.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
public ConnectionConfig.Builder getConnection() {
  return connection;
}
 
Example #23
Source File: SimpleHttpClientConnectionManager.java    From lavaplayer with Apache License 2.0 4 votes vote down vote up
public void setConnectionConfig(ConnectionConfig config) {
  this.connectionConfig = config;
}
 
Example #24
Source File: ClickHouseHttpClientBuilder.java    From clickhouse-jdbc with Apache License 2.0 4 votes vote down vote up
private ConnectionConfig getConnectionConfig() {
    return ConnectionConfig.custom()
            .setBufferSize(properties.getApacheBufferSize())
            .build();
}
 
Example #25
Source File: HttpFetch.java    From activitystreams with Apache License 2.0 4 votes vote down vote up
public Builder connectionConfig(ConnectionConfig config) {
  defaultConnectionConfig = config;
  return this;
}
 
Example #26
Source File: HttpFetch.java    From activitystreams with Apache License 2.0 4 votes vote down vote up
public Builder connectionConfig(HttpHost host, ConnectionConfig config) {
  connectionConfigs.put(host,config);
  return this;
}
 
Example #27
Source File: HttpConnectionPoolBuilder.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param proxy    Proxy configuration
 * @param listener Log listener
 * @param prompt   Prompt for proxy credentials
 * @return Builder for HTTP client
 */
public HttpClientBuilder build(final Proxy proxy, final TranscriptListener listener, final LoginCallback prompt) {
    final HttpClientBuilder configuration = HttpClients.custom();
    // Use HTTP Connect proxy implementation provided here instead of
    // relying on internal proxy support in socket factory
    switch(proxy.getType()) {
        case HTTP:
        case HTTPS:
            final HttpHost h = new HttpHost(proxy.getHostname(), proxy.getPort(), Scheme.http.name());
            if(log.isInfoEnabled()) {
                log.info(String.format("Setup proxy %s", h));
            }
            configuration.setProxy(h);
            configuration.setProxyAuthenticationStrategy(new CallbackProxyAuthenticationStrategy(ProxyCredentialsStoreFactory.get(), host, prompt));
            break;
    }
    configuration.setUserAgent(new PreferencesUseragentProvider().get());
    final int timeout = preferences.getInteger("connection.timeout.seconds") * 1000;
    configuration.setDefaultSocketConfig(SocketConfig.custom()
        .setTcpNoDelay(true)
        .setSoTimeout(timeout)
        .build());
    configuration.setDefaultRequestConfig(this.createRequestConfig(timeout));
    configuration.setDefaultConnectionConfig(ConnectionConfig.custom()
        .setBufferSize(preferences.getInteger("http.socket.buffer"))
        .setCharset(Charset.forName(host.getEncoding()))
        .build());
    if(preferences.getBoolean("http.connections.reuse")) {
        configuration.setConnectionReuseStrategy(new DefaultClientConnectionReuseStrategy());
    }
    else {
        configuration.setConnectionReuseStrategy(new NoConnectionReuseStrategy());
    }
    configuration.setRetryHandler(new ExtendedHttpRequestRetryHandler(preferences.getInteger("http.connections.retry")));
    configuration.setServiceUnavailableRetryStrategy(new DisabledServiceUnavailableRetryStrategy());
    if(!preferences.getBoolean("http.compression.enable")) {
        configuration.disableContentCompression();
    }
    configuration.setRequestExecutor(new LoggingHttpRequestExecutor(listener));
    // Always register HTTP for possible use with proxy. Contains a number of protocol properties such as the
    // default port and the socket factory to be used to create the java.net.Socket instances for the given protocol
    configuration.setConnectionManager(this.createConnectionManager(this.createRegistry()));
    configuration.setDefaultAuthSchemeRegistry(RegistryBuilder.<AuthSchemeProvider>create()
        .register(AuthSchemes.BASIC, new BasicSchemeFactory(
            Charset.forName(preferences.getProperty("http.credentials.charset"))))
        .register(AuthSchemes.DIGEST, new DigestSchemeFactory(
            Charset.forName(preferences.getProperty("http.credentials.charset"))))
        .register(AuthSchemes.NTLM, preferences.getBoolean("webdav.ntlm.windows.authentication.enable") && WinHttpClients.isWinAuthAvailable() ?
            new BackportWindowsNTLMSchemeFactory(null) :
            new NTLMSchemeFactory())
        .register(AuthSchemes.SPNEGO, preferences.getBoolean("webdav.ntlm.windows.authentication.enable") && WinHttpClients.isWinAuthAvailable() ?
            new BackportWindowsNegotiateSchemeFactory(null) :
            new SPNegoSchemeFactory())
        .register(AuthSchemes.KERBEROS, new KerberosSchemeFactory()).build());
    return configuration;
}
 
Example #28
Source File: ElasticEndpoint.java    From sagacity-sqltoy with Apache License 2.0 4 votes vote down vote up
/**
 * @param restClient the restClient to set
 */
public void initRestClient() {
	if (StringUtil.isBlank(this.getUrl()))
		return;
	if (restClient == null) {
		// 替换全角字符
		String[] urls = this.getUrl().replaceAll("\\;", ";").replaceAll("\\,", ",").replaceAll("\\;", ",")
				.split("\\,");
		// 当为单一地址时使用httpclient直接调用
		if (urls.length < 2)
			return;
		List<HttpHost> hosts = new ArrayList<HttpHost>();
		for (String urlStr : urls) {
			try {
				if (StringUtil.isNotBlank(urlStr)) {
					URL url = new java.net.URL(urlStr.trim());
					hosts.add(new HttpHost(url.getHost(), url.getPort(), url.getProtocol()));
				}
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
		}
		if (!hosts.isEmpty()) {
			HttpHost[] hostAry = new HttpHost[hosts.size()];
			hosts.toArray(hostAry);
			RestClientBuilder builder = RestClient.builder(hostAry);
			final ConnectionConfig connectionConfig = ConnectionConfig.custom()
					.setCharset(Charset.forName(this.charset == null ? "UTF-8" : this.charset)).build();
			RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(this.requestTimeout)
					.setConnectTimeout(this.connectTimeout).setSocketTimeout(this.socketTimeout).build();
			final CredentialsProvider credsProvider = new BasicCredentialsProvider();
			final boolean hasCrede = (StringUtil.isNotBlank(this.getUsername())
					&& StringUtil.isNotBlank(getPassword())) ? true : false;
			// 凭据提供器
			if (hasCrede) {
				credsProvider.setCredentials(AuthScope.ANY,
						// 认证用户名和密码
						new UsernamePasswordCredentials(getUsername(), getPassword()));
			}
			builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
				@Override
				public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
					httpClientBuilder.setDefaultConnectionConfig(connectionConfig)
							.setDefaultRequestConfig(requestConfig);
					if (hasCrede) {
						httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
					}
					return httpClientBuilder;
				}
			});
			restClient = builder.build();
		}
	}
}