Example usage for javax.net.ssl SSLSocketFactory createSocket

List of usage examples for javax.net.ssl SSLSocketFactory createSocket

Introduction

In this page you can find the example usage for javax.net.ssl SSLSocketFactory createSocket.

Prototype

public abstract Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException;

Source Link

Document

Returns a socket layered over an existing socket connected to the named host, at the given port.

Usage

From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraClientFactory.java

public static Cassandra.Client getClientInternal(InetSocketAddress addr, boolean isSsl, int socketTimeoutMillis,
        int socketQueryTimeoutMillis) throws TTransportException {
    TSocket tSocket = new TSocket(addr.getHostString(), addr.getPort(), socketTimeoutMillis);
    tSocket.open();/*from   ww  w.j  ava2  s .  c om*/
    try {
        tSocket.getSocket().setKeepAlive(true);
        tSocket.getSocket().setSoTimeout(socketQueryTimeoutMillis);
    } catch (SocketException e) {
        log.error("Couldn't set socket keep alive for {}", addr);
    }

    if (isSsl) {
        boolean success = false;
        try {
            SSLSocketFactory factory = sslSocketFactories.getUnchecked(addr);
            SSLSocket socket = (SSLSocket) factory.createSocket(tSocket.getSocket(), addr.getHostString(),
                    addr.getPort(), true);
            tSocket = new TSocket(socket);
            success = true;
        } catch (IOException e) {
            throw new TTransportException(e);
        } finally {
            if (!success) {
                tSocket.close();
            }
        }
    }
    TTransport tFramedTransport = new TFramedTransport(tSocket,
            CassandraConstants.CLIENT_MAX_THRIFT_FRAME_SIZE_BYTES);
    TProtocol protocol = new TBinaryProtocol(tFramedTransport);
    Cassandra.Client client = new Cassandra.Client(protocol);
    return client;
}

From source file:Messenger.TorLib.java

public static void postToURL(String hostname, int port, String postKey, String data) throws IOException {
    Socket socket = TorSocket(hostname, port);
    SSLSocketFactory sslSf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket sslSocket = (SSLSocket) sslSf.createSocket(socket, null, socket.getPort(), false);
    sslSocket.setUseClientMode(true);//from ww w. j  a  v  a2  s .c o m
    sslSocket.startHandshake();
    String path = "/" + postKey;
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sslSocket.getOutputStream(), "UTF8"));
    wr.write("POST " + path + " HTTP/1.0\r\n");
    wr.write("Content-Length: " + data.length() + "\r\n");
    wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
    wr.write("\r\n");

    wr.write(data);
    wr.flush();

    BufferedReader rd = new BufferedReader(new InputStreamReader(sslSocket.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        System.out.println(line);
    }
    wr.close();
    rd.close();
    sslSocket.close();
}

From source file:Messenger.TorLib.java

/**
 * This method makes a http GET request for the specified resource to the specified hostname.
 * It uses the SOCKS proxy to a connection over Tor.
 * The DNS lookup is also done over Tor.
 * This method only uses port 443 for SSL.
 *
 * @param hostname hostname for target server.
 * @param port port to connect to.//from  w w w.  j a  v  a  2s.  c o m
 * @param resource resource to lookup with GET request.
 * @return returns a JSON object.
 * @throws IOException
 * @throws JSONException
 */
public static JSONObject getJSON(String hostname, int port, String resource)
        throws IOException, JSONException, HttpException {
    //Create a SSL socket using Tor
    Socket socket = TorSocket(hostname, port);
    SSLSocketFactory sslSf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket sslSocket = (SSLSocket) sslSf.createSocket(socket, null, socket.getPort(), false);
    sslSocket.setUseClientMode(true);
    sslSocket.startHandshake();
    openSockets.add(sslSocket);

    //Create the HTTP GET request and push it over the outputstream
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sslSocket.getOutputStream(), "UTF8"));
    wr.write("GET /" + resource + " HTTP/1.0\r\n");
    wr.write("Host: " + hostname + "\r\n");
    wr.write("\r\n");
    wr.flush();

    //Listen for a response on the inputstream
    BufferedReader br = new BufferedReader(new InputStreamReader(sslSocket.getInputStream()));
    String t;
    boolean start = false;
    String output = "";
    while ((t = br.readLine()) != null) {
        if (t.equals("")) {
            start = true;
        }
        if (start) {
            output = output + t;
        }
    }
    br.close();
    wr.close();
    sslSocket.close();
    System.out.println(output);
    openSockets.remove(sslSocket);
    return new JSONObject(output);
}

From source file:de.fun2code.google.cloudprint.push.PushReceiver.java

/**
 * Sets up the SSL socket for use with XMPP.
 * /*from  ww w  .  j  a  v a 2s  . c  o m*/
 * @param socket
 *            the socket to do TLS over.
 * @return   The SSL Socket.
 * @throws IOException
 */
public static SSLSocket setupSSLSocket(Socket socket) throws NoSuchAlgorithmException, KeyManagementException,
        KeyStoreException, UnrecoverableKeyException, IOException {
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    TrustManagerFactory tfactory = TrustManagerFactory.getInstance("SunPKIX");
    tfactory.init(keyStore);
    SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket sslsocket = (SSLSocket) factory.createSocket(socket, socket.getInetAddress().getHostAddress(),
            socket.getPort(), true);
    sslsocket.setUseClientMode(true);
    return sslsocket;
}

From source file:SocketFetcher.java

/**
 * Start TLS on an existing socket. Supports the "STARTTLS" command in many
 * protocols.//from   w  ww .  ja va2  s. com
 */
public static Socket startTLS(Socket socket, Properties props, String prefix) throws IOException {
    InetAddress a = socket.getInetAddress();
    String host = a.getHostName();
    int port = socket.getPort();
    // System.out.println("SocketFetcher: startTLS host " + host + ", port " +
    // port);

    try {
        SSLSocketFactory ssf;
        String sfClass = props.getProperty(prefix + ".socketFactory.class", null);
        SocketFactory sf = getSocketFactory(sfClass);
        if (sf != null && sf instanceof SSLSocketFactory)
            ssf = (SSLSocketFactory) sf;
        else
            ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
        socket = ssf.createSocket(socket, host, port, true);
        configureSSLSocket(socket, props, prefix);
    } catch (Exception ex) {
        if (ex instanceof InvocationTargetException) {
            Throwable t = ((InvocationTargetException) ex).getTargetException();
            if (t instanceof Exception)
                ex = (Exception) t;
        }
        if (ex instanceof IOException)
            throw (IOException) ex;
        // wrap anything else before sending it on
        IOException ioex = new IOException(
                "Exception in startTLS: host " + host + ", port " + port + "; Exception: " + ex);
        throw ioex;
    }
    return socket;
}

From source file:SocketFetcher.java

/**
 * Start TLS on an existing socket.//from w  ww.  j a  va  2s  .  c  o  m
 * Supports the "STARTTLS" command in many protocols.
 */
public static Socket startTLS(Socket socket, Properties props, String prefix) throws IOException {
    InetAddress a = socket.getInetAddress();
    String host = a.getHostName();
    int port = socket.getPort();
    //System.out.println("SocketFetcher: startTLS host " + host + ", port " + port);

    try {
        SSLSocketFactory ssf;
        String sfClass = props.getProperty(prefix + ".socketFactory.class", null);
        SocketFactory sf = getSocketFactory(sfClass);
        if (sf != null && sf instanceof SSLSocketFactory)
            ssf = (SSLSocketFactory) sf;
        else
            ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
        socket = ssf.createSocket(socket, host, port, true);
        configureSSLSocket(socket, props, prefix);
    } catch (Exception ex) {
        if (ex instanceof InvocationTargetException) {
            Throwable t = ((InvocationTargetException) ex).getTargetException();
            if (t instanceof Exception)
                ex = (Exception) t;
        }
        if (ex instanceof IOException)
            throw (IOException) ex;
        // wrap anything else before sending it on
        IOException ioex = new IOException(
                "Exception in startTLS: host " + host + ", port " + port + "; Exception: " + ex);
        ioex.initCause(ex);
        throw ioex;
    }
    return socket;
}

From source file:org.eclipse.ecf.internal.provider.filetransfer.httpclient.ECFHttpClientSecureProtocolSocketFactory.java

public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
        throws IOException, UnknownHostException {
    // Socket over the tunnel need not be monitored or do they ?
    SSLSocketFactory sslSocketFactory = sslSocketFactoryModifier.getSSLSocketFactory();
    return sslSocketFactory.createSocket(socket, host, port, autoClose);
}

From source file:org.eclipse.lyo.testsuite.server.util.SSLProtocolSocketFactory.java

public Socket createSocket(Socket socket, String host, int port, boolean autoclose)
        throws IOException, UnknownHostException {
    SSLContext ctx = getSSLContext();
    SSLSocketFactory sf = ctx.getSocketFactory();
    Socket newSocket = sf.createSocket(socket, host, port, autoclose);
    return newSocket;
}

From source file:com.subgraph.vega.internal.http.proxy.VegaHttpService.java

private SSLSocket createSSLSocketForHost(HttpHost host, Socket socket) throws IOException {
    final SSLContext ctx = sslContextRepository.getContextForName(host.getHostName());
    if (ctx == null) {
        throw new IOException("Failed to create SSLContext for host " + host.getHostName());
    }/*from  w ww  .ja  v a  2  s . c  o  m*/
    SSLSocketFactory sslSocketFactory = ctx.getSocketFactory();
    SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(socket, host.getHostName(), host.getPort(),
            true);
    sslSocket.setUseClientMode(false);
    return sslSocket;
}

From source file:com.apporiented.hermesftp.cmd.impl.FtpCmdAuth.java

private SSLSocket createSslSocket() throws IOException {
    String clientHost = getCtx().getClientSocket().getInetAddress().getHostAddress();
    SSLContext sslContext = getCtx().getOptions().getSslContext();
    SSLSocketFactory factory = sslContext.getSocketFactory();
    SSLSocket sslSocket = (SSLSocket) factory.createSocket(getCtx().getClientSocket(), clientHost,
            getCtx().getOptions().getFtpPort(), true);
    sslSocket.setUseClientMode(false);/*from   w  w  w.j  av a  2 s .  c  om*/
    sslSocket.addHandshakeCompletedListener(this);
    enableCipherSuites(sslSocket);
    log.info("Enabled cipher suites (explicit SSL): "
            + StringUtils.arrayToCommaDelimitedString(sslSocket.getEnabledCipherSuites()));
    return sslSocket;
}