Search in sources :

Example 16 with BareJid

use of org.jxmpp.jid.BareJid in project Smack by igniterealtime.

the class RosterTest method testSimpleRosterPush.

/**
     * Test a simple roster push according to the example in
     * <a href="http://xmpp.org/internet-drafts/draft-ietf-xmpp-3921bis-03.html#roster-syntax-actions-push"
     *     >RFC3921bis-03: Roster Push</a>.
     */
@Test
public void testSimpleRosterPush() throws Throwable {
    final BareJid contactJID = JidCreate.entityBareFrom("nurse@example.com");
    assertNotNull("Can't get the roster from the provided connection!", roster);
    final StringBuilder sb = new StringBuilder();
    sb.append("<iq id=\"rostertest1\" type=\"set\" ").append("to=\"").append(connection.getUser()).append("\">").append("<query xmlns=\"jabber:iq:roster\">").append("<item jid=\"").append(contactJID).append("\"/>").append("</query>").append("</iq>");
    final XmlPullParser parser = TestUtils.getIQParser(sb.toString());
    final IQ rosterPush = PacketParserUtils.parseIQ(parser);
    initRoster();
    rosterListener.reset();
    // Simulate receiving the roster push
    connection.processStanza(rosterPush);
    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 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 : BareJid(org.jxmpp.jid.BareJid) XmlPullParser(org.xmlpull.v1.XmlPullParser) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) IQ(org.jivesoftware.smack.packet.IQ) Test(org.junit.Test)

Example 17 with BareJid

use of org.jxmpp.jid.BareJid in project Smack by igniterealtime.

the class Roster method deleteEntry.

private void deleteEntry(Collection<Jid> deletedEntries, RosterEntry entry) {
    BareJid user = entry.getJid();
    entries.remove(user);
    unfiledEntries.remove(entry);
    // Move the presences from the presenceMap to the nonRosterPresenceMap.
    move(user, presenceMap, nonRosterPresenceMap);
    deletedEntries.add(user);
    for (Entry<String, RosterGroup> e : groups.entrySet()) {
        RosterGroup group = e.getValue();
        group.removeEntryLocal(entry);
        if (group.getEntryCount() == 0) {
            groups.remove(e.getKey());
        }
    }
}
Also used : EntityBareJid(org.jxmpp.jid.EntityBareJid) BareJid(org.jxmpp.jid.BareJid)

Example 18 with BareJid

use of org.jxmpp.jid.BareJid in project Smack by igniterealtime.

the class Roster method addUpdateEntry.

private void addUpdateEntry(Collection<Jid> addedEntries, Collection<Jid> updatedEntries, Collection<Jid> unchangedEntries, RosterPacket.Item item, RosterEntry entry) {
    RosterEntry oldEntry;
    synchronized (rosterListenersAndEntriesLock) {
        oldEntry = entries.put(item.getJid(), entry);
    }
    if (oldEntry == null) {
        BareJid jid = item.getJid();
        addedEntries.add(jid);
        // Move the eventually existing presences from nonRosterPresenceMap to presenceMap.
        move(jid, nonRosterPresenceMap, presenceMap);
    } else {
        RosterPacket.Item oldItem = RosterEntry.toRosterItem(oldEntry);
        if (!oldEntry.equalsDeep(entry) || !item.getGroupNames().equals(oldItem.getGroupNames())) {
            updatedEntries.add(item.getJid());
            oldEntry.updateItem(item);
        } else {
            // Record the entry as unchanged, so that it doesn't end up as deleted entry
            unchangedEntries.add(item.getJid());
        }
    }
    // Mark the entry as unfiled if it does not belong to any groups.
    if (item.getGroupNames().isEmpty()) {
        unfiledEntries.add(entry);
    } else {
        unfiledEntries.remove(entry);
    }
    // Add the entry/user to the groups
    List<String> newGroupNames = new ArrayList<String>();
    for (String groupName : item.getGroupNames()) {
        // Add the group name to the list.
        newGroupNames.add(groupName);
        // Add the entry to the group.
        RosterGroup group = getGroup(groupName);
        if (group == null) {
            group = createGroup(groupName);
            groups.put(groupName, group);
        }
        // Add the entry.
        group.addEntryLocal(entry);
    }
    // Remove user from the remaining groups.
    List<String> oldGroupNames = new ArrayList<String>();
    for (RosterGroup group : getGroups()) {
        oldGroupNames.add(group.getName());
    }
    oldGroupNames.removeAll(newGroupNames);
    for (String groupName : oldGroupNames) {
        RosterGroup group = getGroup(groupName);
        group.removeEntryLocal(entry);
        if (group.getEntryCount() == 0) {
            groups.remove(groupName);
        }
    }
}
Also used : EntityBareJid(org.jxmpp.jid.EntityBareJid) BareJid(org.jxmpp.jid.BareJid) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) ArrayList(java.util.ArrayList)

Example 19 with BareJid

use of org.jxmpp.jid.BareJid in project Smack by igniterealtime.

the class RosterUtil method ensureSubscribedTo.

public static void ensureSubscribedTo(final XMPPConnection connectionOne, final XMPPConnection connectionTwo, final Date deadline) throws NotLoggedInException, NotConnectedException, InterruptedException, TimeoutException {
    final Roster rosterOne = Roster.getInstanceFor(connectionOne);
    final BareJid jidTwo = connectionTwo.getUser().asBareJid();
    if (rosterOne.iAmSubscribedTo(jidTwo))
        return;
    final BareJid jidOne = connectionOne.getUser().asBareJid();
    final SubscribeListener subscribeListener = new SubscribeListener() {

        @Override
        public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) {
            if (from.equals(jidOne)) {
                return SubscribeAnswer.Approve;
            }
            return null;
        }
    };
    final Roster rosterTwo = Roster.getInstanceFor(connectionTwo);
    rosterTwo.addSubscribeListener(subscribeListener);
    try {
        rosterOne.sendSubscriptionRequest(jidTwo);
        waitUntilOtherEntityIsSubscribed(rosterTwo, jidOne, deadline);
    } finally {
        rosterTwo.removeSubscribeListener(subscribeListener);
    }
}
Also used : Jid(org.jxmpp.jid.Jid) BareJid(org.jxmpp.jid.BareJid) BareJid(org.jxmpp.jid.BareJid) Presence(org.jivesoftware.smack.packet.Presence)

Example 20 with BareJid

use of org.jxmpp.jid.BareJid in project Smack by igniterealtime.

the class RosterPacketProvider method parseItem.

public static RosterPacket.Item parseItem(XmlPullParser parser) throws XmlPullParserException, IOException {
    ParserUtils.assertAtStartTag(parser, RosterPacket.Item.ELEMENT);
    final int initialDepth = parser.getDepth();
    String jidString = parser.getAttributeValue("", "jid");
    String itemName = parser.getAttributeValue("", "name");
    BareJid jid = JidCreate.bareFrom(jidString);
    // Create item.
    RosterPacket.Item item = new RosterPacket.Item(jid, itemName);
    // Set status.
    String ask = parser.getAttributeValue("", "ask");
    item.setSubscriptionPending("subscribe".equals(ask));
    // Set type.
    String subscription = parser.getAttributeValue("", "subscription");
    RosterPacket.ItemType type = RosterPacket.ItemType.fromString(subscription);
    item.setItemType(type);
    // Set approval status.
    boolean approved = ParserUtils.getBooleanAttribute(parser, "approved", false);
    item.setApproved(approved);
    outerloop: while (true) {
        int eventType = parser.next();
        switch(eventType) {
            case XmlPullParser.START_TAG:
                String name = parser.getName();
                switch(name) {
                    case RosterPacket.Item.GROUP:
                        final String groupName = parser.nextText();
                        if (groupName != null && groupName.trim().length() > 0) {
                            item.addGroupName(groupName);
                        }
                        break;
                }
                break;
            case XmlPullParser.END_TAG:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
        }
    }
    ParserUtils.assertAtEndTag(parser);
    assert (item != null);
    return item;
}
Also used : BareJid(org.jxmpp.jid.BareJid) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket)

Aggregations

BareJid (org.jxmpp.jid.BareJid)26 RosterPacket (org.jivesoftware.smack.roster.packet.RosterPacket)9 Test (org.junit.Test)9 Item (org.jivesoftware.smack.roster.packet.RosterPacket.Item)8 Jid (org.jxmpp.jid.Jid)7 EntityBareJid (org.jxmpp.jid.EntityBareJid)6 Presence (org.jivesoftware.smack.packet.Presence)5 ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)3 ArrayList (java.util.ArrayList)2 SimpleResultSyncPoint (org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)2 SmackException (org.jivesoftware.smack.SmackException)2 IQ (org.jivesoftware.smack.packet.IQ)2 IoTIsFriendResponse (org.jivesoftware.smackx.iot.provisioning.element.IoTIsFriendResponse)2 XmlPullParser (org.xmlpull.v1.XmlPullParser)2 File (java.io.File)1 AbstractSmackIntegrationTest (org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)1 SmackIntegrationTest (org.igniterealtime.smack.inttest.SmackIntegrationTest)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 ServiceDiscoveryManager (org.jivesoftware.smackx.disco.ServiceDiscoveryManager)1 IoTRemove (org.jivesoftware.smackx.iot.discovery.element.IoTRemove)1