Search in sources :

Example 16 with RosterPacket

use of org.jivesoftware.smack.roster.packet.RosterPacket in project Smack by igniterealtime.

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 a synchronous call -- Smack must wait for the server
     * to receive the updated roster.
     *
     * @param entry a roster entry.
     * @throws XMPPErrorException if an error occured while trying to add the entry to the group.
     * @throws NoResponseException if there was no response from the server.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public void addEntry(RosterEntry entry) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    // 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.
            connection().createStanzaCollectorAndSend(packet).nextResultOrThrow();
        }
    }
}
Also used : RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket)

Example 17 with RosterPacket

use of org.jivesoftware.smack.roster.packet.RosterPacket in project Smack by igniterealtime.

the class RosterGroup method removeEntry.

/**
     * Removes a roster entry from this group. If the entry does not belong to any other group 
     * then it will be considered as unfiled, therefore it will be added to the list of unfiled 
     * entries.
     * Note that this is a synchronous call -- Smack must wait for the server
     * to receive the updated roster.
     *
     * @param entry a roster entry.
     * @throws XMPPErrorException if an error occurred while trying to remove the entry from the group. 
     * @throws NoResponseException if there was no response from the server.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public void removeEntry(RosterEntry entry) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    // server.
    synchronized (entries) {
        if (entries.contains(entry)) {
            RosterPacket packet = new RosterPacket();
            packet.setType(IQ.Type.set);
            RosterPacket.Item item = RosterEntry.toRosterItem(entry);
            item.removeGroupName(this.getName());
            packet.addRosterItem(item);
            // Wait up to a certain number of seconds for a reply from the server.
            connection().createStanzaCollectorAndSend(packet).nextResultOrThrow();
        }
    }
}
Also used : RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket)

Example 18 with RosterPacket

use of org.jivesoftware.smack.roster.packet.RosterPacket in project Smack by igniterealtime.

the class RosterTest method testAddRosterItem.

/**
     * Test adding a roster item according to the example in
     * <a href="http://xmpp.org/rfcs/rfc3921.html#roster-add"
     *     >RFC3921: Adding a Roster Item</a>.
     */
@Test
public void testAddRosterItem() throws Throwable {
    // Constants for the new contact
    final BareJid contactJID = JidCreate.entityBareFrom("nurse@example.com");
    final String contactName = "Nurse";
    final String[] contactGroup = { "Servants" };
    // Setup
    assertNotNull("Can't get the roster from the provided connection!", roster);
    initRoster();
    rosterListener.reset();
    // Adding the new roster item
    final RosterUpdateResponder serverSimulator = new RosterUpdateResponder() {

        @Override
        void verifyUpdateRequest(final RosterPacket updateRequest) {
            final Item item = updateRequest.getRosterItems().iterator().next();
            assertEquals("The provided JID doesn't match the requested!", contactJID, item.getJid());
            assertSame("The provided name doesn't match the requested!", contactName, item.getName());
            assertSame("The provided group number doesn't match the requested!", contactGroup.length, item.getGroupNames().size());
            assertSame("The provided group doesn't match the requested!", contactGroup[0], item.getGroupNames().iterator().next());
        }
    };
    serverSimulator.start();
    roster.createEntry(contactJID, contactName, contactGroup);
    serverSimulator.join();
    // Check if an error occurred within the simulator
    final Throwable exception = serverSimulator.getException();
    if (exception != null) {
        throw exception;
    }
    rosterListener.waitUntilInvocationOrTimeout();
    // Verify the roster entry of the new contact
    final RosterEntry addedEntry = roster.getEntry(contactJID);
    assertNotNull("The new contact wasn't added to the roster!", addedEntry);
    assertTrue("The roster listener wasn't invoked for the new contact!", rosterListener.getAddedAddresses().contains(contactJID));
    assertSame("Setup wrong name for the new contact!", contactName, addedEntry.getName());
    assertSame("Setup wrong default subscription status!", ItemType.none, addedEntry.getType());
    assertSame("The new contact should be member of exactly one group!", 1, addedEntry.getGroups().size());
    assertSame("Setup wrong group name for the added contact!", contactGroup[0], addedEntry.getGroups().iterator().next().getName());
    // Verify the unchanged roster items
    verifyRomeosEntry(roster.getEntry(JidCreate.entityBareFrom("romeo@example.net")));
    verifyMercutiosEntry(roster.getEntry(JidCreate.entityBareFrom("mercutio@example.com")));
    verifyBenvoliosEntry(roster.getEntry(JidCreate.entityBareFrom("benvolio@example.net")));
    assertSame("Wrong number of roster entries.", 4, roster.getEntries().size());
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) BareJid(org.jxmpp.jid.BareJid) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Test(org.junit.Test)

Example 19 with RosterPacket

use of org.jivesoftware.smack.roster.packet.RosterPacket in project Smack by igniterealtime.

the class RosterTest method testUpdateRosterItem.

/**
     * Test updating a roster item according to the example in
     * <a href="http://xmpp.org/rfcs/rfc3921.html#roster-update"
     *     >RFC3921: Updating a Roster Item</a>.
     */
@Test
public void testUpdateRosterItem() throws Throwable {
    // Constants for the updated contact
    final BareJid contactJID = JidCreate.entityBareFrom("romeo@example.net");
    final String contactName = "Romeo";
    final String[] contactGroups = { "Friends", "Lovers" };
    // Setup
    assertNotNull("Can't get the roster from the provided connection!", roster);
    initRoster();
    rosterListener.reset();
    // Updating the roster item
    final RosterUpdateResponder serverSimulator = new RosterUpdateResponder() {

        @Override
        void verifyUpdateRequest(final RosterPacket updateRequest) {
            final Item item = updateRequest.getRosterItems().iterator().next();
            assertEquals("The provided JID doesn't match the requested!", contactJID, item.getJid());
            assertSame("The provided name doesn't match the requested!", contactName, item.getName());
            assertTrue("The updated contact doesn't belong to the requested groups (" + contactGroups[0] + ")!", item.getGroupNames().contains(contactGroups[0]));
            assertTrue("The updated contact doesn't belong to the requested groups (" + contactGroups[1] + ")!", item.getGroupNames().contains(contactGroups[1]));
            assertSame("The provided group number doesn't match the requested!", contactGroups.length, item.getGroupNames().size());
        }
    };
    serverSimulator.start();
    roster.createGroup(contactGroups[1]).addEntry(roster.getEntry(contactJID));
    serverSimulator.join();
    // Check if an error occurred within the simulator
    final Throwable exception = serverSimulator.getException();
    if (exception != null) {
        throw exception;
    }
    rosterListener.waitUntilInvocationOrTimeout();
    // Verify the roster entry of the updated contact
    final RosterEntry addedEntry = roster.getEntry(contactJID);
    assertNotNull("The contact was deleted from the roster!", addedEntry);
    assertTrue("The roster listener wasn't invoked for the updated contact!", rosterListener.getUpdatedAddresses().contains(contactJID));
    assertSame("Setup wrong name for the changed contact!", contactName, addedEntry.getName());
    assertTrue("The updated contact doesn't belong to the requested groups (" + contactGroups[0] + ")!", roster.getGroup(contactGroups[0]).contains(addedEntry));
    assertTrue("The updated contact doesn't belong to the requested groups (" + contactGroups[1] + ")!", roster.getGroup(contactGroups[1]).contains(addedEntry));
    assertSame("The updated contact should be member of two groups!", contactGroups.length, addedEntry.getGroups().size());
    // Verify the unchanged roster items
    verifyMercutiosEntry(roster.getEntry(JidCreate.entityBareFrom("mercutio@example.com")));
    verifyBenvoliosEntry(roster.getEntry(JidCreate.entityBareFrom("benvolio@example.net")));
    assertSame("Wrong number of roster entries (" + roster.getEntries() + ").", 3, roster.getEntries().size());
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) BareJid(org.jxmpp.jid.BareJid) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Test(org.junit.Test)

Example 20 with RosterPacket

use of org.jivesoftware.smack.roster.packet.RosterPacket in project xabber-android by redsolution.

the class RosterManager method setGroups.

public void setGroups(String account, String bareAddress, Collection<String> groups) throws NetworkException {
    final Roster roster = getRoster(account);
    if (roster == null) {
        return;
    }
    final RosterEntry entry = roster.getEntry(bareAddress);
    if (entry == null) {
        return;
    }
    RosterPacket packet = new RosterPacket();
    packet.setType(IQ.Type.set);
    RosterPacket.Item item = new RosterPacket.Item(bareAddress, entry.getName());
    for (String group : groups) {
        item.addGroupName(group);
    }
    packet.addRosterItem(item);
    ConnectionManager.getInstance().sendStanza(account, packet);
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) ConnectionItem(com.xabber.android.data.connection.ConnectionItem) Roster(org.jivesoftware.smack.roster.Roster) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) RosterEntry(org.jivesoftware.smack.roster.RosterEntry)

Aggregations

RosterPacket (org.jivesoftware.smack.roster.packet.RosterPacket)20 Item (org.jivesoftware.smack.roster.packet.RosterPacket.Item)12 Test (org.junit.Test)8 BareJid (org.jxmpp.jid.BareJid)6 XMPPConnection (org.jivesoftware.smack.XMPPConnection)3 Stanza (org.jivesoftware.smack.packet.Stanza)3 ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)2 IQ (org.jivesoftware.smack.packet.IQ)2 DirectoryRosterStore (org.jivesoftware.smack.roster.rosterstore.DirectoryRosterStore)2 RosterStore (org.jivesoftware.smack.roster.rosterstore.RosterStore)2 AccountItem (com.xabber.android.data.account.AccountItem)1 ConnectionItem (com.xabber.android.data.connection.ConnectionItem)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Level (java.util.logging.Level)1 ExceptionCallback (org.jivesoftware.smack.ExceptionCallback)1 SmackException (org.jivesoftware.smack.SmackException)1 FeatureNotSupportedException (org.jivesoftware.smack.SmackException.FeatureNotSupportedException)1 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)1 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)1