Search in sources :

Example 26 with ThreadSafeClientConnManager

use of org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager in project androidquery by androidquery.

the class AbstractAjaxCallback method getClient.

private static DefaultHttpClient getClient() {
    if (client == null || !REUSE_CLIENT) {
        AQUtility.debug("creating http client");
        HttpParams httpParams = new BasicHttpParams();
        //httpParams.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpConnectionParams.setConnectionTimeout(httpParams, NET_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParams, NET_TIMEOUT);
        //ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(NETWORK_POOL));
        ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(25));
        //Added this line to avoid issue at: http://stackoverflow.com/questions/5358014/android-httpclient-oom-on-4g-lte-htc-thunderbolt
        HttpConnectionParams.setSocketBufferSize(httpParams, 8192);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", ssf == null ? SSLSocketFactory.getSocketFactory() : ssf, 443));
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, registry);
        client = new DefaultHttpClient(cm, httpParams);
    }
    return client;
}
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) ConnPerRouteBean(org.apache.http.conn.params.ConnPerRouteBean) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 27 with ThreadSafeClientConnManager

use of org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager in project ribbon by Netflix.

the class NFHttpClientTest method testNFHttpClient.

@Test
public void testNFHttpClient() throws Exception {
    NFHttpClient client = NFHttpClientFactory.getNFHttpClient("localhost", server.getServerPort());
    ThreadSafeClientConnManager cm = (ThreadSafeClientConnManager) client.getConnectionManager();
    cm.setDefaultMaxPerRoute(10);
    HttpGet get = new HttpGet(server.getServerURI());
    ResponseHandler<Integer> respHandler = new ResponseHandler<Integer>() {

        public Integer handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            HttpEntity entity = response.getEntity();
            String contentStr = EntityUtils.toString(entity);
            return contentStr.length();
        }
    };
    long contentLen = client.execute(get, respHandler);
    assertTrue(contentLen > 0);
}
Also used : ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) ResponseHandler(org.apache.http.client.ResponseHandler) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 28 with ThreadSafeClientConnManager

use of org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager in project Anki-Android by Ramblurr.

the class BasicHttpSyncer method req.

public HttpResponse req(String method, InputStream fobj, int comp, boolean hkey, JSONObject registerData, Connection.CancelCallback cancelCallback) {
    File tmpFileBuffer = null;
    try {
        String bdry = "--" + BOUNDARY;
        StringWriter buf = new StringWriter();
        HashMap<String, Object> vars = new HashMap<String, Object>();
        // compression flag and session key as post vars
        vars.put("c", comp != 0 ? 1 : 0);
        if (hkey) {
            vars.put("k", mHKey);
            vars.put("s", mSKey);
        }
        for (String key : vars.keySet()) {
            buf.write(bdry + "\r\n");
            buf.write(String.format(Locale.US, "Content-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n", key, vars.get(key)));
        }
        tmpFileBuffer = File.createTempFile("syncer", ".tmp", new File(AnkiDroidApp.getCacheStorageDirectory()));
        FileOutputStream fos = new FileOutputStream(tmpFileBuffer);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        GZIPOutputStream tgt;
        // payload as raw data or json
        if (fobj != null) {
            // header
            buf.write(bdry + "\r\n");
            buf.write("Content-Disposition: form-data; name=\"data\"; filename=\"data\"\r\nContent-Type: application/octet-stream\r\n\r\n");
            buf.close();
            bos.write(buf.toString().getBytes("UTF-8"));
            // write file into buffer, optionally compressing
            int len;
            BufferedInputStream bfobj = new BufferedInputStream(fobj);
            byte[] chunk = new byte[65536];
            if (comp != 0) {
                tgt = new GZIPOutputStream(bos);
                while ((len = bfobj.read(chunk)) >= 0) {
                    tgt.write(chunk, 0, len);
                }
                tgt.close();
                bos = new BufferedOutputStream(new FileOutputStream(tmpFileBuffer, true));
            } else {
                while ((len = bfobj.read(chunk)) >= 0) {
                    bos.write(chunk, 0, len);
                }
            }
            bos.write(("\r\n" + bdry + "--\r\n").getBytes("UTF-8"));
        } else {
            buf.close();
            bos.write(buf.toString().getBytes("UTF-8"));
        }
        bos.flush();
        bos.close();
        // connection headers
        String url = Collection.SYNC_URL;
        if (method.equals("register")) {
            url = url + "account/signup" + "?username=" + registerData.getString("u") + "&password=" + registerData.getString("p");
        } else if (method.startsWith("upgrade")) {
            url = url + method;
        } else {
            url = url + "sync/" + method;
        }
        HttpPost httpPost = new HttpPost(url);
        HttpEntity entity = new ProgressByteEntity(tmpFileBuffer);
        // body
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-type", "multipart/form-data; boundary=" + BOUNDARY);
        // HttpParams
        HttpParams params = new BasicHttpParams();
        params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
        params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
        params.setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
        params.setParameter(CoreProtocolPNames.USER_AGENT, "AnkiDroid-" + AnkiDroidApp.getPkgVersionName());
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpConnectionParams.setSoTimeout(params, Connection.CONN_TIMEOUT);
        // Registry
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, registry);
        if (cancelCallback != null) {
            cancelCallback.setConnectionManager(cm);
        }
        try {
            HttpClient httpClient = new DefaultHttpClient(cm, params);
            return httpClient.execute(httpPost);
        } catch (SSLException e) {
            Log.e(AnkiDroidApp.TAG, "SSLException while building HttpClient", e);
            return null;
        }
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        Log.e(AnkiDroidApp.TAG, "BasicHttpSyncer.sync: IOException", e);
        return null;
    } catch (JSONException e) {
        throw new RuntimeException(e);
    } finally {
        if (tmpFileBuffer != null && tmpFileBuffer.exists()) {
            tmpFileBuffer.delete();
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) Scheme(org.apache.http.conn.scheme.Scheme) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) SSLException(javax.net.ssl.SSLException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) GZIPOutputStream(java.util.zip.GZIPOutputStream) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) JSONException(org.json.JSONException) ConnPerRouteBean(org.apache.http.conn.params.ConnPerRouteBean) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) JSONObject(org.json.JSONObject) EasySSLSocketFactory(org.apache.commons.httpclient.contrib.ssl.EasySSLSocketFactory)

Example 29 with ThreadSafeClientConnManager

use of org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager in project RoboZombie by sahan.

the class ConfigurationService method getDefault.

/**
	 * <p>The <i>out-of-the-box</i> configuration for an instance of {@link HttpClient} which will be used for 
	 * executing all endpoint requests. Below is a detailed description of all configured properties.</p> 
	 * <br>
	 * <ul>
	 * <li>
	 * <p><b>HttpClient</b></p>
	 * <br>
	 * <p>It registers two {@link Scheme}s:</p>
	 * <br>
	 * <ol>
	 * 	<li><b>HTTP</b> on port <b>80</b> using sockets from {@link PlainSocketFactory#getSocketFactory}</li>
	 * 	<li><b>HTTPS</b> on port <b>443</b> using sockets from {@link SSLSocketFactory#getSocketFactory}</li>
	 * </ol>
	 * 
	 * <p>It uses a {@link ThreadSafeClientConnManager} with the following parameters:</p>
	 * <br>
	 * <ol>
	 * 	<li><b>Redirecting:</b> enabled</li>
	 * 	<li><b>Connection Timeout:</b> 30 seconds</li>
	 * 	<li><b>Socket Timeout:</b> 30 seconds</li>
	 * 	<li><b>Socket Buffer Size:</b> 12000 bytes</li>
	 * 	<li><b>User-Agent:</b> via <code>System.getProperty("http.agent")</code></li>
	 * </ol>
	 * </li>
	 * </ul>
	 * @return the instance of {@link HttpClient} which will be used for request execution
	 * <br><br>
	 * @since 1.3.0
	 */
@Override
public Configuration getDefault() {
    return new Configuration() {

        @Override
        public HttpClient httpClient() {
            try {
                HttpParams params = new BasicHttpParams();
                HttpClientParams.setRedirecting(params, true);
                HttpConnectionParams.setConnectionTimeout(params, 30 * 1000);
                HttpConnectionParams.setSoTimeout(params, 30 * 1000);
                HttpConnectionParams.setSocketBufferSize(params, 12000);
                HttpProtocolParams.setUserAgent(params, System.getProperty("http.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);
                return new DefaultHttpClient(manager, params);
            } catch (Exception e) {
                throw new ConfigurationFailedException(e);
            }
        }
    };
}
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) Configuration(com.lonepulse.robozombie.proxy.Zombie.Configuration) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 30 with ThreadSafeClientConnManager

use of org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager in project RoboZombie by sahan.

the class ZombieConfig method httpClient.

@Override
public HttpClient httpClient() {
    HttpParams params = new BasicHttpParams();
    //to simulate a socket timeout
    HttpConnectionParams.setSoTimeout(params, 2 * 1000);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);
    return new DefaultHttpClient(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) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Aggregations

ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)36 Scheme (org.apache.http.conn.scheme.Scheme)29 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)28 BasicHttpParams (org.apache.http.params.BasicHttpParams)28 HttpParams (org.apache.http.params.HttpParams)27 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)26 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)23 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)10 IOException (java.io.IOException)7 ConnPerRouteBean (org.apache.http.conn.params.ConnPerRouteBean)7 KeyManagementException (java.security.KeyManagementException)5 KeyStoreException (java.security.KeyStoreException)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 UnrecoverableKeyException (java.security.UnrecoverableKeyException)5 CertificateException (java.security.cert.CertificateException)5 HttpHost (org.apache.http.HttpHost)5 UnknownHostException (java.net.UnknownHostException)4 SSLSessionCache (android.net.SSLSessionCache)3 KeyStore (java.security.KeyStore)3 GeneralSecurityException (java.security.GeneralSecurityException)2