Search in sources :

Example 1 with SecurityRequiredByClientException

use of org.jivesoftware.smack.SmackException.SecurityRequiredByClientException in project Smack by igniterealtime.

the class TlsTest method tlsTest.

public static boolean tlsTest(EntityBareJid jid, String password, String host, int port, String tlsPin, boolean shouldThrow) throws KeyManagementException, NoSuchAlgorithmException {
    XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
    // @formatter:off
    builder.setUsernameAndPassword(jid.getLocalpart(), password).setXmppDomain(JidCreate.domainBareFrom(jid.getDomain())).setHost(host).setPort(port).setSecurityMode(SecurityMode.required);
    // @formatter:on
    builder.setDebuggerEnabled(DEBUG);
    if (StringUtils.isNotEmpty(tlsPin)) {
        SSLContext sslContext = Java7Pinning.forPin(tlsPin);
        builder.setCustomSSLContext(sslContext);
    }
    XMPPTCPConnection connection = new XMPPTCPConnection(builder.build());
    connection.setReplyTimeout(20000);
    try {
        connection.connect().login();
        if (shouldThrow) {
            // Test not success, should have throwed on login().
            return false;
        }
    } catch (SecurityRequiredByClientException e) {
        if (!shouldThrow) {
            return false;
        }
    } catch (XMPPException | SmackException | IOException | InterruptedException e) {
        throw new IllegalStateException(e);
    } finally {
        connection.disconnect();
    }
    return true;
}
Also used : XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) SmackException(org.jivesoftware.smack.SmackException) SecurityRequiredByClientException(org.jivesoftware.smack.SmackException.SecurityRequiredByClientException) XMPPTCPConnectionConfiguration(org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) XMPPException(org.jivesoftware.smack.XMPPException)

Aggregations

IOException (java.io.IOException)1 SSLContext (javax.net.ssl.SSLContext)1 SmackException (org.jivesoftware.smack.SmackException)1 SecurityRequiredByClientException (org.jivesoftware.smack.SmackException.SecurityRequiredByClientException)1 XMPPException (org.jivesoftware.smack.XMPPException)1 XMPPTCPConnection (org.jivesoftware.smack.tcp.XMPPTCPConnection)1 XMPPTCPConnectionConfiguration (org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration)1