Example usage for org.apache.http.impl.conn PoolingClientConnectionManager PoolingClientConnectionManager

List of usage examples for org.apache.http.impl.conn PoolingClientConnectionManager PoolingClientConnectionManager

Introduction

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

Prototype

public PoolingClientConnectionManager(final SchemeRegistry schreg) 

Source Link

Usage

From source file:org.sbs.goodcrawler.fetcher.Fetcher.java

public void init(File fetchConfFile) {
    FetchConfig fetchConfig = new FetchConfig();
    Document document;/*w w w. j  av a 2s  .  c  o m*/
    try {
        document = Jsoup.parse(fetchConfFile, "utf-8");
        Fetcher.config = fetchConfig.loadConfig(document);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ConfigurationException e) {
        e.printStackTrace();
    }

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

    params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getAgent());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeoutMilliseconds());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());

    params.setBooleanParameter("http.protocol.handle-redirects", false);

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

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

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

    if (config.getProxyHost() != null) {

        if (config.getProxyUsername() != null) {
            httpClient.getCredentialsProvider().setCredentials(
                    new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
        }

        HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        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) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }

    });

    if (connectionMonitorThread == null) {
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();

}

From source file:fr.ippon.wip.http.hc.HttpClientResourceManager.java

private HttpClientResourceManager() {
    perUserClientMap = Collections.synchronizedMap(new HashMap<String, HttpClient>());
    perUserCookieStoreMap = Collections.synchronizedMap(new HashMap<String, CookieStore>());
    perUserWindowCredentialProviderMap = Collections
            .synchronizedMap(new HashMap<String, CredentialsProvider>());
    currentPortletRequest = new ThreadLocal<PortletRequest>();
    currentPortletResponse = new ThreadLocal<PortletResponse>();
    currentRequest = new ThreadLocal<RequestBuilder>();

    try {//from w  ww.j a va2 s.  c  om
        SSLSocketFactory ssf = new SSLSocketFactory(new TrustSelfSignedStrategy(),
                new AllowAllHostnameVerifier());
        Scheme httpsScheme = new Scheme("https", 443, ssf);
        PlainSocketFactory psf = new PlainSocketFactory();
        Scheme httpScheme = new Scheme("http", 80, psf);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(httpsScheme);
        registry.register(httpScheme);
        connectionManager = new PoolingClientConnectionManager(registry);
        connectionManager.setDefaultMaxPerRoute(10);
        connectionManager.setMaxTotal(100);

        DefaultHttpClient defaultHttpClient = new DefaultHttpClient(connectionManager);

        // automatically redirects all HEAD, GET and POST requests
        defaultHttpClient.setRedirectStrategy(new LaxRedirectStrategy());

        CacheConfig cacheConfig = createAndConfigureCache();

        URL ehCacheConfig = getClass().getResource("/ehcache.xml");
        cacheManager = CacheManager.create(ehCacheConfig);
        Ehcache ehcache = cacheManager.getEhcache("public");
        EhcacheHttpCacheStorage httpCacheStorage = new EhcacheHttpCacheStorage(ehcache);

        CachingHttpClient sharedCacheClient = new CachingHttpClient(defaultHttpClient, httpCacheStorage,
                cacheConfig);
        HttpClientDecorator decoratedClient = new HttpClientDecorator(sharedCacheClient);

        decoratedClient.addPreProcessor(new LtpaRequestInterceptor());
        decoratedClient.addPreProcessor(new StaleIfErrorRequestInterceptor(staleIfErrorTime));
        decoratedClient.addFilter(new IgnoreHttpRequestFilter());

        decoratedClient.addPostProcessor(new TransformerResponseInterceptor());

        rootClient = decoratedClient;

    } catch (Exception e) {
        throw new RuntimeException("Could not initialize connection manager", e);
    }

}

From source file:org.wrml.runtime.service.rest.RestService.java

@Override
protected void initFromConfiguration(final ServiceConfiguration config) {

    final SchemeRegistry schemeRegistry = new SchemeRegistry();

    // Tips on HttpClient performance: http://hc.apache.org/httpclient-3.x/performance.html

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

    // TODO: Make configurable
    final PoolingClientConnectionManager _ConnectionManager = new PoolingClientConnectionManager(
            schemeRegistry);//from  w w w .  j  a v  a 2  s .  co m
    _ConnectionManager.setMaxTotal(200);
    _ConnectionManager.setDefaultMaxPerRoute(20);

    _HttpClient = new DefaultHttpClient(_ConnectionManager);
}

From source file:org.bungeni.ext.integration.bungeniportal.BungeniAppConnector.java

/**
 * Thread safe client ,since we access the same client across threads
 * @return //ww w  .jav  a2  s  .  c o m
 */
public DefaultHttpClient getThreadSafeClient() {
    PoolingClientConnectionManager cxMgr = new PoolingClientConnectionManager(
            SchemeRegistryFactory.createDefault());
    cxMgr.setMaxTotal(100);
    cxMgr.setDefaultMaxPerRoute(20);

    DefaultHttpClient aClient = new DefaultHttpClient();
    HttpParams params = aClient.getParams();
    return new DefaultHttpClient(cxMgr, params);
}

From source file:com.kurento.kmf.content.internal.StreamingProxy.java

/**
 * After constructor method; it created the HTTP client using configuration
 * parameters {@link ContentApiConfiguration}.
 * /* w  w w.  ja v a2 s  .c  o  m*/
 * @see ContentApiConfiguration
 */
@PostConstruct
public void afterPropertiesSet() {
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, configuration.getProxyConnectionTimeout());
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, configuration.getProxySocketTimeout());

    // Thread-safe configuration (using PoolingClientConnectionManager)
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
    cm.setMaxTotal(configuration.getProxyMaxConnections());
    cm.setDefaultMaxPerRoute(configuration.getProxyMaxConnectionsPerRoute());

    httpClient = new DefaultHttpClient(cm, params);
}

From source file:com.dumiduh.das.AnalyticsAPIInvoker.java

private String invokePost(String url, String username, String pwd, String type, String payload) {
    TrustStrategyExt strategy = new TrustStrategyExt();

    String jsonString = "";
    try {// w w  w.  jav a  2  s.  com
        SSLSocketFactory sf = new SSLSocketFactory(strategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("https", Integer.parseInt(port), sf));
        ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);

        DefaultHttpClient client = new DefaultHttpClient(ccm);

        StringEntity stringEntity = new StringEntity(payload);
        HttpPost post = new HttpPost();
        post.setEntity(stringEntity);
        String header = "Basic " + getBase64EncodedToken(username, pwd);
        post.setHeader("Authorization", header);
        post.setHeader("Accept", "application/json");
        post.setHeader("Content-Type", "application/json");

        HttpResponse resp = client.execute(post);
        if (type.equals("body")) {
            BufferedReader rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));

            StringBuffer result = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            jsonString = result.toString();
        } else if (type.equals("header")) {
            StringBuffer result = new StringBuffer();
            Header[] headers = resp.getAllHeaders();
            for (Header h : headers) {

                result.append(h.getName() + " : " + h.getValue());
            }
            result.append("status code : " + resp.getStatusLine().getStatusCode());
            jsonString = result.toString();
        }

        client.close();

    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(AnalyticsAPIInvoker.class.getName()).log(Level.SEVERE, null, ex);
    } catch (KeyManagementException ex) {
        Logger.getLogger(AnalyticsAPIInvoker.class.getName()).log(Level.SEVERE, null, ex);
    } catch (KeyStoreException ex) {
        Logger.getLogger(AnalyticsAPIInvoker.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnrecoverableKeyException ex) {
        Logger.getLogger(AnalyticsAPIInvoker.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NumberFormatException ex) {
        Logger.getLogger(AnalyticsAPIInvoker.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return jsonString;
}

From source file:net.netheos.pcsapi.providers.StorageProviderFactory.java

/**
 * Builds a specific HttpClient to certain providers
 *
 * @param providerName/* w  ww. j  ava2  s .c  o m*/
 * @return client to be used, or null if default should be used.
 */
private static HttpClient buildDedicatedHttpClient(String providerName) throws IOException {
    /**
     * Basic java does not trust CloudMe CA CloudMe CA needs to be added
     */
    if (providerName.equals("cloudme") && !PcsUtils.ANDROID) {
        try {
            KeyStore ks = KeyStore.getInstance("JKS");
            InputStream is = null;

            try {
                is = StorageProviderFactory.class.getResourceAsStream("/cloudme.jks");
                ks.load(is, "changeit".toCharArray());
            } finally {
                PcsUtils.closeQuietly(is);
            }

            SSLContext context = SSLContext.getInstance("TLS");
            TrustManagerFactory caTrustManagerFactory = TrustManagerFactory.getInstance("SunX509");
            caTrustManagerFactory.init(ks);
            context.init(null, caTrustManagerFactory.getTrustManagers(), null);

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

            ClientConnectionManager cnxManager = new PoolingClientConnectionManager(schemeRegistry);

            return new DefaultHttpClient(cnxManager);

        } catch (GeneralSecurityException ex) {
            throw new UnsupportedOperationException("Can't configure HttpClient for Cloud Me", ex);
        }
    }

    return null;
}

From source file:com.rsa.redchallenge.standaloneapp.utils.RestInteractor.java

private static DefaultHttpClient getHttpClient() {

    if (ApplicationConstant.SA_BASE_URL.contains("https")) {
        TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
            @Override// w  ww  .java 2s  .  c  o m
            public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                return false;
            }
        };

        SSLSocketFactory sf = null;
        try {
            sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        } catch (NoSuchAlgorithmException | UnrecoverableKeyException | KeyStoreException
                | KeyManagementException e) {
            e.printStackTrace();
        }

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("https", 443, sf));
        ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);

        return new DefaultHttpClient(ccm);
    } else {
        return new DefaultHttpClient();
    }
}

From source file:edu.jhu.pha.vospace.storage.SwiftStorageManager.java

public static HttpClient getHttpClient() {
    if (null == cm) {

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

        cm = new PoolingClientConnectionManager(schemeRegistry);
        // Increase max total connection to 200
        cm.setMaxTotal(200);/* w  ww  .  j  ava2 s .c  o  m*/
        // Increase default max connection per route to 20
        cm.setDefaultMaxPerRoute(50);
    }

    if (null == httpClient)
        httpClient = new DefaultHttpClient(cm);

    return httpClient;
}

From source file:com.emc.esu.api.rest.EsuRestApiApache.java

/**
 * Creates a new EsuRestApiApache object.
 * //from  w w  w  .  j  a  v a  2  s  .c o  m
 * @param host the hostname or IP address of the ESU server
 * @param port the port on the server to communicate with. Generally this is
 *            80 for HTTP and 443 for HTTPS.
 * @param uid the username to use when connecting to the server
 * @param sharedSecret the Base64 encoded shared secret to use to sign
 *            requests to the server.
 */
public EsuRestApiApache(String host, int port, String uid, String sharedSecret) {
    super(host, port, uid, sharedSecret);

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

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

    httpClient = new DefaultHttpClient(cm, null);
}