public KylinClient(KylinConnectionInfo connInfo) { this.connInfo = connInfo; this.connProps = connInfo.getConnectionProperties(); this.httpClient = new DefaultHttpClient(); this.jsonMapper = new ObjectMapper(); // trust all certificates if (isSSL()) { try { SSLSocketFactory sslsf = new SSLSocketFactory(new TrustStrategy() { public boolean isTrusted(final X509Certificate[] chain, String authType) throws CertificateException { // Oh, I am easy... return true; } }); httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, sslsf)); } catch (Exception e) { throw new RuntimeException("Initialize HTTPS client failed", e); } } }
HttpParams httpParams = new BasicHttpParams(); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", ssf == null ? SSLSocketFactory.getSocketFactory() : ssf, 443)); client = new DefaultHttpClient(cm, httpParams);
public WebDavSocketFactory(TrustManagerFactory trustManagerFactory, String host, int port) throws NoSuchAlgorithmException, KeyManagementException { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[] { trustManagerFactory.getTrustManagerForDomain(host, port) }, null); mSocketFactory = sslContext.getSocketFactory(); mSchemeSocketFactory = org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory(); mSchemeSocketFactory.setHostnameVerifier( org.apache.http.conn.ssl.SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); }
private static class NoPreloadHolder { /** * The factory using the default JVM settings for secure connections. */ private static final SSLSocketFactory DEFAULT_FACTORY = new SSLSocketFactory(); }
/** * Since Jersey 1.19 depends on legacy apache http-client API, we have to as well. */ private ThreadSafeClientConnManager createConnectionManager() { try { ThreadSafeClientConnManager connectionManager; if (sslContext != null) { SchemeSocketFactory socketFactory = new SSLSocketFactory(sslContext, new AllowAllHostnameVerifier()); SchemeRegistry sslSchemeRegistry = new SchemeRegistry(); sslSchemeRegistry.register(new Scheme("https", 443, socketFactory)); connectionManager = new ThreadSafeClientConnManager(sslSchemeRegistry); } else { connectionManager = new ThreadSafeClientConnManager(); } return connectionManager; } catch (Exception e) { throw new IllegalStateException("Cannot initialize Apache connection manager", e); } }
public static DefaultHttpClient httpClientTrustingAllSSLCerts() throws NoSuchAlgorithmException, KeyManagementException { DefaultHttpClient httpclient = new DefaultHttpClient(); SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, getTrustingManager(), new java.security.SecureRandom()); SSLSocketFactory socketFactory = new SSLSocketFactory(sc); Scheme sch = new Scheme("https", socketFactory, 443); httpclient.getConnectionManager().getSchemeRegistry().register(sch); return httpclient; }
SSLContext sslContext; try { sslContext = SSLContext.getInstance(protocol); } catch (NoSuchAlgorithmException e) { return SafeExceptionRethrower.safeRethrow(e); sslContext.init(null, new TrustManager[]{new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; SSLSocketFactory sf = new SSLSocketFactory(sslContext, ALLOW_ALL_HOSTNAME_VERIFIER); return sslSocketFactory(sf);
public static Scheme getMockedScheme() throws NoSuchAlgorithmException, KeyManagementException { SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(null, new TrustManager[] { new DummyX509TrustManager() }, null); SSLSocketFactory sf = new SSLSocketFactory(sslcontext); Scheme https = new Scheme("https", 443, sf); return https; }
/** * Returns a HTTP client configured to use SSL. * * @param url * * @return HTTP client configured to use SSL * @throws org.cloudifysource.restclient.exceptions.RestClientException * Reporting different failures while creating the HTTP client */ private static DefaultHttpClient createSSLHttpClient(final URL url) throws NoSuchAlgorithmException, KeyManagementException { final X509TrustManager trustManager = createTrustManager(); final SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, new TrustManager[] { trustManager }, null); final SSLSocketFactory ssf = new SSLSocketFactory(ctx, createHostnameVerifier()); SyncBasicHttpParams params = new SyncBasicHttpParams(); DefaultHttpClient.setDefaultHttpParams(params); DefaultHttpClient httpClient = createSimpleHttpClient(params); httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme(HTTPS, url.getPort(), ssf)); return httpClient; }
protected void acceptAllCertificatesOf(@Nonnull HttpClient client) throws Exception { final SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, new TrustManager[]{new X509TrustManagerImpl()}, null); final SSLSocketFactory ssf = new SSLSocketFactory(ctx, ALLOW_ALL_HOSTNAME_VERIFIER); client.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, ssf)); }
public static synchronized HttpClient getInstance() throws MalformedURLException { if (instance == null) { final HttpParams params = new BasicHttpParams(); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(1000)); final SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry .register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 5984)); schemeRegistry .register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); final ClientConnectionManager cm = new ShieldedClientConnManager( new ThreadSafeClientConnManager(params, schemeRegistry)); instance = new DefaultHttpClient(cm, params); instance.setCredentialsProvider(credsProvider); instance.addRequestInterceptor(new PreemptiveAuthenticationRequestInterceptor(), 0);
private static void createThreadSafeClient(boolean forceSecure) { httpClient = new DefaultHttpClient(); ClientConnectionManager mgr = httpClient.getConnectionManager(); HttpParams params = httpClient.getParams(); SchemeRegistry schemeRegistry = mgr.getSchemeRegistry(); if (forceSecure) { schemeRegistry.register(new Scheme("https", getSecureConnectionSetting(), 443)); } else { HostnameVerifier hostnameVerifier = SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; SSLSocketFactory socketFactory = SSLSocketFactory .getSocketFactory(); socketFactory .setHostnameVerifier((X509HostnameVerifier) hostnameVerifier); schemeRegistry.register(new Scheme("https", socketFactory, 443)); } httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(params, schemeRegistry), params); }
public static HttpClient createHttpClient(URI uri, Optional<Credentials> credentials) { final DefaultHttpClient httpClient = new DefaultHttpClient(); // TODO if supplier returns null, we may wish to defer initialization until url available? if (uri != null && "https".equalsIgnoreCase(uri.getScheme())) { try { int port = (uri.getPort() >= 0) ? uri.getPort() : 443; SSLSocketFactory socketFactory = new SSLSocketFactory( new TrustAllStrategy(), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Scheme sch = new Scheme("https", port, socketFactory); httpClient.getConnectionManager().getSchemeRegistry().register(sch); } catch (Exception e) { LOG.warn("Error in HTTP Feed of {}, setting trust for uri {}", uri); throw Exceptions.propagate(e); } } // Set credentials if (uri != null && credentials.isPresent()) { String hostname = uri.getHost(); int port = uri.getPort(); httpClient.getCredentialsProvider().setCredentials(new AuthScope(hostname, port), credentials.get()); } return httpClient; }
final HttpParams httpClientParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpClientParams, config.getConnectionTimeout()); HttpConnectionParams.setSoTimeout(httpClientParams, config.getSocketTimeout()); final Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), HTTP_PORT); final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory(); sslSocketFactory.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); final Scheme https = new Scheme("https", sslSocketFactory, HTTPS_PORT); final SchemeRegistry sr = connectionManager.getSchemeRegistry(); sr.register(http); sr.register(https);
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore); SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore); Scheme sch = new Scheme("https", socketFactory, 443); httpclient.getConnectionManager().getSchemeRegistry().register(sch);
/** * Constructor. * @param config the client configuration. */ public ApacheHttpClient(ClientConfiguration config) { HttpClientFactory httpClientFactory = new HttpClientFactory(); httpClient = httpClientFactory.createHttpClient(config); // disable retry ((AbstractHttpClient) httpClient) .setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry(); Scheme https = schemeRegistry.getScheme("https"); ((SSLSocketFactory) https.getSocketFactory()) .setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); }
public static void setHostNameVerifier(DefaultHttpClient httpClient, X509HostnameVerifier hostNameVerifier) { Scheme httpsScheme = httpClient.getConnectionManager().getSchemeRegistry().get("https"); if (httpsScheme != null) { SSLSocketFactory sslSocketFactory = (SSLSocketFactory) httpsScheme.getSchemeSocketFactory(); sslSocketFactory.setHostnameVerifier(hostNameVerifier); } }
public static SSLSocketFactory getFixedSocketFactory() { SSLSocketFactory socketFactory; try { socketFactory = new MySSLSocketFactory(getKeystore()); socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); } catch (Throwable t) { t.printStackTrace(); socketFactory = SSLSocketFactory.getSocketFactory(); } return socketFactory; }
Args.notNull(host, "HTTP host"); Args.notNull(remoteAddress, "Remote address"); final Socket sock = socket != null ? socket : createSocket(context); if (localAddress != null) { sock.bind(localAddress); final SSLSocket sslsock = (SSLSocket) sock; sslsock.startHandshake(); verifyHostname(sslsock, host.getHostName()); return sock; } else { return createLayeredSocket(sock, host.getHostName(), remoteAddress.getPort(), context);
((sock != null) ? sock : createSocket());