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.adrup.saldo.bank.statoil.StatoilManager.java

@Override
public Map<AccountHashKey, RemoteAccount> getAccounts(Map<AccountHashKey, RemoteAccount> accounts)
        throws BankException {
    Log.d(TAG, "-> getAccounts()");
    // HttpClient httpClient = new SaldoHttpClient(mContext);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    // Android doesn't like ICA's cert, so we need a forgiving TrustManager
    schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
    HttpParams params = new BasicHttpParams();
    HttpClient httpClient = new SaldoHttpClient(mContext,
            new ThreadSafeClientConnManager(params, schemeRegistry), null);

    try {//from  ww  w. ja v  a  2  s  . c o  m

        //Do login
        List<NameValuePair> parameters = new ArrayList<NameValuePair>(3);
        parameters.add(new BasicNameValuePair("USERNAME", ("0122" + mBankLogin.getUsername()).toUpperCase()));
        parameters.add(new BasicNameValuePair("referer", "login.jsp"));
        String res = HttpHelper.post(httpClient, LOGIN_URL2, parameters);

        // Do login
        parameters = new ArrayList<NameValuePair>(3);
        parameters.add(new BasicNameValuePair(USER_PARAM, mBankLogin.getUsername()));
        parameters.add(new BasicNameValuePair(PASS_PARAM, mBankLogin.getPassword()));
        parameters.add(new BasicNameValuePair("target", "/nis/stse/main.do"));
        parameters.add(new BasicNameValuePair("prodgroup", "0122"));
        parameters.add(new BasicNameValuePair("USERNAME", ("0122" + mBankLogin.getUsername()).toUpperCase()));
        parameters.add(new BasicNameValuePair("METHOD", "LOGIN"));
        parameters.add(new BasicNameValuePair("CURRENT_METHOD", "PWD"));
        parameters.add(new BasicNameValuePair("choice", "PWD"));
        parameters.add(new BasicNameValuePair("forward", "Logga in"));

        Log.d(TAG, "logging in...");
        res = HttpHelper.post(httpClient, LOGIN_URL3, parameters);

        if (res.contains("errors.header")) {
            //login failed.. bail
            throw new AuthenticationException("auth fail");
        }

        //ACCOUNTS
        Log.d(TAG, "getting account info...");
        res = HttpHelper.get(httpClient, ACCOUNT_URL);
        //Log.d(TAG, "accounts html dump:");
        //Log.d(TAG, res);*

        Pattern pattern = Pattern.compile(ACCOUNTS_REGEX, Pattern.DOTALL);
        Matcher matcher = pattern.matcher(res);

        int ordinal = 1;
        while (matcher.find()) {

            String remoteId = String.valueOf(ordinal);
            ordinal = ordinal++;
            String name = "Statoil Mastercard";
            long balance = Long.parseLong(matcher.group(1).replaceAll("[^0-9\\-]", "")) / 100;
            accounts.put(new AccountHashKey(remoteId, mBankLogin.getId()),
                    new Account(remoteId, mBankLogin.getId(), ordinal, name, balance));
        }

    } catch (IOException e) {
        Log.e(TAG, e.getMessage(), e);
        throw new StatoilException(e.getMessage(), e);
    } catch (HttpException e) {
        Log.e(TAG, e.getMessage(), e);
        throw new StatoilException(e.getMessage(), e);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }

    Log.d(TAG, "<- getAccounts()");

    return accounts;

}

From source file:org.dataconservancy.archive.impl.fcrepo.ri.MultiThreadedHttpClient.java

@Override
protected ClientConnectionManager createClientConnectionManager() {
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https",
            createSSLSocketFactory(config.getSkipSSLTrustCheck(), config.getSkipSSLHostnameVerification()),
            443));/*from   w  w  w  .j  a  v a 2  s .  c o  m*/

    return new ThreadSafeClientConnManager(getParams(), schemeRegistry);
}

From source file:gr.ndre.scuttloid.APITask.java

protected DefaultHttpClient getClient() {
    DefaultHttpClient client;/*from w ww .  j  ava  2  s.c  o  m*/
    if (this.url.startsWith("https://") & this.accept_all_certs) {
        try {
            SchemeRegistry schemeRegistry = new SchemeRegistry();
            schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            schemeRegistry.register(new Scheme("https", new TrustingSSLSocketFactory(), 443));
            HttpParams params = new BasicHttpParams();
            ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);
            client = new DefaultHttpClient(connectionManager, params);
        } catch (Exception e) {
            client = new DefaultHttpClient();
        }
    } else {
        client = new DefaultHttpClient();
    }
    return client;
}

From source file:com.blackboard.LearnServer.java

private AbstractHttpClient getTrustAllSSLHttpClient() {
    try {//www.  j a v a  2  s.co m
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

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

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

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

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        System.out.println("WARNING: Could not create Trust All SSL client, using default" + e.getMessage());
        return new DefaultHttpClient();
    }
}

From source file:com.chatsdk.kenai.jbosh.ApacheHTTPSender.java

private synchronized HttpClient initHttpClient(final BOSHClientConfig config) {
    // Create and initialize HTTP parameters
    HttpParams params = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(params, 100);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUseExpectContinue(params, false);
    if (config != null && config.getProxyHost() != null && config.getProxyPort() != 0) {
        HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }/*from w w w  .j a v  a  2s .com*/

    // Create and initialize scheme registry 
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    SSLSocketFactory sslFactory = SSLSocketFactory.getSocketFactory();
    sslFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    schemeRegistry.register(new Scheme("https", sslFactory, 443));

    // Create an HttpClient with the ThreadSafeClientConnManager.
    // This connection manager must be used if more than one thread will
    // be using the HttpClient.
    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    return new DefaultHttpClient(cm, params);
}

From source file:br.com.anteros.android.synchronism.communication.AndroidHttpClient.java

/**
 * Create a new HttpClient with reasonable defaults (which you can update).
 * /*from   w ww.j  a v  a 2  s.c o  m*/
 * @param userAgent
 *            to report in your HTTP requests.
 * @param sessionCache
 *            persistent session cache
 * @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));

    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);

    // We use a factory method to modify superclass initialization
    // parameters without the funny call-a-static-method dance.
    return new AndroidHttpClient(manager, params);
}

From source file:com.github.rnewson.couchdb.lucene.HttpClientFactory.java

public static synchronized HttpClient getInstance() throws MalformedURLException {
    if (instance == null) {
        final HttpParams params = new BasicHttpParams();
        // protocol params.
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setUseExpectContinue(params, false);
        // connection params.
        HttpConnectionParams.setTcpNoDelay(params, true);
        HttpConnectionParams.setStaleCheckingEnabled(params, false);
        ConnManagerParams.setMaxTotalConnections(params, 1000);
        ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(1000));

        final SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 5984));
        schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
        final ClientConnectionManager cm = new ShieldedClientConnManager(
                new ThreadSafeClientConnManager(params, schemeRegistry));

        instance = new DefaultHttpClient(cm, params);

        if (INI != null) {
            final CredentialsProvider credsProvider = new BasicCredentialsProvider();
            final Iterator<?> it = INI.getKeys();
            while (it.hasNext()) {
                final String key = (String) it.next();
                if (!key.startsWith("lucene.") && key.endsWith(".url")) {
                    final URL url = new URL(INI.getString(key));
                    if (url.getUserInfo() != null) {
                        credsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort()),
                                new UsernamePasswordCredentials(url.getUserInfo()));
                    }/* w  ww .ja va  2s .  c  o m*/
                }
            }
            instance.setCredentialsProvider(credsProvider);
            instance.addRequestInterceptor(new PreemptiveAuthenticationRequestInterceptor(), 0);
        }
    }
    return instance;
}

From source file:eu.sisob.uma.api.crawler4j.crawler.PageFetcher.java

public synchronized static void startConnectionMonitorThread() {
    if (connectionMonitorThread == null) {
        HttpParams params = new BasicHttpParams();
        HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
        paramsBean.setVersion(HttpVersion.HTTP_1_1);
        paramsBean.setContentCharset("UTF-8");
        paramsBean.setUseExpectContinue(false);

        params.setParameter("http.useragent", Configurations.getStringProperty("fetcher.user_agent",
                "crawler4j (http://code.google.com/p/crawler4j/)"));

        params.setIntParameter("http.socket.timeout",
                Configurations.getIntProperty("fetcher.socket_timeout", 20000));

        params.setIntParameter("http.connection.timeout",
                Configurations.getIntProperty("fetcher.connection_timeout", 30000));

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

        ConnPerRouteBean connPerRouteBean = new ConnPerRouteBean();
        connPerRouteBean//www .  j  a v  a 2 s.c o m
                .setDefaultMaxPerRoute(Configurations.getIntProperty("fetcher.max_connections_per_host", 100));
        ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRouteBean);
        ConnManagerParams.setMaxTotalConnections(params,
                Configurations.getIntProperty("fetcher.max_total_connections", 100));

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

        if (Configurations.getBooleanProperty("fetcher.crawl_https", false)) {
            schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
        }

        connectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);

        ProjectLogger.LOGGER.setLevel(Level.INFO);
        httpclient = new DefaultHttpClient(connectionManager, params);
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();
}

From source file:cn.salesuite.saf.download.AndroidHttpClient.java

/**
 * 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.
 *//*from  w w  w . jav a2 s  .c  o  m*/
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);

    // Use a session cache for SSL sockets
    //        SSLSessionCache sessionCache = context == null ? null : new SSLSessionCache(context);

    // 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",
    //                SSLCertificateSocketFactory.getHttpSocketFactory(
    //                SOCKET_OPERATION_TIMEOUT, sessionCache), 443));

    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);

    // We use a factory method to modify superclass initialization
    // parameters without the funny call-a-static-method dance.
    return new AndroidHttpClient(manager, params);
}