Package rocks.xmpp.core.roster.model

Examples of rocks.xmpp.core.roster.model.Contact


public class ContactExchangeManagerTest extends ExtensionTest {

    @BeforeClass
    public void prepareRoster() throws Exception {
        Roster roster = new Roster();
        roster.getContacts().add(new Contact(Jid.valueOf("juliet@example.net"), "juliet", "friends", "friends2"));
        roster.getContacts().add(new Contact(Jid.valueOf("romeo@example.net"), "romeo", "friends"));
        IQ iq = new IQ(AbstractIQ.Type.SET, roster);
        // Simulate a roster push in order to fill the roster.
        xmppSession.handleElement(iq);
    }
View Full Code Here


        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

        Roster roster = new Roster();
        List<Contact> contacts = new ArrayList<>();
        contacts.add(new Contact(new Jid("domain")));
        roster.getContacts().addAll(contacts);
        IQ iq = new IQ("1", IQ.Type.GET, roster);

        marshaller.marshal(iq, prefixFreeWriter);
        Assert.assertEquals("<iq id=\"1\" type=\"get\"><query xmlns=\"jabber:iq:roster\"><item jid=\"domain\"></item></query></iq>", writer.toString());
View Full Code Here

                }
            }
        });

        Roster roster1 = new Roster();
        roster1.getContacts().add(new Contact(Jid.valueOf("contact1@domain")));
        roster1.getContacts().add(new Contact(Jid.valueOf("contact2@domain")));
        roster1.getContacts().add(new Contact(Jid.valueOf("contact3@domain")));
        rosterManager.updateRoster(roster1, false);
        rosterPushCount[0]++;

        Roster roster2 = new Roster();
        roster2.getContacts().add(new Contact(Jid.valueOf("contact4@domain")));
        rosterManager.updateRoster(roster2, true);

        rosterPushCount[0]++;
        Roster roster3 = new Roster();
        Contact contact = new Contact(Jid.valueOf("contact2@domain"));
        contact.setSubscription(Contact.Subscription.REMOVE);
        roster3.getContacts().add(contact);
        rosterManager.updateRoster(roster3, true);

        rosterPushCount[0]++;
        Roster roster4 = new Roster();
        Contact contact2 = new Contact(Jid.valueOf("contact1@domain"));
        contact2.setName("Name");
        roster4.getContacts().add(contact2);
        rosterManager.updateRoster(roster4, true);
    }
View Full Code Here

    @Test
    public void testRosterGroups() {
        RosterManager rosterManager = new RosterManager(new TestXmppSession());

        Roster roster1 = new Roster();
        roster1.getContacts().add(new Contact(Jid.valueOf("contact1@domain"), "contact1", "Group1"));
        roster1.getContacts().add(new Contact(Jid.valueOf("contact2@domain"), "contact2", "Group2"));
        roster1.getContacts().add(new Contact(Jid.valueOf("contact4@domain"), "contact4", "Group3"));
        roster1.getContacts().add(new Contact(Jid.valueOf("contact3@domain"), "contact3", "Group3"));
        roster1.getContacts().add(new Contact(Jid.valueOf("contact5@domain"), "contact5", "Group3"));
        rosterManager.updateRoster(roster1, false);

        List<ContactGroup> list = new ArrayList<>(rosterManager.getContactGroups());
        Assert.assertEquals(list.size(), 3);
        Assert.assertEquals(list.get(0).getName(), "Group1");
View Full Code Here

    @Test
    public void testNestedRosterGroups() {
        RosterManager rosterManager = new RosterManager(new TestXmppSession());
        rosterManager.setGroupDelimiter("::");
        Roster roster1 = new Roster();
        roster1.getContacts().add(new Contact(Jid.valueOf("contact3@domain"), "contact3", "Group3::SubGroup"));
        roster1.getContacts().add(new Contact(Jid.valueOf("contact4@domain"), "contact4", "Group3::SubGroup::3rdLevel"));
        roster1.getContacts().add(new Contact(Jid.valueOf("contact5@domain"), "contact5", "Group3"));
        rosterManager.updateRoster(roster1, false);

        List<ContactGroup> list = new ArrayList<>(rosterManager.getContactGroups());
        Assert.assertEquals(list.size(), 1);
        Assert.assertEquals(list.get(0).getContacts().iterator().next().getJid(), Jid.valueOf("contact5@domain"));
View Full Code Here

        RosterManager rosterManager = new RosterManager(new TestXmppSession());

        // Initial roster
        Roster roster1 = new Roster();
        roster1.getContacts().add(new Contact(Jid.valueOf("contact1@domain"), "contact1", "group1"));
        roster1.getContacts().add(new Contact(Jid.valueOf("contact2@domain"), "contact2", "group2"));
        roster1.getContacts().add(new Contact(Jid.valueOf("contact3@domain"), "contact3", true, Contact.Subscription.FROM));
        roster1.getContacts().add(new Contact(Jid.valueOf("contact4@domain"), "contact4", true, Contact.Subscription.FROM, "group2"));
        rosterManager.updateRoster(roster1, false);

        Assert.assertEquals(rosterManager.getUnaffiliatedContacts().size(), 1);
        Assert.assertEquals(rosterManager.getUnaffiliatedContacts().iterator().next().getSubscription(), Contact.Subscription.FROM);
        List<ContactGroup> groups = new ArrayList<>(rosterManager.getContactGroups());
        Assert.assertEquals(groups.get(0).getContacts().size(), 1);
        Assert.assertEquals(groups.get(1).getContacts().size(), 2);

        Roster roster2 = new Roster();
        roster2.getContacts().add(new Contact(Jid.valueOf("contact3@domain"), "contact3", true, Contact.Subscription.BOTH));
        rosterManager.updateRoster(roster2, true);

        Assert.assertEquals(rosterManager.getUnaffiliatedContacts().size(), 1);
        Assert.assertEquals(rosterManager.getUnaffiliatedContacts().iterator().next().getSubscription(), Contact.Subscription.BOTH);

        Assert.assertEquals(rosterManager.getContactGroups().size(), 2);

        Roster roster3 = new Roster();
        roster3.getContacts().add(new Contact(Jid.valueOf("contact2@domain"), "contact2", true, Contact.Subscription.TO, "group1"));
        rosterManager.updateRoster(roster3, true);

        groups = new ArrayList<>(rosterManager.getContactGroups());
        List<Contact> contacts = new ArrayList<>(groups.get(0).getContacts());
        Assert.assertEquals(groups.get(0).getContacts().size(), 2);
        Assert.assertEquals(contacts.get(1).getSubscription(), Contact.Subscription.TO);
        Assert.assertTrue(contacts.get(1).isPending());
        Assert.assertEquals(groups.get(1).getContacts().size(), 1);

        Roster roster4 = new Roster();
        Contact contact2 = new Contact(Jid.valueOf("contact3@domain"), "", false, Contact.Subscription.REMOVE);
        roster4.getContacts().add(contact2);
        rosterManager.updateRoster(roster4, true);
        Assert.assertTrue(rosterManager.getUnaffiliatedContacts().isEmpty());
    }
View Full Code Here

                                    if (e.getPresence().isAvailable()) {
                                        Platform.runLater(new Runnable() {
                                            @Override
                                            public void run() {
                                                Presence presence = e.getPresence();
                                                Contact contact = xmppSession.getRosterManager().getContact(presence.getFrom());
                                                if (contact != null) {
                                                    ContactItem contactItem1 = contactMap.get(contact);
                                                    contactItem1.presence.set(presence);
                                                    FXCollections.sort(contactItems);
                                                }
                                            }
                                        });
                                    } else if (e.getPresence().getType() == Presence.Type.SUBSCRIBE) {
                                        xmppSession.getPresenceManager().denySubscription(e.getPresence().getFrom());
                                    }
                                }
                            }
                        });

                        RpcManager rpcManager = xmppSession.getExtensionManager(RpcManager.class);
                        rpcManager.setRpcHandler(new RpcHandler() {
                            @Override
                            public Value process(Jid requester, String methodName, List<Value> parameters) throws RpcException {
                                if (methodName.equals("examples.getStateName")) {
                                    if (!parameters.isEmpty()) {
                                        if (parameters.get(0).getAsInteger() == 6) {
                                            return new Value("Colorado");
                                        }
                                    }
                                }
                                throw new RpcException(123, "Invalid method name or parameter.");
                            }
                        });

                        AvatarManager avatarManager = xmppSession.getExtensionManager(AvatarManager.class);
                        avatarManager.addAvatarChangeListener(new AvatarChangeListener() {
                            @Override
                            public void avatarChanged(final AvatarChangeEvent e) {
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {

                                        Contact contact = xmppSession.getRosterManager().getContact(e.getContact());
                                        if (contact != null) {
                                            ContactItem contactItem = contactMap.get(contact);
                                            if (contactItem != null) {
                                                contactItem.avatar.set(e.getAvatar());
                                            }
View Full Code Here

    }

    List<ContactExchange.Item> getItemsToProcess(List<ContactExchange.Item> items) {
        List<ContactExchange.Item> newItems = new ArrayList<>();
        for (ContactExchange.Item item : items) {
            Contact contact = xmppSession.getRosterManager().getContact(item.getJid());
            // If "action" attribute is missing, it is implicitly "add" by default.
            if (item.getAction() == null || item.getAction() == ContactExchange.Item.Action.ADD) {
                if (contact != null) {
                    // 1. If the item already exists in the roster and the item is in the specified group (or no group is specified),
                    // the receiving application MUST NOT prompt a human user for approval regarding that item and MUST NOT add that item to the roster.

                    // 3. If the item already exists in the roster but not in the specified group, the receiving application MAY prompt the user
                    // for approval and SHOULD edit the existing item so that will also belong to the specified group (in addition to the existing group, if any).
                    List<String> specifiedGroups = new ArrayList<>(item.getGroups());
                    // Remove all existing groups.
                    specifiedGroups.removeAll(contact.getGroups());
                    // If there are still new groups.
                    if (!specifiedGroups.isEmpty()) {
                        // Only notify if the item will be added to new groups.
                        newItems.add(new ContactExchange.Item(item.getJid(), item.getName(), specifiedGroups, ContactExchange.Item.Action.ADD));
                    }
                } else {
                    // 2. If the item does not already exist in the roster, the receiving application SHOULD prompt a human user for approval
                    // regarding that item and, if approval is granted, MUST add that item to the roster.
                    newItems.add(item);
                }
            } else if (item.getAction() == ContactExchange.Item.Action.DELETE) {
                // 1. If the item does not exist in the roster, the receiving application MUST NOT prompt a human user for approval regarding that item and MUST NOT delete that item from the roster.
                if (contact != null) {
                    // 2. If the item exists in the roster but not in the specified group, the receiving application MUST NOT prompt the user for approval and MUST NOT delete the existing item.
                    // 3. If the item exists in the roster and is in both the specified group and another group, the receiving application MAY prompt the user for approval and SHOULD edit the existing item so that it no longer belongs to the specified group.
                    List<String> specifiedGroups = new ArrayList<>(item.getGroups());
                    // Retain only the groups, that exist in the roster.
                    specifiedGroups.retainAll(contact.getGroups());
                    if (!specifiedGroups.isEmpty() || contact.getGroups().isEmpty()) {
                        // Only notify if the item will be added to new groups.
                        newItems.add(new ContactExchange.Item(item.getJid(), item.getName(), specifiedGroups, ContactExchange.Item.Action.DELETE));
                    }
                }
            } else if (item.getAction() == ContactExchange.Item.Action.MODIFY) {
View Full Code Here

     * @throws rocks.xmpp.core.stanza.model.StanzaException If the entity returned a stanza error.
     * @throws rocks.xmpp.core.session.NoResponseException  If the entity did not respond.
     */
    public ContactExchange.Item.Action approve(ContactExchange.Item item) throws XmppException {
        RosterManager rosterManager = xmppSession.getRosterManager();
        Contact contact = rosterManager.getContact(item.getJid());
        ContactExchange.Item.Action action = null;
        if (item.getAction() == null || item.getAction() == ContactExchange.Item.Action.ADD) {
            // If the contact does not exist yes, add it and request subscription.
            // (After completing the roster set, the receiving application SHOULD also send a <presence/> stanza of type "subscribe" to the JID of the new item.)
            if (contact == null) {
                rosterManager.addContact(new Contact(item.getJid(), item.getName(), item.getGroups()), true, null);
                action = ContactExchange.Item.Action.ADD;
            } else {
                List<String> newGroups = new ArrayList<>(contact.getGroups());
                List<String> additionalGroups = new ArrayList<>(item.getGroups());
                // Remove all existing groups from the list.
                additionalGroups.removeAll(newGroups);
                if (!additionalGroups.isEmpty()) {
                    // Then add all additional groups to the existing groups.
                    newGroups.addAll(additionalGroups);
                    // ... SHOULD edit the existing item so that will also belong to the specified group (in addition to the existing group, if any).
                    rosterManager.updateContact(new Contact(contact.getJid(), contact.getName(), newGroups));
                    action = ContactExchange.Item.Action.MODIFY;
                }
            }
        } else if (item.getAction() == ContactExchange.Item.Action.DELETE) {
            if (contact != null) {
                List<String> existingGroups = new ArrayList<>(contact.getGroups());
                List<String> specifiedGroups = new ArrayList<>(item.getGroups());
                // Remove all specified groups from the existing groups.
                existingGroups.removeAll(specifiedGroups);
                // If there are still some groups left, only update the contact, but do not delete it.
                if (!existingGroups.isEmpty()) {
                    rosterManager.updateContact(new Contact(contact.getJid(), contact.getName(), existingGroups));
                    action = ContactExchange.Item.Action.MODIFY;
                } else {
                    rosterManager.removeContact(item.getJid());
                    action = ContactExchange.Item.Action.DELETE;
                }
            }
        } else if (item.getAction() == ContactExchange.Item.Action.MODIFY) {
            if (contact != null) {
                rosterManager.updateContact(new Contact(item.getJid(), item.getName(), item.getGroups()));
                action = ContactExchange.Item.Action.MODIFY;
            }
        }
        return action;
    }
View Full Code Here

                contactMap.clear();
            }

            // Loop through the new roster and compare it with the old one.
            for (Contact contact : roster.getContacts()) {
                Contact oldContact = contactMap.get(contact.getJid());
                if (contact.getSubscription() == Contact.Subscription.REMOVE) {
                    contactMap.remove(contact.getJid());
                    removedContacts.add(contact);
                } else if (oldContact != null && !oldContact.equals(contact)) {
                    contactMap.put(contact.getJid(), contact);
                    updatedContacts.add(contact);
                } else if (oldContact == null) {
                    contactMap.put(contact.getJid(), contact);
                    addedContacts.add(contact);
View Full Code Here

TOP

Related Classes of rocks.xmpp.core.roster.model.Contact

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.