Example usage for org.apache.http.conn.scheme SchemeRegistry getScheme

List of usage examples for org.apache.http.conn.scheme SchemeRegistry getScheme

Introduction

In this page you can find the example usage for org.apache.http.conn.scheme SchemeRegistry getScheme.

Prototype

public final Scheme getScheme(final HttpHost host) 

Source Link

Document

Obtains the scheme for a host.

Usage

From source file:org.anhonesteffort.flock.test.registration.HttpClientFactoryTest.java

public void testScheme() throws Exception {
    final HttpClientFactory httpFactory = new HttpClientFactory(getInstrumentation().getContext());
    final DefaultHttpClient httpClient = httpFactory.buildClient();
    final SchemeRegistry schemes = httpClient.getConnectionManager().getSchemeRegistry();

    final Scheme httpScheme = schemes.getScheme("http");
    final Scheme httpsScheme = schemes.getScheme("https");

    assertTrue(httpScheme != null && httpsScheme != null);
    assertTrue(schemes.getSchemeNames().size() == 2);

    assertTrue(httpsScheme.getDefaultPort() == 443);
    assertTrue(httpsScheme.getSocketFactory() instanceof SSLSocketFactory);
}

From source file:com.amazonaws.http.ApacheHttpClient.java

public ApacheHttpClient(ClientConfiguration config) {
    HttpClientFactory httpClientFactory = new HttpClientFactory();
    httpClient = httpClientFactory.createHttpClient(config);
    // disable retry
    ((AbstractHttpClient) httpClient).setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));

    SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry();
    Scheme https = schemeRegistry.getScheme("https");
    ((SSLSocketFactory) https.getSocketFactory())
            .setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
}

From source file:com.unboundid.scim.sdk.PreemptiveAuthInterceptor.java

/**
 * {@inheritDoc}//  w ww.  java  2s  .  c  o m
 */
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    if (target.getPort() < 0) {
        SchemeRegistry schemeRegistry = (SchemeRegistry) context.getAttribute(ClientContext.SCHEME_REGISTRY);
        Scheme scheme = schemeRegistry.getScheme(target);
        target = new HttpHost(target.getHostName(), scheme.resolvePort(target.getPort()),
                target.getSchemeName());
    }

    AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
    if (authCache == null) {
        authCache = new BasicAuthCache();
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(target, basicAuth);
        context.setAttribute(ClientContext.AUTH_CACHE, authCache);
        return;
    }

    CredentialsProvider credsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    if (credsProvider == null) {
        return;
    }

    final AuthState targetState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (targetState != null && targetState.getState() == AuthProtocolState.UNCHALLENGED) {
        final AuthScheme authScheme = authCache.get(target);
        if (authScheme != null) {
            doPreemptiveAuth(target, authScheme, targetState, credsProvider);
        }
    }

    final HttpHost proxy = (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
    final AuthState proxyState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
    if (proxy != null && proxyState != null && proxyState.getState() == AuthProtocolState.UNCHALLENGED) {
        final AuthScheme authScheme = authCache.get(proxy);
        if (authScheme != null) {
            doPreemptiveAuth(proxy, authScheme, proxyState, credsProvider);
        }
    }
}

From source file:illab.nabal.proxy.AbstractContext.java

/**
 * Get a singleton object of AndroidHttpClient.
 * /*ww  w. jav a  2  s . c  o  m*/
 * @return AndroidHttpClient
 */
protected AndroidHttpClient getHttpClient() {
    if (mHttpClient == null) {
        mHttpClient = AndroidHttpClient.newInstance(SystemProperties.USER_AGENT_ANDROID, mContext);

        ClientConnectionManager conMgr = mHttpClient.getConnectionManager();
        SchemeRegistry schReg = conMgr.getSchemeRegistry();
        for (String scheme : schReg.getSchemeNames()) {
            Log.i(TAG, "Scheme: " + scheme + ", port: " + schReg.getScheme(scheme).getDefaultPort()
                    + ", factory: " + schReg.getScheme(scheme).getSocketFactory().getClass().getName());
        }
    }
    return mHttpClient;
}

From source file:org.codegist.crest.io.http.HttpClientFactoryTest.java

@Test
public void createWithMoreThanOneShouldCreateDefaultHttpClientWithConnectionManagerSetup() throws Exception {
    DefaultHttpClient expected = mock(DefaultHttpClient.class);
    ProxySelectorRoutePlanner planner = mock(ProxySelectorRoutePlanner.class);
    ClientConnectionManager clientConnectionManager = mock(ClientConnectionManager.class);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    ProxySelector proxySelector = mock(ProxySelector.class);
    BasicHttpParams httpParams = mock(BasicHttpParams.class);
    ConnPerRouteBean routeBean = mock(ConnPerRouteBean.class);
    PlainSocketFactory plainSocketFactory = mock(PlainSocketFactory.class);
    SSLSocketFactory sslSocketFactory = mock(SSLSocketFactory.class);
    Scheme plainScheme = new Scheme("http", plainSocketFactory, 80);
    Scheme sslScheme = new Scheme("https", sslSocketFactory, 443);
    ThreadSafeClientConnManager threadSafeClientConnManager = mock(ThreadSafeClientConnManager.class);

    when(expected.getConnectionManager()).thenReturn(clientConnectionManager);
    when(clientConnectionManager.getSchemeRegistry()).thenReturn(schemeRegistry);

    mockStatic(ProxySelector.class);
    when(ProxySelector.getDefault()).thenReturn(proxySelector);

    mockStatic(PlainSocketFactory.class);
    when(PlainSocketFactory.getSocketFactory()).thenReturn(plainSocketFactory);

    mockStatic(SSLSocketFactory.class);
    when(SSLSocketFactory.getSocketFactory()).thenReturn(sslSocketFactory);

    whenNew(SchemeRegistry.class).withNoArguments().thenReturn(schemeRegistry);
    whenNew(Scheme.class).withArguments("http", plainSocketFactory, 80).thenReturn(plainScheme);
    whenNew(Scheme.class).withArguments("https", sslSocketFactory, 443).thenReturn(sslScheme);
    whenNew(ThreadSafeClientConnManager.class).withArguments(httpParams, schemeRegistry)
            .thenReturn(threadSafeClientConnManager);
    whenNew(ConnPerRouteBean.class).withArguments(2).thenReturn(routeBean);
    whenNew(BasicHttpParams.class).withNoArguments().thenReturn(httpParams);
    whenNew(DefaultHttpClient.class).withArguments(threadSafeClientConnManager, httpParams)
            .thenReturn(expected);//from w  w  w.  ja v a 2s .co  m
    whenNew(ProxySelectorRoutePlanner.class).withArguments(schemeRegistry, proxySelector).thenReturn(planner);

    when(crestConfig.getConcurrencyLevel()).thenReturn(2);
    HttpClient actual = HttpClientFactory.create(crestConfig, getClass());
    assertSame(expected, actual);

    verify(expected).setRoutePlanner(planner);
    verify(httpParams).setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    verify(httpParams).setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, routeBean);
    verify(httpParams).setIntParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 2);
    assertSame(plainScheme, schemeRegistry.getScheme("http"));
    assertSame(sslScheme, schemeRegistry.getScheme("https"));
}

From source file:org.apache.http.client.protocol.ResponseAuthCache.java

public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
    Args.notNull(response, "HTTP request");
    Args.notNull(context, "HTTP context");
    AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);

    HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    final AuthState targetState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (target != null && targetState != null) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("Target auth state: " + targetState.getState());
        }/*from  w  ww .j  av a  2 s . com*/
        if (isCachable(targetState)) {
            final SchemeRegistry schemeRegistry = (SchemeRegistry) context
                    .getAttribute(ClientContext.SCHEME_REGISTRY);
            if (target.getPort() < 0) {
                final Scheme scheme = schemeRegistry.getScheme(target);
                target = new HttpHost(target.getHostName(), scheme.resolvePort(target.getPort()),
                        target.getSchemeName());
            }
            if (authCache == null) {
                authCache = new BasicAuthCache();
                context.setAttribute(ClientContext.AUTH_CACHE, authCache);
            }
            switch (targetState.getState()) {
            case CHALLENGED:
                cache(authCache, target, targetState.getAuthScheme());
                break;
            case FAILURE:
                uncache(authCache, target, targetState.getAuthScheme());
            }
        }
    }

    final HttpHost proxy = (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
    final AuthState proxyState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
    if (proxy != null && proxyState != null) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("Proxy auth state: " + proxyState.getState());
        }
        if (isCachable(proxyState)) {
            if (authCache == null) {
                authCache = new BasicAuthCache();
                context.setAttribute(ClientContext.AUTH_CACHE, authCache);
            }
            switch (proxyState.getState()) {
            case CHALLENGED:
                cache(authCache, proxy, proxyState.getAuthScheme());
                break;
            case FAILURE:
                uncache(authCache, proxy, proxyState.getAuthScheme());
            }
        }
    }
}

From source file:org.apache.http.impl.conn.DefaultClientConnectionOperator.java

public void openConnection(final OperatedClientConnection conn, final HttpHost target, final InetAddress local,
        final HttpContext context, final HttpParams params) throws IOException {
    Args.notNull(conn, "Connection");
    Args.notNull(target, "Target host");
    Args.notNull(params, "HTTP parameters");
    Asserts.check(!conn.isOpen(), "Connection must not be open");

    final SchemeRegistry registry = getSchemeRegistry(context);
    final Scheme schm = registry.getScheme(target.getSchemeName());
    final SchemeSocketFactory sf = schm.getSchemeSocketFactory();

    final InetAddress[] addresses = resolveHostname(target.getHostName());
    final int port = schm.resolvePort(target.getPort());
    for (int i = 0; i < addresses.length; i++) {
        final InetAddress address = addresses[i];
        final boolean last = i == addresses.length - 1;

        Socket sock = sf.createSocket(params);
        conn.opening(sock, target);//from  w  w  w . j av  a 2  s .c o  m

        final InetSocketAddress remoteAddress = new HttpInetSocketAddress(target, address, port);
        InetSocketAddress localAddress = null;
        if (local != null) {
            localAddress = new InetSocketAddress(local, 0);
        }
        if (this.log.isDebugEnabled()) {
            this.log.debug("Connecting to " + remoteAddress);
        }
        try {
            final Socket connsock = sf.connectSocket(sock, remoteAddress, localAddress, params);
            if (sock != connsock) {
                sock = connsock;
                conn.opening(sock, target);
            }
            prepareSocket(sock, context, params);
            conn.openCompleted(sf.isSecure(sock), params);
            return;
        } catch (final ConnectException ex) {
            if (last) {
                throw ex;
            }
        } catch (final ConnectTimeoutException ex) {
            if (last) {
                throw ex;
            }
        }
        if (this.log.isDebugEnabled()) {
            this.log.debug("Connect to " + remoteAddress + " timed out. "
                    + "Connection will be retried using another IP address");
        }
    }
}

From source file:org.apache.http.impl.conn.DefaultClientConnectionOperator.java

public void updateSecureConnection(final OperatedClientConnection conn, final HttpHost target,
        final HttpContext context, final HttpParams params) throws IOException {
    Args.notNull(conn, "Connection");
    Args.notNull(target, "Target host");
    Args.notNull(params, "Parameters");
    Asserts.check(conn.isOpen(), "Connection must be open");

    final SchemeRegistry registry = getSchemeRegistry(context);
    final Scheme schm = registry.getScheme(target.getSchemeName());
    Asserts.check(schm.getSchemeSocketFactory() instanceof SchemeLayeredSocketFactory,
            "Socket factory must implement SchemeLayeredSocketFactory");
    final SchemeLayeredSocketFactory lsf = (SchemeLayeredSocketFactory) schm.getSchemeSocketFactory();
    final Socket sock = lsf.createLayeredSocket(conn.getSocket(), target.getHostName(),
            schm.resolvePort(target.getPort()), params);
    prepareSocket(sock, context, params);
    conn.update(sock, target, lsf.isSecure(sock), params);
}