Example usage for org.apache.http.conn.scheme Scheme getSocketFactory

List of usage examples for org.apache.http.conn.scheme Scheme getSocketFactory

Introduction

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

Prototype

@Deprecated
public final SocketFactory getSocketFactory() 

Source Link

Document

Obtains the socket factory.

Usage

From source file:com.googlecode.androidannotations.test15.SSLConnectionTest.java

@Test
public void strictHostnameVerifier() {
    assertNotNull(activity.mHttpsClientTest2);
    ClientConnectionManager ccm = activity.mHttpsClientTest2.getConnectionManager();
    Scheme httpsScheme = ccm.getSchemeRegistry().getScheme("https");
    SSLSocketFactory socketFactHttps = (SSLSocketFactory) httpsScheme.getSocketFactory();

    assertEquals(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER,
            ((SSLSocketFactory) socketFactHttps).getHostnameVerifier());
}

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:org.androidannotations.test.SSLConnectionTest.java

@Test
public void strictHostnameVerifier() {
    assertNotNull(activity.mHttpsClientTest2);
    ClientConnectionManager ccm = activity.mHttpsClientTest2.getConnectionManager();
    Scheme httpsScheme = ccm.getSchemeRegistry().getScheme("https");
    SSLSocketFactory socketFactHttps = (SSLSocketFactory) httpsScheme.getSocketFactory();

    assertEquals(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER, socketFactHttps.getHostnameVerifier());
}

From source file:com.googlecode.androidannotations.test15.SSLConnectionTest.java

@Test
public void truststoreProvided() {
    assertNotNull(activity.mHttpsClientTest1);
    ClientConnectionManager ccm = activity.mHttpsClientTest1.getConnectionManager();
    assertNotNull(ccm);/* w  w w. j  a  v  a  2 s.  c  o  m*/

    Scheme httpsScheme = ccm.getSchemeRegistry().getScheme("https");
    assertNotNull(httpsScheme);

    assertEquals(443, httpsScheme.getDefaultPort());
    SocketFactory socketFactHttps = httpsScheme.getSocketFactory();

    if (!(socketFactHttps instanceof SSLSocketFactory)) {
        Assert.fail("wrong instance should be org.apache.http.conn.ssl.SSLSocketFactory, getting "
                + socketFactHttps);
    }
    assertEquals(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER,
            ((SSLSocketFactory) socketFactHttps).getHostnameVerifier());
}

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:org.androidannotations.test.SSLConnectionTest.java

@Test
public void truststoreProvided() {
    assertNotNull(activity.mHttpsClientTest1);
    ClientConnectionManager ccm = activity.mHttpsClientTest1.getConnectionManager();
    assertNotNull(ccm);/*w  ww .  j  av  a  2s  .co m*/

    Scheme httpsScheme = ccm.getSchemeRegistry().getScheme("https");
    assertNotNull(httpsScheme);

    assertEquals(443, httpsScheme.getDefaultPort());
    SocketFactory socketFactHttps = httpsScheme.getSocketFactory();

    if (!(socketFactHttps instanceof SSLSocketFactory)) {
        fail("wrong instance should be org.apache.http.conn.ssl.SSLSocketFactory, getting " + socketFactHttps);
    }
    assertEquals(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER,
            ((SSLSocketFactory) socketFactHttps).getHostnameVerifier());
}

From source file:com.qiniu.android.http.ClientConnectionOperator.java

public void updateSecureConnection(OperatedClientConnection conn, HttpHost target, HttpContext context,
        HttpParams params) throws IOException {
    if (conn == null) {
        throw new IllegalArgumentException("Connection must not be null.");
    }// ww w  . j  a  v a 2s  .  c om
    if (target == null) {
        throw new IllegalArgumentException("Target host must not be null.");
    }
    if (params == null) {
        throw new IllegalArgumentException("Parameters must not be null.");
    }
    if (!conn.isOpen()) {
        throw new IllegalArgumentException("Connection must be open.");
    }

    final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
    if (!(schm.getSocketFactory() instanceof LayeredSocketFactory)) {
        throw new IllegalArgumentException(
                "Target scheme (" + schm.getName() + ") must have layered socket factory.");
    }

    final LayeredSocketFactory lsf = (LayeredSocketFactory) schm.getSocketFactory();
    final Socket sock;
    try {
        sock = lsf.createSocket(conn.getSocket(), target.getHostName(), target.getPort(), true);
    } catch (ConnectException ex) {
        throw new HttpHostConnectException(target, ex);
    }
    prepareSocket(sock, context, params);
    conn.update(sock, target, lsf.isSecure(sock), params);
}

From source file:com.android.mms.service.http.NetworkAwareClientConnectionOperator.java

/**
 * This method is mostly copied from the overridden one in parent. The only change
 * is how we resolve host name./*  w  w w .  j a v  a  2  s . co  m*/
 */
@Override
public void openConnection(OperatedClientConnection conn, HttpHost target, InetAddress local,
        HttpContext context, HttpParams params) throws IOException {
    if (conn == null) {
        throw new IllegalArgumentException("Connection must not be null.");
    }
    if (target == null) {
        throw new IllegalArgumentException("Target host must not be null.");
    }
    // local address may be null
    //@@@ is context allowed to be null?
    if (params == null) {
        throw new IllegalArgumentException("Parameters must not be null.");
    }
    if (conn.isOpen()) {
        throw new IllegalArgumentException("Connection must not be open.");
    }

    final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
    final SocketFactory sf = schm.getSocketFactory();
    final SocketFactory plain_sf;
    final LayeredSocketFactory layered_sf;
    if (sf instanceof LayeredSocketFactory) {
        plain_sf = staticPlainSocketFactory;
        layered_sf = (LayeredSocketFactory) sf;
    } else {
        plain_sf = sf;
        layered_sf = null;
    }
    // CHANGE FOR MmsService
    ArrayList<InetAddress> addresses = resolveHostName(target.getHostName());

    for (int i = 0; i < addresses.size(); ++i) {
        Log.d(TAG, "NetworkAwareClientConnectionOperator: connecting " + addresses.get(i));
        Socket sock = plain_sf.createSocket();
        conn.opening(sock, target);

        try {
            Socket connsock = plain_sf.connectSocket(sock, addresses.get(i).getHostAddress(),
                    schm.resolvePort(target.getPort()), local, 0, params);
            if (sock != connsock) {
                sock = connsock;
                conn.opening(sock, target);
            }
            /*
             * prepareSocket is called on the just connected
             * socket before the creation of the layered socket to
             * ensure that desired socket options such as
             * TCP_NODELAY, SO_RCVTIMEO, SO_LINGER will be set
             * before any I/O is performed on the socket. This
             * happens in the common case as
             * SSLSocketFactory.createSocket performs hostname
             * verification which requires that SSL handshaking be
             * performed.
             */
            prepareSocket(sock, context, params);
            if (layered_sf != null) {
                Socket layeredsock = layered_sf.createSocket(sock, target.getHostName(),
                        schm.resolvePort(target.getPort()), true);
                if (layeredsock != sock) {
                    conn.opening(layeredsock, target);
                }
                conn.openCompleted(sf.isSecure(layeredsock), params);
            } else {
                conn.openCompleted(sf.isSecure(sock), params);
            }
            break;
            // BEGIN android-changed
            //       catch SocketException to cover any kind of connect failure
        } catch (SocketException ex) {
            if (i == addresses.size() - 1) {
                ConnectException cause = ex instanceof ConnectException ? (ConnectException) ex
                        : (ConnectException) new ConnectException(ex.getMessage()).initCause(ex);
                throw new HttpHostConnectException(target, cause);
            }
            // END android-changed
        } catch (ConnectTimeoutException ex) {
            if (i == addresses.size() - 1) {
                throw ex;
            }
        }
    }
}

From source file:com.qiniu.android.http.ClientConnectionOperator.java

public void openConnection(final OperatedClientConnection conn, final HttpHost target, final InetAddress local,
        final HttpContext context, final HttpParams params) throws IOException {
    if (conn == null) {
        throw new IllegalArgumentException("Connection must not be null.");
    }/*from   w w  w. j  a v  a2s  .co m*/
    if (target == null) {
        throw new IllegalArgumentException("Target host must not be null.");
    }
    // local address may be null
    //@@@ is context allowed to be null?
    if (params == null) {
        throw new IllegalArgumentException("Parameters must not be null.");
    }
    if (conn.isOpen()) {
        throw new IllegalArgumentException("Connection must not be open.");
    }

    final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
    final SocketFactory sf = schm.getSocketFactory();
    String host = target.getHostName();
    String[] ips;
    if (validIP(host)) {
        ips = new String[] { host };
    } else {
        ips = systemResolv(host);
        if (ips == null || ips.length == 0) {
            throw new UnknownHostException("no ip for " + host);
        }
    }

    final int port = schm.resolvePort(target.getPort());
    for (int i = 0; i < ips.length; i++) {
        final String ip = ips[i];
        final boolean last = i == ips.length - 1;

        Socket sock = sf.createSocket();
        conn.opening(sock, target);

        try {
            Socket connsock = sf.connectSocket(sock, ip, port, local, 0, params);
            if (sock != connsock) {
                sock = connsock;
                conn.opening(sock, target);
            }
            prepareSocket(sock, context, params);
            conn.openCompleted(sf.isSecure(sock), params);
            AsyncHttpClientMod.ip.set(ip);
            return;
        } catch (ConnectException ex) {
            if (last) {
                throw new HttpHostConnectException(target, ex);
            }
        }
    }
}

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

@Override
public void openConnection(final OperatedClientConnection conn, final HttpHost target, final InetAddress local,
        final HttpContext context, final HttpParams params) throws IOException {
    Socket socket = null;/*  w w  w.  j  a  va 2s.c o m*/
    Socket sslSocket = null;
    try {
        if (conn == null || target == null || params == null) {
            throw new IllegalArgumentException("Required argument may not be null");
        }
        if (conn.isOpen()) {
            throw new IllegalStateException("Connection must not be open");
        }

        // The original NetCipher code uses a SchemeSocketFactory class that isn't supported by the version
        // of Apache that ships standard with Android. It also doesn't support the layered socket factory
        // interface either. We work around this later on but for now we just get our HttpKeySSLSocketFactory
        Scheme scheme = schemeRegistry.getScheme(target.getSchemeName());
        HttpKeySSLSocketFactory httpKeySSLSocketFactory = (HttpKeySSLSocketFactory) scheme.getSocketFactory();

        int port = scheme.resolvePort(target.getPort());
        String host = target.getHostName();

        // Perform explicit SOCKS4a connection request. SOCKS4a supports remote host name resolution
        // (i.e., Tor resolves the hostname, which may be an onion address).
        // The Android (Apache Harmony) Socket class appears to support only SOCKS4 and throws an
        // exception on an address created using INetAddress.createUnresolved() -- so the typical
        // technique for using Java SOCKS4a/5 doesn't appear to work on Android:
        // https://android.googlesource.com/platform/libcore/+/master/luni/src/main/java/java/net/PlainSocketImpl.java
        // See also: http://www.mit.edu/~foley/TinFoil/src/tinfoil/TorLib.java, for a similar implementation

        // From http://en.wikipedia.org/wiki/SOCKS#SOCKS4a:
        //
        // field 1: SOCKS version number, 1 byte, must be 0x04 for this version
        // field 2: command code, 1 byte:
        //     0x01 = establish a TCP/IP stream connection
        //     0x02 = establish a TCP/IP port binding
        // field 3: network byte order port number, 2 bytes
        // field 4: deliberate invalid IP address, 4 bytes, first three must be 0x00 and the last one must not be 0x00
        // field 5: the user ID string, variable length, terminated with a null (0x00)
        // field 6: the domain name of the host we want to contact, variable length, terminated with a null (0x00)

        socket = new Socket();
        conn.opening(socket, target);
        socket.setSoTimeout(READ_TIMEOUT_MILLISECONDS);
        socket.connect(proxy.address(), CONNECT_TIMEOUT_MILLISECONDS);

        DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
        outputStream.write((byte) 0x04);
        outputStream.write((byte) 0x01);
        outputStream.writeShort((short) port);
        outputStream.writeInt(0x01);
        outputStream.write((byte) 0x00);
        outputStream.write(host.getBytes());
        outputStream.write((byte) 0x00);

        DataInputStream inputStream = new DataInputStream(socket.getInputStream());
        if (inputStream.readByte() != (byte) 0x00 || inputStream.readByte() != (byte) 0x5a) {
            throw new IOException("SOCKS4a connect failed");
        }
        inputStream.readShort();
        inputStream.readInt();

        // In the NetCipher code we cast to SchemeLayeredSocketFactory and call createLayeredSocket which amongst
        // other things takes 'params' as an argument. But none of this is supported in Android. When I looked in
        // Java at what createLayeredSocket was actually doing it was just calling createSocket with exactly the
        // arguments used below (it ignored params completely). So we should be good.
        sslSocket = ((HttpKeySSLSocketFactory) httpKeySSLSocketFactory).createSocket(socket, host, port, true);
        conn.opening(sslSocket, target);
        sslSocket.setSoTimeout(READ_TIMEOUT_MILLISECONDS);
        prepareSocket(sslSocket, context, params);
        conn.openCompleted(httpKeySSLSocketFactory.isSecure(sslSocket), params);
        // TODO: clarify which connection throws java.net.SocketTimeoutException?
    } catch (IOException e) {
        try {
            if (sslSocket != null) {
                sslSocket.close();
            }
            if (socket != null) {
                socket.close();
            }
        } catch (IOException ioe) {
        }
        throw e;
    }
}