Search in sources :

Example 66 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project baker-android by bakerframework.

the class AndroidHttpClient method newInstance.

/**
 * 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.
 */
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);
    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);
    Object sessionCache = null;
    // Use a session cache for SSL sockets -- Froyo only
    if (null != context && null != sSslSessionCacheClass) {
        Constructor<?> ct;
        try {
            ct = sSslSessionCacheClass.getConstructor(Context.class);
            sessionCache = ct.newInstance(context);
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    // 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));
    SocketFactory sslCertificateSocketFactory = null;
    if (null != sessionCache) {
        Method getHttpSocketFactoryMethod;
        try {
            getHttpSocketFactoryMethod = SSLCertificateSocketFactory.class.getDeclaredMethod("getHttpSocketFactory", Integer.TYPE, sSslSessionCacheClass);
            sslCertificateSocketFactory = (SocketFactory) getHttpSocketFactoryMethod.invoke(null, SOCKET_OPERATION_TIMEOUT, sessionCache);
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if (null == sslCertificateSocketFactory) {
        sslCertificateSocketFactory = SSLSocketFactory.getSocketFactory();
    }
    schemeRegistry.register(new Scheme("https", sslCertificateSocketFactory, 443));
    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);
    // parameters without the funny call-a-static-method dance.
    return new AndroidHttpClient(manager, params);
}
Also used : Context(android.content.Context) ClientContext(org.apache.http.client.protocol.ClientContext) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) Scheme(org.apache.http.conn.scheme.Scheme) PlainSocketFactory(org.apache.http.conn.scheme.PlainSocketFactory) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) SSLCertificateSocketFactory(android.net.SSLCertificateSocketFactory) SocketFactory(org.apache.http.conn.scheme.SocketFactory) Method(java.lang.reflect.Method) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) InvocationTargetException(java.lang.reflect.InvocationTargetException) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SSLCertificateSocketFactory(android.net.SSLCertificateSocketFactory) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicHttpParams(org.apache.http.params.BasicHttpParams)

Example 67 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project keycloak by keycloak.

the class HttpClientBuilder method build.

public HttpClient build() {
    X509HostnameVerifier verifier = null;
    if (this.verifier != null)
        verifier = new VerifierWrapper(this.verifier);
    else {
        switch(policy) {
            case ANY:
                verifier = new AllowAllHostnameVerifier();
                break;
            case WILDCARD:
                verifier = new BrowserCompatHostnameVerifier();
                break;
            case STRICT:
                verifier = new StrictHostnameVerifier();
                break;
        }
    }
    try {
        SSLSocketFactory sslsf = null;
        SSLContext theContext = sslContext;
        if (disableTrustManager) {
            theContext = SSLContext.getInstance("SSL");
            theContext.init(null, new TrustManager[] { new PassthroughTrustManager() }, new SecureRandom());
            verifier = new AllowAllHostnameVerifier();
            sslsf = new SniSSLSocketFactory(theContext, verifier);
        } else if (theContext != null) {
            sslsf = new SniSSLSocketFactory(theContext, verifier);
        } else if (clientKeyStore != null || truststore != null) {
            sslsf = new SniSSLSocketFactory(SSLSocketFactory.TLS, clientKeyStore, clientPrivateKeyPassword, truststore, null, verifier);
        } else {
            final SSLContext tlsContext = SSLContext.getInstance(SSLSocketFactory.TLS);
            tlsContext.init(null, null, null);
            sslsf = new SniSSLSocketFactory(tlsContext, verifier);
        }
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        Scheme httpsScheme = new Scheme("https", 443, sslsf);
        registry.register(httpsScheme);
        ClientConnectionManager cm = null;
        if (connectionPoolSize > 0) {
            ThreadSafeClientConnManager tcm = new ThreadSafeClientConnManager(registry, connectionTTL, connectionTTLUnit);
            tcm.setMaxTotal(connectionPoolSize);
            if (maxPooledPerRoute == 0)
                maxPooledPerRoute = connectionPoolSize;
            tcm.setDefaultMaxPerRoute(maxPooledPerRoute);
            cm = tcm;
        } else {
            cm = new SingleClientConnManager(registry);
        }
        BasicHttpParams params = new BasicHttpParams();
        params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
        if (proxyHost != null) {
            params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHost);
        }
        if (socketTimeout > -1) {
            HttpConnectionParams.setSoTimeout(params, (int) socketTimeoutUnits.toMillis(socketTimeout));
        }
        if (establishConnectionTimeout > -1) {
            HttpConnectionParams.setConnectionTimeout(params, (int) establishConnectionTimeoutUnits.toMillis(establishConnectionTimeout));
        }
        DefaultHttpClient client = new DefaultHttpClient(cm, params);
        if (disableCookieCache) {
            client.setCookieStore(new CookieStore() {

                @Override
                public void addCookie(Cookie cookie) {
                // To change body of implemented methods use File | Settings | File Templates.
                }

                @Override
                public List<Cookie> getCookies() {
                    return Collections.emptyList();
                }

                @Override
                public boolean clearExpired(Date date) {
                    // To change body of implemented methods use File | Settings | File Templates.
                    return false;
                }

                @Override
                public void clear() {
                // To change body of implemented methods use File | Settings | File Templates.
                }
            });
        }
        return client;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Cookie(org.apache.http.cookie.Cookie) Scheme(org.apache.http.conn.scheme.Scheme) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Date(java.util.Date) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SSLException(javax.net.ssl.SSLException) CookieStore(org.apache.http.client.CookieStore) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) X509HostnameVerifier(org.apache.http.conn.ssl.X509HostnameVerifier) StrictHostnameVerifier(org.apache.http.conn.ssl.StrictHostnameVerifier) SingleClientConnManager(org.apache.http.impl.conn.SingleClientConnManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BrowserCompatHostnameVerifier(org.apache.http.conn.ssl.BrowserCompatHostnameVerifier) List(java.util.List) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) BasicHttpParams(org.apache.http.params.BasicHttpParams)

Example 68 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project keycloak by keycloak.

the class HttpClientBuilder method build.

public HttpClient build() {
    X509HostnameVerifier verifier = null;
    if (this.verifier != null)
        verifier = new VerifierWrapper(this.verifier);
    else {
        switch(policy) {
            case ANY:
                verifier = new AllowAllHostnameVerifier();
                break;
            case WILDCARD:
                verifier = new BrowserCompatHostnameVerifier();
                break;
            case STRICT:
                verifier = new StrictHostnameVerifier();
                break;
        }
    }
    try {
        SSLSocketFactory sslsf = null;
        SSLContext theContext = sslContext;
        if (disableTrustManager) {
            theContext = SSLContext.getInstance("SSL");
            theContext.init(null, new TrustManager[] { new PassthroughTrustManager() }, new SecureRandom());
            verifier = new AllowAllHostnameVerifier();
            sslsf = new SniSSLSocketFactory(theContext, verifier);
        } else if (theContext != null) {
            sslsf = new SniSSLSocketFactory(theContext, verifier);
        } else if (clientKeyStore != null || truststore != null) {
            sslsf = new SniSSLSocketFactory(SSLSocketFactory.TLS, clientKeyStore, clientPrivateKeyPassword, truststore, null, verifier);
        } else {
            final SSLContext tlsContext = SSLContext.getInstance(SSLSocketFactory.TLS);
            tlsContext.init(null, null, null);
            sslsf = new SniSSLSocketFactory(tlsContext, verifier);
        }
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        Scheme httpsScheme = new Scheme("https", 443, sslsf);
        registry.register(httpsScheme);
        ClientConnectionManager cm = null;
        if (connectionPoolSize > 0) {
            ThreadSafeClientConnManager tcm = new ThreadSafeClientConnManager(registry, connectionTTL, connectionTTLUnit);
            tcm.setMaxTotal(connectionPoolSize);
            if (maxPooledPerRoute == 0)
                maxPooledPerRoute = connectionPoolSize;
            tcm.setDefaultMaxPerRoute(maxPooledPerRoute);
            cm = tcm;
        } else {
            cm = new SingleClientConnManager(registry);
        }
        BasicHttpParams params = new BasicHttpParams();
        params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
        if (proxyHost != null) {
            params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHost);
        }
        if (socketTimeout > -1) {
            HttpConnectionParams.setSoTimeout(params, (int) socketTimeoutUnits.toMillis(socketTimeout));
        }
        if (establishConnectionTimeout > -1) {
            HttpConnectionParams.setConnectionTimeout(params, (int) establishConnectionTimeoutUnits.toMillis(establishConnectionTimeout));
        }
        DefaultHttpClient client = new DefaultHttpClient(cm, params);
        if (disableCookieCache) {
            client.setCookieStore(new CookieStore() {

                @Override
                public void addCookie(Cookie cookie) {
                // To change body of implemented methods use File | Settings | File Templates.
                }

                @Override
                public List<Cookie> getCookies() {
                    return Collections.emptyList();
                }

                @Override
                public boolean clearExpired(Date date) {
                    // To change body of implemented methods use File | Settings | File Templates.
                    return false;
                }

                @Override
                public void clear() {
                // To change body of implemented methods use File | Settings | File Templates.
                }
            });
        }
        return client;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Cookie(org.apache.http.cookie.Cookie) Scheme(org.apache.http.conn.scheme.Scheme) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Date(java.util.Date) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SSLException(javax.net.ssl.SSLException) CookieStore(org.apache.http.client.CookieStore) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) X509HostnameVerifier(org.apache.http.conn.ssl.X509HostnameVerifier) StrictHostnameVerifier(org.apache.http.conn.ssl.StrictHostnameVerifier) SingleClientConnManager(org.apache.http.impl.conn.SingleClientConnManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BrowserCompatHostnameVerifier(org.apache.http.conn.ssl.BrowserCompatHostnameVerifier) List(java.util.List) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) BasicHttpParams(org.apache.http.params.BasicHttpParams)

Example 69 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project newsrob by marianokamp.

the class CountingInputStream method newInstance.

public static NewsRobHttpClient newInstance(boolean followRedirects, Context ctx) {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setStaleCheckingEnabled(params, true);
    HttpConnectionParams.setConnectionTimeout(params, 45 * 1000);
    HttpConnectionParams.setSoTimeout(params, 45 * 1000);
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    // HttpConnectionParams.setTcpNoDelay(params, true);
    // 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);
    HttpClientParams.setRedirecting(params, followRedirects);
    if (followRedirects)
        params.setIntParameter(ClientPNames.MAX_REDIRECTS, 10);
    // Set the specified user agent and register standard protocols.
    HttpProtocolParams.setUserAgent(params, USER_AGENT);
    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);
    // parameters without the funny call-a-static-method dance.
    return new NewsRobHttpClient(manager, params, ctx);
}
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) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager)

Example 70 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project playn by threerings.

the class AndroidHttpClient method newInstance.

/**
 * Create a new HttpClient with reasonable defaults (which you can update).
 *
 * @param userAgent to report in your HTTP requests.
 * @return AndroidHttpClient for you to use for all your requests.
 */
public static AndroidHttpClient 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);
    // 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);
    // parameters without the funny call-a-static-method dance.
    return new AndroidHttpClient(manager, 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) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager)

Aggregations

ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)77 Scheme (org.apache.http.conn.scheme.Scheme)54 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)51 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)42 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)38 HttpParams (org.apache.http.params.HttpParams)38 BasicHttpParams (org.apache.http.params.BasicHttpParams)31 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)29 IOException (java.io.IOException)24 CertificateException (java.security.cert.CertificateException)17 HttpClient (org.apache.http.client.HttpClient)15 Test (org.junit.Test)15 KeyManagementException (java.security.KeyManagementException)14 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 SSLContext (javax.net.ssl.SSLContext)14 HttpResponse (org.apache.http.HttpResponse)12 KeyStoreException (java.security.KeyStoreException)11 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)11 UnrecoverableKeyException (java.security.UnrecoverableKeyException)10 PoolingClientConnectionManager (org.apache.http.impl.conn.PoolingClientConnectionManager)10