Search in sources :

Example 1 with OfflineMessageInfo

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());
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) OfflineMessageInfo(org.jivesoftware.smackx.packet.OfflineMessageInfo) ArrayList(java.util.ArrayList) XMPPException(org.jivesoftware.smack.XMPPException) MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Chat(org.jivesoftware.smack.Chat) Presence(org.jivesoftware.smack.packet.Presence) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 2 with OfflineMessageInfo

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();
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) Message(org.jivesoftware.smack.packet.Message) OfflineMessageInfo(org.jivesoftware.smackx.packet.OfflineMessageInfo) ArrayList(java.util.ArrayList) IQ(org.jivesoftware.smack.packet.IQ) OfflineMessageRequest(org.jivesoftware.smackx.packet.OfflineMessageRequest) PacketCollector(org.jivesoftware.smack.PacketCollector) XMPPException(org.jivesoftware.smack.XMPPException)

Example 3 with OfflineMessageInfo

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());
    }
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) OfflineMessageInfo(org.jivesoftware.smackx.packet.OfflineMessageInfo) Chat(org.jivesoftware.smack.Chat) ArrayList(java.util.ArrayList) Presence(org.jivesoftware.smack.packet.Presence) StanzaCollector(org.jivesoftware.smack.StanzaCollector) XMPPException(org.jivesoftware.smack.XMPPException)

Aggregations

ArrayList (java.util.ArrayList)3 XMPPException (org.jivesoftware.smack.XMPPException)3 Message (org.jivesoftware.smack.packet.Message)3 OfflineMessageInfo (org.jivesoftware.smackx.packet.OfflineMessageInfo)3 Chat (org.jivesoftware.smack.Chat)2 StanzaCollector (org.jivesoftware.smack.StanzaCollector)2 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)2 Presence (org.jivesoftware.smack.packet.Presence)2 PacketCollector (org.jivesoftware.smack.PacketCollector)1 IQ (org.jivesoftware.smack.packet.IQ)1 Packet (org.jivesoftware.smack.packet.Packet)1 OfflineMessageRequest (org.jivesoftware.smackx.packet.OfflineMessageRequest)1