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);
}
}
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());
}
}
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());
}
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);
}
}
});
}
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();
}
Aggregations