Search in sources :

Example 1 with RosterListener

use of org.jivesoftware.smack.roster.RosterListener in project spring-integration by spring-projects.

the class PresenceListeningEndpointTests method testRosterPresenceChangeEvent.

@Test
@SuppressWarnings("unchecked")
public void testRosterPresenceChangeEvent() {
    XMPPConnection connection = mock(XMPPConnection.class);
    PresenceListeningEndpoint rosterEndpoint = new PresenceListeningEndpoint(connection);
    QueueChannel channel = new QueueChannel();
    rosterEndpoint.setOutputChannel(channel);
    rosterEndpoint.setBeanFactory(mock(BeanFactory.class));
    rosterEndpoint.afterPropertiesSet();
    rosterEndpoint.start();
    RosterListener rosterListener = (RosterListener) TestUtils.getPropertyValue(rosterEndpoint, "rosterListener");
    Presence presence = new Presence(Type.available, "Hello", 1, Mode.chat);
    rosterListener.presenceChanged(presence);
    Message<?> message = channel.receive(10);
    assertEquals(presence, message.getPayload());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Presence(org.jivesoftware.smack.packet.Presence) XMPPConnection(org.jivesoftware.smack.XMPPConnection) RosterListener(org.jivesoftware.smack.roster.RosterListener) Test(org.junit.Test)

Example 2 with RosterListener

use of org.jivesoftware.smack.roster.RosterListener in project spring-integration by spring-projects.

the class PresenceListeningEndpointTests method testWithErrorChannel.

@Test
public void testWithErrorChannel() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    XMPPConnection connection = mock(XMPPConnection.class);
    bf.registerSingleton(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, connection);
    PresenceListeningEndpoint endpoint = new PresenceListeningEndpoint();
    DirectChannel outChannel = new DirectChannel();
    outChannel.subscribe(message -> {
        throw new RuntimeException("ooops");
    });
    PollableChannel errorChannel = new QueueChannel();
    endpoint.setBeanFactory(bf);
    endpoint.setOutputChannel(outChannel);
    endpoint.setErrorChannel(errorChannel);
    endpoint.afterPropertiesSet();
    RosterListener listener = (RosterListener) TestUtils.getPropertyValue(endpoint, "rosterListener");
    Presence presence = new Presence(Type.available);
    listener.presenceChanged(presence);
    ErrorMessage msg = (ErrorMessage) errorChannel.receive();
    assertSame(presence, ((MessagingException) msg.getPayload()).getFailedMessage().getPayload());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) PollableChannel(org.springframework.messaging.PollableChannel) Presence(org.jivesoftware.smack.packet.Presence) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ErrorMessage(org.springframework.messaging.support.ErrorMessage) RosterListener(org.jivesoftware.smack.roster.RosterListener) Test(org.junit.Test)

Example 3 with RosterListener

use of org.jivesoftware.smack.roster.RosterListener in project Spark by igniterealtime.

the class RosterTree method buildFromRoster.

private void buildFromRoster() {
    final Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
    roster.addRosterListener(new RosterListener() {

        @Override
        public void entriesAdded(Collection<Jid> addresses) {
        }

        @Override
        public void entriesUpdated(Collection<Jid> addresses) {
        }

        @Override
        public void entriesDeleted(Collection<Jid> addresses) {
        }

        @Override
        public void presenceChanged(Presence presence) {
        // changePresence(presence.getFrom(), presence.getMode() == Presence.Mode.available);
        }
    });
    for (RosterGroup group : roster.getGroups()) {
        final JiveTreeNode groupNode = new JiveTreeNode(group.getName(), true);
        groupNode.setAllowsChildren(true);
        if (group.getEntryCount() > 0) {
            rootNode.add(groupNode);
        }
        for (RosterEntry entry : group.getEntries()) {
            String name = entry.getName();
            if (name == null) {
                name = entry.getJid().toString();
            }
            final JiveTreeNode entryNode = new JiveTreeNode(name, false);
            final Presence usersPresence = PresenceManager.getPresence(entry.getJid());
            addressMap.put(entryNode, entry.getJid().toString());
            boolean showUnavailableAgents = true;
            if (usersPresence.isAvailable() && !usersPresence.isAway()) {
                groupNode.add(entryNode);
            } else if (showUnavailableAgents) {
                groupNode.add(entryNode);
            }
            changePresence(entry.getJid().toString(), usersPresence);
            final DefaultTreeModel model = (DefaultTreeModel) rosterTree.getModel();
            model.nodeStructureChanged(groupNode);
        }
    }
}
Also used : Roster(org.jivesoftware.smack.roster.Roster) Jid(org.jxmpp.jid.Jid) Presence(org.jivesoftware.smack.packet.Presence) RosterEntry(org.jivesoftware.smack.roster.RosterEntry) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) RosterListener(org.jivesoftware.smack.roster.RosterListener) RosterGroup(org.jivesoftware.smack.roster.RosterGroup)

Example 4 with RosterListener

use of org.jivesoftware.smack.roster.RosterListener in project spring-integration by spring-projects.

the class PresenceListeningEndpointTests method testEndpointLifecycle.

@Test
@SuppressWarnings("unchecked")
public void testEndpointLifecycle() {
    XMPPConnection connection = mock(XMPPConnection.class);
    Roster roster = Roster.getInstanceFor(connection);
    Set<RosterListener> rosterSet = TestUtils.getPropertyValue(roster, "rosterListeners", Set.class);
    PresenceListeningEndpoint rosterEndpoint = new PresenceListeningEndpoint(connection);
    rosterEndpoint.setOutputChannel(new QueueChannel());
    rosterEndpoint.setBeanFactory(mock(BeanFactory.class));
    rosterEndpoint.afterPropertiesSet();
    assertEquals(0, rosterSet.size());
    rosterEndpoint.start();
    assertEquals(1, rosterSet.size());
    rosterEndpoint.stop();
    assertEquals(0, rosterSet.size());
}
Also used : Roster(org.jivesoftware.smack.roster.Roster) QueueChannel(org.springframework.integration.channel.QueueChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) XMPPConnection(org.jivesoftware.smack.XMPPConnection) RosterListener(org.jivesoftware.smack.roster.RosterListener) Test(org.junit.Test)

Example 5 with RosterListener

use of org.jivesoftware.smack.roster.RosterListener in project Spark by igniterealtime.

the class WorkgroupRosterTree method buildFromRoster.

private void buildFromRoster() {
    final Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
    roster.addRosterListener(new RosterListener() {

        public void entriesAdded(Collection collection) {
        }

        public void entriesUpdated(Collection collection) {
        }

        public void entriesDeleted(Collection collection) {
        }

        public void presenceChanged(Presence presence) {
            changePresence(presence.getFrom(), presence);
        }
    });
    for (RosterGroup group : roster.getGroups()) {
        if (workgroupList.contains(group.getName())) {
            continue;
        }
        final JiveTreeNode groupNode = new JiveTreeNode(group.getName(), true);
        groupNode.setAllowsChildren(true);
        if (group.getEntryCount() > 0) {
            rootNode.add(groupNode);
        }
        for (RosterEntry entry : group.getEntries()) {
            String name = entry.getName();
            if (name == null) {
                name = entry.getJid().toString();
            }
            if (exclusionList.contains(entry.getJid())) {
                continue;
            }
            final JiveTreeNode entryNode = new JiveTreeNode(name, false);
            final Presence usersPresence = roster.getPresence(entry.getJid());
            addressMap.put(entryNode, entry.getJid().asEntityBareJidOrThrow());
            if (PresenceManager.isAvailable(usersPresence)) {
                groupNode.add(entryNode);
            } else if (showUnavailableAgents) {
                groupNode.add(entryNode);
            }
            changePresence(entry.getJid(), usersPresence);
            final DefaultTreeModel model = (DefaultTreeModel) rosterTree.getModel();
            model.nodeStructureChanged(groupNode);
        }
    }
}
Also used : Roster(org.jivesoftware.smack.roster.Roster) JiveTreeNode(org.jivesoftware.spark.component.JiveTreeNode) Collection(java.util.Collection) Presence(org.jivesoftware.smack.packet.Presence) RosterEntry(org.jivesoftware.smack.roster.RosterEntry) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) RosterListener(org.jivesoftware.smack.roster.RosterListener) RosterGroup(org.jivesoftware.smack.roster.RosterGroup)

Aggregations

RosterListener (org.jivesoftware.smack.roster.RosterListener)5 Presence (org.jivesoftware.smack.packet.Presence)4 XMPPConnection (org.jivesoftware.smack.XMPPConnection)3 Roster (org.jivesoftware.smack.roster.Roster)3 Test (org.junit.Test)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)3 QueueChannel (org.springframework.integration.channel.QueueChannel)3 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)2 RosterEntry (org.jivesoftware.smack.roster.RosterEntry)2 RosterGroup (org.jivesoftware.smack.roster.RosterGroup)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 Collection (java.util.Collection)1 JiveTreeNode (org.jivesoftware.spark.component.JiveTreeNode)1 Jid (org.jxmpp.jid.Jid)1 DirectChannel (org.springframework.integration.channel.DirectChannel)1 MessagingException (org.springframework.messaging.MessagingException)1 PollableChannel (org.springframework.messaging.PollableChannel)1 ErrorMessage (org.springframework.messaging.support.ErrorMessage)1