List of usage examples for org.apache.http.conn.routing HttpRoute HttpRoute
public HttpRoute(final HttpHost target)
From source file:ai.susi.server.ClientConnection.java
private static PoolingHttpClientConnectionManager getConnctionManager(boolean useAuthentication) { // allow opportunistic encryption if needed boolean trustAllCerts = !"none".equals(DAO.getConfig("httpsclient.trustselfsignedcerts", "peers")) && (!useAuthentication || "all".equals(DAO.getConfig("httpsclient.trustselfsignedcerts", "peers"))); Registry<ConnectionSocketFactory> socketFactoryRegistry = null; if (trustAllCerts) { try {//from w ww . ja v a 2 s . com SSLConnectionSocketFactory trustSelfSignedSocketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), new TrustAllHostNameVerifier()); socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", new PlainConnectionSocketFactory()) .register("https", trustSelfSignedSocketFactory).build(); } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) { Log.getLog().warn(e); } } PoolingHttpClientConnectionManager cm = (trustAllCerts && socketFactoryRegistry != null) ? new PoolingHttpClientConnectionManager(socketFactoryRegistry) : new PoolingHttpClientConnectionManager(); // twitter specific options cm.setMaxTotal(200); cm.setDefaultMaxPerRoute(20); HttpHost twitter = new HttpHost("twitter.com", 443); cm.setMaxPerRoute(new HttpRoute(twitter), 50); return cm; }
From source file:org.mobicents.client.slee.resource.http.HttpClientResourceAdaptor.java
@SuppressWarnings("unchecked") public void raConfigure(ConfigProperties properties) { String httpClientFactoryClassName = (String) properties.getProperty(CFG_PROPERTY_HTTP_CLIENT_FACTORY) .getValue();// w w w.j ava2 s .co m if (httpClientFactoryClassName.isEmpty()) { maxTotal = (Integer) properties.getProperty(CFG_PROPERTY_MAX_CONNECTIONS_TOTAL).getValue(); maxForRoutes = new HashMap<HttpRoute, Integer>(); String maxForRoutesString = (String) properties.getProperty(CFG_PROPERTY_MAX_CONNECTIONS_FOR_ROUTES) .getValue(); String[] maxForRoutesStrings = maxForRoutesString.split(","); for (String maxForRoute : maxForRoutesStrings) { if (maxForRoute.isEmpty()) { continue; } String[] maxForRouteParts = maxForRoute.split(":"); String scheme = maxForRouteParts[HTTP_SCHEME_INDEX]; if (scheme == null || scheme.equals("")) { scheme = HttpHost.DEFAULT_SCHEME_NAME; } String host = maxForRouteParts[HTTP_HOST_INDEX]; String portStr = maxForRouteParts[HTTP_PORT_INDEX]; if (portStr == null || portStr.equals("")) { portStr = "80"; } int port = Integer.parseInt(portStr); String maxForRoutePartsStr = maxForRouteParts[HTTP_MAXFORROUTE_INDEX]; Integer max = Integer.valueOf(maxForRoutePartsStr); HttpRoute httpRoute = new HttpRoute(new HttpHost(host, port, scheme)); maxForRoutes.put(httpRoute, max); } } else { try { httpClientFactory = ((Class<? extends HttpClientFactory>) Class.forName(httpClientFactoryClassName)) .newInstance(); } catch (Exception e) { tracer.severe("failed to load http client factory class", e); } } }
From source file:com.ea.core.bridge.ws.rest.client.AbstractRestClient.java
public AbstractRestClient(URL httpUrl) { super(httpUrl); HttpMessageParserFactory<HttpResponse> responseParserFactory = new DefaultHttpResponseParserFactory() { @Override//from w w w .j a va 2 s . c om public HttpMessageParser<HttpResponse> create(SessionInputBuffer buffer, MessageConstraints constraints) { LineParser lineParser = new BasicLineParser() { @Override public Header parseHeader(final CharArrayBuffer buffer) { try { return super.parseHeader(buffer); } catch (ParseException ex) { return new BasicHeader(buffer.toString(), null); } } }; return new DefaultHttpResponseParser(buffer, lineParser, DefaultHttpResponseFactory.INSTANCE, constraints) { @Override protected boolean reject(final CharArrayBuffer line, int count) { // try to ignore all garbage preceding a status line infinitely return false; } }; } }; HttpMessageWriterFactory<HttpRequest> requestWriterFactory = new DefaultHttpRequestWriterFactory(); HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory( requestWriterFactory, responseParserFactory); SSLContext sslcontext = SSLContexts.createSystemDefault(); X509HostnameVerifier hostnameVerifier = new BrowserCompatHostnameVerifier(); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", new SSLConnectionSocketFactory(sslcontext, hostnameVerifier)).build(); DnsResolver dnsResolver = new SystemDefaultDnsResolver() { @Override public InetAddress[] resolve(final String host) throws UnknownHostException { if (host.equalsIgnoreCase("myhost") || host.equalsIgnoreCase("localhost")) { return new InetAddress[] { InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }) }; } else { return super.resolve(host); } } }; PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager( socketFactoryRegistry, connFactory, dnsResolver); SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).build(); connManager.setDefaultSocketConfig(socketConfig); connManager.setSocketConfig(new HttpHost("somehost", 80), socketConfig); 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(); connManager.setDefaultConnectionConfig(connectionConfig); connManager.setConnectionConfig(new HttpHost("somehost", 80), ConnectionConfig.DEFAULT); connManager.setMaxTotal(100); connManager.setDefaultMaxPerRoute(10); connManager.setMaxPerRoute(new HttpRoute(new HttpHost("somehost", 80)), 20); CookieStore cookieStore = new BasicCookieStore(); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); RequestConfig defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH) .setExpectContinueEnabled(true).setStaleConnectionCheckEnabled(true) .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).setConnectionRequestTimeout(3000) .setConnectTimeout(3000).setSocketTimeout(3000).build(); client = HttpClients.custom().setConnectionManager(connManager).setDefaultCookieStore(cookieStore) .setDefaultCredentialsProvider(credentialsProvider) // .setProxy(new HttpHost("myproxy", 8080)) .setDefaultRequestConfig(defaultRequestConfig).build(); }
From source file:com.bigdata.rdf.sail.webapp.AbstractProtocolTest.java
protected ClientConnectionManager newInstance() { final ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(newSchemeRegistry()); // Increase max total connection to 200 cm.setMaxTotal(200);//from ww w . j a v a2 s. com // Increase default max connection per route to 20 cm.setDefaultMaxPerRoute(20); // Increase max connections for localhost to 50 final HttpHost localhost = new HttpHost("locahost"); cm.setMaxForRoute(new HttpRoute(localhost), 50); return cm; }
From source file:org.springframework.ws.transport.http.HttpComponentsMessageSender.java
/** * Sets the maximum number of connections per host for the underlying HttpClient. The maximum number of connections * per host can be set in a form accepted by the {@code java.util.Properties} class, like as follows: * <p/>//from ww w . j ava2 s . c o m * <pre> * https://www.example.com=1 * http://www.example.com:8080=7 * http://www.springframework.org=10 * </pre> * <p/> * The host can be specified as a URI (with scheme and port). * * @param maxConnectionsPerHost a properties object specifying the maximum number of connection * @see org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager#setMaxForRoute(org.apache.http.conn.routing.HttpRoute, * int) */ public void setMaxConnectionsPerHost(Map<String, String> maxConnectionsPerHost) throws URISyntaxException { ClientConnectionManager connectionManager = getHttpClient().getConnectionManager(); if (!(connectionManager instanceof ThreadSafeClientConnManager)) { throw new IllegalArgumentException( "maxConnectionsPerHost is not supported on " + connectionManager.getClass().getName() + ". Use " + ThreadSafeClientConnManager.class.getName() + " instead"); } for (Object o : maxConnectionsPerHost.keySet()) { String host = (String) o; URI uri = new URI(host); HttpHost httpHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); int maxHostConnections = Integer.parseInt(maxConnectionsPerHost.get(host)); ((ThreadSafeClientConnManager) connectionManager).setMaxForRoute(new HttpRoute(httpHost), maxHostConnections); } }
From source file:org.springframework.integration.splunk.support.SplunkHECWriter.java
@Override public void start() { try {//from ww w.j a v a 2 s. c o m this.batchBuffer = Collections.synchronizedList(new LinkedList<String>()); this.lastEventReceivedTime = System.currentTimeMillis(); Registry<SchemeIOSessionStrategy> sslSessionStrategy = RegistryBuilder.<SchemeIOSessionStrategy>create() .register("http", NoopIOSessionStrategy.INSTANCE) .register("https", new SSLIOSessionStrategy(getSSLContext(), HOSTNAME_VERIFIER)).build(); ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(); PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor, sslSessionStrategy); cm.setMaxTotal(getPoolsize()); HttpHost splunk = new HttpHost(getHost(), getPort()); cm.setMaxPerRoute(new HttpRoute(splunk), getPoolsize()); httpClient = HttpAsyncClients.custom().setConnectionManager(cm).build(); uri = new URIBuilder().setScheme(isHttps() ? "https" : "http").setHost(getHost()).setPort(getPort()) .setPath("/services/collector").build(); httpClient.start(); if (isBatchMode()) { new BatchBufferActivityCheckerThread(this).start(); } } catch (Exception e) { } this.running = true; }
From source file:org.votingsystem.util.HttpHelper.java
private HttpHelper() { try {//from w w w. j a va2 s . c o m KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLContext sslcontext = null; SSLConnectionSocketFactory sslsf = null; if (ContextVS.getInstance().getVotingSystemSSLCerts() != null) { log.info("loading SSLContext with app certificates"); X509Certificate sslServerCert = ContextVS.getInstance().getVotingSystemSSLCerts().iterator().next(); trustStore.setCertificateEntry(sslServerCert.getSubjectDN().toString(), sslServerCert); sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore).build(); X509HostnameVerifier hostnameVerifier = (X509HostnameVerifier) new AllowAllHostnameVerifier(); sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, hostnameVerifier); } else { sslcontext = SSLContexts.createSystemDefault(); sslsf = new SSLConnectionSocketFactory(sslcontext); log.info("loading default SSLContext"); } // Create a registry of custom connection socket factories for supported protocol schemes. Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder .<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", sslsf).build(); //Create socket configuration //SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).build(); //Configure the connection manager to use socket configuration either by default or for a specific host. //connManager.setDefaultSocketConfig(socketConfig); connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry, connFactory, dnsResolver); connManager.setMaxTotal(200); connManager.setDefaultMaxPerRoute(100); connEvictor = new IdleConnectionEvictor(connManager); connEvictor.start(); HttpRoute httpRouteVS = new HttpRoute(new HttpHost("www.sistemavotacion.org", 80)); connManager.setMaxPerRoute(httpRouteVS, 200); /* timeouts with large simulations -> RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(REQUEST_TIME_OUT) .setConnectionRequestTimeout(REQUEST_TIME_OUT).setSocketTimeout(REQUEST_TIME_OUT).build(); httpClient = HttpClients.custom().setConnectionManager(connManager).setDefaultRequestConfig( requestConfig).build();*/ httpClient = HttpClients.custom().setConnectionManager(connManager).build(); } catch (Exception ex) { log.log(Level.SEVERE, ex.getMessage(), ex); } }
From source file:com.sangupta.jerry.http.HttpExecutor.java
/** * Set maximum connections that will be operated over the given host on port * 80, that will be handled by the underlying connection manager. * //from ww w. j a va 2 s . co m * @param hostName * the host name for which the limit needs to be set * * @param numConnections * the number of connections to set * * @throws IllegalArgumentException * if the number of connections is less than <code>ZERO</code> * * @throws IllegalArgumentException * if the host name is <code>null</code> or empty. */ public static void setMaxConnectionsOnHost(String hostName, int numConnections) { if (AssertUtils.isEmpty(hostName)) { throw new IllegalArgumentException("Hostname cannot be null/empty"); } HttpRoute route = new HttpRoute(new HttpHost(hostName)); setMaxConnectionsOnHost(route, numConnections); }
From source file:org.jasig.cas.util.http.SimpleHttpClientFactoryBean.java
/** * Build a HTTP client based on the current properties. * * @return the built HTTP client//w w w . ja va2 s . c om */ private CloseableHttpClient buildHttpClient() { try { final ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory(); final LayeredConnectionSocketFactory sslsf = this.sslSocketFactory; final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", plainsf).register("https", sslsf).build(); final PoolingHttpClientConnectionManager connMgmr = new PoolingHttpClientConnectionManager(registry); connMgmr.setMaxTotal(this.maxPooledConnections); connMgmr.setDefaultMaxPerRoute(this.maxConnectionsPerRoute); final HttpHost httpHost = new HttpHost(InetAddress.getLocalHost()); final HttpRoute httpRoute = new HttpRoute(httpHost); connMgmr.setMaxPerRoute(httpRoute, MAX_CONNECTIONS_PER_ROUTE); final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(this.readTimeout) .setConnectTimeout(this.connectionTimeout).setConnectionRequestTimeout(this.connectionTimeout) .setStaleConnectionCheckEnabled(true).setCircularRedirectsAllowed(this.circularRedirectsAllowed) .setRedirectsEnabled(this.redirectsEnabled).setAuthenticationEnabled(this.authenticationEnabled) .build(); final HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connMgmr) .setDefaultRequestConfig(requestConfig).setSSLSocketFactory(sslsf) .setSSLHostnameVerifier(this.hostnameVerifier).setRedirectStrategy(this.redirectionStrategy) .setDefaultCredentialsProvider(this.credentialsProvider).setDefaultCookieStore(this.cookieStore) .setConnectionReuseStrategy(this.connectionReuseStrategy) .setConnectionBackoffStrategy(this.connectionBackoffStrategy) .setServiceUnavailableRetryStrategy(this.serviceUnavailableRetryStrategy) .setProxyAuthenticationStrategy(this.proxyAuthenticationStrategy) .setDefaultHeaders(this.defaultHeaders).useSystemProperties(); return builder.build(); } catch (final Exception e) { LOGGER.error(e.getMessage(), e); throw new RuntimeException(e); } }
From source file:org.ovirt.engine.sdk.web.ConnectionsPoolBuilder.java
/** * Creates PoolingClientConnectionManager * * @param url/*from w ww. jav a 2s . co m*/ * @param port * * @return {@link ClientConnectionManager} */ private ClientConnectionManager createPoolingClientConnectionManager(String url, int port) { SchemeRegistry schemeRegistry = createSchemeRegistry(url, port); PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry); cm.setMaxTotal(MAX_CONNECTIONS); cm.setDefaultMaxPerRoute(MAX_CONNECTIONS_PER_ROUTE); cm.setMaxPerRoute(new HttpRoute(new HttpHost(getHost(url), getPort(url, port))), MAX_CONNECTIONS_PER_HOST); return cm; }