List of usage examples for org.apache.http.message BasicHeaderElementIterator BasicHeaderElementIterator
public BasicHeaderElementIterator(HeaderIterator headerIterator)
From source file:org.apache.dubbo.rpc.protocol.rest.RestProtocol.java
@Override protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException { if (connectionMonitor == null) { connectionMonitor = new ConnectionMonitor(); }//from w w w . j a va 2s .c o m // TODO more configs to add PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); // 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); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)) .setSocketTimeout(url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)).build(); SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build(); CloseableHttpClient httpClient = HttpClientBuilder.create() .setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { @Override 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; } }).setDefaultRequestConfig(requestConfig).setDefaultSocketConfig(socketConfig).build(); 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.senseidb.svc.impl.HttpRestSenseiServiceImpl.java
private DefaultHttpClient createHttpClient(HttpRequestRetryHandler retryHandler) { HttpParams params = new BasicHttpParams(); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme(_scheme, _port, PlainSocketFactory.getSocketFactory())); ClientConnectionManager cm = new ThreadSafeClientConnManager(registry); DefaultHttpClient client = new DefaultHttpClient(cm, params); if (retryHandler == null) { retryHandler = new HttpRequestRetryHandler() { public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount >= _maxRetries) { // Do not retry if over max retry count return false; }//w w w . j av a 2 s. c o m if (exception instanceof NoHttpResponseException) { // Retry if the server dropped connection on us return true; } if (exception instanceof SSLHandshakeException) { // Do not retry on SSL handshake exception return false; } HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // Retry if the request is considered idempotent return true; } return false; } }; } client.setHttpRequestRetryHandler(retryHandler); client.addRequestInterceptor(new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip"); } } }); client.addResponseInterceptor(new HttpResponseInterceptor() { public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (int i = 0; i < codecs.length; i++) { if (codecs[i].getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } } } } }); client.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() { @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) { } } } long keepAlive = super.getKeepAliveDuration(response, context); if (keepAlive == -1) { keepAlive = _defaultKeepAliveDurationMS; } return keepAlive; } }); return client; }
From source file:org.sdr.webrec.core.WebRec.java
private void initParameters() { // initialize HTTP parameters params = new BasicHttpParams(); ConnManagerParams.setMaxTotalConnections(params, 100); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); httpclient = new DefaultHttpClient(cm, params); //TODO: CloseableHttpClient httpclient = HttpClients.createDefault(); //set proxy if available in settings if (settings.getProxyHost() != null && settings.getProxyHost().length() > 0) { HttpHost proxy = new HttpHost(settings.getProxyHost(), settings.getProxyPort()); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); //set authentication to proxy is available is settings if (settings.getProxyUserName() != null && settings.getProxyUserName().length() > 0 && settings.getProxyPasswd() != null) { httpclient.getCredentialsProvider().setCredentials( new AuthScope(settings.getProxyHost(), settings.getProxyPort()), new UsernamePasswordCredentials(settings.getProxyUserName(), settings.getProxyPasswd())); LOGGER.debug("autentication for proxy on"); }/*from w w w . java 2s . c om*/ } // initialize scheme registry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); // Create an HttpClient with the ThreadSafeClientConnManager. // This connection manager must be used if more than one thread will // be using the HttpClient. cm = new ThreadSafeClientConnManager(params, schemeRegistry); httpclient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { 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); } catch (NumberFormatException ignore) { } } } //otherwise keep alive for 30 seconds return 30 * 1000; } }); httpget = null; httpclient = new DefaultHttpClient(cm, params); // Create global request configuration RequestConfig defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH) .setExpectContinueEnabled(true).setStaleConnectionCheckEnabled(settings.isKeepConnectionAlive()) .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build(); requestConfig = RequestConfig.copy(defaultRequestConfig).setSocketTimeout(timeout) .setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).build(); }
From source file:org.frontcache.FrontCacheEngine.java
private CloseableHttpClient newClient() { final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(5500) // should be slightly more then hystrix timeout for http client .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); ConnectionKeepAliveStrategy keepAliveStrategy = new ConnectionKeepAliveStrategy() { @Override/*from w w w . j a va 2 s . c o m*/ 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; } }; return HttpClients.custom().setConnectionManager(newConnectionManager()) .setDefaultRequestConfig(requestConfig) // .setSSLHostnameVerifier(new NoopHostnameVerifier()) // for SSL do not verify certificate's host .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(); }
From source file:com.networknt.client.Client.java
private CloseableHttpClient httpClient() throws ClientException { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry()); Map<String, Object> httpClientMap = (Map<String, Object>) config.get(SYNC); connectionManager.setMaxTotal((Integer) httpClientMap.get(MAX_CONNECTION_TOTAL)); connectionManager.setDefaultMaxPerRoute((Integer) httpClientMap.get(MAX_CONNECTION_PER_ROUTE)); // Now handle all the specific route defined. Map<String, Object> routeMap = (Map<String, Object>) httpClientMap.get(ROUTES); Iterator<String> it = routeMap.keySet().iterator(); while (it.hasNext()) { String route = it.next(); Integer maxConnection = (Integer) routeMap.get(route); connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost(route)), maxConnection); }//from w ww.j a v a 2s .co m final int timeout = (Integer) httpClientMap.get(TIMEOUT); RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout).build(); final long keepAliveMilliseconds = (Integer) httpClientMap.get(KEEP_ALIVE); return HttpClientBuilder.create().setConnectionManager(connectionManager) .setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { @Override 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 { logger.trace("Use server timeout for keepAliveMilliseconds"); return Long.parseLong(value) * 1000; } catch (NumberFormatException ignore) { } } } //logger.trace("Use keepAliveMilliseconds from config " + keepAliveMilliseconds); return keepAliveMilliseconds; } }).setDefaultRequestConfig(config).build(); }
From source file:com.buffalokiwi.api.APIHttpClient.java
/** * Retrieve a keep alive strategy.// w w w. j a v a 2s . c om * This will use the value of readTimeout. * @return strategy. */ protected ConnectionKeepAliveStrategy createConnectionKeepAliveStrategy() { return new ConnectionKeepAliveStrategy() { @Override public long getKeepAliveDuration(HttpResponse hr, HttpContext hc) { final HeaderElementIterator it = new BasicHeaderElementIterator( hr.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { final HeaderElement he = it.nextElement(); final String param = he.getName(); final String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) return Long.parseLong(value) * 1000; } return readTimeout; } }; }
From source file:com.networknt.client.Client.java
private CloseableHttpAsyncClient httpAsyncClient() throws ClientException { PoolingNHttpClientConnectionManager connectionManager = new PoolingNHttpClientConnectionManager(ioReactor(), asyncRegistry());/*w ww. j a v a2 s .com*/ Map<String, Object> asyncHttpClientMap = (Map<String, Object>) config.get(ASYNC); connectionManager.setMaxTotal((Integer) asyncHttpClientMap.get(MAX_CONNECTION_TOTAL)); connectionManager.setDefaultMaxPerRoute((Integer) asyncHttpClientMap.get(MAX_CONNECTION_PER_ROUTE)); // Now handle all the specific route defined. Map<String, Object> routeMap = (Map<String, Object>) asyncHttpClientMap.get(ROUTES); Iterator<String> it = routeMap.keySet().iterator(); while (it.hasNext()) { String route = it.next(); Integer maxConnection = (Integer) routeMap.get(route); connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost(route)), maxConnection); } final int timeout = (Integer) asyncHttpClientMap.get(TIMEOUT); RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout).build(); final long keepAliveMilliseconds = (Integer) asyncHttpClientMap.get(KEEP_ALIVE); return HttpAsyncClientBuilder.create().setConnectionManager(connectionManager) .setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { @Override 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 { logger.trace("Use server timeout for keepAliveMilliseconds"); return Long.parseLong(value) * 1000; } catch (NumberFormatException ignore) { } } } //logger.trace("Use keepAliveMilliseconds from config " + keepAliveMilliseconds); return keepAliveMilliseconds; } }).setDefaultRequestConfig(config).build(); }
From source file:com.wudaosoft.net.httpclient.Request.java
protected void init() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException { Args.notNull(hostConfig, "Host config"); SSLConnectionSocketFactory sslConnectionSocketFactory = null; if (sslcontext == null) { if (hostConfig.getCA() != null) { // Trust root CA and all self-signed certs SSLContext sslcontext1 = SSLContexts.custom().loadTrustMaterial(hostConfig.getCA(), hostConfig.getCAPassword(), TrustSelfSignedStrategy.INSTANCE).build(); // Allow TLSv1 protocol only sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslcontext1, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()); } else {/*from w w w. j ava2 s .co m*/ if (isTrustAll) { SSLContext sslcontext1 = SSLContext.getInstance("TLS"); TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws CertificateException { } @Override public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws CertificateException { } } }; sslcontext1.init(null, trustAllCerts, null); sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslcontext1, NoopHostnameVerifier.INSTANCE); } else { sslConnectionSocketFactory = SSLConnectionSocketFactory.getSocketFactory(); } } } else { sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()); } if (keepAliveStrategy == null) { keepAliveStrategy = new ConnectionKeepAliveStrategy() { 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 // ("xxxxx".equalsIgnoreCase(target.getHostName())) // { // // Keep alive for 5 seconds only // return 3 * 1000; // } else { // // otherwise keep alive for 30 seconds // return 30 * 1000; // } return 30 * 1000; } }; } if (retryHandler == null) { retryHandler = new HttpRequestRetryHandler() { public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount >= 3) { // Do not retry if over max retry count return false; } if (exception instanceof InterruptedIOException) { // Timeout return false; } if (exception instanceof UnknownHostException) { // Unknown host return false; } if (exception instanceof ConnectTimeoutException) { // Connection refused return false; } if (exception instanceof SSLException) { // SSL handshake exception return false; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // Retry if the request is considered idempotent return true; } return false; } }; } connManager = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", sslConnectionSocketFactory).build()); if (hostConfig.getHost() != null) { connManager.setMaxTotal(hostConfig.getPoolSize() + 60); connManager.setMaxPerRoute( new HttpRoute(hostConfig.getHost(), null, !HttpHost.DEFAULT_SCHEME_NAME.equals(hostConfig.getHost().getSchemeName())), hostConfig.getPoolSize()); connManager.setDefaultMaxPerRoute(20); } else { connManager.setMaxTotal(hostConfig.getPoolSize()); int hostCount = hostConfig.getHostCount() == 0 ? 10 : hostConfig.getHostCount(); connManager.setDefaultMaxPerRoute(hostConfig.getPoolSize() / hostCount); } // connManager.setValidateAfterInactivity(2000); // Create socket configuration SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).setSoKeepAlive(isKeepAlive).build(); connManager.setDefaultSocketConfig(socketConfig); // Create connection configuration ConnectionConfig connectionConfig = ConnectionConfig.custom() .setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE) .setCharset(hostConfig.getCharset() == null ? Consts.UTF_8 : hostConfig.getCharset()).build(); connManager.setDefaultConnectionConfig(connectionConfig); new IdleConnectionMonitorThread(connManager).start(); if (requestInterceptor == null) { requestInterceptor = new SortHeadersInterceptor(hostConfig); } if (!hostConfig.isMulticlient()) { defaultHttpContext = HttpClientContext.create(); httpClient = create(); } }
From source file:org.ellis.yun.search.test.httpclient.HttpClientTest.java
@Test public void testKeepAliveTime() throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); // httpContext ?httpClienthttp? HttpContext httpContext = new BasicHttpContext(); HttpGet httpGet = new HttpGet("http://www.baidu.com"); httpclient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { public long getKeepAliveDuration(HttpResponse response, HttpContext context) { // ?Keep-Alive? HeaderIterator hit = response.headerIterator(HTTP.CONN_KEEP_ALIVE); HeaderElementIterator it = new BasicHeaderElementIterator(hit); while (it.hasNext()) { HeaderElement element = it.nextElement(); String name = element.getName(); String value = element.getValue(); if ("timeout".equalsIgnoreCase(name) && !StringUtils.isBlank(value)) { return Long.parseLong(value) * 1000; }// w w w . ja v a2 s.c om } HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if ("www.baidu.com".equalsIgnoreCase(target.getHostName())) { // ???5 return 5 * 1000; } else { // ???30 return 30 * 1000; } } }); HttpResponse response = httpclient.execute(httpGet, httpContext); String content = parseEntity(response.getEntity()); System.out.println(content); System.out.println("=============================================\n"); // ?Keep-Alive? HeaderIterator hit = response.headerIterator(); HeaderElementIterator it = new BasicHeaderElementIterator(hit); while (it.hasNext()) { HeaderElement element = it.nextElement(); String name = element.getName(); String value = element.getValue(); System.out.println(" ==> " + name + " >> " + value); } }
From source file:org.venice.beachfront.bfapi.BfApiConfig.java
/** * Defines a keep-alive strategy for requests that do not provide a proper timeout value. This will avoid a case * where keep-alive is set to permanent (when the header is missing) in cases where this application's environment * will not allow for permanent connections. */// w w w .ja v a 2 s .c o m private ConnectionKeepAliveStrategy getKeepAliveStrategy() { // Returns the keep alive duration, in milliseconds return (HttpResponse response, HttpContext context) -> { HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { final HeaderElement headerElement = it.nextElement(); final String param = headerElement.getName(); final String value = headerElement.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { return Long.parseLong(value) * 1000; } } return httpKeepAliveDurationSeconds * 1000; }; }