List of usage examples for org.apache.http.impl.conn SchemeRegistryFactory createDefault
public static SchemeRegistry createDefault()
From source file:com.twitter.hbc.ClientBuilder.java
public ClientBuilder() { enableGZip = true;//w ww .ja va2s. co m name = "hosebird-client-" + clientNum.getAndIncrement(); ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true) .setNameFormat("hosebird-client-io-thread-%d").build(); executorService = Executors.newSingleThreadExecutor(threadFactory); ThreadFactory rateTrackerThreadFactory = new ThreadFactoryBuilder().setDaemon(true) .setNameFormat("hosebird-client-rateTracker-thread-%d").build(); ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(1, rateTrackerThreadFactory); rateTracker = new BasicRateTracker(30000, 100, true, scheduledExecutor); reconnectionManager = new BasicReconnectionManager(5); socketTimeoutMillis = 60000; connectionTimeoutMillis = 4000; schemeRegistry = SchemeRegistryFactory.createDefault(); }
From source file:org.bungeni.ext.integration.bungeniportal.BungeniAppConnector.java
/** * Thread safe client ,since we access the same client across threads * @return /*from ww w . j a va 2s. c om*/ */ public DefaultHttpClient getThreadSafeClient() { PoolingClientConnectionManager cxMgr = new PoolingClientConnectionManager( SchemeRegistryFactory.createDefault()); cxMgr.setMaxTotal(100); cxMgr.setDefaultMaxPerRoute(20); DefaultHttpClient aClient = new DefaultHttpClient(); HttpParams params = aClient.getParams(); return new DefaultHttpClient(cxMgr, params); }
From source file:com.mgmtp.perfload.core.client.web.config.HttpClientManagerModule.java
/** * If the property {@code ssl.trust.all} equals {@code true}, a {@link TrustAllManager} is * installed, i. e. all certificates are trusted, and host name verification is turned off. * Otherwise, {@link LtSSLSocketFactory} is registered for HTTPS, if either a key store, a trust * store or both are configured using the following properties:</p> * <p>//from w w w .j a va 2 s . c o m * <ul> * <li>{@code javax.net.ssl.keyStore}</li> * <li>{@code javax.net.ssl.keyStorePassword}</li> * <li>{@code javax.net.ssl.keyStoreType}</li> * <li>{@code javax.net.ssl.trustStore}</li> * <li>{@code javax.net.ssl.trustStorePassword}</li> * <li>{@code javax.net.ssl.trustStoreType}</li> * </ul> * </p> * <p> * {@code javax.net.ssl.trustStore} and {@code javax.net.ssl.keyStore} must point to resources * on the classpath. * </p> * * @param properties * the properties * @return the {@link SchemeRegistry} the SchemeRegistry used for the HttpClient's * {@link ClientConnectionManager} registered for HTTPS */ @Provides @Singleton protected SchemeRegistry provideSchemeRegistry(final PropertiesMap properties) { SchemeRegistry registry = SchemeRegistryFactory.createDefault(); if (properties.getBoolean(SSL_TRUST_ALL)) { try { SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, new TrustManager[] { new TrustAllManager() }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx, new AllowAllHostnameVerifier()); registry.register(new Scheme("https", 443, ssf)); } catch (GeneralSecurityException ex) { Throwables.propagate(ex); } } else { String keyStore = trimToNull(properties.get(KEY_STORE)); String trustStore = trimToNull(properties.get(TRUST_STORE)); if (keyStore != null || trustStore != null) { String keyStorePassword = trimToNull(properties.get(KEY_STORE_PASSWORD)); String keyStoreType = trimToNull(properties.get(KEY_STORE_TYPE)); String trustStorePassword = trimToNull(properties.get(TRUST_STORE_PASSWORD)); String trustStoreType = trimToNull(properties.get(TRUST_STORE_TYPE)); ClassLoader loader = Thread.currentThread().getContextClassLoader(); URL keyStoreUrl = keyStore != null ? loader.getResource(keyStore) : null; URL trustStoreUrl = trustStore != null ? loader.getResource(trustStore) : null; LayeredSchemeSocketFactory socketFactory = new LtSSLSocketFactory(keyStoreUrl, keyStorePassword, keyStoreType, trustStoreUrl, trustStorePassword, trustStoreType); registry.register(new Scheme(HTTPS, 443, socketFactory)); } } return registry; }
From source file:com.googlecode.jdeltasync.DeltaSyncClient.java
/** * Creates a new {@link DeltaSyncClient} using a * {@link ThreadSafeClientConnManager} with the default settings. * * @deprecated ThreadSafeClientConnManager has been deprecated in HttpClient. Use {@link #DeltaSyncClient(org.apache.http.client.HttpClient)} *///from w ww.ja va2s.c om @Deprecated public DeltaSyncClient() { this(new ThreadSafeClientConnManager(SchemeRegistryFactory.createDefault())); }
From source file:com.seajas.search.contender.http.ExclusiveConnectionManager.java
/** * Create a scheme registry which either trusts all certificates, or uses the HttpClient default. * * @param trustAllCertificates/*from w ww.java 2s . c o m*/ * @return SchemeRegistry * @throws NoSuchAlgorithmException * @throws KeyManagementException * @throws KeyStoreException * @throws UnrecoverableKeyException */ private static SchemeRegistry createSchemeRegistry(final Boolean trustAllCertificates) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { if (trustAllCertificates) { SchemeRegistry registry = new SchemeRegistry(); if (logger.isInfoEnabled()) logger.info("Trusting all certificates"); X509HostnameVerifier hostnameVerifier = new X509HostnameVerifier() { @Override public void verify(String host, SSLSocket ssl) throws IOException { // Do nothing } @Override public void verify(String host, X509Certificate cert) throws SSLException { //Do nothing } @Override public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException { //Do nothing } @Override public boolean verify(String s, SSLSession sslSession) { return true; } }; registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); registry.register(new Scheme("https", 443, new SSLSocketFactory(new TrustStrategy() { @Override public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { return true; } }, hostnameVerifier)/* { @Override public Socket connectSocket(final Socket socket, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpParams param) throws IOException, UnknownHostException, ConnectTimeoutException { if (socket instanceof SSLSocket) { try { if (logger.isDebugEnabled()) { String currentHost = null; logger.debug("This JVM seems to potentially not support SNI - trying to fix"); try { currentHost = (String) FieldUtils.getFieldValue(socket, "host"); } catch (IllegalAccessException e) { logger.debug("Unable to access field 'host' from the underlaying SSLSocket"); } logger.debug("Overriding default socket hostname of " + (currentHost != null ? currentHost : "(null)") + " with " + remoteAddress.getHostName()); } PropertyUtils.setProperty(socket, "host", remoteAddress.getHostName()); } catch (NoSuchMethodException e) { if (logger.isDebugEnabled()) logger.debug(e); } catch (IllegalAccessException e) { if (logger.isDebugEnabled()) logger.debug(e); } catch (InvocationTargetException e) { if (logger.isDebugEnabled()) logger.debug(e); } } else { if (logger.isDebugEnabled()) logger.debug("This is not the JVM we're looking for - should support SNI"); } return super.connectSocket(socket, remoteAddress, localAddress, param); } } */)); return registry; } else return SchemeRegistryFactory.createDefault(); }
From source file:org.brunocvcunha.taskerbox.core.http.TaskerboxHttpBox.java
/** * Build a new HTTP Client for the given parameters * * @param params/* w w w . ja v a2s . c o m*/ * @return */ public DefaultHttpClient buildNewHttpClient(HttpParams params) { PoolingClientConnectionManager cxMgr = new PoolingClientConnectionManager( SchemeRegistryFactory.createDefault()); cxMgr.setMaxTotal(100); cxMgr.setDefaultMaxPerRoute(20); DefaultHttpClient httpClient = new DefaultHttpClient(cxMgr, params); httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"); // httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, // CookiePolicy.BROWSER_COMPATIBILITY); if (this.useNtlm) { httpClient.getAuthSchemes().register("NTLM", new NTLMSchemeFactory()); httpClient.getAuthSchemes().register("BASIC", new BasicSchemeFactory()); httpClient.getAuthSchemes().register("DIGEST", new DigestSchemeFactory()); httpClient.getAuthSchemes().register("SPNEGO", new SPNegoSchemeFactory()); httpClient.getAuthSchemes().register("KERBEROS", new KerberosSchemeFactory()); } try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, getTrustingManager(), new java.security.SecureRandom()); SSLSocketFactory socketFactory = new SSLSocketFactory(sc); Scheme sch = new Scheme("https", 443, socketFactory); httpClient.getConnectionManager().getSchemeRegistry().register(sch); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } if (this.useProxy) { if (this.proxySocks) { log.info("Using proxy socks " + this.socksHost + ":" + this.socksPort); System.setProperty("socksProxyHost", this.socksHost); System.setProperty("socksProxyPort", String.valueOf(this.socksPort)); } else { HttpHost proxy = new HttpHost(this.proxyHost, this.proxyPort); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); if (this.authProxy) { List<String> authPreferences = new ArrayList<>(); if (this.ntlmProxy) { NTCredentials creds = new NTCredentials(this.proxyUser, this.proxyPassword, this.proxyWorkstation, this.proxyDomain); httpClient.getCredentialsProvider() .setCredentials(new AuthScope(this.proxyHost, this.proxyPort), creds); // httpClient.getCredentialsProvider().setCredentials( // AuthScope.ANY, creds); authPreferences.add(AuthPolicy.NTLM); } else { UsernamePasswordCredentials creds = new UsernamePasswordCredentials(this.proxyUser, this.proxyPassword); httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds); authPreferences.add(AuthPolicy.BASIC); } httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authPreferences); } } } return httpClient; }
From source file:org.apache.wink.client.internal.handlers.httpclient.ApacheHttpClientConnectionHandler.java
private synchronized HttpClient openConnection(ClientRequest request) throws NoSuchAlgorithmException, KeyManagementException { if (this.httpclient != null) { return this.httpclient; }//from ww w . j a va 2s.c om // cast is safe because we're on the client ApacheHttpClientConfig config = (ApacheHttpClientConfig) request.getAttribute(WinkConfiguration.class); BasicHttpParams params = new BasicHttpParams(); params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, Integer.valueOf(config.getConnectTimeout())); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, Integer.valueOf(config.getReadTimeout())); params.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.valueOf(config.isFollowRedirects())); if (config.isFollowRedirects()) { params.setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE); } // setup proxy if (config.getProxyHost() != null) { params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(config.getProxyHost(), config.getProxyPort())); } if (config.getMaxPooledConnections() > 0) { SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault(); ThreadSafeClientConnManager httpConnectionManager = new ThreadSafeClientConnManager(schemeRegistry); httpConnectionManager.setMaxTotal(config.getMaxPooledConnections()); httpConnectionManager.setDefaultMaxPerRoute(config.getMaxPooledConnections()); this.httpclient = new DefaultHttpClient(httpConnectionManager, params); } else { this.httpclient = new DefaultHttpClient(params); } if (config.getBypassHostnameVerification()) { SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(null, null, null); SSLSocketFactory sf = new SSLSocketFactory(sslcontext, new X509HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException { } public void verify(String host, X509Certificate cert) throws SSLException { } public void verify(String host, SSLSocket ssl) throws IOException { } }); httpclient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, sf)); } return this.httpclient; }
From source file:com.wso2telco.dep.verificationhandler.verifier.ACRHandler.java
/** * Gets the msisdn./*w w w . j a v a 2 s.c om*/ * * @param Url the url * @param Requeststr the requeststr * @return the msisdn * @throws UnsupportedEncodingException the unsupported encoding exception * @throws IOException Signals that an I/O exception has occurred. * @throws JSONException the JSON exception */ private String getMSISDN(String Url, String Requeststr) throws UnsupportedEncodingException, IOException, JSONException { String Authtoken = "con123"; String retStr = ""; log.info("RequestString =" + Requeststr); PoolingClientConnectionManager connManager = new PoolingClientConnectionManager( SchemeRegistryFactory.createDefault()); HttpClient client = new DefaultHttpClient(connManager); //DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost(Url); postRequest.addHeader("accept", "application/json"); postRequest.addHeader("authorization", "ServiceKey " + Authtoken); StringEntity input = new StringEntity(Requeststr); input.setContentType("application/json"); postRequest.setEntity(input); HttpResponse response = client.execute(postRequest); BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); String output; while ((output = br.readLine()) != null) { retStr += output; } log.info("ACR Response " + retStr); client.getConnectionManager().shutdown(); org.json.JSONObject jsonBody = null; jsonBody = new org.json.JSONObject(retStr.toString()); String msisdn = jsonBody.getJSONObject("decodeAcrResponse").getString("msisdn"); return msisdn; }
From source file:net.yacy.cora.federate.solr.instance.RemoteInstance.java
/** * @return a connection manager with a HTTP connection pool *//* w w w. j a v a 2s . c o m*/ private static org.apache.http.impl.conn.PoolingClientConnectionManager buildConnectionManager() { /* Important note : use of deprecated Apache classes is required because SolrJ still use them internally (see HttpClientUtil). * Upgrade only when Solr implementation will become compatible */ final org.apache.http.impl.conn.PoolingClientConnectionManager cm = new org.apache.http.impl.conn.PoolingClientConnectionManager( SchemeRegistryFactory.createDefault(), DEFAULT_POOLED_CONNECTION_TIME_TO_LIVE, TimeUnit.SECONDS); initPoolMaxConnections(cm, DEFAULT_POOL_MAX_TOTAL); return cm; }