Search in sources :

Example 1 with RosterPacket

use of org.jivesoftware.smack.packet.RosterPacket in project ecf by eclipse.

the class XMPPContainerPresenceHelper method handleIQEvent.

protected void handleIQEvent(IQEvent evt) {
    final IQ iq = evt.getIQ();
    if (iq instanceof RosterPacket) {
        // Roster packet...report to UI
        final RosterPacket rosterPacket = (RosterPacket) iq;
        if (rosterPacket.getType() == IQ.Type.SET || rosterPacket.getType() == IQ.Type.RESULT) {
            for (final Iterator i = rosterPacket.getRosterItems().iterator(); i.hasNext(); ) {
                final RosterPacket.Item item = (RosterPacket.Item) i.next();
                final RosterPacket.ItemType itemType = item.getItemType();
                XMPPID newID = createIDFromName(item.getUser());
                boolean remove = false;
                if (itemType == RosterPacket.ItemType.none || itemType == RosterPacket.ItemType.remove) {
                    removeItemFromRoster(roster.getItems(), createIDFromName(item.getUser()));
                    remove = true;
                } else {
                    remove = false;
                    addUniqueToRoster(createRosterEntries(newID, item));
                }
                // In both cases fire set roster entry
                fireSetRosterEntry(remove, createRosterEntry(newID, item));
            }
        }
    } else {
        trace("Received non rosterpacket IQ message");
    }
}
Also used : IRosterItem(org.eclipse.ecf.presence.roster.IRosterItem) RosterPacket(org.jivesoftware.smack.packet.RosterPacket) IQ(org.jivesoftware.smack.packet.IQ) Iterator(java.util.Iterator) XMPPID(org.eclipse.ecf.provider.xmpp.identity.XMPPID)

Example 2 with RosterPacket

use of org.jivesoftware.smack.packet.RosterPacket in project ecf by eclipse.

the class Roster method removeEntry.

/**
 * Removes a roster entry from the roster. The roster entry will also be removed from the
 * unfiled entries or from any roster group where it could belong and will no longer be part
 * of the roster. Note that this is an asynchronous call -- Smack must wait for the server
 * to send an updated subscription status.
 *
 * @param entry a roster entry.
 * @throws XMPPException if an XMPP error occurs.
 * @throws IllegalStateException if connection is not logged in or logged in anonymously
 */
public void removeEntry(RosterEntry entry) throws XMPPException {
    if (!connection.isAuthenticated()) {
        throw new IllegalStateException("Not logged in to server.");
    }
    if (connection.isAnonymous()) {
        throw new IllegalStateException("Anonymous users can't have a roster.");
    }
    // The actual removal logic takes place in RosterPacketListenerprocess>>Packet(Packet)
    if (!entries.containsKey(entry.getUser())) {
        return;
    }
    RosterPacket packet = new RosterPacket();
    packet.setType(IQ.Type.SET);
    RosterPacket.Item item = RosterEntry.toRosterItem(entry);
    // Set the item type as REMOVE so that the server will delete the entry
    item.setItemType(RosterPacket.ItemType.remove);
    packet.addRosterItem(item);
    PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(packet.getPacketID()));
    connection.sendPacket(packet);
    IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    collector.cancel();
    if (response == null) {
        throw new XMPPException("No response from the server.");
    } else // If the server replied with an error, throw an exception.
    if (response.getType() == IQ.Type.ERROR) {
        throw new XMPPException(response.getError());
    }
}
Also used : RosterPacket(org.jivesoftware.smack.packet.RosterPacket) IQ(org.jivesoftware.smack.packet.IQ) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 3 with RosterPacket

use of org.jivesoftware.smack.packet.RosterPacket in project ecf by eclipse.

the class Roster method createEntry.

/**
 * Creates a new roster entry and presence subscription. The server will asynchronously
 * update the roster with the subscription status.
 *
 * @param user   the user. (e.g. johndoe@jabber.org)
 * @param name   the nickname of the user.
 * @param groups the list of group names the entry will belong to, or <tt>null</tt> if the
 *               the roster entry won't belong to a group.
 * @throws XMPPException if an XMPP exception occurs.
 * @throws IllegalStateException if connection is not logged in or logged in anonymously
 */
public void createEntry(String user, String name, String[] groups) throws XMPPException {
    if (!connection.isAuthenticated()) {
        throw new IllegalStateException("Not logged in to server.");
    }
    if (connection.isAnonymous()) {
        throw new IllegalStateException("Anonymous users can't have a roster.");
    }
    // Create and send roster entry creation packet.
    RosterPacket rosterPacket = new RosterPacket();
    rosterPacket.setType(IQ.Type.SET);
    RosterPacket.Item item = new RosterPacket.Item(user, name);
    if (groups != null) {
        for (String group : groups) {
            if (group != null && group.trim().length() > 0) {
                item.addGroupName(group);
            }
        }
    }
    rosterPacket.addRosterItem(item);
    // Wait up to a certain number of seconds for a reply from the server.
    PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(rosterPacket.getPacketID()));
    connection.sendPacket(rosterPacket);
    IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    collector.cancel();
    if (response == null) {
        throw new XMPPException("No response from the server.");
    } else // If the server replied with an error, throw an exception.
    if (response.getType() == IQ.Type.ERROR) {
        throw new XMPPException(response.getError());
    }
    // Create a presence subscription packet and send.
    Presence presencePacket = new Presence(Presence.Type.subscribe);
    presencePacket.setTo(user);
    connection.sendPacket(presencePacket);
}
Also used : RosterPacket(org.jivesoftware.smack.packet.RosterPacket) IQ(org.jivesoftware.smack.packet.IQ) Presence(org.jivesoftware.smack.packet.Presence) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 4 with RosterPacket

use of org.jivesoftware.smack.packet.RosterPacket in project ecf by eclipse.

the class RosterGroup method setName.

/**
 * Sets the name of the group. Changing the group's name is like moving all the group entries
 * of the group to a new group specified by the new name. Since this group won't have entries
 * it will be removed from the roster. This means that all the references to this object will
 * be invalid and will need to be updated to the new group specified by the new name.
 *
 * @param name the name of the group.
 */
public void setName(String name) {
    synchronized (entries) {
        for (RosterEntry entry : entries) {
            RosterPacket packet = new RosterPacket();
            packet.setType(IQ.Type.SET);
            RosterPacket.Item item = RosterEntry.toRosterItem(entry);
            item.removeGroupName(this.name);
            item.addGroupName(name);
            packet.addRosterItem(item);
            connection.sendPacket(packet);
        }
    }
}
Also used : RosterPacket(org.jivesoftware.smack.packet.RosterPacket)

Example 5 with RosterPacket

use of org.jivesoftware.smack.packet.RosterPacket in project ecf by eclipse.

the class RosterGroup method addEntry.

/**
 * Adds a roster entry to this group. If the entry was unfiled then it will be removed from
 * the unfiled list and will be added to this group.
 * Note that this is an asynchronous call -- Smack must wait for the server
 * to receive the updated roster.
 *
 * @param entry a roster entry.
 * @throws XMPPException if an error occured while trying to add the entry to the group.
 */
public void addEntry(RosterEntry entry) throws XMPPException {
    PacketCollector collector = null;
    // Only add the entry if it isn't already in the list.
    synchronized (entries) {
        if (!entries.contains(entry)) {
            RosterPacket packet = new RosterPacket();
            packet.setType(IQ.Type.SET);
            RosterPacket.Item item = RosterEntry.toRosterItem(entry);
            item.addGroupName(getName());
            packet.addRosterItem(item);
            // Wait up to a certain number of seconds for a reply from the server.
            collector = connection.createPacketCollector(new PacketIDFilter(packet.getPacketID()));
            connection.sendPacket(packet);
        }
    }
    if (collector != null) {
        IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
        collector.cancel();
        if (response == null) {
            throw new XMPPException("No response from the server.");
        } else // If the server replied with an error, throw an exception.
        if (response.getType() == IQ.Type.ERROR) {
            throw new XMPPException(response.getError());
        }
    }
}
Also used : RosterPacket(org.jivesoftware.smack.packet.RosterPacket) IQ(org.jivesoftware.smack.packet.IQ) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Aggregations

RosterPacket (org.jivesoftware.smack.packet.RosterPacket)8 IQ (org.jivesoftware.smack.packet.IQ)5 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)4 Iterator (java.util.Iterator)1 IRosterItem (org.eclipse.ecf.presence.roster.IRosterItem)1 XMPPID (org.eclipse.ecf.provider.xmpp.identity.XMPPID)1 Presence (org.jivesoftware.smack.packet.Presence)1