use of org.jivesoftware.smack.StanzaCollector in project Smack by igniterealtime.
the class OfflineMessageManager method getMessages.
/**
* Returns a List of the offline <tt>Messages</tt> whose stamp matches the specified
* request. The request will include the list of stamps that uniquely identifies
* the offline messages to retrieve. The returned offline messages will not be deleted
* from the server. Use {@link #deleteMessages(java.util.List)} to delete the messages.
*
* @param nodes the list of stamps that uniquely identifies offline message.
* @return a List with the offline <tt>Messages</tt> that were received as part of
* this request.
* @throws XMPPErrorException If the user is not allowed to make this request or the server does
* not support offline message retrieval.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
* @throws InterruptedException
*/
public List<Message> getMessages(final List<String> nodes) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
List<Message> messages = new ArrayList<Message>();
OfflineMessageRequest request = new OfflineMessageRequest();
for (String node : nodes) {
OfflineMessageRequest.Item item = new OfflineMessageRequest.Item(node);
item.setAction("view");
request.addItem(item);
}
// Filter offline messages that were requested by this request
StanzaFilter messageFilter = new AndFilter(PACKET_FILTER, new StanzaFilter() {
@Override
public boolean accept(Stanza packet) {
OfflineMessageInfo info = (OfflineMessageInfo) packet.getExtension("offline", namespace);
return nodes.contains(info.getNode());
}
});
int pendingNodes = nodes.size();
StanzaCollector messageCollector = connection.createStanzaCollector(messageFilter);
try {
connection.createStanzaCollectorAndSend(request).nextResultOrThrow();
// Collect the received offline messages
Message message = messageCollector.nextResult();
while (message != null && pendingNodes > 0) {
pendingNodes--;
messages.add(message);
message = messageCollector.nextResult();
}
} finally {
// Stop queuing offline messages
messageCollector.cancel();
}
return messages;
}
use of org.jivesoftware.smack.StanzaCollector in project Smack by igniterealtime.
the class MultiUserChat method changeNickname.
/**
* Changes the occupant's nickname to a new nickname within the room. Each room occupant
* will receive two presence packets. One of type "unavailable" for the old nickname and one
* indicating availability for the new nickname. The unavailable presence will contain the new
* nickname and an appropriate status code (namely 303) as extended presence information. The
* status code 303 indicates that the occupant is changing his/her nickname.
*
* @param nickname the new nickname within the room.
* @throws XMPPErrorException if the new nickname is already in use by another occupant.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
* @throws InterruptedException
* @throws MucNotJoinedException
*/
public synchronized void changeNickname(Resourcepart nickname) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, MucNotJoinedException {
StringUtils.requireNotNullOrEmpty(nickname, "Nickname must not be null or blank.");
// nickname.
if (!joined) {
throw new MucNotJoinedException(this);
}
final EntityFullJid jid = JidCreate.fullFrom(room, nickname);
// We change the nickname by sending a presence packet where the "to"
// field is in the form "roomName@service/nickname"
// We don't have to signal the MUC support again
Presence joinPresence = new Presence(Presence.Type.available);
joinPresence.setTo(jid);
// Wait for a presence packet back from the server.
StanzaFilter responseFilter = new AndFilter(FromMatchesFilter.createFull(jid), new StanzaTypeFilter(Presence.class));
StanzaCollector response = connection.createStanzaCollectorAndSend(responseFilter, joinPresence);
// Wait up to a certain number of seconds for a reply. If there is a negative reply, an
// exception will be thrown
response.nextResultOrThrow();
this.nickname = nickname;
}
use of org.jivesoftware.smack.StanzaCollector in project Smack by igniterealtime.
the class JingleProviderTest method testParseIQSimple.
/**
* Test for parsing a Jingle
*/
public void testParseIQSimple() {
// Create a dummy packet for testing...
IQfake iqSent = new IQfake(" <jingle xmlns='urn:xmpp:tmp:jingle'" + " initiator=\"gorrino@viejo.com\"" + " responder=\"colico@hepatico.com\"" + " action=\"transport-info\" sid=\"\">" + " <transport xmlns='urn:xmpp:tmp:jingle:transports:ice-udp'>" + " <candidate generation=\"1\"" + " ip=\"192.168.1.1\"" + " password=\"secret\"" + " port=\"8080\"" + " username=\"username\"" + " preference=\"1\"/>" + " </transport>" + "</jingle>");
iqSent.setTo(getFullJID(0));
iqSent.setFrom(getFullJID(0));
iqSent.setType(IQ.Type.get);
// Create a filter and a collector...
PacketFilter filter = new StanzaTypeFilter(IQ.class);
StanzaCollector collector = getConnection(0).createStanzaCollector(filter);
System.out.println("Testing if a Jingle IQ can be sent and received...");
// Send the iq packet with an invalid namespace
getConnection(0).sendStanza(iqSent);
// Receive the packet
IQ iqReceived = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
collector.cancel();
if (iqReceived == null) {
fail("No response from server");
} else if (iqReceived.getType() == IQ.Type.error) {
fail("The server did reply with an error packet: " + iqReceived.getError().getCode());
} else {
assertTrue(iqReceived instanceof Jingle);
Jingle jin = (Jingle) iqReceived;
System.out.println("Sent: " + iqSent.toXML());
System.out.println("Received: " + jin.toXML());
}
}
use of org.jivesoftware.smack.StanzaCollector in project Smack by igniterealtime.
the class MultipleRecipientManagerTest method testSending.
/**
* Ensures that sending and receiving of packets is ok.
*/
public void testSending() throws XMPPException {
StanzaCollector collector1 = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
StanzaCollector collector2 = getConnection(2).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
StanzaCollector collector3 = getConnection(3).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
Message message = new Message();
message.setBody("Hola");
List<String> to = Arrays.asList(new String[] { getBareJID(1) });
List<String> cc = Arrays.asList(new String[] { getBareJID(2) });
List<String> bcc = Arrays.asList(new String[] { getBareJID(3) });
MultipleRecipientManager.send(getConnection(0), message, to, cc, bcc);
Packet message1 = collector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection 1 never received the message", message1);
MultipleRecipientInfo info1 = MultipleRecipientManager.getMultipleRecipientInfo(message1);
assertNotNull("Message 1 does not contain MultipleRecipientInfo", info1);
assertFalse("Message 1 should be 'replyable'", info1.shouldNotReply());
List<?> addresses1 = info1.getTOAddresses();
assertEquals("Incorrect number of TO addresses", 1, addresses1.size());
String address1 = ((MultipleAddresses.Address) addresses1.get(0)).getJid();
assertEquals("Incorrect TO address", getBareJID(1), address1);
addresses1 = info1.getCCAddresses();
assertEquals("Incorrect number of CC addresses", 1, addresses1.size());
address1 = ((MultipleAddresses.Address) addresses1.get(0)).getJid();
assertEquals("Incorrect CC address", getBareJID(2), address1);
Packet message2 = collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection 2 never received the message", message2);
MultipleRecipientInfo info2 = MultipleRecipientManager.getMultipleRecipientInfo(message2);
assertNotNull("Message 2 does not contain MultipleRecipientInfo", info2);
assertFalse("Message 2 should be 'replyable'", info2.shouldNotReply());
List<MultipleAddresses.Address> addresses2 = info2.getTOAddresses();
assertEquals("Incorrect number of TO addresses", 1, addresses2.size());
String address2 = ((MultipleAddresses.Address) addresses2.get(0)).getJid();
assertEquals("Incorrect TO address", getBareJID(1), address2);
addresses2 = info2.getCCAddresses();
assertEquals("Incorrect number of CC addresses", 1, addresses2.size());
address2 = ((MultipleAddresses.Address) addresses2.get(0)).getJid();
assertEquals("Incorrect CC address", getBareJID(2), address2);
Packet message3 = collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection 3 never received the message", message3);
MultipleRecipientInfo info3 = MultipleRecipientManager.getMultipleRecipientInfo(message3);
assertNotNull("Message 3 does not contain MultipleRecipientInfo", info3);
assertFalse("Message 3 should be 'replyable'", info3.shouldNotReply());
List<MultipleAddresses.Address> addresses3 = info3.getTOAddresses();
assertEquals("Incorrect number of TO addresses", 1, addresses3.size());
String address3 = ((MultipleAddresses.Address) addresses3.get(0)).getJid();
assertEquals("Incorrect TO address", getBareJID(1), address3);
addresses3 = info3.getCCAddresses();
assertEquals("Incorrect number of CC addresses", 1, addresses3.size());
address3 = ((MultipleAddresses.Address) addresses3.get(0)).getJid();
assertEquals("Incorrect CC address", getBareJID(2), address3);
collector1.cancel();
collector2.cancel();
collector3.cancel();
}
use of org.jivesoftware.smack.StanzaCollector in project Smack by igniterealtime.
the class MultipleRecipientManagerTest method testNoReply.
/**
* Ensures that replying is not allowed when disabled.
*/
public void testNoReply() throws XMPPException {
StanzaCollector collector1 = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
StanzaCollector collector2 = getConnection(2).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
StanzaCollector collector3 = getConnection(3).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
// Send the intial message with multiple recipients
Message message = new Message();
message.setBody("Hola");
List<String> to = Arrays.asList(new String[] { getBareJID(1) });
List<String> cc = Arrays.asList(new String[] { getBareJID(2) });
List<String> bcc = Arrays.asList(new String[] { getBareJID(3) });
MultipleRecipientManager.send(getConnection(0), message, to, cc, bcc, null, null, true);
// Get the message and ensure it's ok
Message message1 = (Message) collector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection 1 never received the message", message1);
MultipleRecipientInfo info = MultipleRecipientManager.getMultipleRecipientInfo(message1);
assertNotNull("Message 1 does not contain MultipleRecipientInfo", info);
assertTrue("Message 1 should be not 'replyable'", info.shouldNotReply());
assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
// Prepare and send the reply
Message reply1 = new Message();
reply1.setBody("This is my reply");
try {
MultipleRecipientManager.reply(getConnection(1), message1, reply1);
fail("It was possible to send a reply to a not replyable message");
} catch (XMPPException e) {
// Exception was expected since replying was not allowed
}
// Check that connection2 recevied 1 messages
message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection2 didn't receive the 1 message", message1);
message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNull("XMPPConnection2 received 2 messages", message1);
// Check that connection3 recevied only 1 message (was BCC in the first message)
message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNotNull("XMPPConnection3 didn't receive the 1 message", message1);
message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
assertNull("XMPPConnection2 received 2 messages", message1);
collector1.cancel();
collector2.cancel();
collector3.cancel();
}
Aggregations