use of org.jivesoftware.smack.roster.packet.RosterPacket.Item 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);
}
}
}
use of org.jivesoftware.smack.roster.packet.RosterPacket.Item in project Smack by igniterealtime.
the class DirectoryRosterStore method readEntry.
@SuppressWarnings("DefaultCharset")
private static Item readEntry(File file) {
Reader reader;
try {
// TODO: Should use Files.newBufferedReader() but it is not available on Android.
reader = new FileReader(file);
} catch (FileNotFoundException e) {
LOGGER.log(Level.FINE, "Roster entry file not found", e);
return null;
}
try {
XmlPullParser parser = PacketParserUtils.getParserFor(reader);
Item item = RosterPacketProvider.parseItem(parser);
reader.close();
return item;
} catch (XmlPullParserException | IOException e) {
boolean deleted = file.delete();
String message = "Exception while parsing roster entry.";
if (deleted) {
message += " File was deleted.";
}
LOGGER.log(Level.SEVERE, message, e);
return null;
}
}
use of org.jivesoftware.smack.roster.packet.RosterPacket.Item in project Smack by igniterealtime.
the class RosterTest method testAddRosterItem.
/**
* Test adding a roster item according to the example in
* <a href="http://xmpp.org/rfcs/rfc3921.html#roster-add"
* >RFC3921: Adding a Roster Item</a>.
*/
@Test
public void testAddRosterItem() throws Throwable {
// Constants for the new contact
final BareJid contactJID = JidCreate.entityBareFrom("nurse@example.com");
final String contactName = "Nurse";
final String[] contactGroup = { "Servants" };
// Setup
assertNotNull("Can't get the roster from the provided connection!", roster);
initRoster();
rosterListener.reset();
// Adding the new roster item
final RosterUpdateResponder serverSimulator = new RosterUpdateResponder() {
@Override
void verifyUpdateRequest(final RosterPacket updateRequest) {
final Item item = updateRequest.getRosterItems().iterator().next();
assertEquals("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!", contactGroup.length, item.getGroupNames().size());
assertSame("The provided group doesn't match the requested!", contactGroup[0], item.getGroupNames().iterator().next());
}
};
serverSimulator.start();
roster.createEntry(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!", 1, addedEntry.getGroups().size());
assertSame("Setup wrong group name for the added contact!", contactGroup[0], addedEntry.getGroups().iterator().next().getName());
// 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());
}
use of org.jivesoftware.smack.roster.packet.RosterPacket.Item in project Smack by igniterealtime.
the class RosterTest method testUpdateRosterItem.
/**
* Test updating a roster item according to the example in
* <a href="http://xmpp.org/rfcs/rfc3921.html#roster-update"
* >RFC3921: Updating a Roster Item</a>.
*/
@Test
public void testUpdateRosterItem() throws Throwable {
// Constants for the updated contact
final BareJid contactJID = JidCreate.entityBareFrom("romeo@example.net");
final String contactName = "Romeo";
final String[] contactGroups = { "Friends", "Lovers" };
// Setup
assertNotNull("Can't get the roster from the provided connection!", roster);
initRoster();
rosterListener.reset();
// Updating the roster item
final RosterUpdateResponder serverSimulator = new RosterUpdateResponder() {
@Override
void verifyUpdateRequest(final RosterPacket updateRequest) {
final Item item = updateRequest.getRosterItems().iterator().next();
assertEquals("The provided JID doesn't match the requested!", contactJID, item.getJid());
assertSame("The provided name doesn't match the requested!", contactName, item.getName());
assertTrue("The updated contact doesn't belong to the requested groups (" + contactGroups[0] + ")!", item.getGroupNames().contains(contactGroups[0]));
assertTrue("The updated contact doesn't belong to the requested groups (" + contactGroups[1] + ")!", item.getGroupNames().contains(contactGroups[1]));
assertSame("The provided group number doesn't match the requested!", contactGroups.length, item.getGroupNames().size());
}
};
serverSimulator.start();
roster.createGroup(contactGroups[1]).addEntry(roster.getEntry(contactJID));
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 updated contact
final RosterEntry addedEntry = roster.getEntry(contactJID);
assertNotNull("The contact was deleted from the roster!", addedEntry);
assertTrue("The roster listener wasn't invoked for the updated contact!", rosterListener.getUpdatedAddresses().contains(contactJID));
assertSame("Setup wrong name for the changed contact!", contactName, addedEntry.getName());
assertTrue("The updated contact doesn't belong to the requested groups (" + contactGroups[0] + ")!", roster.getGroup(contactGroups[0]).contains(addedEntry));
assertTrue("The updated contact doesn't belong to the requested groups (" + contactGroups[1] + ")!", roster.getGroup(contactGroups[1]).contains(addedEntry));
assertSame("The updated contact should be member of two groups!", contactGroups.length, addedEntry.getGroups().size());
// Verify the unchanged roster items
verifyMercutiosEntry(roster.getEntry(JidCreate.entityBareFrom("mercutio@example.com")));
verifyBenvoliosEntry(roster.getEntry(JidCreate.entityBareFrom("benvolio@example.net")));
assertSame("Wrong number of roster entries (" + roster.getEntries() + ").", 3, roster.getEntries().size());
}
Aggregations