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