Search in sources :

Example 1 with Item

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

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 NoResponseException if there was no response from the server.
     * @throws XMPPErrorException if an XMPP exception occurs.
     * @throws NotLoggedInException If not logged in.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public void createEntry(BareJid user, String name, String[] groups) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
    // 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);
    connection.createStanzaCollectorAndSend(rosterPacket).nextResultOrThrow();
    sendSubscriptionRequest(user);
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Example 2 with Item

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

the class DirectoryRosterStore method getEntries.

@Override
public List<Item> getEntries() {
    List<Item> entries = new ArrayList<RosterPacket.Item>();
    for (File file : fileDir.listFiles(rosterDirFilter)) {
        Item entry = readEntry(file);
        if (entry == null) {
            // Roster directory store corrupt. Abort and signal this by returning null.
            return null;
        }
        entries.add(entry);
    }
    return entries;
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) ArrayList(java.util.ArrayList) File(java.io.File)

Example 3 with Item

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

the class RosterTest method testDeleteRosterItem.

/**
     * Test deleting a roster item according to the example in
     * <a href="http://xmpp.org/rfcs/rfc3921.html#roster-delete"
     *     >RFC3921: Deleting a Roster Item</a>.
     */
@Test
public void testDeleteRosterItem() throws Throwable {
    // The contact which should be deleted
    final BareJid contactJID = JidCreate.entityBareFrom("romeo@example.net");
    // Setup
    assertNotNull("Can't get the roster from the provided connection!", roster);
    initRoster();
    rosterListener.reset();
    // Delete a 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());
        }
    };
    serverSimulator.start();
    roster.removeEntry(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
    final RosterEntry deletedEntry = roster.getEntry(contactJID);
    assertNull("The contact wasn't deleted from the roster!", deletedEntry);
    assertTrue("The roster listener wasn't invoked for the deleted contact!", rosterListener.getDeletedAddresses().contains(contactJID));
    verifyMercutiosEntry(roster.getEntry(JidCreate.entityBareFrom("mercutio@example.com")));
    verifyBenvoliosEntry(roster.getEntry(JidCreate.entityBareFrom("benvolio@example.net")));
    assertSame("Wrong number of roster entries (" + roster.getEntries() + ").", 2, 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 4 with Item

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

the class RosterTest method removeAllRosterEntries.

/**
     * Remove all roster entries by iterating trough {@link Roster#getEntries()}
     * and simulating receiving roster pushes from the server.
     * 
     * @param connection the dummy connection of which the provided roster belongs to.
     * @param roster the roster (or buddy list) which should be initialized.
     */
public static void removeAllRosterEntries(DummyConnection connection, Roster roster) {
    for (RosterEntry entry : roster.getEntries()) {
        // prepare the roster push packet
        final RosterPacket rosterPush = new RosterPacket();
        rosterPush.setType(Type.set);
        rosterPush.setTo(connection.getUser());
        // prepare the buddy's item entry which should be removed
        final RosterPacket.Item item = new RosterPacket.Item(entry.getJid(), entry.getName());
        item.setItemType(ItemType.remove);
        rosterPush.addRosterItem(item);
        // simulate receiving the roster push
        connection.processStanza(rosterPush);
    }
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item)

Example 5 with Item

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

the class RosterTest method testAddEmptyGroupEntry.

/**
     * Test if adding an user with an empty group is equivalent with providing
     * no group.
     * 
     * @see <a href="http://www.igniterealtime.org/issues/browse/SMACK-294">SMACK-294</a>
     */
@Test(timeout = 5000)
public void testAddEmptyGroupEntry() throws Throwable {
    // Constants for the new contact
    final BareJid contactJID = JidCreate.entityBareFrom("nurse@example.com");
    final String contactName = "Nurse";
    final String[] contactGroup = { "" };
    // 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();
            assertSame("The provided JID doesn't match the requested!", contactJID, item.getJid());
            assertSame("The provided name doesn't match the requested!", contactName, item.getName());
            assertSame("Shouldn't provide an empty group element!", 0, item.getGroupNames().size());
        }
    };
    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 shouldn't be member of any group!", 0, addedEntry.getGroups().size());
    // 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)

Aggregations

Item (org.jivesoftware.smack.roster.packet.RosterPacket.Item)19 RosterPacket (org.jivesoftware.smack.roster.packet.RosterPacket)16 Test (org.junit.Test)10 BareJid (org.jxmpp.jid.BareJid)8 ArrayList (java.util.ArrayList)3 DirectoryRosterStore (org.jivesoftware.smack.roster.rosterstore.DirectoryRosterStore)3 RosterStore (org.jivesoftware.smack.roster.rosterstore.RosterStore)3 File (java.io.File)2 HashSet (java.util.HashSet)2 XMPPConnection (org.jivesoftware.smack.XMPPConnection)2 ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)2 Stanza (org.jivesoftware.smack.packet.Stanza)2 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 Collection (java.util.Collection)1 IQ (org.jivesoftware.smack.packet.IQ)1 Presence (org.jivesoftware.smack.packet.Presence)1 EntityBareJid (org.jxmpp.jid.EntityBareJid)1