use of org.jivesoftware.smackx.packet.OfflineMessageInfo in project Smack by igniterealtime.
the class OfflineMessageManagerTest method testReadAndDelete.
/**
* While user2 is connected but unavailable, user1 sends 2 messages to user1. User2 then
* performs some "Flexible Offline Message Retrieval" checking the number of offline messages,
* retriving the headers, then the real messages of the headers and finally removing the
* loaded messages.
*/
public void testReadAndDelete() {
// Make user2 unavailable
getConnection(1).sendStanza(new Presence(Presence.Type.unavailable));
try {
Thread.sleep(500);
// User1 sends some messages to User2 which is not available at the moment
Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
chat.sendMessage("Test 1");
chat.sendMessage("Test 2");
Thread.sleep(500);
// User2 checks the number of offline messages
OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1));
assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
// Check the message headers
Iterator<OfflineMessageHeader> headers = offlineManager.getHeaders();
assertTrue("No message header was found", headers.hasNext());
List<String> stamps = new ArrayList<String>();
while (headers.hasNext()) {
OfflineMessageHeader header = headers.next();
assertEquals("Incorrect sender", getFullJID(0), header.getJid());
assertNotNull("No stamp was found in the message header", header.getStamp());
stamps.add(header.getStamp());
}
assertEquals("Wrong number of headers", 2, stamps.size());
// Get the offline messages
Iterator<Message> messages = offlineManager.getMessages(stamps);
assertTrue("No message was found", messages.hasNext());
stamps = new ArrayList<String>();
while (messages.hasNext()) {
Message message = messages.next();
OfflineMessageInfo info = (OfflineMessageInfo) message.getExtension("offline", "http://jabber.org/protocol/offline");
assertNotNull("No offline information was included in the offline message", info);
assertNotNull("No stamp was found in the message header", info.getNode());
stamps.add(info.getNode());
}
assertEquals("Wrong number of messages", 2, stamps.size());
// Check that the offline messages have not been deleted
assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
// User2 becomes available again
StanzaCollector collector = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
getConnection(1).sendStanza(new Presence(Presence.Type.available));
// Check that no offline messages was sent to the user
Message message = (Message) collector.nextResult(2500);
assertNull("An offline message was sent from the server", message);
// Delete the retrieved offline messages
offlineManager.deleteMessages(stamps);
// Check that there are no offline message for this user
assertEquals("Wrong number of offline messages", 0, offlineManager.getMessageCount());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.jivesoftware.smackx.packet.OfflineMessageInfo in project ecf by eclipse.
the class OfflineMessageManager method getMessages.
/**
* Returns an Iterator with 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 an Iterator with the offline <tt>Messages</tt> that were received as part of
* this request.
* @throws XMPPException If the user is not allowed to make this request or the server does
* not support offline message retrieval.
*/
public Iterator<Message> getMessages(final List<String> nodes) throws XMPPException {
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 packets looking for an answer from the server.
PacketFilter responseFilter = new PacketIDFilter(request.getPacketID());
PacketCollector response = connection.createPacketCollector(responseFilter);
// Filter offline messages that were requested by this request
PacketFilter messageFilter = new AndFilter(packetFilter, new PacketFilter() {
public boolean accept(Packet packet) {
OfflineMessageInfo info = (OfflineMessageInfo) packet.getExtension("offline", namespace);
return nodes.contains(info.getNode());
}
});
PacketCollector messageCollector = connection.createPacketCollector(messageFilter);
// Send the retrieval request to the server.
connection.sendPacket(request);
// Wait up to a certain number of seconds for a reply.
IQ answer = (IQ) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
response.cancel();
if (answer == null) {
throw new XMPPException("No response from server.");
} else if (answer.getError() != null) {
throw new XMPPException(answer.getError());
}
// Collect the received offline messages
Message message = (Message) messageCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
while (message != null) {
messages.add(message);
message = (Message) messageCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
}
// Stop queuing offline messages
messageCollector.cancel();
return messages.iterator();
}
use of org.jivesoftware.smackx.packet.OfflineMessageInfo in project Smack by igniterealtime.
the class OfflineMessageManagerTest method testFetchAndPurge.
/**
* While user2 is connected but unavailable, user1 sends 2 messages to user1. User2 then
* performs some "Flexible Offline Message Retrieval" by fetching all the offline messages
* and then removing all the offline messages.
*/
public void testFetchAndPurge() {
// Make user2 unavailable
getConnection(1).sendStanza(new Presence(Presence.Type.unavailable));
try {
Thread.sleep(500);
// User1 sends some messages to User2 which is not available at the moment
Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
chat.sendMessage("Test 1");
chat.sendMessage("Test 2");
Thread.sleep(500);
// User2 checks the number of offline messages
OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1));
assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
// Get all offline messages
Iterator<Message> messages = offlineManager.getMessages();
assertTrue("No message was found", messages.hasNext());
List<String> stamps = new ArrayList<String>();
while (messages.hasNext()) {
Message message = messages.next();
OfflineMessageInfo info = (OfflineMessageInfo) message.getExtension("offline", "http://jabber.org/protocol/offline");
assertNotNull("No offline information was included in the offline message", info);
assertNotNull("No stamp was found in the message header", info.getNode());
stamps.add(info.getNode());
}
assertEquals("Wrong number of messages", 2, stamps.size());
// Check that the offline messages have not been deleted
assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
// User2 becomes available again
StanzaCollector collector = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
getConnection(1).sendStanza(new Presence(Presence.Type.available));
// Check that no offline messages was sent to the user
Message message = (Message) collector.nextResult(2500);
assertNull("An offline message was sent from the server", message);
// Delete all offline messages
offlineManager.deleteMessages();
// Check that there are no offline message for this user
assertEquals("Wrong number of offline messages", 0, offlineManager.getMessageCount());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations