Example usage for org.apache.http.conn.ssl SSLSocketFactory getSocketFactory

List of usage examples for org.apache.http.conn.ssl SSLSocketFactory getSocketFactory

Introduction

In this page you can find the example usage for org.apache.http.conn.ssl SSLSocketFactory getSocketFactory.

Prototype

public static SSLSocketFactory getSocketFactory() throws SSLInitializationException 

Source Link

Document

Obtains default SSL socket factory with an SSL context based on the standard JSSE trust material (cacerts file in the security properties directory).

Usage

From source file:com.squeezecontrol.image.HttpFetchingImageStore.java

public HttpFetchingImageStore(String baseUrl, String username, String password) {
    this.baseUrl = baseUrl;

    HttpParams params = new BasicHttpParams();

    // Turn off stale checking. Our connections break all the time anyway,
    // and it's not worth it to pay the penalty of checking every time.
    HttpConnectionParams.setStaleCheckingEnabled(params, false);

    // Default connection and socket timeout of 20 seconds. Tweak to taste.
    HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
    HttpConnectionParams.setSoTimeout(params, 20 * 1000);
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

    ClientConnectionManager mgr = new ThreadSafeClientConnManager(params, schemeRegistry);
    mClient = new DefaultHttpClient(mgr, params);
    if (username != null && !"".equals(username)) {
        Credentials defaultcreds = new UsernamePasswordCredentials("dag", "test");
        mClient.getCredentialsProvider().setCredentials(AuthScope.ANY, defaultcreds);
    }//from   w  w  w.j  av  a 2  s.c o m
}

From source file:org.apache.oozie.service.CallbackActionService.java

/**
 * Initializes the {@link CallbackActionService}.
 *
 * @param services services instance.//from  www. ja va  2s .c om
 */
public void init(Services services) throws ServiceException {
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    registry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    connectionManager = new PoolingClientConnectionManager(registry);
    connectionManager.setMaxTotal(ConfigurationService.getInt(POOL_SIZE));
}

From source file:chen.android.toolkit.network.HttpConnection.java

/**
 * </br><b>title : </b>      HttpClient
 * </br><b>description :</b>TODO
 * </br><b>time :</b>      2012-7-10 ?10:49:35
 * @param charset//from  w  ww .j av a 2s.com
 * @return
 */
public static HttpClient createHttpClient(String charset) {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, charset);
    HttpProtocolParams.setUseExpectContinue(params, true);
    HttpProtocolParams.setUserAgent(params, USER_AGENT);
    ConnManagerParams.setTimeout(params, 1 * 1000);
    HttpConnectionParams.setConnectionTimeout(params, 2 * 1000);
    HttpConnectionParams.setSoTimeout(params, 4 * 1000);
    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    // ??HttpClient
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
    return new DefaultHttpClient(conMgr, params);
}

From source file:org.pixmob.appengine.client.SSLEnabledHttpClient.java

public static SSLEnabledHttpClient newInstance(String userAgent) {
    // the following code comes from AndroidHttpClient (API level 10)

    final HttpParams params = new BasicHttpParams();

    // Turn off stale checking. Our connections break all the time anyway,
    // and it's not worth it to pay the penalty of checking every time.
    HttpConnectionParams.setStaleCheckingEnabled(params, false);

    final int timeout = 60 * 1000;
    HttpConnectionParams.setConnectionTimeout(params, timeout);
    HttpConnectionParams.setSoTimeout(params, timeout);
    HttpConnectionParams.setSocketBufferSize(params, 8192);

    // Don't handle redirects -- return them to the caller. Our code
    // often wants to re-POST after a redirect, which we must do ourselves.
    HttpClientParams.setRedirecting(params, false);

    // Set the specified user agent and register standard protocols.
    HttpProtocolParams.setUserAgent(params, userAgent);

    // Prevent UnknownHostException error with 3G connections:
    // http://stackoverflow.com/questions/2052299/httpclient-on-android-nohttpresponseexception-through-umts-3g
    HttpProtocolParams.setUseExpectContinue(params, false);

    final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    final SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));

    final ClientConnectionManager manager = new SingleClientConnManager(params, schemeRegistry);
    final SSLEnabledHttpClient client = new SSLEnabledHttpClient(manager, params);
    client.addRequestInterceptor(new GzipRequestInterceptor());
    client.addResponseInterceptor(new GzipResponseInterceptor());

    return client;
}

From source file:com.sparkplatform.api.core.HttpClientTest.java

@Test
public void testSSL() throws Exception {

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, new TrustManager[] { new ConnectionApacheHttps.FullTrustManager() }, null);
    HttpClient c = new DefaultHttpClient();
    //SSLSocketFactory sf = new SSLSocketFactory(sslContext,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    SSLSocketFactory sf = SSLSocketFactory.getSocketFactory();
    @SuppressWarnings("deprecation")
    Scheme https = new Scheme("https", sf, 443);
    c.getConnectionManager().getSchemeRegistry().register(https);
    HttpHost h = new HttpHost("api.flexmls.com", 443, "https");

    HttpRequest r = new HttpGet("/v1/");
    HttpResponse rs = c.execute(h, r);/* w w w. j a  va 2 s  .c om*/

    assertEquals(404, rs.getStatusLine().getStatusCode());
    String s = readString(rs.getEntity().getContent());
    assertEquals(s, "{\"D\":{\"Success\":false,\"Code\":404,\"Message\":\"Not Found\"}}");
}

From source file:com.puppetlabs.puppetdb.javaclient.impl.DefaultModule.java

@Override
protected void configure() {
    bind(APIPreferences.class).toInstance(preferences);
    bind(Integer.class).annotatedWith(Names.named(CoreConnectionPNames.CONNECTION_TIMEOUT))
            .toInstance(preferences.getConnectTimeout());
    bind(Integer.class).annotatedWith(Names.named(CoreConnectionPNames.SO_TIMEOUT))
            .toInstance(preferences.getSoTimeout());
    bind(Gson.class).toProvider(GsonProvider.class);
    if (preferences.getCertPEM() != null)
        bind(SSLSocketFactory.class).toProvider(PEM_SSLSocketFactoryProvider.class).in(Singleton.class);
    else//from   w  ww  .j  a v  a  2  s.c  o m
        bind(SSLSocketFactory.class).toInstance(SSLSocketFactory.getSocketFactory());
    bind(HttpConnector.class).to(HttpComponentsConnector.class);
    bind(PuppetDBClient.class).to(PuppetDBClientImpl.class);
}

From source file:org.ttrssreader.net.deprecated.HttpClientFactory.java

public HttpClientFactory() {

    boolean trustAllSslCerts = Controller.getInstance().trustAllSsl();
    boolean useCustomKeyStore = Controller.getInstance().useKeystore();

    registry = new SchemeRegistry();
    registry.register(new Scheme("http", new PlainSocketFactory(), 80));

    SocketFactory socketFactory;//from  w  w  w .  ja  va2s .com

    if (useCustomKeyStore && !trustAllSslCerts) {
        String keystorePassword = Controller.getInstance().getKeystorePassword();

        socketFactory = newSslSocketFactory(keystorePassword);
        if (socketFactory == null) {
            socketFactory = SSLSocketFactory.getSocketFactory();
            Log.w(TAG, "Custom key store could not be read, using default settings.");
        }

    } else if (trustAllSslCerts) {
        socketFactory = new FakeSocketFactory();
    } else {
        socketFactory = SSLSocketFactory.getSocketFactory();
    }

    registry.register(new Scheme("https", socketFactory, 443));

}

From source file:com.inferiorhumanorgans.WayToGo.Agency.BART.BaseXMLTask.java

@Override
protected Void doInBackground(final BARTAgency... someAgencies) {
    Assert.assertEquals(1, someAgencies.length);
    theAgency = someAgencies[0];//  ww w.j a v  a 2  s .  c o m

    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);
    /*HttpConnectionParams.setConnectionTimeout(params, 1000);
    HttpConnectionParams.setSoTimeout(params, 1000);
    ConnManagerParams.setTimeout(params, 1000);*/

    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

    return null;
}

From source file:org.msjs.config.MsjsModule.java

private ClientConnectionManager getConnectionManager() {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(schemeRegistry);
    manager.setMaxTotalConnections(10);//from w w w.j a va  2 s  . co m
    return manager;
}

From source file:org.codehaus.httpcache4j.resolver.HTTPClientResponseResolver.java

public static HTTPClientResponseResolver createMultithreadedInstance() {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(new BasicHttpParams(), schemeRegistry);
    return new HTTPClientResponseResolver(new DefaultHttpClient(cm, new BasicHttpParams()));
}