Search in sources :

Example 46 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project XobotOS by xamarin.

the class AndroidHttpClient method newInstance.

/**
     * 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.
     */
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);
    // parameters without the funny call-a-static-method dance.
    return new AndroidHttpClient(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) SSLSessionCache(android.net.SSLSessionCache) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager)

Example 47 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project XobotOS by xamarin.

the class DefaultHttpClient method createClientConnectionManager.

@Override
protected ClientConnectionManager createClientConnectionManager() {
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager connManager = null;
    HttpParams params = getParams();
    ClientConnectionManagerFactory factory = null;
    // Try first getting the factory directly as an object.
    factory = (ClientConnectionManagerFactory) params.getParameter(ClientPNames.CONNECTION_MANAGER_FACTORY);
    if (factory == null) {
        // then try getting its class name.
        String className = (String) params.getParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME);
        if (className != null) {
            try {
                Class<?> clazz = Class.forName(className);
                factory = (ClientConnectionManagerFactory) clazz.newInstance();
            } catch (ClassNotFoundException ex) {
                throw new IllegalStateException("Invalid class name: " + className);
            } catch (IllegalAccessException ex) {
                throw new IllegalAccessError(ex.getMessage());
            } catch (InstantiationException ex) {
                throw new InstantiationError(ex.getMessage());
            }
        }
    }
    if (factory != null) {
        connManager = factory.newInstance(params, registry);
    } else {
        connManager = new SingleClientConnManager(getParams(), registry);
    }
    return connManager;
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) ClientConnectionManagerFactory(org.apache.http.conn.ClientConnectionManagerFactory) SingleClientConnManager(org.apache.http.impl.conn.SingleClientConnManager) AuthSchemeRegistry(org.apache.http.auth.AuthSchemeRegistry) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry)

Example 48 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project newsrob by marianokamp.

the class CountingInputStream method newInstance.

public static NewsRobHttpClient newInstance(boolean followRedirects, Context ctx) {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setStaleCheckingEnabled(params, true);
    HttpConnectionParams.setConnectionTimeout(params, 45 * 1000);
    HttpConnectionParams.setSoTimeout(params, 45 * 1000);
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    // HttpConnectionParams.setTcpNoDelay(params, true);
    // 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);
    HttpClientParams.setRedirecting(params, followRedirects);
    if (followRedirects)
        params.setIntParameter(ClientPNames.MAX_REDIRECTS, 10);
    // Set the specified user agent and register standard protocols.
    HttpProtocolParams.setUserAgent(params, USER_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);
    // parameters without the funny call-a-static-method dance.
    return new NewsRobHttpClient(manager, params, ctx);
}
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)

Example 49 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project robovm by robovm.

the class SingleClientConnManager method releaseConnection.

// non-javadoc, see interface ClientConnectionManager
public void releaseConnection(ManagedClientConnection conn, long validDuration, TimeUnit timeUnit) {
    assertStillUp();
    if (!(conn instanceof ConnAdapter)) {
        throw new IllegalArgumentException("Connection class mismatch, " + "connection not obtained from this manager.");
    }
    if (log.isDebugEnabled()) {
        log.debug("Releasing connection " + conn);
    }
    ConnAdapter sca = (ConnAdapter) conn;
    if (sca.poolEntry == null)
        // already released
        return;
    ClientConnectionManager manager = sca.getManager();
    if (manager != null && manager != this) {
        throw new IllegalArgumentException("Connection not obtained from this manager.");
    }
    try {
        // BEGIN android-changed
        // When recycling a Socket, we un-tag it to avoid collecting
        // statistics from future users.
        final Socket socket = uniquePoolEntry.connection.getSocket();
        if (socket != null) {
            SocketTagger.get().untag(socket);
        }
        // make sure that the response has been read completely
        if (sca.isOpen() && (this.alwaysShutDown || !sca.isMarkedReusable())) {
            if (log.isDebugEnabled()) {
                log.debug("Released connection open but not reusable.");
            }
            // make sure this connection will not be re-used
            // we might have gotten here because of a shutdown trigger
            // shutdown of the adapter also clears the tracked route
            sca.shutdown();
        }
    } catch (IOException iox) {
        //@@@ log as warning? let pass?
        if (log.isDebugEnabled())
            log.debug("Exception shutting down released connection.", iox);
    } finally {
        sca.detach();
        managedConn = null;
        lastReleaseTime = System.currentTimeMillis();
        if (validDuration > 0)
            connectionExpiresTime = timeUnit.toMillis(validDuration) + lastReleaseTime;
        else
            connectionExpiresTime = Long.MAX_VALUE;
    }
}
Also used : IOException(java.io.IOException) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) Socket(java.net.Socket)

Example 50 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project robovm by robovm.

the class DefaultHttpClient method createClientConnectionManager.

@Override
protected ClientConnectionManager createClientConnectionManager() {
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager connManager = null;
    HttpParams params = getParams();
    ClientConnectionManagerFactory factory = null;
    // Try first getting the factory directly as an object.
    factory = (ClientConnectionManagerFactory) params.getParameter(ClientPNames.CONNECTION_MANAGER_FACTORY);
    if (factory == null) {
        // then try getting its class name.
        String className = (String) params.getParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME);
        if (className != null) {
            try {
                Class<?> clazz = Class.forName(className);
                factory = (ClientConnectionManagerFactory) clazz.newInstance();
            } catch (ClassNotFoundException ex) {
                throw new IllegalStateException("Invalid class name: " + className);
            } catch (IllegalAccessException ex) {
                throw new IllegalAccessError(ex.getMessage());
            } catch (InstantiationException ex) {
                throw new InstantiationError(ex.getMessage());
            }
        }
    }
    if (factory != null) {
        connManager = factory.newInstance(params, registry);
    } else {
        connManager = new SingleClientConnManager(getParams(), registry);
    }
    return connManager;
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) ClientConnectionManagerFactory(org.apache.http.conn.ClientConnectionManagerFactory) SingleClientConnManager(org.apache.http.impl.conn.SingleClientConnManager) AuthSchemeRegistry(org.apache.http.auth.AuthSchemeRegistry) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry)

Aggregations

ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)56 Scheme (org.apache.http.conn.scheme.Scheme)41 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)38 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)31 HttpParams (org.apache.http.params.HttpParams)29 BasicHttpParams (org.apache.http.params.BasicHttpParams)27 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)26 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)24 IOException (java.io.IOException)16 CertificateException (java.security.cert.CertificateException)13 HttpClient (org.apache.http.client.HttpClient)9 KeyManagementException (java.security.KeyManagementException)7 KeyStoreException (java.security.KeyStoreException)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 UnrecoverableKeyException (java.security.UnrecoverableKeyException)7 SSLContext (javax.net.ssl.SSLContext)7 PoolingClientConnectionManager (org.apache.http.impl.conn.PoolingClientConnectionManager)7 Test (org.junit.Test)7 ResourceUtil (com.github.hakko.musiccabinet.util.ResourceUtil)5 UnknownHostException (java.net.UnknownHostException)5