Example usage for javax.net.ssl SSLSocket isConnected

List of usage examples for javax.net.ssl SSLSocket isConnected

Introduction

In this page you can find the example usage for javax.net.ssl SSLSocket isConnected.

Prototype

public boolean isConnected() 

Source Link

Document

Returns the connection state of the socket.

Usage

From source file:org.kuali.mobility.push.factory.iOSConnectionFactory.java

@Override
public boolean validateObject(SSLSocket obj) {
    if (obj == null) {
        return false; // null is obviously not valid
    }/*from ww  w  . jav a  2  s  .  c o  m*/
    return obj.isConnected() && !obj.isClosed();
}

From source file:com.isecpartners.gizmo.HttpRequest.java

private SSLSocket negotiateSSL(Socket sock, String hostname) throws KeyManagementException, KeyStoreException,
        NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, IOException,
        InvalidKeyException, SignatureException, NoSuchProviderException, NoCertException {
    synchronized (_factories) {
        SSLSocketFactory factory = _factories.get(hostname);

        if (factory == null) {
            factory = initSSL(hostname);
        }/*w ww . j a  v  a2 s.  co  m*/
        String inbound_hostname = sock.getInetAddress().getHostName();
        int inbound_port = sock.getPort();
        SSLSocket sslsock = (SSLSocket) factory.createSocket(sock, inbound_hostname, inbound_port, true);
        sslsock.setUseClientMode(false);
        if (!sslsock.isConnected()) {
            Logger.getLogger(HttpRequest.class.getName()).log(Level.SEVERE, "Couldn't negotiate SSL");
            System.exit(-1);
        }
        return sslsock;
    }
}

From source file:org.kuali.mobility.push.dao.PushDaoImpl.java

private SSLSocket openConnectionToAPNS(String host, int port, String key, String passphrase) {
    SSLSocket socket;
    try {/*from w w  w . j a v  a  2s  .  c  o m*/
        KeyStore keyStore = KeyStore.getInstance("PKCS12");

        //          keyStore.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("newcert.p12"), "strange word to use".toCharArray());
        //          keyStore.load(getClass().getResourceAsStream("/newcert.p12"), "strange word to use".toCharArray());
        //          keyStore.load(this.getClass().getClassLoader().getResourceAsStream("newcert.p12"), "strange word to use".toCharArray());

        // This works when built with Eclipse, but not when built from command line. 
        // Has to do with where the build system puts /resources/*.p12 file
        //          keyStore.load(this.getClass().getClassLoader().getResourceAsStream(key), "strange word to use".toCharArray());

        // Currently only works when read from the server's FS. Won't currently read from within eclipse project. 
        // Putting it in /opt/kme/push prevents naming conflicts. 
        keyStore.load(new FileInputStream("/opt/kme/push/newcert.p12"), "strange word to use".toCharArray());

        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("sunx509");
        keyManagerFactory.init(keyStore, "strange word to use".toCharArray());
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("sunx509");
        trustManagerFactory.init(keyStore);
        SSLContext sslCtx = SSLContext.getInstance("TLS");
        sslCtx.init(keyManagerFactory.getKeyManagers(), null, null);
        SSLSocketFactory sslSocketFactory = sslCtx.getSocketFactory();
        socket = (SSLSocket) sslSocketFactory.createSocket(host, port);
        socket.startHandshake();

        //Diagnostic output
        Enumeration e = keyStore.aliases();
        LOG.info(e.toString());
        while (e.hasMoreElements()) {
            LOG.info("Alias: " + e.nextElement().toString());
        }

        String not = (socket.isConnected()) ? "" : "NOT ";
        LOG.info("SSLSocket is " + not + "Connected");

        LOG.info("Connected to: " + socket.getInetAddress().getCanonicalHostName());
        LOG.info("Connected to: " + socket.getInetAddress().getHostAddress());

        String cs[] = socket.getEnabledCipherSuites();
        LOG.info("CipherSuites: " + Arrays.toString(cs));

        String ep[] = socket.getEnabledProtocols();
        LOG.info("Enabled Protocols: " + Arrays.toString(ep));

        LOG.info("Timeout: " + socket.getSoTimeout());
        LOG.info("Send Buffer Size: " + socket.getSendBufferSize());

        return socket;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}