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

public ThreadSafeClientConnManager(final SchemeRegistry schreg) 

Source Link

Document

Creates a new thread safe connection manager.

Usage

From source file:gov.nih.nci.nbia.StandaloneDMV2.java

private static List<String> connectAndReadFromURL(URL url, String fileName, String userId, String passWd) {
    List<String> data = null;
    DefaultHttpClient httpClient = null;
    TrustStrategy easyStrategy = new TrustStrategy() {
        @Override/* w ww  . ja v a  2s .c  o  m*/
        public boolean isTrusted(X509Certificate[] certificate, String authType) throws CertificateException {
            return true;
        }
    };
    try {
        // SSLContext sslContext = SSLContext.getInstance("SSL");
        // set up a TrustManager that trusts everything
        // sslContext.init(null, new TrustManager[] { new
        // EasyX509TrustManager(null)}, null);

        SSLSocketFactory sslsf = new SSLSocketFactory(easyStrategy,
                SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Scheme httpsScheme = new Scheme("https", 443, sslsf);
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(httpsScheme);
        schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        ClientConnectionManager ccm = new ThreadSafeClientConnManager(schemeRegistry);

        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 50000);
        HttpConnectionParams.setSoTimeout(httpParams, new Integer(12000));
        httpClient = new DefaultHttpClient(ccm, httpParams);
        httpClient.setRoutePlanner(new ProxySelectorRoutePlanner(schemeRegistry, ProxySelector.getDefault()));
        // // Additions by lrt for tcia -
        // // attempt to reduce errors going through a Coyote Point
        // Equalizer load balance switch
        httpClient.getParams().setParameter("http.socket.timeout", new Integer(12000));
        httpClient.getParams().setParameter("http.socket.receivebuffer", new Integer(16384));
        httpClient.getParams().setParameter("http.tcp.nodelay", true);
        httpClient.getParams().setParameter("http.connection.stalecheck", false);
        // // end lrt additions

        HttpPost httpPostMethod = new HttpPost(url.toString());

        List<BasicNameValuePair> postParams = new ArrayList<BasicNameValuePair>();
        postParams.add(new BasicNameValuePair("serverManifestLoc", fileName));
        if (userId != null && passWd != null) {
            postParams.add(new BasicNameValuePair("userId", userId));
            httpPostMethod.addHeader("password", passWd);
        }

        UrlEncodedFormEntity query = new UrlEncodedFormEntity(postParams);
        httpPostMethod.setEntity(query);
        HttpResponse response = httpClient.execute(httpPostMethod);
        int responseCode = response.getStatusLine().getStatusCode();
        // System.out.println("!!!!!Response code for requesting datda file:
        // " + responseCode);

        if (responseCode != HttpURLConnection.HTTP_OK) {
            return null;
        } else {
            InputStream inputStream = response.getEntity().getContent();
            data = IOUtils.readLines(inputStream);
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (KeyStoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnrecoverableKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (httpClient != null) {
            httpClient.getConnectionManager().shutdown();
        }
    }
    return data;
}

From source file:com.msopentech.thali.utilities.universal.HttpKeyHttpClient.java

protected ClientConnectionManager javaCreateClientConnectionManager() {
    // Note that HttpKeySocksProxyClientConnOperator only supports SOCKS connections
    // which is why we return a standard ThreadSafeClientConnManager when there is no
    // proxy and return a version with createConnectionOperator overridden only when
    // the connection is for SOCKS.

    ThreadSafeClientConnManager connManager;

    // It turns out that there is a bug in the Apache code we are using that if you use the argument
    // constructor below it will ignore anything you set in terms of httpParams and so we only get
    // 2 connections per destination which doesn't work for the realy who is sending lots of connections
    // to the local TDH. The work around is to use the single argument constructor below. Unfortunately this
    // constructor doesn't work in Android who has an even older version of the code, hence why we have to
    // methods.//from   w  ww.  j  a v  a2 s.c o m
    if (proxy == null) {
        connManager = new ThreadSafeClientConnManager(schemeRegistry);
    } else {
        connManager = new ThreadSafeClientConnManager(schemeRegistry) {
            @Override
            protected ClientConnectionOperator createConnectionOperator(SchemeRegistry schreg) {
                return new HttpKeySocksProxyClientConnOperator(schreg, proxy);
            }
        };
    }

    connManager.setDefaultMaxPerRoute(maxConnections);
    connManager.setMaxTotal(maxConnections);
    return connManager;
}

From source file:com.senseidb.svc.impl.HttpRestSenseiServiceImpl.java

private DefaultHttpClient createHttpClient(HttpRequestRetryHandler retryHandler) {
    HttpParams params = new BasicHttpParams();
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme(_scheme, _port, PlainSocketFactory.getSocketFactory()));
    ClientConnectionManager cm = new ThreadSafeClientConnManager(registry);
    DefaultHttpClient client = new DefaultHttpClient(cm, params);
    if (retryHandler == null) {
        retryHandler = new HttpRequestRetryHandler() {
            public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
                if (executionCount >= _maxRetries) {
                    // Do not retry if over max retry count
                    return false;
                }/*from w  w w . ja va  2 s .  co  m*/
                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;
            }
        };
    }
    client.setHttpRequestRetryHandler(retryHandler);

    client.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");
            }
        }
    });

    client.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            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.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {
        @Override
        public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
            // Honor 'keep-alive' header
            HeaderElementIterator it = new BasicHeaderElementIterator(
                    response.headerIterator(HTTP.CONN_KEEP_ALIVE));
            while (it.hasNext()) {
                HeaderElement he = it.nextElement();
                String param = he.getName();
                String value = he.getValue();
                if ((value != null) && param.equalsIgnoreCase("timeout")) {
                    try {
                        return Long.parseLong(value) * 1000;
                    } catch (NumberFormatException ignore) {
                    }
                }
            }

            long keepAlive = super.getKeepAliveDuration(response, context);
            if (keepAlive == -1) {
                keepAlive = _defaultKeepAliveDurationMS;
            }
            return keepAlive;
        }
    });

    return client;
}

From source file:com.bigdata.rdf.sail.webapp.AbstractProtocolTest.java

protected ClientConnectionManager newInstance() {

    final ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(newSchemeRegistry());

    // Increase max total connection to 200
    cm.setMaxTotal(200);/* w w  w.  ja  v  a2  s .  c o  m*/

    // Increase default max connection per route to 20
    cm.setDefaultMaxPerRoute(20);

    // Increase max connections for localhost to 50
    final HttpHost localhost = new HttpHost("locahost");

    cm.setMaxForRoute(new HttpRoute(localhost), 50);

    return cm;

}

From source file:org.eweb4j.spiderman.plugin.util.PageFetcherImpl.java

/**
 * client?Header?Cookie// www .jav a 2  s .c  o m
 * @param aconfig
 * @param cookies
 */
public void init(Site _site) {
    //HTTP?
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgentString());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeout());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());

    HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
    paramsBean.setVersion(HttpVersion.HTTP_1_1);
    paramsBean.setContentCharset("UTF-8");
    paramsBean.setUseExpectContinue(false);

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

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

    connectionManager = new ThreadSafeClientConnManager(schemeRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());
    httpClient = new DefaultHttpClient(connectionManager, params);

    httpClient.getParams().setIntParameter("http.socket.timeout", 60000);
    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);
    httpClient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, config.isFollowRedirects());
    //      HttpClientParams.setCookiePolicy(httpClient.getParams(),CookiePolicy.BEST_MATCH);

    //?
    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header contentEncoding = entity.getContentEncoding();
            if (contentEncoding != null) {
                HeaderElement[] codecs = contentEncoding.getElements();
                for (HeaderElement codec : codecs) {
                    //?GZIP
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }
    });

    if (_site != null) {
        this.site = _site;
        if (this.site.getHeaders() != null && this.site.getHeaders().getHeader() != null) {
            for (org.eweb4j.spiderman.xml.Header header : this.site.getHeaders().getHeader()) {
                this.addHeader(header.getName(), header.getValue());
            }
        }
        if (this.site.getCookies() != null && this.site.getCookies().getCookie() != null) {
            for (org.eweb4j.spiderman.xml.Cookie cookie : this.site.getCookies().getCookie()) {
                this.addCookie(cookie.getName(), cookie.getValue(), cookie.getHost(), cookie.getPath());
            }
        }
    }
}

From source file:org.apache.wink.client.internal.handlers.httpclient.ApacheHttpClientConnectionHandler.java

private synchronized HttpClient openConnection(ClientRequest request)
        throws NoSuchAlgorithmException, KeyManagementException {
    if (this.httpclient != null) {
        return this.httpclient;
    }/*from   w  w w .  j av a  2s .c  o m*/

    // cast is safe because we're on the client
    ApacheHttpClientConfig config = (ApacheHttpClientConfig) request.getAttribute(WinkConfiguration.class);
    BasicHttpParams params = new BasicHttpParams();
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, Integer.valueOf(config.getConnectTimeout()));
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, Integer.valueOf(config.getReadTimeout()));
    params.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.valueOf(config.isFollowRedirects()));
    if (config.isFollowRedirects()) {
        params.setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE);
    }
    // setup proxy
    if (config.getProxyHost() != null) {
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY,
                new HttpHost(config.getProxyHost(), config.getProxyPort()));
    }

    if (config.getMaxPooledConnections() > 0) {
        SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault();
        ThreadSafeClientConnManager httpConnectionManager = new ThreadSafeClientConnManager(schemeRegistry);

        httpConnectionManager.setMaxTotal(config.getMaxPooledConnections());
        httpConnectionManager.setDefaultMaxPerRoute(config.getMaxPooledConnections());

        this.httpclient = new DefaultHttpClient(httpConnectionManager, params);
    } else {
        this.httpclient = new DefaultHttpClient(params);
    }

    if (config.getBypassHostnameVerification()) {
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, null, null);

        SSLSocketFactory sf = new SSLSocketFactory(sslcontext, new X509HostnameVerifier() {

            public boolean verify(String hostname, SSLSession session) {
                return true;
            }

            public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
            }

            public void verify(String host, X509Certificate cert) throws SSLException {
            }

            public void verify(String host, SSLSocket ssl) throws IOException {
            }
        });
        httpclient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, sf));
    }

    return this.httpclient;
}

From source file:org.ancoron.osgi.test.glassfish.GlassfishDerbyTest.java

protected DefaultHttpClient getHTTPClient() throws NoSuchAlgorithmException, KeyManagementException {
    SSLContext sslContext = SSLContext.getInstance("SSL");

    // set up a TrustManager that trusts everything
    sslContext.init(null, new TrustManager[] { new X509TrustManager() {
        @Override//from  w  w w. j  av  a  2 s.c  o  m
        public X509Certificate[] getAcceptedIssuers() {
            System.out.println("getAcceptedIssuers =============");
            return null;
        }

        @Override
        public void checkClientTrusted(X509Certificate[] certs, String authType) {
            System.out.println("checkClientTrusted =============");
        }

        @Override
        public void checkServerTrusted(X509Certificate[] certs, String authType) {
            System.out.println("checkServerTrusted =============");
        }
    } }, new SecureRandom());

    SSLSocketFactory sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    Scheme httpsScheme = new Scheme("https", 8181, sf);

    PlainSocketFactory plain = new PlainSocketFactory();
    Scheme httpScheme = new Scheme("http", 8080, plain);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(httpsScheme);
    schemeRegistry.register(httpScheme);

    HttpParams params = new BasicHttpParams();

    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
    // Increase max total connection to 200
    cm.setMaxTotal(200);
    // Increase default max connection per route to 20
    cm.setDefaultMaxPerRoute(20);

    DefaultHttpClient httpClient = new DefaultHttpClient(cm, params);
    httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
    return httpClient;
}

From source file:com.su.search.client.solrj.PaHttpSolrServer.java

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

    ccm = new ThreadSafeClientConnManager(schemeRegistry);
    // Increase default max connection per route to 32
    ccm.setDefaultMaxPerRoute(32);/*from w  w  w  .  j  a v a2s  .  c  o m*/
    // Increase max total connection to 128
    ccm.setMaxTotal(128);
    DefaultHttpClient httpClient = new DefaultHttpClient(ccm);
    httpClient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, followRedirects);
    return httpClient;
}