org.jivesoftware.smack.roster.Roster Java Examples

The following examples show how to use org.jivesoftware.smack.roster.Roster. 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: TransferGroupUI.java    From Spark with Apache License 2.0 6 votes vote down vote up
public TransferGroupUI(String groupName) {
    setLayout(new VerticalFlowLayout(VerticalFlowLayout.TOP, 0, 0, true, false));
    setBackground(Color.white);

    final Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
    final RosterGroup rosterGroup = roster.getGroup(groupName);

    final List<RosterEntry> entries = new ArrayList<RosterEntry>(rosterGroup.getEntries());

    Collections.sort(entries, entryComparator);

    for (RosterEntry entry : entries) {
        final UserEntry userEntry = new UserEntry(entry);
        userEntries.add(userEntry);
        add(userEntry);
    }
}
 
Example #2
Source File: RosterManager.java    From mangosta-android with Apache License 2.0 6 votes vote down vote up
public void removeContact(String jidString)
        throws SmackException.NotLoggedInException, InterruptedException,
        SmackException.NotConnectedException, XMPPException.XMPPErrorException,
        SmackException.NoResponseException, XmppStringprepException {
    Roster roster = Roster.getInstanceFor(XMPPSession.getInstance().getXMPPConnection());
    if (!roster.isLoaded()) {
        roster.reloadAndWait();
    }

    BareJid jid = JidCreate.bareFrom(jidString);
    roster.removeEntry(roster.getEntry(jid));

    Presence presence = new Presence(Presence.Type.unsubscribe);
    presence.setTo(JidCreate.from(jidString));
    XMPPSession.getInstance().sendStanza(presence);
}
 
Example #3
Source File: RosterManager.java    From mangosta-android with Apache License 2.0 6 votes vote down vote up
public void addContact(String jidString)
        throws SmackException.NotLoggedInException, InterruptedException,
        SmackException.NotConnectedException, XMPPException.XMPPErrorException,
        SmackException.NoResponseException, XmppStringprepException {
    Roster roster = Roster.getInstanceFor(XMPPSession.getInstance().getXMPPConnection());
    if (!roster.isLoaded()) {
        roster.reloadAndWait();
    }

    BareJid jid = JidCreate.bareFrom(jidString);
    String name = XMPPUtils.fromJIDToUserName(jidString);
    String[] groups = new String[]{"Buddies"};

    roster.createEntry(jid, name, groups);
    roster.sendSubscriptionRequest(jid);
}
 
Example #4
Source File: ActivityChatScreen.java    From XMPPSample_Studio with Apache License 2.0 6 votes vote down vote up
@SuppressLint("NewApi")
    @Override
    protected void onDestroy() {

//        XMPP.getInstance().getConnection(acitiviy).getRoster()
//                .removeRosterListener(rosterListener);
        Roster.getInstanceFor(XMPP.getInstance().getConnection(acitiviy)).removeRosterListener(rosterListener);
        ChatManager.getInstanceFor(XMPP.getInstance().getConnection(acitiviy))
                .removeChatListener(chatListener);
        if (chat != null && messageListener != null) {
            chat.removeMessageListener(messageListener);
        }

        if (popupWindow != null) {
            popupWindow.dismiss();
        }
        if (emoticonsCover != null) {
            emoticonsCover.setVisibility(View.GONE);
        }

        // actionBar.setIcon(R.drawable.ic_launcher);

        super.onDestroy();
    }
 
Example #5
Source File: OmemoManagerSetupHelper.java    From Smack with Apache License 2.0 6 votes vote down vote up
public static void trustAllIdentities(OmemoManager alice, OmemoManager bob)
        throws InterruptedException, SmackException.NotConnectedException, SmackException.NotLoggedInException,
        SmackException.NoResponseException, CannotEstablishOmemoSessionException, CorruptedOmemoKeyException,
        XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException, IOException {
    Roster roster = Roster.getInstanceFor(alice.getConnection());

    if (alice.getOwnJid() != bob.getOwnJid() &&
            (!roster.iAmSubscribedTo(bob.getOwnJid()) || !roster.isSubscribedToMyPresence(bob.getOwnJid()))) {
        throw new IllegalStateException("Before trusting identities of a user, we must be subscribed to one another.");
    }

    alice.requestDeviceListUpdateFor(bob.getOwnJid());
    HashMap<OmemoDevice, OmemoFingerprint> fingerprints = alice.getActiveFingerprints(bob.getOwnJid());

    for (OmemoDevice device : fingerprints.keySet()) {
        OmemoFingerprint fingerprint = fingerprints.get(device);
        alice.trustOmemoIdentity(device, fingerprint);
    }
}
 
Example #6
Source File: ContactList.java    From Spark with Apache License 2.0 6 votes vote down vote up
/**
 * Removes a contact item from the group.
 *
 * @param item the ContactItem to remove.
 */
private void removeContactFromGroup(ContactItem item) {
    String groupName = item.getGroupName();
    ContactGroup contactGroup = getContactGroup(groupName);
    Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
    RosterEntry entry = roster.getEntry(item.getJid().asBareJid());
    if (entry != null && contactGroup != offlineGroup) {
        try {
            RosterGroup rosterGroup = roster.getGroup(groupName);
            if (rosterGroup != null) {
                RosterEntry rosterEntry = rosterGroup.getEntry(entry.getJid());
                if (rosterEntry != null) {
                    rosterGroup.removeEntry(rosterEntry);
                }
            }
            contactGroup.removeContactItem(contactGroup.getContactItemByJID(item.getJid()));
            checkGroup(contactGroup);
        }
        catch (Exception e) {
            Log.error("Error removing user from contact list.", e);
        }
    }
}
 
Example #7
Source File: ContactList.java    From Spark with Apache License 2.0 6 votes vote down vote up
/**
    * Switches all users to Offline and Creates a Reconnection Group
    */
   private synchronized void switchAllUserOffline(final boolean onError)
   {
SwingWorker worker = new SwingWorker() {
    
    @Override
    public Object construct() { 
	mainPanel.add(_reconnectpanelsmall,0);	
	_reconnectpanelsmall.setClosedOnError(onError);
	final Collection<RosterEntry> roster = Roster.getInstanceFor( SparkManager.getConnection() ).getEntries();
	
	for(RosterEntry r : roster)
	{
	    Presence p = new Presence(Presence.Type.unavailable);
	    moveToOfflineGroup(p, r.getJid());
	}
	return true;
    }
};	
worker.start();
   }
 
Example #8
Source File: ContactList.java    From Spark with Apache License 2.0 6 votes vote down vote up
/**
    * Switches all Users to Offline and Creates an Icon in the CommandBar
    */
   private synchronized void switchAllUserOfflineNoGroupEntry(final boolean onError) {
SwingWorker worker = new SwingWorker() {
    @Override
    public Object construct() {
	_reconnectpanelicon.getPanel().add(_reconnectpanelicon.getButton(), 0);
	_reconnectpanelicon.getPanel().revalidate();
	_reconnectpanelicon.setClosedOnError(onError);
	final Collection<RosterEntry> roster = Roster.getInstanceFor( SparkManager.getConnection()).getEntries();
	for (RosterEntry r : roster) {
	    Presence p = new Presence(Presence.Type.unavailable);
	    moveToOfflineGroup(p, r.getJid());
	}
	return true;
    }
};
worker.start();
   }
 
Example #9
Source File: Client.java    From desktopclient-java with GNU General Public License v3.0 6 votes vote down vote up
public boolean addToRoster(JID jid, String name) {
    if (!this.isConnected()) {
        LOGGER.info("not connected");
        return false;
    }

    if (!jid.isValid()) {
        LOGGER.warning("invalid JID: " + jid);
        return false;
    }

    try {
        // also sends presence subscription request
        Roster.getInstanceFor(mConn).createEntry(jid.toBareSmack(), name,
                null);
    } catch (SmackException.NotLoggedInException |
            SmackException.NoResponseException |
            XMPPException.XMPPErrorException |
            SmackException.NotConnectedException |
            InterruptedException ex) {
        LOGGER.log(Level.WARNING, "can't add contact to roster", ex);
        return false;
    }
    return true;
}
 
Example #10
Source File: Client.java    From desktopclient-java with GNU General Public License v3.0 6 votes vote down vote up
public boolean removeFromRoster(JID jid) {
    if (!this.isConnected()) {
        LOGGER.info("not connected");
        return false;
    }
    Roster roster = Roster.getInstanceFor(mConn);
    RosterEntry entry = roster.getEntry(jid.toBareSmack());
    if (entry == null) {
        LOGGER.info("can't find roster entry for jid: "+jid);
        return true;
    }
    try {
        // blocking
        roster.removeEntry(entry);
    } catch (SmackException.NotLoggedInException |
            SmackException.NoResponseException |
            XMPPException.XMPPErrorException |
            SmackException.NotConnectedException |
            InterruptedException ex) {
        LOGGER.log(Level.WARNING, "can't remove contact from roster", ex);
        return false;
    }
    return true;
}
 
Example #11
Source File: Client.java    From desktopclient-java with GNU General Public License v3.0 6 votes vote down vote up
public void updateRosterEntry(JID jid, String newName) {
    if (!this.isConnected()) {
        LOGGER.info("not connected");
        return;
    }
    Roster roster = Roster.getInstanceFor(mConn);
    RosterEntry entry = roster.getEntry(jid.toBareSmack());
    if (entry == null) {
        LOGGER.warning("can't find roster entry for jid: "+jid);
        return;
    }
    try {
        entry.setName(newName);
    } catch (SmackException.NotConnectedException |
            SmackException.NoResponseException |
            XMPPException.XMPPErrorException |
            InterruptedException ex) {
        LOGGER.log(Level.WARNING, "can't set name for entry", ex);
    }
}
 
Example #12
Source File: ContactList.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Called when NEW entries are added.
 *
 * @param addresses the address added.
 */
@Override
public void entriesAdded(final Collection<Jid> addresses) {
    SwingUtilities.invokeLater( () -> {
        Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );

        for (Jid jid : addresses) {
            RosterEntry entry = roster.getEntry(jid.asBareJid());
            addUser(entry);
        }
    } );
}
 
Example #13
Source File: IoT.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Override
public void iotScenario(XMPPTCPConnection dataThingConnection, XMPPTCPConnection readingThingConnection) throws TimeoutException, Exception {
    ThingState dataThingState = actAsDataThing(dataThingConnection);

    final SimpleResultSyncPoint syncPoint = new SimpleResultSyncPoint();
    dataThingState.setThingStateChangeListener(new AbstractThingStateChangeListener() {
        @Override
        public void owned(BareJid jid) {
            syncPoint.signal();
        }
    });
    // Wait until the thing is owned.
    syncPoint.waitForResult(TIMEOUT);
    printStatus("OWNED - Thing now owned by " + dataThingState.getOwner());

    // Make sure things are befriended.
    IoTProvisioningManager readingThingProvisioningManager = IoTProvisioningManager.getInstanceFor(readingThingConnection);
    readingThingProvisioningManager.sendFriendshipRequestIfRequired(dataThingConnection.getUser().asBareJid());

    Roster dataThingRoster = Roster.getInstanceFor(dataThingConnection);
    RosterUtil.waitUntilOtherEntityIsSubscribed(dataThingRoster, readingThingConnection.getUser().asBareJid(), TIMEOUT);
    printStatus("FRIENDSHIP ACCEPTED - Trying to read out data");

    IoTDataManager readingThingDataManager = IoTDataManager.getInstanceFor(readingThingConnection);
    List<IoTFieldsExtension> values = readingThingDataManager.requestMomentaryValuesReadOut(dataThingConnection.getUser());
    if (values.size() != 1) {
        throw new IllegalStateException("Unexpected number of values returned: " + values.size());
    }
    IoTFieldsExtension field = values.get(0);
    printStatus("DATA READ-OUT SUCCESS: " + field.toXML());
    printStatus("IoT SCENARIO FINISHED SUCCESSFULLY");
}
 
Example #14
Source File: PresenceManager.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the presence of a user.
 *
 * @param jidString the users jid.
 * @return the users presence.
 */
public static Presence getPresence(BareJid jid) {
    if (jid.equals(SparkManager.getSessionManager().getBareAddress())) {
        return SparkManager.getWorkspace().getStatusBar().getPresence();
    } else {
        final Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
        return roster.getPresence(jid);
    }
}
 
Example #15
Source File: PresenceManager.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the fully qualified jid of a user. May return {@code null}.
 *
 * @param jidString the users bare jid (ex. [email protected])
 * @return the fully qualified jid of a user (ex. [email protected] --> [email protected]/spark) or {@code null}.
 */
public static EntityFullJid getFullyQualifiedJID(BareJid jid) {
    final Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
    Presence presence = roster.getPresence(jid);
    Jid result = presence.getFrom();
    EntityFullJid entityFullJid = result.asEntityFullJidIfPossible();
    return entityFullJid;
}
 
Example #16
Source File: UriManager.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
    * Handles the ?remove URI
    * 
    * @param uri
    *            the decoded uri
    * @throws Exception
    */
   public void handleRemove(URI uri) throws Exception {
// xmpp:[email protected]?remove

   BareJid jid;
   try {
       jid = JidCreate.bareFrom(retrieveJID(uri));
   } catch (XmppStringprepException e) {
       throw new IllegalStateException(e);
   }

   Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
RosterEntry entry = roster.getEntry(jid);
roster.removeEntry(entry);
   }
 
Example #17
Source File: ContactList.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the users presence.
 *
 * @param presence the user to update.
 * @throws Exception if there is a problem while updating the user's presence.
 */
private synchronized void updateUserPresence(Presence presence) throws Exception {
    if (presence.getError() != null) {
        // We ignore this.
        return;
    }

    final Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );

    final BareJid bareJID = presence.getFrom().asBareJid();

    RosterEntry entry = roster.getEntry(bareJID);
    boolean isPending = entry != null && (entry.getType() == RosterPacket.ItemType.none || entry.getType() == RosterPacket.ItemType.from)
        && entry.isSubscriptionPending();

    // If online, check to see if they are in the offline group.
    // If so, remove from offline group and add to all groups they
    // belong to.

    if (presence.getType() == Presence.Type.available && offlineGroup.getContactItemByJID(bareJID) != null || ( presence.getFrom().toString().contains( "workgroup." ) )) {
        changeOfflineToOnline(bareJID, entry, presence);
    }
    else if (presence.getType() == Presence.Type.available) {
        updateContactItemsPresence(presence, entry, bareJID);
    }
    else if (presence.getType() == Presence.Type.unavailable && !isPending) {
        // If not available, move to offline group.
        Presence rosterPresence = PresenceManager.getPresence(bareJID);
        if (!rosterPresence.isAvailable()) {
            moveToOfflineGroup(presence, bareJID);
        }
        else {
            updateContactItemsPresence(rosterPresence, entry, bareJID);
        }
    }
    
}
 
Example #18
Source File: RosterManager.java    From mangosta-android with Apache License 2.0 5 votes vote down vote up
public HashMap<Jid, Presence.Type> getContacts()
        throws SmackException.NotLoggedInException, InterruptedException,
        SmackException.NotConnectedException {
    Roster roster = Roster.getInstanceFor(XMPPSession.getInstance().getXMPPConnection());
    if (!roster.isLoaded()) {
        roster.reloadAndWait();
    }

    String groupName = "Buddies";

    RosterGroup group = roster.getGroup(groupName);

    if (group == null) {
        roster.createGroup(groupName);
        group = roster.getGroup(groupName);
    }

    HashMap<Jid, Presence.Type> buddies = new HashMap<>();

    List<RosterEntry> entries = group.getEntries();
    for (RosterEntry entry : entries) {
        BareJid jid = entry.getJid();
        Presence.Type status = roster.getPresence(jid).getType();
        buddies.put(jid, status);
    }

    return buddies;
}
 
Example #19
Source File: ContactList.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a single user to the ContactList.
 *
 * @param entry the <code>RosterEntry</code> of the the user.
 */
private void addUser(RosterEntry entry) {
    ContactItem newContactItem = UIComponentRegistry.createContactItem(entry.getName(), null, entry.getJid());

    if (entry.getType() == RosterPacket.ItemType.none || entry.getType() == RosterPacket.ItemType.from) {
        // Ignore, since the new user is pending to be added.
        for (RosterGroup group : entry.getGroups()) {
            ContactGroup contactGroup = getContactGroup(group.getName());
            if (contactGroup == null) {
                contactGroup = addContactGroup(group.getName());
            }

            boolean isPending = entry.getType() == RosterPacket.ItemType.none || entry.getType() == RosterPacket.ItemType.from
                && entry.isSubscriptionPending();
            if (isPending) {
                contactGroup.setVisible(true);
            }
            contactGroup.addContactItem(newContactItem);

        }
        return;
    }
    else {
        moveToOffline(newContactItem);
    }

    // Update users icon
    Presence presence = Roster.getInstanceFor( SparkManager.getConnection() ).getPresence(entry.getJid());
    try {
        updateUserPresence(presence);
    }
    catch (Exception e) {
        Log.error(e);
    }
}
 
Example #20
Source File: ContactList.java    From Spark with Apache License 2.0 5 votes vote down vote up
private void removeContactFromRoster(ContactItem item) {
    Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
    RosterEntry entry = roster.getEntry(item.getJid().asBareJid());
    if (entry != null) {
        try {
            roster.removeEntry(entry);
        }
        catch (XMPPException | SmackException | InterruptedException e) {
            Log.warning("Unable to remove roster entry.", e);
        }
    }
}
 
Example #21
Source File: RosterDialog.java    From Spark with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(ActionEvent e) {
       String group = JOptionPane.showInputDialog(dialog, Res.getString("label.enter.group.name") + ":", Res.getString("title.new.roster.group"), 3);
       if (group != null && group.length() > 0 && !groupModel.contains(group)) {
           Roster.getInstanceFor( SparkManager.getConnection() ).createGroup(group);
           groupModel.add(group);
           int size = groupModel.size();
           groupBox.setSelectedIndex(size - 1);
       }
   }
 
Example #22
Source File: OTRPrefPanel.java    From Spark with Apache License 2.0 5 votes vote down vote up
private void loadRemoteKeys() {

        for (RosterEntry entry : Roster.getInstanceFor( SparkManager.getConnection() ).getEntries()) {
            SessionID curSession = new SessionID(SparkManager.getConnection().getUser(), entry.getUser(), "Scytale");
            String remoteKey = _keyManager.getRemoteFingerprint(curSession);
            if (remoteKey != null) {
                boolean isVerified = _keyManager.isVerified(curSession);
                _keytable.addEntry(entry.getUser(), remoteKey, isVerified);
            }
        }

        _keytable.addTableChangeListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                int col = e.getColumn();
                int row = e.getFirstRow();

                if (col == 2) {
                    boolean selection = (Boolean) _keytable.getValueAt(row, col);
                    String JID = (String) _keytable.getValueAt(row, 0);
                    SessionID curSelectedSession = new SessionID(SparkManager.getConnection().getUser(), JID, "Scytale");
                    if (!selection) {
                        _keyManager.verify(curSelectedSession);
                    } else {
                        _keyManager.unverify(curSelectedSession);
                    }
                }
            }
        });

    }
 
Example #23
Source File: SoftPhonePlugin.java    From Spark with Apache License 2.0 5 votes vote down vote up
private void loadVCards() {
    // Load vCard information.
    final Roster roster = Roster.getInstanceFor( SparkManager.getConnection() );
    for (RosterEntry entry : roster.getEntries()) {
        SparkManager.getVCardManager().getVCardFromMemory(entry.getUser());
    }
}
 
Example #24
Source File: RosterManager.java    From mangosta-android with Apache License 2.0 5 votes vote down vote up
public void removeAllContacts()
        throws SmackException.NotLoggedInException, InterruptedException,
        SmackException.NotConnectedException, XMPPException.XMPPErrorException,
        SmackException.NoResponseException {
    Roster roster = Roster.getInstanceFor(XMPPSession.getInstance().getXMPPConnection());
    if (!roster.isLoaded()) {
        roster.reloadAndWait();
    }
    for (RosterEntry entry : roster.getEntries()) {
        roster.removeEntry(entry);
        Presence presence = new Presence(Presence.Type.unsubscribe);
        presence.setTo(entry.getJid());
        XMPPSession.getInstance().sendStanza(presence);
    }
}
 
Example #25
Source File: MucManager.java    From Yahala-Messenger with MIT License 5 votes vote down vote up
public List<RosterEntry> getEntriesByGroup(Roster roster, String groupName) {
    List<RosterEntry> entriesList = new ArrayList<RosterEntry>();
    RosterGroup rosterGroup = roster.getGroup(groupName);
    Collection<RosterEntry> rosterEntries = rosterGroup.getEntries();
    Iterator<RosterEntry> i = rosterEntries.iterator();
    while (i.hasNext()) {
        entriesList.add(i.next());
    }
    return entriesList;
}
 
Example #26
Source File: MucManager.java    From Yahala-Messenger with MIT License 5 votes vote down vote up
public boolean CreateGroup(Roster roster, String groupName) {
    try {
        roster.createGroup(groupName);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
 
Example #27
Source File: IntegrationTestRosterUtil.java    From Smack with Apache License 2.0 5 votes vote down vote up
private static void notInRoster(XMPPConnection c1, XMPPConnection c2) throws NotLoggedInException,
        NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    Roster roster = Roster.getInstanceFor(c1);
    RosterEntry c2Entry = roster.getEntry(c2.getUser().asBareJid());
    if (c2Entry == null) {
        return;
    }
    roster.removeEntry(c2Entry);
}
 
Example #28
Source File: KonRosterListener.java    From desktopclient-java with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onRosterLoaded(Roster roster) {
    Set<RosterEntry> entries = roster.getEntries();
    LOGGER.info("loading "+entries.size()+" entries");

    mHandler.onLoaded(entries.stream()
            .map(KonRosterListener::clientToModel)
            .collect(Collectors.toList()));
    mLoaded = true;
}
 
Example #29
Source File: PresenceListener.java    From desktopclient-java with GNU General Public License v3.0 5 votes vote down vote up
public PresenceListener(Roster roster, RosterHandler handler) {
    mRoster = roster;
    mHandler = handler;

    ProviderManager.addExtensionProvider(
            PublicKeyPresence.ELEMENT_NAME,
            PublicKeyPresence.NAMESPACE,
            new PublicKeyPresence.Provider());

    ProviderManager.addExtensionProvider(
            PresenceSignature.ELEMENT_NAME,
            PresenceSignature.NAMESPACE,
            new PresenceSignature.Provider());
}
 
Example #30
Source File: RosterExchangeManager.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a roster to userID. All the entries of the roster will be sent to the
 * target user.
 *
 * @param roster the roster to send
 * @param targetUserID the user that will receive the roster entries
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void send(Roster roster, Jid targetUserID) throws NotConnectedException, InterruptedException {
    XMPPConnection connection = weakRefConnection.get();

    // Create a new message to send the roster
    MessageBuilder messageBuilder = connection.getStanzaFactory().buildMessageStanza().to(targetUserID);
    // Create a RosterExchange Package and add it to the message
    RosterExchange rosterExchange = new RosterExchange(roster);
    messageBuilder.addExtension(rosterExchange);

    // Send the message that contains the roster
    connection.sendStanza(messageBuilder.build());
}