List of usage examples for org.apache.http.impl.client HttpClientBuilder setSslcontext
public final HttpClientBuilder setSslcontext(final SSLContext sslcontext)
From source file:photosharing.api.ExecutorUtil.java
/** * helper method that returns an HTTPClient executor with credentials * available.//from w w w .java 2 s. co m * * Also enables the test case to connect to ANY SSL Certificate * valid/invalid * * @return {Executor} or Null if there is an issue */ public static Executor getExecutor() { Executor executor = null; /* * if using one of the environments without a trusted CA chain or * you are using Fiddler, you want to set TRUST=TRUE in appconfig.properties */ Configuration config = Configuration.getInstance(null); String sTrust = config.getValue(Configuration.TRUST); boolean trusted = Boolean.parseBoolean(sTrust); if (trusted) { try { HttpClientBuilder builder = HttpClients.custom(); // Setup the SSL Context to Trust Any SSL Certificate SSLContextBuilder sslBuilder = new SSLContextBuilder(); sslBuilder.loadTrustMaterial(null, new TrustStrategy() { /** * override for fiddler proxy */ public boolean isTrusted(X509Certificate[] certs, String host) throws CertificateException { return true; } }); SSLContext sslContext = sslBuilder.build(); builder.setHostnameVerifier(new AllowAllHostnameVerifier()); builder.setSslcontext(sslContext); CloseableHttpClient httpClient = builder.build(); executor = Executor.newInstance(httpClient); } catch (NoSuchAlgorithmException e) { logger.log(Level.SEVERE, "Issue with No Algorithm " + e.toString()); } catch (KeyStoreException e) { logger.log(Level.SEVERE, "Issue with KeyStore " + e.toString()); } catch (KeyManagementException e) { logger.log(Level.SEVERE, "Issue with KeyManagement " + e.toString()); } } return executor; }
From source file:org.wso2.mdm.qsg.utils.HTTPInvoker.java
private static HttpClient createHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { HttpClientBuilder b = HttpClientBuilder.create(); // setup a Trust Strategy that allows all certificates. //// w w w . jav a 2s . com SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { return true; } }).build(); b.setSSLContext(sslContext); //b.setSSLHostnameVerifier(new NoopHostnameVerifier()); // don't check Hostnames, either. // -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; // here's the special part: // -- need to create an SSL Socket Factory, to use our weakened "trust strategy"; // -- and create a Registry, to register it. // SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", sslSocketFactory).build(); // now, we create connection-manager using our Registry. // -- allows multi-threaded use PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry); b.setConnectionManager(connMgr); // finally, build the HttpClient; // -- done! CloseableHttpClient client = b.build(); return client; }
From source file:utils.HttpClientGenerator.java
public static CloseableHttpClient getHttpClient(boolean checkCert) { if (checkCert == false) { HttpClientBuilder b = HttpClientBuilder.create(); // setup a Trust Strategy that allows all certificates. SSLContext sslContext = null; try {//from w ww . j a v a 2 s . com sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { return true; } }).build(); } catch (NoSuchAlgorithmException e) { String err = "error occurred while creating SSL disables hhtp client"; } catch (KeyManagementException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } b.setSslcontext(sslContext); // not to check Hostnames HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; // create an SSL Socket Factory, to use weakened "trust strategy"; // and create a Registry, to register it. SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, (X509HostnameVerifier) hostnameVerifier); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder .<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", sslSocketFactory).build(); // creating connection-manager using our Registry. // -- allows multi-threaded use PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager( socketFactoryRegistry); connMgr.setDefaultMaxPerRoute(20); // Increase max connections for localhost:80 to 50 HttpHost localhost = new HttpHost("localhost", 9443); connMgr.setMaxPerRoute(new HttpRoute(localhost), 10); b.setConnectionManager(connMgr); // finally, build the HttpClient; CloseableHttpClient client = b.build(); return client; } else { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); // Increase default max connection per route to 20 cm.setDefaultMaxPerRoute(20); // Increase max connections for localhost:80 to 50 HttpHost localhost = new HttpHost("localhost", 9443); cm.setMaxPerRoute(new HttpRoute(localhost), 10); CloseableHttpClient client = HttpClients.custom().setConnectionManager(cm).build(); return client; } }
From source file:org.metaeffekt.dcc.shell.RemoteAgentTest.java
private HttpClient newHttpClient() throws GeneralSecurityException, IOException { final char[] password = "DYKK8T8m9nKqBRPZ".toCharArray(); final KeyStore keyStore = KeyStore.getInstance("JKS"); keyStore.load(getClass().getResourceAsStream("/dcc-shell.keystore"), password); final KeyStore trustStore = KeyStore.getInstance("JKS"); trustStore.load(getClass().getResourceAsStream("/dcc-shell.truststore"), password); final SSLContextBuilder sslContextBuilder = SSLContexts.custom(); sslContextBuilder.loadKeyMaterial(keyStore, password); sslContextBuilder.loadTrustMaterial(trustStore); final HttpClientBuilder builder = HttpClientBuilder.create(); builder.setSslcontext(sslContextBuilder.build()); builder.setHostnameVerifier(new AllowAllHostnameVerifier()); final HttpClient client = builder.build(); return client; }
From source file:com.diversityarrays.dalclient.httpimpl.DalHttpFactoryImpl.java
@Override public DalCloseableHttpClient createCloseableHttpClient(SSLContext context) { HttpClientBuilder builder = HttpClients.custom(); builder.setSslcontext(context); return new DalCloseableHttpClientImpl(builder.build()); }
From source file:HCEngine.java
private CloseableHttpClient createCloseableHttpClient() throws Exception { HttpClientBuilder builder = HttpClientBuilder.create(); builder.useSystemProperties();//from w ww.jav a 2 s . com builder.setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE); builder.setSSLContext(SSLContext.getDefault()); CloseableHttpClient hc = builder.build(); return hc; }
From source file:org.springframework.cloud.config.server.support.HttpClientSupport.java
public static HttpClientBuilder builder(HttpEnvironmentRepositoryProperties environmentProperties) throws GeneralSecurityException { SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); HttpClientBuilder httpClientBuilder = HttpClients.custom(); if (environmentProperties.isSkipSslValidation()) { sslContextBuilder.loadTrustMaterial(null, (certificate, authType) -> true); httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier()); }//from ww w . j a v a 2s. c om if (!CollectionUtils.isEmpty(environmentProperties.getProxy())) { ProxyHostProperties httpsProxy = environmentProperties.getProxy() .get(ProxyHostProperties.ProxyForScheme.HTTPS); ProxyHostProperties httpProxy = environmentProperties.getProxy() .get(ProxyHostProperties.ProxyForScheme.HTTP); httpClientBuilder.setRoutePlanner(new SchemeBasedRoutePlanner(httpsProxy, httpProxy)); httpClientBuilder .setDefaultCredentialsProvider(new ProxyHostCredentialsProvider(httpProxy, httpsProxy)); } else { httpClientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault())); httpClientBuilder.setDefaultCredentialsProvider(new SystemDefaultCredentialsProvider()); } int timeout = environmentProperties.getTimeout() * 1000; return httpClientBuilder.setSSLContext(sslContextBuilder.build()).setDefaultRequestConfig( RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build()); }
From source file:eionet.webq.web.interceptor.CdrAuthorizationInterceptor.java
/** * Calls a resource in CDR with redirect disabled. Then it is possible to catch if the user is redirected to login page. * * @param url CDR url to fetch./* w w w . j a v a2 s . co m*/ * @param headers HTTP headers to send. * @return HTTP response object * @throws IOException if network error occurs * @throws java.security.NoSuchAlgorithmException * @throws java.security.KeyManagementException */ protected CloseableHttpResponse fetchUrlWithoutRedirection(String url, HttpHeaders headers) throws IOException, NoSuchAlgorithmException, KeyManagementException { HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); httpClientBuilder.setSSLContext(SSLContexts.custom().useProtocol("TLSv1.2").build()) .setRedirectStrategy(new RedirectStrategy() { @Override public boolean isRedirected(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) throws ProtocolException { return false; } @Override public HttpUriRequest getRedirect(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) throws ProtocolException { return null; } }); HttpGet httpget = new HttpGet(url); for (Map.Entry<String, List<String>> header : headers.entrySet()) { for (String value : header.getValue()) { httpget.addHeader(header.getKey(), value); } } CloseableHttpClient client = httpClientBuilder.build(); CloseableHttpResponse httpResponse = client.execute(httpget); return httpResponse; }
From source file:org.metaeffekt.dcc.agent.DccAgentTest.java
private HttpClient newHttpClient() throws GeneralSecurityException, IOException { final char[] password = "changeit".toCharArray(); final KeyStore keyStore = KeyStore.getInstance("JKS"); keyStore.load(DccAgentTest.class.getResourceAsStream("/client.keystore"), password); final KeyStore trustStore = KeyStore.getInstance("JKS"); trustStore.load(DccAgentTest.class.getResourceAsStream("/client.truststore"), password); final SSLContextBuilder sslContextBuilder = SSLContexts.custom(); sslContextBuilder.loadKeyMaterial(keyStore, password); sslContextBuilder.loadTrustMaterial(trustStore); final HttpClientBuilder builder = HttpClientBuilder.create(); builder.setSslcontext(sslContextBuilder.build()); builder.setHostnameVerifier(new AllowAllHostnameVerifier()); final HttpClient client = builder.build(); return client; }
From source file:com.nextdoor.bender.ipc.http.AbstractHttpTransportFactory.java
protected HttpClientBuilder getClientBuilder(boolean useSSL, String url, Map<String, String> stringHeaders, int socketTimeout) { HttpClientBuilder cb = HttpClientBuilder.create(); /*// w w w . ja v a 2s. co m * Setup SSL */ if (useSSL) { /* * All trusting SSL context */ try { cb.setSSLContext(getSSLContext()); } catch (Exception e) { throw new RuntimeException(e); } /* * All trusting hostname verifier */ cb.setSSLHostnameVerifier(new HostnameVerifier() { public boolean verify(String s, SSLSession sslSession) { return true; } }); } /* * Add default headers */ ArrayList<BasicHeader> headers = new ArrayList<BasicHeader>(stringHeaders.size()); stringHeaders.forEach((k, v) -> headers.add(new BasicHeader(k, v))); cb.setDefaultHeaders(headers); /* * Set socket timeout and transport threads */ SocketConfig sc = SocketConfig.custom().setSoTimeout(socketTimeout).build(); cb.setDefaultSocketConfig(sc); cb.setMaxConnPerRoute(this.config.getThreads()); cb.setMaxConnTotal(this.config.getThreads()); return cb; }