Search in sources :

Example 11 with XMPPTCPConnection

use of org.jivesoftware.smack.tcp.XMPPTCPConnection in project Smack by igniterealtime.

the class SmackIntegrationTestFramework method getConnectedConnectionFor.

private XMPPTCPConnection getConnectedConnectionFor(AccountNum accountNum) throws SmackException, IOException, XMPPException, InterruptedException, KeyManagementException, NoSuchAlgorithmException {
    String middlefix;
    String accountUsername;
    String accountPassword;
    switch(accountNum) {
        case One:
            accountUsername = config.accountOneUsername;
            accountPassword = config.accountOnePassword;
            middlefix = "one";
            break;
        case Two:
            accountUsername = config.accountTwoUsername;
            accountPassword = config.accountTwoPassword;
            middlefix = "two";
            break;
        case Three:
            accountUsername = config.accountThreeUsername;
            accountPassword = config.accountThreePassword;
            middlefix = "three";
            break;
        default:
            throw new IllegalStateException();
    }
    if (StringUtils.isNullOrEmpty(accountUsername)) {
        accountUsername = USERNAME_PREFIX + '-' + middlefix + '-' + testRunResult.testRunId;
    }
    if (StringUtils.isNullOrEmpty(accountPassword)) {
        accountPassword = StringUtils.insecureRandomString(16);
    }
    // @formatter:off
    Builder builder = XMPPTCPConnectionConfiguration.builder().setXmppDomain(config.service).setUsernameAndPassword(accountUsername, accountPassword).setResource(middlefix + '-' + testRunResult.testRunId).setSecurityMode(config.securityMode);
    // @formatter:on
    if (config.tlsContext != null) {
        builder.setCustomSSLContext(config.tlsContext);
    }
    XMPPTCPConnection connection = new XMPPTCPConnection(builder.build());
    connection.connect();
    if (config.isAccountRegistrationPossible()) {
        UsernameAndPassword uap = IntTestUtil.registerAccount(connection, accountUsername, accountPassword, config);
        // TODO is this still required?
        // Some servers, e.g. Openfire, do not support a login right after the account was
        // created, so disconnect and re-connection the connection first.
        connection.disconnect();
        connection.connect();
        connection.login(uap.username, uap.password);
    } else {
        connection.login();
    }
    return connection;
}
Also used : XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) Builder(org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.Builder) UsernameAndPassword(org.igniterealtime.smack.inttest.IntTestUtil.UsernameAndPassword)

Example 12 with XMPPTCPConnection

use of org.jivesoftware.smack.tcp.XMPPTCPConnection in project openhab1-addons by openhab.

the class XMPPConnect method establishConnection.

private static void establishConnection() {
    if (servername == null) {
        return;
    }
    ConnectionConfiguration config;
    // Create a connection to the jabber server on the given port
    if (proxy != null) {
        config = new ConnectionConfiguration(servername, port, proxy);
    } else {
        config = new ConnectionConfiguration(servername, port);
    }
    config.setSecurityMode(securityMode);
    if (tlsPin != null) {
        try {
            SSLContext sc = JavaPinning.forPin(tlsPin);
            config.setCustomSSLContext(sc);
        } catch (KeyManagementException | NoSuchAlgorithmException e) {
            logger.error("Could not create TLS Pin for XMPP connection", e);
        }
    }
    if (connection != null && connection.isConnected()) {
        try {
            connection.disconnect();
        } catch (NotConnectedException e) {
            logger.debug("Already disconnected", e);
        }
    }
    connection = new XMPPTCPConnection(config);
    try {
        connection.connect();
        connection.login(username, password, null);
        if (consoleUsers.length > 0) {
            ChatManager.getInstanceFor(connection).addChatListener(new XMPPConsole(consoleUsers));
            connection.addConnectionListener(new XMPPConnectionListener());
        }
        logger.info("Connection to XMPP as '{}' has been established. Is secure/encrypted: {}", connection.getUser(), connection.isSecureConnection());
        initialized = true;
    } catch (Exception e) {
        logger.error("Could not establish connection to XMPP server '" + servername + ":" + port + "': {}", e.getMessage());
    }
}
Also used : ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) SmackException(org.jivesoftware.smack.SmackException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) KeyManagementException(java.security.KeyManagementException) ConfigurationException(org.osgi.service.cm.ConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 13 with XMPPTCPConnection

use of org.jivesoftware.smack.tcp.XMPPTCPConnection in project xabber-android by redsolution.

the class ConnectionThread method onReady.

private void onReady(XMPPTCPConnectionConfiguration.Builder builder) {
    builder.setSecurityMode(tlsMode.getSecurityMode());
    builder.setCompressionEnabled(compression);
    builder.setSendPresence(false);
    try {
        if (SettingsManager.securityCheckCertificate()) {
            SSLContext sslContext = SSLContext.getInstance("TLS");
            MemorizingTrustManager mtm = new MemorizingTrustManager(Application.getInstance());
            sslContext.init(null, new X509TrustManager[] { mtm }, new java.security.SecureRandom());
            builder.setCustomSSLContext(sslContext);
            builder.setHostnameVerifier(mtm.wrapHostnameVerifier(new org.apache.http.conn.ssl.StrictHostnameVerifier()));
        } else {
            TLSUtils.acceptAllCertificates(builder);
            TLSUtils.disableHostnameVerificationForTlsCertificicates(builder);
        }
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        e.printStackTrace();
    }
    setUpSASL();
    xmppConnection = new XMPPTCPConnection(builder.build());
    xmppConnection.addAsyncStanzaListener(this, ACCEPT_ALL);
    xmppConnection.addConnectionListener(this);
    // by default Smack disconnects in case of parsing errors
    xmppConnection.setParsingExceptionCallback(new ExceptionLoggingCallback());
    AccountRosterListener rosterListener = new AccountRosterListener(((AccountItem) connectionItem).getAccount());
    final Roster roster = Roster.getInstanceFor(xmppConnection);
    roster.addRosterListener(rosterListener);
    roster.addRosterLoadedListener(rosterListener);
    roster.setSubscriptionMode(Roster.SubscriptionMode.manual);
    org.jivesoftware.smackx.ping.PingManager.getInstanceFor(xmppConnection).registerPingFailedListener(this);
    connectionItem.onSRVResolved(this);
    final String password = OAuthManager.getInstance().getPassword(protocol, token);
    if (password != null) {
        runOnConnectionThread(new Runnable() {

            @Override
            public void run() {
                connect(password);
            }
        });
    } else {
        runOnConnectionThread(new Runnable() {

            @Override
            public void run() {
                passwordRequest();
            }
        });
    }
}
Also used : XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) MemorizingTrustManager(de.duenndns.ssl.MemorizingTrustManager) AccountRosterListener(com.xabber.android.data.roster.AccountRosterListener) Roster(org.jivesoftware.smack.roster.Roster) ExceptionLoggingCallback(org.jivesoftware.smack.parsing.ExceptionLoggingCallback)

Aggregations

XMPPTCPConnection (org.jivesoftware.smack.tcp.XMPPTCPConnection)13 XMPPTCPConnectionConfiguration (org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration)6 KeyManagementException (java.security.KeyManagementException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 SmackException (org.jivesoftware.smack.SmackException)4 XMPPException (org.jivesoftware.smack.XMPPException)4 IOException (java.io.IOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 SSLContext (javax.net.ssl.SSLContext)3 UsernameAndPassword (org.igniterealtime.smack.inttest.IntTestUtil.UsernameAndPassword)2 ConnectionConfiguration (org.jivesoftware.smack.ConnectionConfiguration)2 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)2 Builder (org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.Builder)2 AccountRosterListener (com.xabber.android.data.roster.AccountRosterListener)1 MemorizingTrustManager (de.duenndns.ssl.MemorizingTrustManager)1 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 LinkedList (java.util.LinkedList)1 SmackIntegrationTest (org.igniterealtime.smack.inttest.SmackIntegrationTest)1 SmackConfiguration (org.jivesoftware.smack.SmackConfiguration)1