Search in sources :

Example 1 with AccountManager

use of org.jivesoftware.smack.AccountManager in project camel by apache.

the class XmppEndpoint method createConnection.

public synchronized XMPPConnection createConnection() throws XMPPException, SmackException, IOException {
    if (connection != null && connection.isConnected()) {
        // use existing working connection
        return connection;
    }
    // prepare for creating new connection
    connection = null;
    LOG.trace("Creating new connection ...");
    XMPPConnection newConnection = createConnectionInternal();
    newConnection.connect();
    newConnection.addPacketListener(new XmppLogger("INBOUND"), new PacketFilter() {

        public boolean accept(Packet packet) {
            return true;
        }
    });
    newConnection.addPacketSendingListener(new XmppLogger("OUTBOUND"), new PacketFilter() {

        public boolean accept(Packet packet) {
            return true;
        }
    });
    if (!newConnection.isAuthenticated()) {
        if (user != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Logging in to XMPP as user: {} on connection: {}", user, getConnectionMessage(newConnection));
            }
            if (password == null) {
                LOG.warn("No password configured for user: {} on connection: {}", user, getConnectionMessage(newConnection));
            }
            if (createAccount) {
                AccountManager accountManager = AccountManager.getInstance(newConnection);
                accountManager.createAccount(user, password);
            }
            if (login) {
                if (resource != null) {
                    newConnection.login(user, password, resource);
                } else {
                    newConnection.login(user, password);
                }
            }
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Logging in anonymously to XMPP on connection: {}", getConnectionMessage(newConnection));
            }
            newConnection.loginAnonymously();
        }
    // presence is not needed to be sent after login
    }
    // okay new connection was created successfully so assign it as the connection
    LOG.debug("Created new connection successfully: {}", newConnection);
    connection = newConnection;
    return connection;
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) AccountManager(org.jivesoftware.smack.AccountManager) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Aggregations

AccountManager (org.jivesoftware.smack.AccountManager)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)1 Packet (org.jivesoftware.smack.packet.Packet)1