Java Code Examples for org.jivesoftware.smack.roster.packet.RosterPacket#setTo()

The following examples show how to use org.jivesoftware.smack.roster.packet.RosterPacket#setTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RosterTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that roster pushes with invalid from are ignored.
 * @throws XmppStringprepException if the provided string is invalid.
 *
 * @see <a href="http://xmpp.org/rfcs/rfc6121.html#roster-syntax-actions-push">RFC 6121, Section 2.1.6</a>
 */
@Test
public void testIgnoreInvalidFrom() throws XmppStringprepException {
    final BareJid spammerJid = JidCreate.entityBareFrom("[email protected]");
    RosterPacket packet = new RosterPacket();
    packet.setType(Type.set);
    packet.setTo(connection.getUser());
    packet.setFrom(JidCreate.entityBareFrom("[email protected]"));
    packet.addRosterItem(new Item(spammerJid, "Cool products!"));

    final String requestId = packet.getStanzaId();
    // Simulate receiving the roster push
    connection.processStanza(packet);

    // Smack should reply with an error IQ
    ErrorIQ errorIQ = connection.getSentPacket();
    assertEquals(requestId, errorIQ.getStanzaId());
    assertEquals(Condition.service_unavailable, errorIQ.getError().getCondition());

    assertNull("Contact was added to roster", Roster.getInstanceFor(connection).getEntry(spammerJid));
}
 
Example 2
Source File: RosterTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 3
Source File: RosterTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    try {
        while (true) {
            final Stanza packet = connection.getSentPacket();
            if (packet instanceof RosterPacket && ((IQ) packet).getType() == Type.set) {
                final RosterPacket rosterRequest = (RosterPacket) packet;

                // Prepare and process the roster push
                final RosterPacket rosterPush = new RosterPacket();
                final Item item = rosterRequest.getRosterItems().iterator().next();
                if (item.getItemType() != ItemType.remove) {
                    item.setItemType(ItemType.none);
                }
                rosterPush.setType(Type.set);
                rosterPush.setTo(connection.getUser());
                rosterPush.addRosterItem(item);
                connection.processStanza(rosterPush);

                // Create and process the IQ response
                final IQ response = IQ.createResultIQ(rosterRequest);
                connection.processStanza(response);

                // Verify the roster update request
                if (rosterRequest.getRosterItemCount() != 1) {
                    throw new AssertionError("A roster set MUST contain one and only one <item/> element.");
                }
                verifyUpdateRequest(rosterRequest);
                break;
            }
        }
    }
    catch (Throwable e) {
        exception = e;
        fail(e.getMessage());
    }
}
 
Example 4
Source File: RosterVersioningTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that a non-empty roster result empties the store.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws XMPPException if an XMPP protocol error was received.
 * @throws XmppStringprepException if the provided string is invalid.
 */
@Test(timeout = 5000)
public void testOtherVersionStored() throws XMPPException, SmackException, XmppStringprepException {
    Item vaglafItem = vaglafItem();

    // We expect that the roster request is the only packet sent. This is not part of the specification,
    // but a shortcut in the test implementation.
    Stanza sentPacket = connection.getSentPacket();
    if (sentPacket instanceof RosterPacket) {
        RosterPacket sentRP = (RosterPacket) sentPacket;
        RosterPacket answer = new RosterPacket();
        answer.setStanzaId(sentRP.getStanzaId());
        answer.setType(Type.result);
        answer.setTo(sentRP.getFrom());

        answer.setVersion("newVersion");
        answer.addRosterItem(vaglafItem);

        rosterListener.reset();
        connection.processStanza(answer);
        rosterListener.waitUntilInvocationOrTimeout();
    } else {
        assertTrue("Expected to get a RosterPacket ", false);
    }

    Roster roster = Roster.getInstanceFor(connection);
    assertEquals("Size of roster", 1, roster.getEntries().size());
    RosterEntry entry = roster.getEntry(vaglafItem.getJid());
    assertNotNull("Roster contains vaglaf entry", entry);
    assertEquals("vaglaf entry in roster equals the sent entry", vaglafItem, RosterEntry.toRosterItem(entry));

    RosterStore store = roster.getRosterStore();
    assertEquals("Size of store", 1, store.getEntries().size());
    Item item = store.getEntry(vaglafItem.getJid());
    assertNotNull("Store contains vaglaf entry", item);
    assertEquals("vaglaf entry in store equals the sent entry", vaglafItem, item);
}
 
Example 5
Source File: SubscriptionPreApprovalTest.java    From Smack with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
    try {
        while (true) {
            final Stanza packet = connection.getSentPacket();
            if (packet instanceof RosterPacket && ((IQ) packet).getType() == Type.set) {
                final RosterPacket rosterRequest = (RosterPacket) packet;

                // Prepare and process the roster push
                final RosterPacket rosterPush = new RosterPacket();
                final Item item = rosterRequest.getRosterItems().iterator().next();
                if (item.getItemType() != ItemType.remove) {
                    item.setItemType(ItemType.none);
                }
                rosterPush.setType(Type.set);
                rosterPush.setTo(connection.getUser());
                rosterPush.addRosterItem(item);
                connection.processStanza(rosterPush);

                // Create and process the IQ response
                final IQ response = IQ.createResultIQ(rosterRequest);
                connection.processStanza(response);

                // Verify the roster update request
                if (rosterRequest.getRosterItemCount() != 1) {
                    throw new AssertionError("A roster set MUST contain one and only one <item/> element.");
                }
                verifyRosterUpdateRequest(rosterRequest);
                break;
            }
            else if (packet instanceof Presence && ((Presence) packet).getType() == Presence.Type.subscribed) {
                final Presence approval = (Presence) packet;
                verifyPreApprovalRequest(approval);
            }
        }
    }
    catch (Throwable e) {
        exception = e;
        fail(e.getMessage());
    }
}
 
Example 6
Source File: RosterTest.java    From Smack with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize the roster according to the example in
 * <a href="http://xmpp.org/rfcs/rfc3921.html#roster-login"
 *     >RFC3921: Retrieving One's Roster on Login</a>.
 *
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws XmppStringprepException if the provided string is invalid.
 */
private void initRoster() throws InterruptedException, SmackException, XmppStringprepException {
    roster.reload();
    while (true) {
        final Stanza sentPacket = connection.getSentPacket();
        if (sentPacket instanceof RosterPacket && ((IQ) sentPacket).getType() == Type.get) {
            // setup the roster get request
            final RosterPacket rosterRequest = (RosterPacket) sentPacket;
            assertSame("The <query/> element MUST NOT contain any <item/> child elements!",
                    0,
                    rosterRequest.getRosterItemCount());

            // prepare the roster result
            final RosterPacket rosterResult = new RosterPacket();
            rosterResult.setTo(connection.getUser());
            rosterResult.setType(Type.result);
            rosterResult.setStanzaId(rosterRequest.getStanzaId());

            // prepare romeo's roster entry
            final Item romeo = new Item(JidCreate.entityBareFrom("[email protected]"), "Romeo");
            romeo.addGroupName("Friends");
            romeo.setItemType(ItemType.both);
            rosterResult.addRosterItem(romeo);

            // prepare mercutio's roster entry
            final Item mercutio = new Item(JidCreate.entityBareFrom("[email protected]"), "Mercutio");
            mercutio.setItemType(ItemType.from);
            rosterResult.addRosterItem(mercutio);

            // prepare benvolio's roster entry
            final Item benvolio = new Item(JidCreate.entityBareFrom("[email protected]"), "Benvolio");
            benvolio.setItemType(ItemType.both);
            rosterResult.addRosterItem(benvolio);

            // simulate receiving the roster result and exit the loop
            connection.processStanza(rosterResult);
            break;
        }
    }
    roster.waitUntilLoaded();
    rosterListener.waitUntilInvocationOrTimeout();
}