Search in sources :

Example 6 with XMPPTCPConnection

use of com.xabber.xmpp.smack.XMPPTCPConnection in project xabber-android by redsolution.

the class ConnectionBuilder method build.

@NonNull
public static XMPPTCPConnection build(AccountJid account, @NonNull final ConnectionSettings connectionSettings) {
    XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
    builder.setXmppDomain(connectionSettings.getServerName());
    if (connectionSettings.isCustomHostAndPort()) {
        setCustomHost(connectionSettings, builder);
        builder.setPort(connectionSettings.getPort());
    }
    builder.setDebuggerEnabled(true);
    builder.setSecurityMode(connectionSettings.getTlsMode().getSecurityMode());
    builder.setCompressionEnabled(connectionSettings.useCompression());
    builder.setSendPresence(false);
    builder.setUsernameAndPassword(connectionSettings.getUserName(), connectionSettings.getPassword());
    builder.setResource(connectionSettings.getResource());
    builder.setProxyInfo(getProxyInfo(connectionSettings));
    try {
        LogManager.i(LOG_TAG, "SettingsManager.securityCheckCertificate: " + SettingsManager.securityCheckCertificate());
        if (SettingsManager.securityCheckCertificate()) {
            SSLContext sslContext = SSLContext.getInstance("TLS");
            MemorizingTrustManager mtm = CertificateManager.getInstance().getNewMemorizingTrustManager(account);
            sslContext.init(null, new X509TrustManager[] { mtm }, new java.security.SecureRandom());
            builder.setCustomSSLContext(sslContext);
            builder.setHostnameVerifier(mtm.wrapHostnameVerifier(new CustomDomainVerifier()));
        } else {
            TLSUtils.acceptAllCertificates(builder);
            builder.setHostnameVerifier(new AllowAllHostnameVerifier());
        }
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        LogManager.exception(LOG_TAG, e);
    }
    // if account have token
    if (connectionSettings.getToken() != null && !connectionSettings.getToken().isEmpty() && connectionSettings.getPassword() != null && connectionSettings.getPassword().isEmpty()) {
        // then enable only SASLXOauth2Mechanism
        builder.addEnabledSaslMechanism(SASLXOauth2Mechanism.NAME);
        // and set token as password
        builder.setUsernameAndPassword(connectionSettings.getUserName(), connectionSettings.getToken());
    }
    // X-TOKEN
    if (connectionSettings.getXToken() != null && !connectionSettings.getXToken().isExpired()) {
        LogManager.d(LOG_TAG, "Authorization with x-token");
        SASLAuthentication.registerSASLMechanism(new SASLXTOKENMechanism());
        builder.addEnabledSaslMechanism(SASLXTOKENMechanism.NAME);
        builder.setUsernameAndPassword(connectionSettings.getUserName(), connectionSettings.getXToken().getToken());
    }
    LogManager.i(LOG_TAG, "new XMPPTCPConnection " + connectionSettings.getServerName());
    return new XMPPTCPConnection(builder.build());
}
Also used : SASLXTOKENMechanism(com.xabber.xmpp.smack.SASLXTOKENMechanism) XMPPTCPConnection(com.xabber.xmpp.smack.XMPPTCPConnection) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) XMPPTCPConnectionConfiguration(com.xabber.xmpp.smack.XMPPTCPConnectionConfiguration) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) MemorizingTrustManager(de.duenndns.ssl.MemorizingTrustManager) NonNull(androidx.annotation.NonNull)

Example 7 with XMPPTCPConnection

use of com.xabber.xmpp.smack.XMPPTCPConnection in project xabber-android by redsolution.

the class StanzaSender method sendStanza.

/**
 * Send stanza to authenticated connection and add acknowledged listener if Stream Management is enabled on server.
 */
public static void sendStanza(AccountJid account, Message stanza, StanzaListener acknowledgedListener) throws NetworkException {
    XMPPTCPConnection xmppConnection = getXmppTcpConnection(account);
    if (xmppConnection.isSmEnabled()) {
        try {
            xmppConnection.addStanzaIdAcknowledgedListener(stanza.getStanzaId(), acknowledgedListener);
        } catch (StreamManagementException.StreamManagementNotEnabledException e) {
            LogManager.exception(LOG_TAG, e);
        }
    }
    sendStanza(xmppConnection, stanza);
}
Also used : XMPPTCPConnection(com.xabber.xmpp.smack.XMPPTCPConnection) StreamManagementException(org.jivesoftware.smack.sm.StreamManagementException)

Example 8 with XMPPTCPConnection

use of com.xabber.xmpp.smack.XMPPTCPConnection in project xabber-android by redsolution.

the class StanzaSender method getXmppTcpConnection.

@NonNull
private static XMPPTCPConnection getXmppTcpConnection(AccountJid account) throws NetworkException {
    AccountItem accountItem = AccountManager.getInstance().getAccount(account);
    if (accountItem == null) {
        throw new NetworkException(R.string.NOT_CONNECTED);
    }
    XMPPTCPConnection returnConnection = accountItem.getConnection();
    if (!returnConnection.isAuthenticated()) {
        throw new NetworkException(R.string.NOT_CONNECTED);
    }
    return returnConnection;
}
Also used : XMPPTCPConnection(com.xabber.xmpp.smack.XMPPTCPConnection) AccountItem(com.xabber.android.data.account.AccountItem) NetworkException(com.xabber.android.data.NetworkException) NonNull(androidx.annotation.NonNull)

Aggregations

XMPPTCPConnection (com.xabber.xmpp.smack.XMPPTCPConnection)8 AccountItem (com.xabber.android.data.account.AccountItem)4 NonNull (androidx.annotation.NonNull)3 Nullable (androidx.annotation.Nullable)2 PrivateDataManager (org.jivesoftware.smackx.iqprivate.PrivateDataManager)2 NetworkException (com.xabber.android.data.NetworkException)1 SASLXTOKENMechanism (com.xabber.xmpp.smack.SASLXTOKENMechanism)1 XMPPTCPConnectionConfiguration (com.xabber.xmpp.smack.XMPPTCPConnectionConfiguration)1 MemorizingTrustManager (de.duenndns.ssl.MemorizingTrustManager)1 IOException (java.io.IOException)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 SSLContext (javax.net.ssl.SSLContext)1 AllowAllHostnameVerifier (org.apache.http.conn.ssl.AllowAllHostnameVerifier)1 SmackException (org.jivesoftware.smack.SmackException)1 XMPPException (org.jivesoftware.smack.XMPPException)1 StreamManagementException (org.jivesoftware.smack.sm.StreamManagementException)1 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)1 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)1