Example usage for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager ThreadSafeClientConnManager

List of usage examples for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager ThreadSafeClientConnManager

Introduction

In this page you can find the example usage for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager ThreadSafeClientConnManager.

Prototype

@Deprecated
public ThreadSafeClientConnManager(final HttpParams params, final SchemeRegistry schreg) 

Source Link

Document

Creates a new thread safe connection manager.

Usage

From source file:com.evernote.client.conn.mobile.TEvernoteHttpClient.java

@Deprecated
private DefaultHttpClient getHTTPClient() {

    try {/*from   w w w  .  j a  v  a 2  s .  com*/
        if (mConnectionManager != null) {
            mConnectionManager.closeExpiredConnections();
            mConnectionManager.closeIdleConnections(1, TimeUnit.SECONDS);
        } else {
            BasicHttpParams params = new BasicHttpParams();

            HttpConnectionParams.setConnectionTimeout(params, 10000);
            HttpConnectionParams.setSoTimeout(params, 20000);

            ConnManagerParams.setMaxTotalConnections(params, ConnManagerParams.DEFAULT_MAX_TOTAL_CONNECTIONS);
            ConnManagerParams.setTimeout(params, 10000);

            ConnPerRouteBean connPerRoute = new ConnPerRouteBean(18); // Giving 18 connections to Evernote
            ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);

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

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

            mConnectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);
            DefaultHttpClient httpClient = new DefaultHttpClient(mConnectionManager, params);
            httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
                @Override
                public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                    return 2 * 60 * 1000; // 2 minutes in millis
                }
            });

            httpClient.setReuseStrategy(new ConnectionReuseStrategy() {
                @Override
                public boolean keepAlive(HttpResponse response, HttpContext context) {
                    return true;
                }
            });
            mHttpClient = httpClient;
        }
    } catch (Exception ex) {
        return null;
    }

    return mHttpClient;
}

From source file:org.mumod.util.HttpManager.java

public HttpManager(Context context, String host) {
    mContext = context;/* w w  w  .  j  a  v a2 s.c o m*/
    HttpParams params = getHttpParams();
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", UntrustedSSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);
    mClient = new DefaultHttpClient(manager, params);
    mHost = host;
}

From source file:org.andrico.andjax.http.HttpClientService.java

/**
 * Create a thread-safe client./*from  w  w  w.j av a2  s  . c o  m*/
 * 
 * @return HttpClient
 */
private final HttpClient createHttpClient() {
    // Sets up the http part of the service.
    final SchemeRegistry supportedSchemes = new SchemeRegistry();

    // Register the "http" protocol scheme, it is required
    // by the default operator to look up socket factories.
    final SocketFactory sf = PlainSocketFactory.getSocketFactory();
    supportedSchemes.register(new Scheme("http", sf, 80));

    // Set some client http client parameter defaults.
    final HttpParams httpParams = this.createHttpParams();

    final ClientConnectionManager ccm = new ThreadSafeClientConnManager(httpParams, supportedSchemes);
    return new DefaultHttpClient(ccm, httpParams);
}

From source file:jp.ambrosoli.quickrestclient.apache.service.ApacheHttpService.java

/**
 * {@link ClientConnectionManager}??????
 * // w  w  w .j  a  v a 2  s.  c  o m
 * @param schreg
 *            {@link SchemeRegistry}?
 * @param params
 *            {@link HttpParams}?
 * @return ???{@link ClientConnectionManager}?
 */
protected ClientConnectionManager createClientConnectionManager(final HttpParams params,
        final SchemeRegistry schreg) {
    return new ThreadSafeClientConnManager(params, schreg);
}

From source file:mobi.dlys.android.core.net.http.client.AsyncHttpClient.java

/**
 * Creates a new AsyncHttpClient./*from  w  w  w . j  a  v a 2  s. com*/
 * 
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 * @throws KeyStoreException
 * @throws UnrecoverableKeyException
 */
public AsyncHttpClient()
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    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));

    KeyStore keyStroe = KeyStore.getInstance(KeyStore.getDefaultType());
    SSLSocketFactory ssf = new ClientSSLSocketFactory(keyStroe);

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

    // threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool();
    threadPool = ThreadPool.getInstance().getDefaultThreadPoolExecutor();

    // threadPool = new ThreadPoolExecutor(10, 30, 60, TimeUnit.SECONDS, new
    // LinkedBlockingQueue<Runnable>());

    requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>();
    clientHeaderMap = new HashMap<String, String>();
}

From source file:com.example.wechatsample.library.http.AsyncHttpClient.java

/**
 * Creates a new AsyncHttpClient./*from w ww .  j  a  va  2  s  .  c  o  m*/
 */
public AsyncHttpClient() {
    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));
    ThreadSafeClientConnManager cm = null;
    try {
        //  https?
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); // ??

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schemeRegistry.register(new Scheme("https", sf, 443));
        schemeRegistry.register(new Scheme("https", sf, 8443));
        cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (UnrecoverableKeyException e) {
        e.printStackTrace();
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (CertificateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    httpContext = new SyncBasicHttpContext(new BasicHttpContext());
    httpClient = new DefaultHttpClient(cm, httpParams);
    httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
        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.addResponseInterceptor(new HttpResponseInterceptor() {
        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));

    threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool();

    requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>();
    clientHeaderMap = new HashMap<String, String>();
}

From source file:org.fedoracommons.funapi.pmh.AbstractPmhResolver.java

protected HttpClient getHttpClient() {
    if (httpClient != null) {
        return httpClient;
    }/*from   w  w  w  . j  a v  a  2  s .c o  m*/

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

    HttpParams params = new BasicHttpParams();
    // Increase max total connection to 200
    params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 200);
    params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, 20);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    DefaultHttpClient httpClient = new DefaultHttpClient(cm, params);
    if (getUsername() != null && getPassword() != null) {
        httpClient.getCredentialsProvider().setCredentials(
                new AuthScope(getPmhBaseUrl().getHost(), getPmhBaseUrl().getPort()),
                new UsernamePasswordCredentials(getUsername(), getPassword()));
    }
    this.httpClient = httpClient;
    return httpClient;
}

From source file:com.pispower.video.sdk.net.SimpleSSLSocketFactory.java

/**
 * Gets a DefaultHttpClient which trusts a set of certificates specified by
 * the KeyStore/*from   www  . ja v  a 2 s .  c o m*/
 * 
 * @param keyStore
 *            custom provided KeyStore instance
 * @return DefaultHttpClient
 */
public static DefaultHttpClient getDefaultHttpClient() {

    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        SSLSocketFactory sf = new SimpleSSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:mixedserver.protocol.jsonrpc.client.HTTPSession.java

HttpClient http() throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException,
        KeyStoreException, CertificateException, IOException {
    if (client == null) {
        HttpParams params = new BasicHttpParams();

        HttpConnectionParams.setConnectionTimeout(params, getConnectionTimeout());
        HttpConnectionParams.setSoTimeout(params, getSoTimeout());
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);//  ww  w .java2 s. c  om

        SSLSocketFactory sf = new EasySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

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

        /*
         * ClientConnectionManager mgr = new ThreadSafeClientConnManager(
         * params, registry);
         */

        ClientConnectionManager mgr = new ThreadSafeClientConnManager(params, registry);

        DefaultHttpClient defaultHttpClient = new DefaultHttpClient(mgr, params);

        // gzip?
        defaultHttpClient.addRequestInterceptor(new HttpRequestInterceptor() {

            public void process(final HttpRequest request, final HttpContext context)
                    throws HttpException, IOException {
                if (!request.containsHeader("Accept-Encoding")) {
                    request.addHeader("Accept-Encoding", "gzip");
                }
            }

        });

        defaultHttpClient.addResponseInterceptor(new HttpResponseInterceptor() {

            public void process(final HttpResponse response, final HttpContext context)
                    throws HttpException, IOException {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    Header ceheader = entity.getContentEncoding();
                    if (ceheader != null) {
                        HeaderElement[] codecs = ceheader.getElements();
                        for (int i = 0; i < codecs.length; i++) {
                            if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                                response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                                return;
                            }
                        }
                    }
                }
            }

        });
        client = defaultHttpClient;
    }
    return client;
}

From source file:com.wso2.mobile.mdm.utils.ServerUtilities.java

public static HttpClient getCertifiedHttpClient(Context context) {
    try {/*  w w  w  .j ava2s  . co  m*/
        KeyStore localTrustStore = KeyStore.getInstance("BKS");
        InputStream in = context.getResources().openRawResource(R.raw.emm_truststore);
        localTrustStore.load(in, CommonUtilities.TRUSTSTORE_PASSWORD.toCharArray());

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        SSLSocketFactory sslSocketFactory = new SSLSocketFactory(localTrustStore);
        schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
        HttpParams params = new BasicHttpParams();
        ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);

        HttpClient client = new DefaultHttpClient(cm, params);
        return client;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}