List of usage examples for org.apache.http.message BasicHeaderElementIterator BasicHeaderElementIterator
public BasicHeaderElementIterator(HeaderIterator headerIterator)
From source file:com.feedeo.web.client.AbstractWebClient.java
protected HttpClient createHttpClient() { final SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build(); final ConnectionConfig connectionConfig = ConnectionConfig.custom().build(); final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(30000) .setConnectTimeout(30000).setSocketTimeout(30000).setStaleConnectionCheckEnabled(false).build(); final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(256);//w w w . jav a2 s . c o m connectionManager.setDefaultMaxPerRoute(256); IdleConnectionMonitorThread staleMonitor = new IdleConnectionMonitorThread(connectionManager); staleMonitor.start(); try { staleMonitor.join(1000); } catch (InterruptedException ignored) { } final ConnectionKeepAliveStrategy connectionKeepAliveStrategy = new ConnectionKeepAliveStrategy() { @Override public long getKeepAliveDuration(HttpResponse response, HttpContext context) { HeaderElementIterator iterator = new BasicHeaderElementIterator( response.headerIterator(CONN_KEEP_ALIVE)); while (iterator.hasNext()) { HeaderElement header = iterator.nextElement(); String param = header.getName(); String value = header.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { return Long.parseLong(value) * 1000; } } return 5 * 1000; } }; return HttpClientBuilder.create().setConnectionManager(connectionManager) .setDefaultRequestConfig(requestConfig).setDefaultSocketConfig(socketConfig) .setDefaultConnectionConfig(connectionConfig).setKeepAliveStrategy(connectionKeepAliveStrategy) .build(); }
From source file:mx.bigdata.utils.pubsubhubbub.Subscriber.java
public Subscriber(CallbackServer webserver) { this.webserver = webserver; SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); //ConnManagerParams.setMaxTotalConnections(params, 200); //ConnPerRouteBean connPerRoute = new ConnPerRouteBean(20); //connPerRoute.setDefaultMaxPerRoute(50); //ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry); cm.setMaxTotal(200);//from w w w . j av a 2 s. c om cm.setDefaultMaxPerRoute(50); httpClient = new DefaultHttpClient(cm, new BasicHttpParams()); httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { public long getKeepAliveDuration(HttpResponse response, HttpContext context) { 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) { } } } return 30 * 1000; } }); }
From source file:org.frontcache.client.FrontCacheClient.java
private FrontCacheClient(String frontcacheURL) { final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(CONNECTION_TIMEOUT) .setConnectTimeout(CONNECTION_TIMEOUT).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); ConnectionKeepAliveStrategy keepAliveStrategy = new ConnectionKeepAliveStrategy() { @Override//from w w w. ja va 2 s.c om public long getKeepAliveDuration(HttpResponse response, HttpContext context) { 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")) { return Long.parseLong(value) * 1000; } } return 10 * 1000; } }; client = HttpClients.custom().setDefaultRequestConfig(requestConfig) .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)) .setKeepAliveStrategy(keepAliveStrategy).setRedirectStrategy(new RedirectStrategy() { @Override public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { return false; } @Override public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { return null; } }).build(); this.frontCacheURL = frontcacheURL; if (frontcacheURL.endsWith("/")) this.frontCacheURI = frontcacheURL + IO_URI; else this.frontCacheURI = frontcacheURL + "/" + IO_URI; }
From source file:ingest.Application.java
@Bean public RestTemplate restTemplate() { RestTemplate restTemplate = new RestTemplate(); HttpClient httpClient = HttpClientBuilder.create().setMaxConnTotal(httpMaxTotal) .setMaxConnPerRoute(httpMaxRoute).setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { @Override/*from w ww .ja v a2 s . c om*/ public long getKeepAliveDuration(HttpResponse response, HttpContext context) { HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement headerElement = it.nextElement(); String param = headerElement.getName(); String value = headerElement.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { return Long.parseLong(value) * 1000; } } return (long) 5 * 1000; } }).build(); restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient)); return restTemplate; }
From source file:org.talend.dataprep.configuration.HttpClient.java
/** * @return The connection keep alive strategy. */// w w w . ja v a2s. com private ConnectionKeepAliveStrategy getKeepAliveStrategy() { return (response, 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 && "timeout".equalsIgnoreCase(param)) { try { return Long.parseLong(value) * 1000; } catch (NumberFormatException ignore) { // let's move on the next header value break; } } } // otherwise use the default value return defaultKeepAlive * 1000; }; }
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.artifactory.util.HttpClientConfigurator.java
/** * Produces a {@link ConnectionKeepAliveStrategy} * * @return keep-alive strategy to be used for connection pool *//*from ww w. j a v a2 s . c o m*/ private ConnectionKeepAliveStrategy getConnectionKeepAliveStrategy() { return 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) { } } } HttpHost target = (HttpHost) context.getAttribute(HttpClientContext.HTTP_TARGET_HOST); if ("www.naughty-server.com".equalsIgnoreCase(target.getHostName())) { return 5 * 1000; } else { return 30 * 1000; } } }; }
From source file:com.alibaba.dubbo.rpc.protocol.rest.RestProtocol.java
protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException { if (connectionMonitor == null) { connectionMonitor = new ConnectionMonitor(); }//w w w . j av a 2s.c o m // TODO more configs to add PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(); // 20 is the default maxTotal of current PoolingClientConnectionManager connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, 20)); connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, 20)); connectionMonitor.addConnectionManager(connectionManager); // BasicHttpContext localContext = new BasicHttpContext(); DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager); httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { public long getKeepAliveDuration(HttpResponse response, HttpContext context) { 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")) { return Long.parseLong(value) * 1000; } } // TODO constant return 30 * 1000; } }); HttpParams params = httpClient.getParams(); // TODO currently no xml config for Constants.CONNECT_TIMEOUT_KEY so we directly reuse Constants.TIMEOUT_KEY for now HttpConnectionParams.setConnectionTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)); HttpConnectionParams.setSoTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)); HttpConnectionParams.setTcpNoDelay(params, true); HttpConnectionParams.setSoKeepalive(params, true); ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient/*, localContext*/); ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build(); clients.add(client); client.register(RpcContextFilter.class); for (String clazz : Constants.COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.EXTENSION_KEY, ""))) { if (!StringUtils.isEmpty(clazz)) { try { client.register(Thread.currentThread().getContextClassLoader().loadClass(clazz.trim())); } catch (ClassNotFoundException e) { throw new RpcException("Error loading JAX-RS extension class: " + clazz.trim(), e); } } } // TODO protocol ResteasyWebTarget target = client .target("http://" + url.getHost() + ":" + url.getPort() + "/" + getContextPath(url)); return target.proxy(serviceType); }
From source file:com.alibaba.dubbo.rpc.protocol.resteasy.RestProtocol.java
protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException { if (connectionMonitor == null) { connectionMonitor = new ConnectionMonitor(); }/* w w w . j av a 2 s .c o m*/ // TODO more configs to add PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(); // 20 is the default maxTotal of current PoolingClientConnectionManager connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, 20)); connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, 20)); connectionMonitor.addConnectionManager(connectionManager); // BasicHttpContext localContext = new BasicHttpContext(); DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager); httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { public long getKeepAliveDuration(HttpResponse response, HttpContext context) { 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")) { return Long.parseLong(value) * 1000; } } // TODO constant return 30 * 1000; } }); HttpParams params = httpClient.getParams(); // TODO currently no xml config for Constants.CONNECT_TIMEOUT_KEY so we directly reuse Constants.TIMEOUT_KEY for now HttpConnectionParams.setConnectionTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)); HttpConnectionParams.setSoTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)); HttpConnectionParams.setTcpNoDelay(params, true); HttpConnectionParams.setSoKeepalive(params, true); ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient/* , localContext */); ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build(); clients.add(client); client.register(RpcContextFilter.class); for (String clazz : Constants.COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.EXTENSION_KEY, ""))) { if (!StringUtils.isEmpty(clazz)) { try { client.register(Thread.currentThread().getContextClassLoader().loadClass(clazz.trim())); } catch (ClassNotFoundException e) { throw new RpcException("Error loading JAX-RS extension class: " + clazz.trim(), e); } } } // dubbo ? String version = url.getParameter(Constants.VERSION_KEY); String versionPath = ""; if (StringUtils.isNotEmpty(version)) { versionPath = version + "/"; } // TODO protocol ResteasyWebTarget target = client .target("http://" + url.getHost() + ":" + url.getPort() + "/" + versionPath + getContextPath(url)); return target.proxy(serviceType); }
From source file:org.ellis.yun.search.test.httpclient.HttpClientTest.java
@Test public void testHttpResponse() throws Exception { HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); System.out.println(response.getProtocolVersion() + ""); System.out.println(response.getStatusLine().getStatusCode() + ""); System.out.println(response.getStatusLine().getReasonPhrase()); System.out.println(response.getStatusLine().toString()); response.addHeader("Set-Cookie", "c1=a; path=/; domain=localhost"); response.addHeader("Set-Cookie", "c2=b; path=\"/\", c3=c; domain=\"localhost\""); HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator("Set-Cookie")); while (it.hasNext()) { HeaderElement elem = it.nextElement(); System.out.println(elem.getName() + " = " + elem.getValue()); NameValuePair[] params = elem.getParameters(); for (int i = 0; i < params.length; i++) { System.out.println(" " + params[i]); }//from w w w . j a va 2 s. c o m } }