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);
}
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;
}
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;
}
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();
}
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());
}
Aggregations