Search in sources :

Example 6 with Localpart

use of org.jxmpp.jid.parts.Localpart in project Smack by igniterealtime.

the class XmppConnectionManager method registerAccount.

private void registerAccount(String username, String password) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, XmppStringprepException {
    if (accountRegistrationConnection == null) {
        throw new IllegalStateException("Account registration not configured");
    }
    switch(sinttestConfiguration.accountRegistration) {
        case serviceAdministration:
            EntityBareJid userJid = JidCreate.entityBareFrom(Localpart.from(username), accountRegistrationConnection.getXMPPServiceDomain());
            adminManager.addUser(userJid, password);
            break;
        case inBandRegistration:
            if (!accountManager.supportsAccountCreation()) {
                throw new UnsupportedOperationException("Account creation/registation is not supported");
            }
            Set<String> requiredAttributes = accountManager.getAccountAttributes();
            if (requiredAttributes.size() > 4) {
                throw new IllegalStateException("Unkown required attributes");
            }
            Map<String, String> additionalAttributes = new HashMap<>();
            additionalAttributes.put("name", "Smack Integration Test");
            additionalAttributes.put("email", "flow@igniterealtime.org");
            Localpart usernameLocalpart = Localpart.from(username);
            accountManager.createAccount(usernameLocalpart, password, additionalAttributes);
            break;
        case disabled:
            throw new IllegalStateException("Account creation no possible");
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Localpart(org.jxmpp.jid.parts.Localpart) EntityBareJid(org.jxmpp.jid.EntityBareJid)

Example 7 with Localpart

use of org.jxmpp.jid.parts.Localpart in project Smack by igniterealtime.

the class IntTestUtil method registerAccountViaIbr.

public static UsernameAndPassword registerAccountViaIbr(XMPPConnection connection, String username, String password) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    AccountManager accountManager = AccountManager.getInstance(connection);
    if (!accountManager.supportsAccountCreation()) {
        throw new UnsupportedOperationException("Account creation/registation is not supported");
    }
    Set<String> requiredAttributes = accountManager.getAccountAttributes();
    if (requiredAttributes.size() > 4) {
        throw new IllegalStateException("Unkown required attributes");
    }
    Map<String, String> additionalAttributes = new HashMap<>();
    additionalAttributes.put("name", "Smack Integration Test");
    additionalAttributes.put("email", "flow@igniterealtime.org");
    Localpart usernameLocalpart;
    try {
        usernameLocalpart = Localpart.from(username);
    } catch (XmppStringprepException e) {
        throw new IllegalArgumentException("Invalid username: " + username, e);
    }
    accountManager.createAccount(usernameLocalpart, password, additionalAttributes);
    return new UsernameAndPassword(username, password);
}
Also used : HashMap(java.util.HashMap) Localpart(org.jxmpp.jid.parts.Localpart) AccountManager(org.jivesoftware.smackx.iqregister.AccountManager) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException)

Example 8 with Localpart

use of org.jxmpp.jid.parts.Localpart in project Smack by igniterealtime.

the class XmppConnectionManager method disconnectAndCleanup.

void disconnectAndCleanup() throws InterruptedException {
    int successfullyDeletedAccountsCount = 0;
    for (AbstractXMPPConnection connection : connections.keySet()) {
        if (sinttestConfiguration.accountRegistration == AccountRegistration.inBandRegistration) {
            // Note that we use the account manager from the to-be-deleted connection.
            AccountManager accountManager = AccountManager.getInstance(connection);
            try {
                accountManager.deleteAccount();
                successfullyDeletedAccountsCount++;
            } catch (NoResponseException | XMPPErrorException | NotConnectedException e) {
                LOGGER.log(Level.WARNING, "Could not delete dynamically registered account", e);
            }
        }
        connection.disconnect();
        if (sinttestConfiguration.accountRegistration == AccountRegistration.serviceAdministration) {
            String username = connection.getConfiguration().getUsername().toString();
            Localpart usernameAsLocalpart;
            try {
                usernameAsLocalpart = Localpart.from(username);
            } catch (XmppStringprepException e) {
                throw new AssertionError(e);
            }
            EntityBareJid connectionAddress = JidCreate.entityBareFrom(usernameAsLocalpart, sinttestConfiguration.service);
            try {
                adminManager.deleteUser(connectionAddress);
                successfullyDeletedAccountsCount++;
            } catch (NoResponseException | XMPPErrorException | NotConnectedException e) {
                LOGGER.log(Level.WARNING, "Could not delete dynamically registered account", e);
            }
        }
    }
    if (sinttestConfiguration.isAccountRegistrationPossible()) {
        int unsuccessfullyDeletedAccountsCount = connections.size() - successfullyDeletedAccountsCount;
        if (unsuccessfullyDeletedAccountsCount == 0) {
            LOGGER.info("Successfully deleted all created accounts ✔");
        } else {
            LOGGER.warning("Could not delete all created accounts, " + unsuccessfullyDeletedAccountsCount + " remainaing");
        }
    }
    connections.clear();
    if (accountRegistrationConnection != null) {
        accountRegistrationConnection.disconnect();
    }
}
Also used : XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) Localpart(org.jxmpp.jid.parts.Localpart) AccountManager(org.jivesoftware.smackx.iqregister.AccountManager) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) EntityBareJid(org.jxmpp.jid.EntityBareJid) AbstractXMPPConnection(org.jivesoftware.smack.AbstractXMPPConnection)

Example 9 with Localpart

use of org.jxmpp.jid.parts.Localpart in project Smack by igniterealtime.

the class XmppTools method createAccount.

public static boolean createAccount(String xmppDomain, String username, String password) throws KeyManagementException, NoSuchAlgorithmException, SmackException, IOException, XMPPException, InterruptedException {
    DomainBareJid xmppDomainJid = JidCreate.domainBareFrom(xmppDomain);
    Localpart localpart = Localpart.from(username);
    return createAccount(xmppDomainJid, localpart, password);
}
Also used : Localpart(org.jxmpp.jid.parts.Localpart) DomainBareJid(org.jxmpp.jid.DomainBareJid)

Aggregations

Localpart (org.jxmpp.jid.parts.Localpart)9 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)7 DomainBareJid (org.jxmpp.jid.DomainBareJid)6 Resourcepart (org.jxmpp.jid.parts.Resourcepart)4 AccountRealm (com.xabber.android.data.database.realm.AccountRealm)2 Realm (io.realm.Realm)2 HashMap (java.util.HashMap)2 AccountManager (org.jivesoftware.smackx.iqregister.AccountManager)2 EntityBareJid (org.jxmpp.jid.EntityBareJid)2 NetworkException (com.xabber.android.data.NetworkException)1 ProxyType (com.xabber.android.data.connection.ProxyType)1 UserJid (com.xabber.android.data.entity.UserJid)1 ArrayList (java.util.ArrayList)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AbstractXMPPConnection (org.jivesoftware.smack.AbstractXMPPConnection)1 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)1 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)1 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)1