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.hoccer.api.CloudService.java

protected void setupHttpClient() {

    LOG.info("setting up http client");

    BasicHttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setSoTimeout(httpParams, 70 * 1000);
    HttpConnectionParams.setConnectionTimeout(httpParams, 10 * 1000);
    ConnManagerParams.setMaxTotalConnections(httpParams, 200);
    ConnPerRoute connPerRoute = new ConnPerRouteBean(400);
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, connPerRoute);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
    // ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams,
    // HttpClientWithKeystore.getSchemeRegistry());
    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(httpParams, "utf-8");
    mHttpClient = new HttpClientWithKeystore(cm, httpParams);
    mHttpClient.getParams().setParameter("http.useragent", mConfig.getApplicationName());
    mHttpClient.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
    mHttpClient.setReuseStrategy(new NoConnectionReuseStrategy());
}

From source file:com.phodev.http.tools.ConnectionHelper.java

private ConnectionHelper() {
    HttpParams httpParams = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(httpParams, MAX_TOTAL_CONNECTIONS);
    ConnPerRouteBean connPerRoute = new ConnPerRouteBean(MAX_CONNECTIONS_PER_HOST);
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, connPerRoute);

    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUseExpectContinue(httpParams, false);
    SchemeRegistry reg = new SchemeRegistry();
    reg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    reg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(httpParams, reg);
    HttpConnectionParams.setConnectionTimeout(httpParams, CON_TIME_OUT_MS);
    HttpConnectionParams.setSoTimeout(httpParams, SO_TIME_OUT_MS);

    HttpClientParams.setCookiePolicy(httpParams, CookiePolicy.BROWSER_COMPATIBILITY);
    httpClient = new DefaultHttpClient(connectionManager, httpParams);
}

From source file:HybridIT.com.fourspaces.couchdb.Session.java

/**
* Constructor for obtaining a Session with an HTTP-AUTH username/password and (optionally) a secure connection
* This isn't supported by CouchDB - you need a proxy in front to use this
* @param host - hostname/*from   ww w. j a  va  2s  .  c  om*/
* @param port - port to use
* @param user - username
* @param pass - password
* @param secure  - use an SSL connection?
*/
public Session(String host, int port, String user, String pass, boolean usesAuth, boolean secure) {
    this.host = host;
    this.port = port;
    this.user = user;
    this.pass = pass;
    this.usesAuth = usesAuth;
    this.secure = secure;

    httpParams = new BasicHttpParams();
    SchemeRegistry schemeRegistry = new SchemeRegistry();

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

    ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
    DefaultHttpClient defaultClient = new DefaultHttpClient(connManager, httpParams);
    if (user != null) {
        defaultClient.getCredentialsProvider().setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(user, pass));
    }

    this.httpClient = defaultClient;

    setUserAgent("couchdb4j");
    setSocketTimeout((30 * 1000));
    setConnectionTimeout((15 * 1000));

}

From source file:com.pretzlav.android.net.http.AndroidHttpClient.java

/**
 * Create a new HttpClient with reasonable defaults (which you can update).
 *
 * @param userAgent to report in your HTTP requests
 * @param context to use for caching SSL sessions (may be null for no caching)
 * @return AndroidHttpClient for you to use for all your requests.
 *//*from w w w. ja v  a 2  s  . c  o m*/
public static AndroidHttpClient newInstance(String userAgent, Context context) {
    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);

    // 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);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);

    // We use a factory method to modify superclass initialization
    // parameters without the funny call-a-static-method dance.
    return new AndroidHttpClient(manager, params);
}

From source file:com.novoda.commons.net.httpclient.NovodaHttpClient.java

/**
 * Create a new HttpClient with reasonable defaults (which you can update).
 * /*from w  ww.  ja va  2 s  .c  o m*/
 * @param userAgent
 *            to report in your HTTP requests.
 * @return AndroidHttpClient for you to use for all your requests.
 */
public static NovodaHttpClient newInstance(String userAgent) {
    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);

    HttpConnectionParams.setConnectionTimeout(params, SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_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);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);

    // We use a factory method to modify superclass initialization
    // parameters without the funny call-a-static-method dance.
    return new NovodaHttpClient(manager, params);
}

From source file:org.xwiki.wysiwyg.internal.plugin.alfresco.server.NoAuthSimpleHttpClient.java

/**
 * @return the HTTP client// w  ww. j  a va 2 s . co m
 */
protected HttpClient getHttpClient() {
    if (httpClient == null) {
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

        BasicHttpParams parameters = new BasicHttpParams();
        ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(parameters,
                schemeRegistry);
        httpClient = new DefaultHttpClient(connectionManager, parameters);
        httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "XWiki's WYSIWYG Content Editor");
    }
    return httpClient;
}

From source file:eu.europeanaconnect.erds.HTTPResolverMultiThreaded.java

/**
 * TODO: ->Nuno: explain this part please
 *///from  w  w w  .jav  a  2s. c  o m
public HTTPResolverMultiThreaded() {
    HttpParams params = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(params, MAX_CONNECTIONS);
    ConnPerRouteBean connPerRoute = new ConnPerRouteBean(CONN_PER_ROUTE);
    ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);

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

    ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);

    this.httpClient = new DefaultHttpClient(clientConnectionManager, params);
    HttpConnectionParams.setConnectionTimeout(this.httpClient.getParams(), CONECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(this.httpClient.getParams(), SOCKET_TIMEOUT);
    this.httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
    this.httpClient.getParams().setBooleanParameter(ClientPNames.REJECT_RELATIVE_REDIRECT, false);
    this.httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, this.userAgentString);

    HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
        @Override
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount >= MAXIMUM_REQUEST_RETRIES) {
                // Do not retry if over max retry count
                return false;
            }
            if (exception instanceof NoHttpResponseException) {
                // Retry if the server dropped connection on us
                return true;
            }
            if (exception instanceof SSLHandshakeException) {
                // Do not retry on SSL handshake exception
                return false;
            }
            HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
            boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
            if (idempotent) {
                // Retry if the request is considered idempotent 
                return true;
            }
            return false;
        }

    };
    this.httpClient.setHttpRequestRetryHandler(myRetryHandler);
}

From source file:org.mobicents.xcap.client.impl.XcapClientImpl.java

/**
 * //from w w w. ja va 2  s. c o  m
 */
public XcapClientImpl() {
    HttpParams params = new SyncBasicHttpParams();
    params.setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    //params.setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE,
    //        false);
    //params.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK,
    //        false);
    //params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE,
    //        8 * 1024);
    //params.setIntParameter(HttpConnectionParams.SO_TIMEOUT,
    //        15000);
    params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    this.client = new DefaultHttpClient(new ThreadSafeClientConnManager(schemeRegistry), params);
    this.client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {

        public boolean retryRequest(final IOException exception, int executionCount,
                final HttpContext context) {
            return false;
        }

    });
    client.setCredentialsProvider(credentialsProvider);
}

From source file:com.loopj.android.http.RdHttpClient.java

/**
 * Creates a new AsyncHttpClient.//from   ww  w  .  j  a va 2  s .c o  m
 */
public RdHttpClient() {
    BasicHttpParams httpParams = new BasicHttpParams();

    ConnManagerParams.setTimeout(httpParams, socketTimeout);
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections));
    ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS);

    HttpConnectionParams.setSoTimeout(httpParams, socketTimeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout);
    HttpConnectionParams.setTcpNoDelay(httpParams, true);
    HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE);

    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUserAgent(httpParams,
            String.format("android-async-http/%s (http://loopj.com/android-async-http)", VERSION));

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry);

    httpContext = new SyncBasicHttpContext(new BasicHttpContext());
    httpClient = new DefaultHttpClient(cm, httpParams);
    httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
        @Override
        public void process(HttpRequest request, HttpContext context) {
            if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) {
                request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
            }
            for (String header : clientHeaderMap.keySet()) {
                request.addHeader(header, clientHeaderMap.get(header));
            }
        }
    });
    httpClient.clearResponseInterceptors();
    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
        @Override
        public void process(HttpResponse response, HttpContext context) {
            final HttpEntity entity = response.getEntity();
            if (entity == null) {
                return;
            }
            final Header encoding = entity.getContentEncoding();
            if (encoding != null) {
                for (HeaderElement element : encoding.getElements()) {
                    if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
                        response.setEntity(new InflatingEntity(response.getEntity()));
                        break;
                    }
                }
            }
        }
    });

    httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES));

    clientHeaderMap = new HashMap<String, String>();
}

From source file:census.couchdroid.CouchSession.java

/**
 * Constructor for obtaining a Session with an HTTP-AUTH username/password and (optionally) a secure connection
 * This isn't supported by CouchDB - you need a proxy in front to use this
 * @param host - hostname/*w  w w.  ja  v a  2 s  . c o  m*/
 * @param port - port to use
 * @param user - username
 * @param pass - password
 * @param secure  - use an SSL connection?
 */
public CouchSession(String host, int port, String user, String pass, boolean usesAuth, boolean secure) {
    this.host = host;
    this.port = port;
    this.user = user;
    this.pass = pass;
    this.usesAuth = usesAuth;
    this.secure = secure;

    httpParams = new BasicHttpParams();
    SchemeRegistry schemeRegistry = new SchemeRegistry();

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

    ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
    DefaultHttpClient defaultClient = new DefaultHttpClient(connManager, httpParams);
    if (user != null) {
        defaultClient.getCredentialsProvider().setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(user, pass));
    }

    this.httpClient = defaultClient;

    setUserAgent("couchdb4j");
    setSocketTimeout((30 * 1000));
    setConnectionTimeout((15 * 1000));

}