static XMPPTCPConnection getConnectedConnection(SmackIntegrationTestEnvironment environment, int connectionId) throws KeyManagementException, NoSuchAlgorithmException, InterruptedException, SmackException, IOException, XMPPException { Configuration config = environment.configuration; XMPPTCPConnectionConfiguration.Builder builder = getConnectionConfigurationBuilder(config); XMPPTCPConnection connection = new XMPPTCPConnection(builder.build()); connection.connect(); UsernameAndPassword uap = IntTestUtil.registerAccount(connection, environment, connectionId); connection.login(uap.username, uap.password); return connection; }
public static void iotScenario(String dataThingJidString, String dataThingPassword, String readingThingJidString, String readingThingPassword, IotScenario scenario) throws Exception { final EntityBareJid dataThingJid = JidCreate.entityBareFrom(dataThingJidString); final EntityBareJid readingThingJid = JidCreate.entityBareFrom(readingThingJidString); final XMPPTCPConnectionConfiguration dataThingConnectionConfiguration = XMPPTCPConnectionConfiguration.builder() .setUsernameAndPassword(dataThingJid.getLocalpart(), dataThingPassword) .setXmppDomain(dataThingJid.asDomainBareJid()).setSecurityMode(SecurityMode.disabled) .enableDefaultDebugger().build(); final XMPPTCPConnectionConfiguration readingThingConnectionConfiguration = XMPPTCPConnectionConfiguration .builder().setUsernameAndPassword(readingThingJid.getLocalpart(), readingThingPassword) .setXmppDomain(readingThingJid.asDomainBareJid()).setSecurityMode(SecurityMode.disabled) .enableDefaultDebugger().build(); final XMPPTCPConnection dataThingConnection = new XMPPTCPConnection(dataThingConnectionConfiguration); final XMPPTCPConnection readingThingConnection = new XMPPTCPConnection(readingThingConnectionConfiguration); dataThingConnection.setReplyTimeout(TIMEOUT); readingThingConnection.setReplyTimeout(TIMEOUT); dataThingConnection.setUseStreamManagement(false); readingThingConnection.setUseStreamManagement(false); try { dataThingConnection.connect().login(); readingThingConnection.connect().login(); scenario.iotScenario(dataThingConnection, readingThingConnection); } finally { dataThingConnection.disconnect(); readingThingConnection.disconnect(); } }
/** * Creates a new XMPP connection over TCP (optionally using proxies). * <p> * Note that XMPPTCPConnection constructors do not establish a connection to the server * and you must call {@link #connect()}. * </p> * * @param config the connection configuration. */ public XMPPTCPConnection(XMPPTCPConnectionConfiguration config) { super(config); this.config = config; addConnectionListener(new AbstractConnectionListener() { @Override public void connectionClosedOnError(Exception e) { if (e instanceof XMPPException.StreamErrorException || e instanceof StreamManagementException) { dropSmState(); } } }); }
public static UsernameAndPassword registerAccountViaAdmin(XMPPTCPConnection connection, String username, String password, String adminAccountUsername, String adminAccountPassword) throws InterruptedException, XMPPException, SmackException, IOException { connection.login(adminAccountUsername, adminAccountPassword); ServiceAdministrationManager adminManager = ServiceAdministrationManager.getInstanceFor(connection); EntityBareJid userJid = JidCreate.entityBareFrom(Localpart.from(username), connection.getXMPPServiceDomain()); adminManager.addUser(userJid, password); connection.disconnect(); connection.connect(); return new UsernameAndPassword(username, password); }
public static boolean supportsIbr(DomainBareJid xmppDomain) throws SmackException, IOException, XMPPException, InterruptedException, KeyManagementException, NoSuchAlgorithmException { XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder() .setXmppDomain(xmppDomain); TLSUtils.acceptAllCertificates(configBuilder); XMPPTCPConnectionConfiguration config = configBuilder.build(); XMPPTCPConnection connection = new XMPPTCPConnection(config); connection.connect(); try { return supportsIbr(connection); } finally { connection.disconnect(); } }
@Override protected XMPPConnection createInstance() throws Exception { XMPPTCPConnectionConfiguration connectionConfig = this.connectionConfiguration; if (connectionConfig == null) { XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder() .setHost(this.host) .setPort(this.port); if (StringUtils.hasText(this.resource)) { builder.setResource(this.resource); } if (StringUtils.hasText(this.serviceName)) { builder.setUsernameAndPassword(this.user, this.password) .setXmppDomain(this.serviceName); } else { builder.setUsernameAndPassword(XmppStringUtils.parseLocalpart(this.user), this.password) .setXmppDomain(this.user); } connectionConfig = builder.build(); } return new XMPPTCPConnection(connectionConfig); }
public void start() { synchronized (this.lifecycleMonitor) { if (this.running) { return; } try { this.connection.connect(); this.connection.addConnectionListener(new LoggingConnectionListener()); this.connection.login(); if (this.subscriptionMode != null) { Roster.getInstanceFor(this.connection).setSubscriptionMode(this.subscriptionMode); } this.running = true; } catch (Exception e) { throw new BeanInitializationException("failed to connect to XMPP service for " + this.connection.getServiceName(), e); } } }
.setResource(middlefix + '-' + testRunResult.testRunId); XMPPTCPConnection connection = new XMPPTCPConnection(builder.build()); connection.connect(); if (config.isAccountRegistrationPossible()) { UsernameAndPassword uap = IntTestUtil.registerAccount(connection, accountUsername, accountPassword, config); connection.disconnect(); connection.connect(); connection.login(uap.username, uap.password); } else { connection.login();
XMPPTCPConnection.setUseStreamManagementResumptionDefault(true); XMPPTCPConnection.setUseStreamManagementDefault(true); config.setCustomSSLContext(sslContext); xmppConn = new XMPPTCPConnection(config.build()); // Create the connection xmppConn.connect(); // Connect SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5"); logger.info("SASL PLAIN authentication enabled ? {}", SASLAuthentication.isSaslMechanismRegistered("PLAIN")); logger.info("Is compression enabled ? {}", xmppConn.isUsingCompression()); logger.info("Is the connection secure ? {}", xmppConn.isSecureConnection()); xmppConn.addConnectionListener(this); xmppConn.addAsyncStanzaListener(this, stanza -> stanza.hasExtension(Util.FCM_ELEMENT_NAME, Util.FCM_NAMESPACE)); xmppConn.addStanzaInterceptor(stanza -> logger.info("Sent: {}", stanza.toXML(null)), ForEveryStanza.INSTANCE); pingManager.registerPingFailedListener(this); xmppConn.login(username, apiKey); logger.info("User logged in: {}", username);
private XMPPTCPConnection connect(long senderId, String apiKey) throws XMPPException, IOException, SmackException { ConnectionConfiguration config = new ConnectionConfiguration(GCM_SERVER, GCM_PORT); config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled); config.setReconnectionAllowed(true); config.setRosterLoadedAtLogin(false); config.setSendPresence(false); config.setSocketFactory(SSLSocketFactory.getDefault()); XMPPTCPConnection connection = new XMPPTCPConnection(config); connection.connect(); connection.addConnectionListener(new LoggingConnectionListener()); connection.addPacketListener(this, new PacketTypeFilter(Message.class)); connection.login(senderId + "@gcm.googleapis.com", apiKey); return connection; }
connection = new XMPPTCPConnection(builder.build()); connection.setPacketReplyTimeout(connectionTimeout); connection.connect(); connection.login(username, password); connection.sendPacket(new Presence(Presence.Type.available));
connection = new XMPPTCPConnection(config); connection.connect();
if (!connection.isConnected()) { try { connection.connect().login(); LOGGER.log(Level.WARNING, "Exception deleting account for " + connection, e); try { connection.connect().login();
@SmackIntegrationTest public void waitForClosingStreamElementTest(XMPPTCPConnection connection) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { connection.disconnect(); Field closingStreamReceivedField = connection.getClass().getDeclaredField("closingStreamReceived"); closingStreamReceivedField.setAccessible(true); SynchronizationPoint<?> closingStreamReceived = (SynchronizationPoint<?>) closingStreamReceivedField.get(connection); Exception failureException = closingStreamReceived.getFailureException(); if (failureException != null) { throw new AssertionError("Sync poing yielded failure exception", failureException); } assertTrue(closingStreamReceived.wasSuccessful()); } }
@Override protected void onShutdown() { if (logger.isInfoEnabled()) { logger.info("Shutting down XMPP receiver [" + connection.getUser() + "]"); } if (connection.isConnected()) { connection.disconnect(); } }
ConnectionConfiguration config = new ConnectionConfiguration("IM_DOMAIN", 5222); config.setDebuggerEnabled(false); config.setSecurityMode(SecurityMode.disabled); XMPPTCPConnection con = new XMPPTCPConnection(config); try { con.connect(); con.login("USERNAME", "PASSWORD"); Message msg = new Message("RECIPIENT_JID"); msg.setBody("message"); msg.setType(Type.chat); con.sendPacket(msg); } catch (SmackException | XMPPException | IOException | InterruptedException e) { throw e; } finally { if (null != con) { try { con.disconnect(); } catch (NotConnectedException e) { } } }
connection2.connect(); connection2.login(); connection2.disconnect(); Thread.sleep(1000);
public static void deleteViaServiceAdministration(XMPPTCPConnection connection, Configuration config) { EntityBareJid accountToDelete = connection.getUser().asEntityBareJid(); final int maxAttempts = 3; int attempts; for (attempts = 0; attempts < maxAttempts; attempts++) { connection.disconnect(); try { connection.connect().login(config.adminAccountUsername, config.adminAccountPassword); } catch (XMPPException | SmackException | IOException | InterruptedException e) { LOGGER.log(Level.WARNING, "Exception deleting account for " + connection, e); continue; } ServiceAdministrationManager adminManager = ServiceAdministrationManager.getInstanceFor(connection); try { adminManager.deleteUser(accountToDelete); break; } catch (NoResponseException | XMPPErrorException | NotConnectedException | InterruptedException e) { LOGGER.log(Level.WARNING, "Exception deleting account for " + connection, e); continue; } } if (attempts > maxAttempts) { LOGGER.log(Level.SEVERE, "Could not delete account for connection: " + connection); } }
@Override public void start() { synchronized (this.lifecycleMonitor) { if (this.running) { return; } XMPPTCPConnection connection = getConnection(); try { connection.connect(); connection.addConnectionListener(new LoggingConnectionListener()); Roster roster = Roster.getInstanceFor(connection); if (this.subscriptionMode != null) { roster.setSubscriptionMode(this.subscriptionMode); } else { roster.setRosterLoadedAtLogin(false); } connection.login(); this.running = true; } catch (Exception e) { throw new BeanInitializationException("failed to connect to XMPP service for " + connection.getXMPPServiceDomain(), e); } } }
/*Example solution. The exact settings would have to be adjusted outside of practice*/ XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration .builder() .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) .setServiceName("192.168.2.14") .setHost("192.168.2.14") .setPort(5222) .setCompressionEnabled(false).build(); XMPPTCPConnection connection = new XMPPTCPConnection(conf); try { connection.connect(); connection.login("john","123"); ...