Search in sources :

Example 6 with RosterPacket

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

the class RosterVersioningTest method answerWithEmptyRosterResult.

private void answerWithEmptyRosterResult() {
    // 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) {
        final IQ emptyIQ = IQ.createResultIQ((RosterPacket) sentPacket);
        connection.processStanza(emptyIQ);
    } else {
        assertTrue("Expected to get a RosterPacket ", false);
    }
}
Also used : RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ)

Example 7 with RosterPacket

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

the class RosterVersioningTest method testRosterVersioningWithCachedRosterAndPushes.

/**
     * Test roster versioning with roster pushes.
     */
@Test(timeout = 5000)
public void testRosterVersioningWithCachedRosterAndPushes() throws Throwable {
    answerWithEmptyRosterResult();
    rosterListener.waitAndReset();
    RosterStore store = roster.getRosterStore();
    // Simulate a roster push adding vaglaf
    {
        RosterPacket rosterPush = new RosterPacket();
        rosterPush.setTo(JidCreate.from("rostertest@example.com/home"));
        rosterPush.setType(Type.set);
        rosterPush.setVersion("v97");
        Item pushedItem = vaglafItem();
        rosterPush.addRosterItem(pushedItem);
        rosterListener.reset();
        connection.processStanza(rosterPush);
        rosterListener.waitAndReset();
        assertEquals("Expect store version after push", "v97", store.getRosterVersion());
        Item storedItem = store.getEntry(JidCreate.from("vaglaf@example.com"));
        assertNotNull("Expect vaglaf to be added", storedItem);
        assertEquals("Expect vaglaf to be equal to pushed item", pushedItem, storedItem);
        Collection<Item> rosterItems = new HashSet<Item>();
        for (RosterEntry entry : roster.getEntries()) {
            rosterItems.add(RosterEntry.toRosterItem(entry));
        }
        assertEquals(rosterItems, new HashSet<Item>(store.getEntries()));
    }
    // Simulate a roster push removing vaglaf
    {
        RosterPacket rosterPush = new RosterPacket();
        rosterPush.setTo(JidCreate.from("rostertest@example.com/home"));
        rosterPush.setType(Type.set);
        rosterPush.setVersion("v98");
        Item item = new Item(JidCreate.entityBareFrom("vaglaf@example.com"), "vaglaf the only");
        item.setItemType(ItemType.remove);
        rosterPush.addRosterItem(item);
        rosterListener.reset();
        connection.processStanza(rosterPush);
        rosterListener.waitAndReset();
        assertNull("Store doses not contain vaglaf", store.getEntry(JidCreate.entityBareFrom("vaglaf@example.com")));
        assertEquals("Expect store version after push", "v98", store.getRosterVersion());
    }
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) DirectoryRosterStore(org.jivesoftware.smack.roster.rosterstore.DirectoryRosterStore) RosterStore(org.jivesoftware.smack.roster.rosterstore.RosterStore) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Collection(java.util.Collection) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with RosterPacket

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

the class SubscriptionPreApprovalTest method testPreApproveAndCreate.

@Test
public void testPreApproveAndCreate() throws Throwable {
    final BareJid contactJID = JidCreate.bareFrom("preapproval@example.com");
    final String contactName = "PreApproval";
    final String[] contactGroup = {};
    connection.enableStreamFeature(SubscriptionPreApproval.INSTANCE);
    final PreApproveAndCreateEntryResponder serverSimulator = new PreApproveAndCreateEntryResponder() {

        @Override
        void verifyRosterUpdateRequest(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("The provided group number doesn't match the requested!", 0, item.getGroupNames().size());
        }

        @Override
        void verifyPreApprovalRequest(Presence preApproval) {
            assertSame("The provided name doesn't match the requested!", contactJID, preApproval.getTo());
            assertSame("The provided presence type is incorrect!", Presence.Type.subscribed, preApproval.getType());
        }
    };
    serverSimulator.start();
    roster.preApproveAndCreateEntry(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!", 0, addedEntry.getGroups().size());
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) BareJid(org.jxmpp.jid.BareJid) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Presence(org.jivesoftware.smack.packet.Presence) Test(org.junit.Test)

Example 9 with RosterPacket

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

the class Roster method reload.

/**
     * Reloads the entire roster from the server. This is an asynchronous operation,
     * which means the method will return immediately, and the roster will be
     * reloaded at a later point when the server responds to the reload request.
     * @throws NotLoggedInException If not logged in.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public void reload() throws NotLoggedInException, NotConnectedException, InterruptedException {
    final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
    RosterPacket packet = new RosterPacket();
    if (rosterStore != null && isRosterVersioningSupported()) {
        packet.setVersion(rosterStore.getRosterVersion());
    }
    rosterState = RosterState.loading;
    connection.sendIqWithResponseCallback(packet, new RosterResultListener(), new ExceptionCallback() {

        @Override
        public void processException(Exception exception) {
            rosterState = RosterState.uninitialized;
            Level logLevel;
            if (exception instanceof NotConnectedException) {
                logLevel = Level.FINE;
            } else {
                logLevel = Level.SEVERE;
            }
            LOGGER.log(logLevel, "Exception reloading roster", exception);
            for (RosterLoadedListener listener : rosterLoadedListeners) {
                listener.onRosterLoadingFailed(exception);
            }
        }
    });
}
Also used : NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Level(java.util.logging.Level) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ExceptionCallback(org.jivesoftware.smack.ExceptionCallback) SmackException(org.jivesoftware.smack.SmackException) FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) NotLoggedInException(org.jivesoftware.smack.SmackException.NotLoggedInException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException)

Example 10 with RosterPacket

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

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 a synchronous call -- Smack must wait for the server
     * to send an updated subscription status.
     *
     * @param entry a roster entry.
     * @throws XMPPErrorException if an XMPP error occurs.
     * @throws NotLoggedInException if not logged in.
     * @throws NoResponseException SmackException if there was no response from the server.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public void removeEntry(RosterEntry entry) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
    // The actual removal logic takes place in RosterPacketListenerprocess>>Packet(Packet)
    if (!entries.containsKey(entry.getJid())) {
        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);
    connection.createStanzaCollectorAndSend(packet).nextResultOrThrow();
}
Also used : RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Aggregations

RosterPacket (org.jivesoftware.smack.roster.packet.RosterPacket)21 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 AccountItem (com.xabber.android.data.account.AccountItem)2 ConnectionItem (com.xabber.android.data.connection.ConnectionItem)2 ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)2 IQ (org.jivesoftware.smack.packet.IQ)2 Roster (org.jivesoftware.smack.roster.Roster)2 RosterEntry (org.jivesoftware.smack.roster.RosterEntry)2 DirectoryRosterStore (org.jivesoftware.smack.roster.rosterstore.DirectoryRosterStore)2 RosterStore (org.jivesoftware.smack.roster.rosterstore.RosterStore)2 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