use of org.jivesoftware.smack.roster.packet.RosterPacket in project Smack by igniterealtime.
the class RosterGroup method setName.
/**
* Sets the name of the group. Changing the group's name is like moving all the group entries
* of the group to a new group specified by the new name. Since this group won't have entries
* it will be removed from the roster. This means that all the references to this object will
* be invalid and will need to be updated to the new group specified by the new name.
*
* @param name the name of the group.
* @throws NotConnectedException
* @throws XMPPErrorException
* @throws NoResponseException
* @throws InterruptedException
*/
public void setName(String name) throws NotConnectedException, NoResponseException, XMPPErrorException, InterruptedException {
synchronized (entries) {
for (RosterEntry entry : entries) {
RosterPacket packet = new RosterPacket();
packet.setType(IQ.Type.set);
RosterPacket.Item item = RosterEntry.toRosterItem(entry);
item.removeGroupName(this.name);
item.addGroupName(name);
packet.addRosterItem(item);
connection().createStanzaCollectorAndSend(packet).nextResultOrThrow();
}
}
}
use of org.jivesoftware.smack.roster.packet.RosterPacket in project Smack by igniterealtime.
the class RosterTest method testIgnoreInvalidFrom.
/**
* Tests that roster pushes with invalid from are ignored.
* @throws XmppStringprepException
*
* @see <a href="http://xmpp.org/rfcs/rfc6121.html#roster-syntax-actions-push">RFC 6121, Section 2.1.6</a>
*/
@Test
public void testIgnoreInvalidFrom() throws XmppStringprepException {
final BareJid spammerJid = JidCreate.entityBareFrom("spam@example.com");
RosterPacket packet = new RosterPacket();
packet.setType(Type.set);
packet.setTo(connection.getUser());
packet.setFrom(JidCreate.entityBareFrom("mallory@example.com"));
packet.addRosterItem(new Item(spammerJid, "Cool products!"));
final String requestId = packet.getStanzaId();
// Simulate receiving the roster push
connection.processStanza(packet);
// Smack should reply with an error IQ
ErrorIQ errorIQ = (ErrorIQ) connection.getSentPacket();
assertEquals(requestId, errorIQ.getStanzaId());
assertEquals(Condition.service_unavailable, errorIQ.getError().getCondition());
assertNull("Contact was added to roster", Roster.getInstanceFor(connection).getEntry(spammerJid));
}
use of org.jivesoftware.smack.roster.packet.RosterPacket in project Smack by igniterealtime.
the class RosterTest method initRoster.
/**
* Initialize the roster according to the example in
* <a href="http://xmpp.org/rfcs/rfc3921.html#roster-login"
* >RFC3921: Retrieving One's Roster on Login</a>.
*
* @param connection the dummy connection of which the provided roster belongs to.
* @param roster the roster (or buddy list) which should be initialized.
* @throws SmackException
* @throws XmppStringprepException
*/
private void initRoster() throws InterruptedException, SmackException, XmppStringprepException {
roster.reload();
while (true) {
final Stanza sentPacket = connection.getSentPacket();
if (sentPacket instanceof RosterPacket && ((IQ) sentPacket).getType() == Type.get) {
// setup the roster get request
final RosterPacket rosterRequest = (RosterPacket) sentPacket;
assertSame("The <query/> element MUST NOT contain any <item/> child elements!", 0, rosterRequest.getRosterItemCount());
// prepare the roster result
final RosterPacket rosterResult = new RosterPacket();
rosterResult.setTo(connection.getUser());
rosterResult.setType(Type.result);
rosterResult.setStanzaId(rosterRequest.getStanzaId());
// prepare romeo's roster entry
final Item romeo = new Item(JidCreate.entityBareFrom("romeo@example.net"), "Romeo");
romeo.addGroupName("Friends");
romeo.setItemType(ItemType.both);
rosterResult.addRosterItem(romeo);
// prepare mercutio's roster entry
final Item mercutio = new Item(JidCreate.entityBareFrom("mercutio@example.com"), "Mercutio");
mercutio.setItemType(ItemType.from);
rosterResult.addRosterItem(mercutio);
// prepare benvolio's roster entry
final Item benvolio = new Item(JidCreate.entityBareFrom("benvolio@example.net"), "Benvolio");
benvolio.setItemType(ItemType.both);
rosterResult.addRosterItem(benvolio);
// simulate receiving the roster result and exit the loop
connection.processStanza(rosterResult);
break;
}
}
roster.waitUntilLoaded();
rosterListener.waitUntilInvocationOrTimeout();
}
use of org.jivesoftware.smack.roster.packet.RosterPacket in project Smack by igniterealtime.
the class RosterVersioningTest method testOtherVersionStored.
/**
* Tests that a non-empty roster result empties the store.
* @throws SmackException
* @throws XMPPException
* @throws XmppStringprepException
*/
@Test(timeout = 5000)
public void testOtherVersionStored() throws XMPPException, SmackException, XmppStringprepException {
Item vaglafItem = vaglafItem();
// 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) {
RosterPacket sentRP = (RosterPacket) sentPacket;
RosterPacket answer = new RosterPacket();
answer.setStanzaId(sentRP.getStanzaId());
answer.setType(Type.result);
answer.setTo(sentRP.getFrom());
answer.setVersion("newVersion");
answer.addRosterItem(vaglafItem);
rosterListener.reset();
connection.processStanza(answer);
rosterListener.waitUntilInvocationOrTimeout();
} else {
assertTrue("Expected to get a RosterPacket ", false);
}
Roster roster = Roster.getInstanceFor(connection);
assertEquals("Size of roster", 1, roster.getEntries().size());
RosterEntry entry = roster.getEntry(vaglafItem.getJid());
assertNotNull("Roster contains vaglaf entry", entry);
assertEquals("vaglaf entry in roster equals the sent entry", vaglafItem, RosterEntry.toRosterItem(entry));
RosterStore store = roster.getRosterStore();
assertEquals("Size of store", 1, store.getEntries().size());
Item item = store.getEntry(vaglafItem.getJid());
assertNotNull("Store contains vaglaf entry");
assertEquals("vaglaf entry in store equals the sent entry", vaglafItem, item);
}
use of org.jivesoftware.smack.roster.packet.RosterPacket in project Smack by igniterealtime.
the class RosterEntry method setName.
/**
* Sets the name associated with this entry.
*
* @param name the name.
* @throws NotConnectedException
* @throws XMPPErrorException
* @throws NoResponseException
* @throws InterruptedException
*/
public synchronized void setName(String name) throws NotConnectedException, NoResponseException, XMPPErrorException, InterruptedException {
// Do nothing if the name hasn't changed.
if (name != null && name.equals(getName())) {
return;
}
RosterPacket packet = new RosterPacket();
packet.setType(IQ.Type.set);
// Create a new roster item with the current RosterEntry and the *new* name. Note that we can't set the name of
// RosterEntry right away, as otherwise the updated event wont get fired, because equalsDeep would return true.
packet.addRosterItem(toRosterItem(this, name));
connection().createStanzaCollectorAndSend(packet).nextResultOrThrow();
// We have received a result response to the IQ set, the name was successfully changed
item.setName(name);
}
Aggregations