Search in sources :

Example 1 with RosterPacket

use of org.jivesoftware.smack.roster.packet.RosterPacket 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 RosterPacket

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

the class RosterPacketProvider method parse.

@Override
public RosterPacket parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException {
    RosterPacket roster = new RosterPacket();
    String version = parser.getAttributeValue("", "ver");
    roster.setVersion(version);
    outerloop: while (true) {
        int eventType = parser.next();
        switch(eventType) {
            case XmlPullParser.START_TAG:
                String startTag = parser.getName();
                switch(startTag) {
                    case "item":
                        RosterPacket.Item item = parseItem(parser);
                        roster.addRosterItem(item);
                        break;
                }
                break;
            case XmlPullParser.END_TAG:
                String endTag = parser.getName();
                switch(endTag) {
                    case IQ.QUERY_ELEMENT:
                        if (parser.getDepth() == initialDepth) {
                            break outerloop;
                        }
                }
        }
    }
    return roster;
}
Also used : RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket)

Example 3 with RosterPacket

use of org.jivesoftware.smack.roster.packet.RosterPacket 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 RosterPacket

use of org.jivesoftware.smack.roster.packet.RosterPacket 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 RosterPacket

use of org.jivesoftware.smack.roster.packet.RosterPacket 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

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