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());
}
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);
}
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;
}
Aggregations