Search in sources :

Example 16 with SSLSocketFactory

use of org.apache.http.conn.ssl.SSLSocketFactory in project custom-cert-https by nelenkov.

the class MainActivity method createHttpClient.

private HttpClient createHttpClient(SocketFactory socketFactory) {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);
    ConnPerRoute connPerRoute = new ConnPerRouteBean(MAX_CONN_PER_ROUTE);
    ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
    ConnManagerParams.setMaxTotalConnections(params, MAX_CONNECTIONS);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    SocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    if (socketFactory != null) {
        sslSocketFactory = socketFactory;
    }
    schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    return new DefaultHttpClient(cm, params);
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Scheme(org.apache.http.conn.scheme.Scheme) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) SocketFactory(org.apache.http.conn.scheme.SocketFactory) PlainSocketFactory(org.apache.http.conn.scheme.PlainSocketFactory) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) ConnPerRoute(org.apache.http.conn.params.ConnPerRoute) BasicHttpParams(org.apache.http.params.BasicHttpParams) ConnPerRouteBean(org.apache.http.conn.params.ConnPerRouteBean) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 17 with SSLSocketFactory

use of org.apache.http.conn.ssl.SSLSocketFactory in project platformlayer by platformlayer.

the class MetricClientImpl method buildHttpClient.

private HttpClient buildHttpClient(CertificateAndKey certificateAndKey, List<String> trustKeys) {
    int port = metricBaseUrl.getPort();
    if (port == -1) {
        String scheme = metricBaseUrl.getScheme();
        if (scheme.equals("https")) {
            port = 443;
        } else if (scheme.equals("http")) {
            port = 80;
        } else {
            throw new IllegalArgumentException("Unknown scheme: " + scheme);
        }
    }
    SchemeSocketFactory schemeSocketFactory;
    try {
        KeyManager keyManager = new SimpleClientCertificateKeyManager(certificateAndKey);
        TrustManager trustManager;
        X509HostnameVerifier hostnameVerifier;
        if (trustKeys != null) {
            trustManager = new PublicKeyTrustManager(trustKeys);
            hostnameVerifier = SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
        } else {
            trustManager = null;
            hostnameVerifier = SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
        }
        javax.net.ssl.SSLSocketFactory sslSocketFactory = SslHelpers.buildSslSocketFactory(keyManager, trustManager);
        schemeSocketFactory = new SSLSocketFactory(sslSocketFactory, hostnameVerifier);
    } catch (GeneralSecurityException e) {
        throw new IllegalArgumentException("Error building SSL client", e);
    }
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("https", port, schemeSocketFactory));
    PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(schemeRegistry);
    HttpClient httpClient = new DefaultHttpClient(connectionManager);
    httpClient = new DecompressingHttpClient(httpClient);
    return httpClient;
}
Also used : SimpleClientCertificateKeyManager(com.fathomdb.crypto.SimpleClientCertificateKeyManager) PoolingClientConnectionManager(org.apache.http.impl.conn.PoolingClientConnectionManager) Scheme(org.apache.http.conn.scheme.Scheme) PublicKeyTrustManager(com.fathomdb.crypto.ssl.PublicKeyTrustManager) SchemeSocketFactory(org.apache.http.conn.scheme.SchemeSocketFactory) GeneralSecurityException(java.security.GeneralSecurityException) DecompressingHttpClient(org.apache.http.impl.client.DecompressingHttpClient) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) TrustManager(javax.net.ssl.TrustManager) PublicKeyTrustManager(com.fathomdb.crypto.ssl.PublicKeyTrustManager) X509HostnameVerifier(org.apache.http.conn.ssl.X509HostnameVerifier) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) DecompressingHttpClient(org.apache.http.impl.client.DecompressingHttpClient) HttpClient(org.apache.http.client.HttpClient) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) SimpleClientCertificateKeyManager(com.fathomdb.crypto.SimpleClientCertificateKeyManager) KeyManager(javax.net.ssl.KeyManager)

Example 18 with SSLSocketFactory

use of org.apache.http.conn.ssl.SSLSocketFactory in project platformlayer by platformlayer.

the class ApacheCommonsHttpConfiguration method buildHttpClient.

HttpClient buildHttpClient(SslConfiguration sslConfiguration) {
    HttpParams httpParams = null;
    if (sslConfiguration == null || sslConfiguration.isEmpty()) {
        sslConfiguration = null;
    }
    ClientConnectionManager connectionManager;
    if (sslConfiguration != null) {
        SchemeSocketFactory schemeSocketFactory;
        try {
            javax.net.ssl.SSLSocketFactory sslSocketFactory = sslConfiguration.getSslSocketFactory();
            X509HostnameVerifier apacheHostnameVerifier = null;
            if (sslConfiguration.getHostnameVerifier() != null) {
                apacheHostnameVerifier = new ApacheHostnameVerifierAdapter(sslConfiguration.getHostnameVerifier());
            } else {
                apacheHostnameVerifier = new ApacheHostnameVerifierAdapter(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
            }
            schemeSocketFactory = new SSLSocketFactory(sslSocketFactory, apacheHostnameVerifier);
        } catch (GeneralSecurityException e) {
            throw new IllegalArgumentException("Error building SSL client", e);
        }
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("https", 443, schemeSocketFactory));
        connectionManager = buildConnectionManager(schemeRegistry);
    } else {
        SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault();
        connectionManager = buildConnectionManager(schemeRegistry);
    }
    HttpClient httpClient = buildDefaultHttpClient(connectionManager, httpParams);
    httpClient = wrapHttpClient(httpClient);
    return httpClient;
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) SchemeSocketFactory(org.apache.http.conn.scheme.SchemeSocketFactory) GeneralSecurityException(java.security.GeneralSecurityException) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) PoolingClientConnectionManager(org.apache.http.impl.conn.PoolingClientConnectionManager) HttpParams(org.apache.http.params.HttpParams) X509HostnameVerifier(org.apache.http.conn.ssl.X509HostnameVerifier) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) DecompressingHttpClient(org.apache.http.impl.client.DecompressingHttpClient) HttpClient(org.apache.http.client.HttpClient) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory)

Example 19 with SSLSocketFactory

use of org.apache.http.conn.ssl.SSLSocketFactory in project gfm_viewer by satyagraha.

the class TestWebServiceClientDefault method shouldHandleSimpleRequest.

@Test
public void shouldHandleSimpleRequest() throws Exception {
    ServletHolder sh = new ServletHolder(ServletContainer.class);
    sh.setInitParameter(ServletContainer.RESOURCE_CONFIG_CLASS, StubResourceConfig.class.getName());
    sh.setInitParameter(JSONConfiguration.FEATURE_POJO_MAPPING, "true");
    Server server = new Server(0);
    ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
    context.addServlet(sh, "/*");
    server.start();
    Connector[] connectors = server.getConnectors();
    int port = ((NetworkConnector) connectors[0]).getLocalPort();
    LOGGER.info("server port: " + port);
    Config config = mock(Config.class);
    String apiUrl = "http://localhost:" + port;
    given(config.getApiUrl()).willReturn(apiUrl);
    WebProxyConfig webProxyConfig = mock(WebProxyConfig.class);
    SSLSocketFactory sslSocketFactory = mock(SSLSocketFactory.class);
    ClientConnectionManager connectionManager = new ClientConnManagerDefault(sslSocketFactory);
    WebServiceClient webServiceClient = new WebServiceClientDefault(config, webProxyConfig, connectionManager);
    Random random = new Random();
    // construct the tasks to run
    int threadCount = 50;
    List<WebServiceClientCallable> tasks = new ArrayList<WebServiceClientCallable>();
    for (int threadIndex = 0; threadIndex < threadCount; threadIndex++) {
        String mdText = RandomStringUtils.randomAlphanumeric(random.nextInt(100));
        WebServiceClientCallable task = new WebServiceClientCallable(webServiceClient, mdText);
        tasks.add(task);
    }
    // run the tasks
    ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
    List<Future<String>> futures = executorService.invokeAll(tasks, 10, TimeUnit.SECONDS);
    // verify results
    assertThat(futures, hasSize(threadCount));
    for (int threadIndex = 0; threadIndex < threadCount; threadIndex++) {
        String result = futures.get(threadIndex).get();
        WebServiceClientCallable task = tasks.get(threadIndex);
        assertThat(result, is(performSampleTransformation(task.mdText)));
    }
    server.stop();
}
Also used : NetworkConnector(org.eclipse.jetty.server.NetworkConnector) Connector(org.eclipse.jetty.server.Connector) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) WebProxyConfig(code.satyagraha.gfm.support.api.WebProxyConfig) DefaultResourceConfig(com.sun.jersey.api.core.DefaultResourceConfig) Config(code.satyagraha.gfm.support.api.Config) ArrayList(java.util.ArrayList) WebServiceClient(code.satyagraha.gfm.support.api.WebServiceClient) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) ClientConnManagerDefault(code.satyagraha.gfm.support.impl.conn.ClientConnManagerDefault) WebProxyConfig(code.satyagraha.gfm.support.api.WebProxyConfig) Random(java.util.Random) ExecutorService(java.util.concurrent.ExecutorService) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) Future(java.util.concurrent.Future) WebServiceClientDefault(code.satyagraha.gfm.support.impl.WebServiceClientDefault) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) Test(org.junit.Test)

Example 20 with SSLSocketFactory

use of org.apache.http.conn.ssl.SSLSocketFactory in project platform_frameworks_base by android.

the class AbstractProxyTest method testConnectToHttps.

public void testConnectToHttps() throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setResponseCode(200).setBody("this response comes via HTTPS"));
    server.play();
    HttpClient httpClient = newHttpClient();
    SSLSocketFactory sslSocketFactory = newSslSocketFactory(testSSLContext);
    sslSocketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
    httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", sslSocketFactory, server.getPort()));
    HttpResponse response = httpClient.execute(new HttpGet("https://localhost:" + server.getPort() + "/foo"));
    assertEquals("this response comes via HTTPS", contentToString(response));
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) Scheme(org.apache.http.conn.scheme.Scheme) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory)

Aggregations

SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)57 Scheme (org.apache.http.conn.scheme.Scheme)52 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)36 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)26 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)24 SSLContext (javax.net.ssl.SSLContext)17 HttpClient (org.apache.http.client.HttpClient)17 AllowAllHostnameVerifier (org.apache.http.conn.ssl.AllowAllHostnameVerifier)17 HttpResponse (org.apache.http.HttpResponse)14 CertificateException (java.security.cert.CertificateException)13 MockResponse (com.google.mockwebserver.MockResponse)12 RecordedRequest (com.google.mockwebserver.RecordedRequest)12 TestSSLContext (libcore.javax.net.ssl.TestSSLContext)12 HttpGet (org.apache.http.client.methods.HttpGet)12 IOException (java.io.IOException)11 HttpParams (org.apache.http.params.HttpParams)11 BasicHttpParams (org.apache.http.params.BasicHttpParams)10 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)9 KeyManagementException (java.security.KeyManagementException)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7