Example usage for org.apache.http.conn.scheme SchemeSocketFactory createSocket

List of usage examples for org.apache.http.conn.scheme SchemeSocketFactory createSocket

Introduction

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

Prototype

Socket createSocket(HttpParams params) throws IOException;

Source Link

Document

Creates a new, unconnected socket.

Usage

From source file:com.subgraph.vega.internal.http.requests.connection.SocksModeClientConnectionOperator.java

@Override
public void openConnection(OperatedClientConnection conn, HttpHost target, InetAddress local,
        HttpContext context, HttpParams params) throws IOException {
    if (!isSocksMode) {
        super.openConnection(conn, target, local, context, params);
        return;//from   www.  j av  a2  s .co m
    }
    final Scheme scheme = schemeRegistry.getScheme(target.getSchemeName());
    final SchemeSocketFactory sf = scheme.getSchemeSocketFactory();

    final int port = scheme.resolvePort(target.getPort());
    Socket sock = sf.createSocket(params);
    conn.opening(sock, target);
    InetSocketAddress remoteAddress = InetSocketAddress.createUnresolved(target.getHostName(), port);
    InetSocketAddress localAddress = null;
    if (local != null) {
        localAddress = new InetSocketAddress(local, 0);
    }
    try {
        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 (ConnectException ex) {
        throw new HttpHostConnectException(target, ex);
    }
}

From source file:org.lightcouch.CouchDbClientBase.java

/**
 * @return {@link DefaultHttpClient} instance.
 *//*from  w w w  . j a  va 2  s.  c  o m*/
private HttpClient createHttpClient(CouchDbProperties props) {
    DefaultHttpClient httpclient = null;
    try {
        SchemeSocketFactory ssf = null;
        if (props.getProtocol().equals("https")) {
            TrustManager trustManager = new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] chain, String authType)
                        throws CertificateException {
                }

                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            SSLContext sslcontext = SSLContext.getInstance("TLS");
            sslcontext.init(null, new TrustManager[] { trustManager }, null);
            ssf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            SSLSocket socket = (SSLSocket) ssf.createSocket(null);
            socket.setEnabledCipherSuites(new String[] { "SSL_RSA_WITH_RC4_128_MD5" });
        } else {
            ssf = PlainSocketFactory.getSocketFactory();
        }
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme(props.getProtocol(), props.getPort(), ssf));
        PoolingClientConnectionManager ccm = new PoolingClientConnectionManager(schemeRegistry);
        httpclient = new DefaultHttpClient(ccm);
        host = new HttpHost(props.getHost(), props.getPort(), props.getProtocol());
        context = new BasicHttpContext();
        // Http params
        httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
        httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, props.getSocketTimeout());
        httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
                props.getConnectionTimeout());
        int maxConnections = props.getMaxConnections();
        if (maxConnections != 0) {
            ccm.setMaxTotal(maxConnections);
            ccm.setDefaultMaxPerRoute(maxConnections);
        }
        if (props.getProxyHost() != null) {
            HttpHost proxy = new HttpHost(props.getProxyHost(), props.getProxyPort());
            httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        }
        // basic authentication
        if (props.getUsername() != null && props.getPassword() != null) {
            httpclient.getCredentialsProvider().setCredentials(new AuthScope(props.getHost(), props.getPort()),
                    new UsernamePasswordCredentials(props.getUsername(), props.getPassword()));
            props.clearPassword();
            AuthCache authCache = new BasicAuthCache();
            BasicScheme basicAuth = new BasicScheme();
            authCache.put(host, basicAuth);
            context.setAttribute(ClientContext.AUTH_CACHE, authCache);
        }
        // request interceptor
        httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
            public void process(final HttpRequest request, final HttpContext context) throws IOException {
                if (log.isInfoEnabled())
                    log.info(">> " + request.getRequestLine());
            }
        });
        // response interceptor
        httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
            public void process(final HttpResponse response, final HttpContext context) throws IOException {
                validate(response);
                if (log.isInfoEnabled())
                    log.info("<< Status: " + response.getStatusLine().getStatusCode());
            }
        });
    } catch (Exception e) {
        log.error("Error Creating HTTP client. " + e.getMessage());
        throw new IllegalStateException(e);
    }
    return httpclient;
}

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  ww  .  ja va2  s. com

        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");
        }
    }
}