Example usage for org.apache.http.conn.ssl SSLSocketFactory createSocket

List of usage examples for org.apache.http.conn.ssl SSLSocketFactory createSocket

Introduction

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

Prototype

public Socket createSocket(final HttpContext context) throws IOException 

Source Link

Usage

From source file:org.apache.hadoop.gateway.jetty.SslSocketTest.java

@Ignore
@Test/*from w w  w . j  a  v  a 2 s.co  m*/
public void testSsl() throws IOException, InterruptedException {
    SslServer server = new SslServer();
    Thread thread = new Thread(server);
    thread.start();
    server.waitUntilReady();

    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "utf-8");
    params.setBooleanParameter("http.protocol.expect-continue", false);

    SSLSocketFactory sslsocketfactory = SSLSocketFactory.getSocketFactory();
    SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket(params);

    sslsocket.connect(new InetSocketAddress("localhost", 9999));

    OutputStream outputstream = sslsocket.getOutputStream();
    OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
    BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);

    bufferedwriter.write("HELLO\n");
    bufferedwriter.flush();
}

From source file:eu.prestoprime.p4gui.connection.P4HttpClient.java

public P4HttpClient(String userID) {
    HttpParams params = new BasicHttpParams();

    // setup SSL//ww w  .  ja  va 2 s .c  o  m
    try {
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[] { easyTrustManager }, null);

        SSLSocketFactory sf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000L);
        SSLSocket socket = (SSLSocket) sf.createSocket(params);
        socket.setEnabledCipherSuites(new String[] { "SSL_RSA_WITH_RC4_128_MD5" });

        Scheme sch = new Scheme("https", 443, sf);
        this.getConnectionManager().getSchemeRegistry().register(sch);
    } catch (IOException | KeyManagementException | NoSuchAlgorithmException e) {
        logger.error("Unable to create SSL handler for HttpClient...");
        e.printStackTrace();
    }

    // save userID
    this.userID = userID;
}

From source file:org.commonjava.maven.galley.transport.htcli.internal.LocationSSLSocketFactory.java

@Override
public Socket createSocket(final HttpParams params) throws IOException {
    //        logger.info( "Creating socket...looking for repository definition in parameters..." );
    final HttpLocation repo = (HttpLocation) params.getParameter(Http.HTTP_PARAM_LOCATION);

    if (repo != null) {
        //            logger.info( "Creating socket...using repository: {}", repo );
        final SSLSocketFactory fac = getSSLFactory(repo);
        if (fac != null) {
            //                logger.info( "Creating socket using repo-specific factory" );
            return fac.createSocket(params);
        } else {//  w w w. j  a v a 2 s  . com
            //                logger.info( "No repo-specific factory; Creating socket using default factory (this)" );
            return super.createSocket(params);
        }
    } else {
        //            logger.info( "No repo; Creating socket using default factory (this)" );
        return super.createSocket(params);
    }
}

From source file:org.apache.commons.httpclient.contrib.ssl.StrictSSLProtocolSocketFactory.java

/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>// w  w w  .j a  v a2s.c o m
 * This method employs several techniques to circumvent the limitations of older JREs that 
 * do not support connect timeout. When running in JRE 1.4 or above reflection is used to 
 * call Socket#connect(SocketAddress endpoint, int timeout) method. When executing in older 
 * JREs a controller thread is executed. The controller thread attempts to create a new socket
 * within the given limit of time. If socket constructor does not return until the timeout 
 * expires, the controller terminates and throws an {@link ConnectTimeoutException}
 * </p>
 *  
 * @param host the host name/IP
 * @param port the port on the host
 * @param clientHost the local host name/IP to bind the socket to
 * @param clientPort the port on the local machine
 * @param params {@link HttpConnectionParams Http connection parameters}
 * 
 * @return Socket a new socket
 * 
 * @throws IOException if an I/O error occurs while creating the socket
 * @throws UnknownHostException if the IP address of the host cannot be
 * determined
 */
public Socket createSocket(final String host, final int port, final InetAddress localAddress,
        final int localPort, final HttpParams params)
        throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = HttpConnectionParams.getConnectionTimeout(params);
    SSLSocket socket = null;

    SSLSocketFactory socketfactory = SSLSocketFactory.getSystemSocketFactory();
    if (timeout == 0) {
        socket = (SSLSocket) socketfactory.createSocket(params);
    } else {
        socket = (SSLSocket) socketfactory.createSocket(params);
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
    }
    verifyHostname(socket);
    return socket;
}

From source file:com.eviware.soapui.impl.wsdl.support.http.SoapUISSLSocketFactory.java

@Override
public Socket createSocket(HttpParams params) throws IOException {
    String sslConfig = (String) params.getParameter(SoapUIHttpRoute.SOAPUI_SSL_CONFIG);

    if (StringUtils.isNullOrEmpty(sslConfig)) {
        return enableSocket((SSLSocket) sslContext.getSocketFactory().createSocket());
    }/*from  w w  w  .ja  v  a 2 s.com*/

    SSLSocketFactory factory = factoryMap.get(sslConfig);

    if (factory != null) {
        if (factory == this)
            return enableSocket((SSLSocket) sslContext.getSocketFactory().createSocket());
        else
            return enableSocket((SSLSocket) factory.createSocket(params));
    }

    try {
        // try to create new factory for specified config
        int ix = sslConfig.lastIndexOf(' ');
        String keyStore = sslConfig.substring(0, ix);
        String pwd = sslConfig.substring(ix + 1);

        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

        if (keyStore.trim().length() > 0) {
            File f = new File(keyStore);

            if (f.exists()) {
                log.info("Initializing Keystore from [" + keyStore + "]");

                try {
                    KeyMaterial km = new KeyMaterial(f, pwd.toCharArray());
                    ks = km.getKeyStore();
                } catch (Exception e) {
                    SoapUI.logError(e);
                    pwd = null;
                }
            }
        }

        factory = new SoapUISSLSocketFactory(ks, pwd);
        factoryMap.put(sslConfig, factory);

        return enableSocket((SSLSocket) factory.createSocket(params));
    } catch (Exception gse) {
        SoapUI.logError(gse);
        return enableSocket((SSLSocket) super.createSocket(params));
    }
}

From source file:no.kantega.kwashc.server.test.SSLProtocolTest.java

private HttpResponse checkClient(Site site, int httpsPort, HttpClient httpclient, String[] protocols,
        String[] ciphers) throws NoSuchAlgorithmException, KeyManagementException, IOException {
    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null, new TrustManager[] { allowAllTrustManager }, null);

    SSLSocketFactory sf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000);
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 1000);

    SSLSocket socket = (SSLSocket) sf.createSocket(params);
    if (protocols != null) {
        socket.setEnabledProtocols(protocols);
    }//from w w  w. j  a v a  2  s .com
    if (ciphers != null) {
        socket.setEnabledCipherSuites(ciphers);
    }

    URL url = new URL(site.getAddress());

    InetSocketAddress address = new InetSocketAddress(url.getHost(), httpsPort);
    sf.connectSocket(socket, address, null, params);

    Scheme sch = new Scheme("https", httpsPort, sf);
    httpclient.getConnectionManager().getSchemeRegistry().register(sch);

    HttpGet request = new HttpGet(
            "https://" + url.getHost() + ":" + site.getSecureport() + url.getPath() + "blog");

    return httpclient.execute(request);
}

From source file:no.kantega.kwashc.server.test.SSLCipherSuiteTest.java

private HttpResponse checkClientForCiphers(Site site, int httpsPort, HttpClient httpclient, String[] ciphers)
        throws NoSuchAlgorithmException, KeyManagementException, IOException {
    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null, new TrustManager[] { allowAllTrustManager }, null);

    SSLSocketFactory sf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000);
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 1000);

    SSLSocket socket = (SSLSocket) sf.createSocket(params);
    socket.setEnabledCipherSuites(ciphers);

    URL url = new URL(site.getAddress());

    InetSocketAddress address = new InetSocketAddress(url.getHost(), httpsPort);
    sf.connectSocket(socket, address, null, params);

    Scheme sch = new Scheme("https", httpsPort, sf);
    httpclient.getConnectionManager().getSchemeRegistry().register(sch);

    HttpGet request = new HttpGet(
            "https://" + url.getHost() + ":" + site.getSecureport() + url.getPath() + "blog");

    return httpclient.execute(request);
}