Search in sources :

Example 6 with MessageTypeFilter

use of org.jivesoftware.smack.filter.MessageTypeFilter in project Smack by igniterealtime.

the class MessageTest method testOfflineStorageWithNegativePriority.

/**
 * User0 is connected from 1 resource with a negative priority presence. User1
 * sends a message to the bare JID of User0. Messages should be stored offline.
 * User0 then changes the priority presence to a positive value. Check that
 * offline messages were delivered to the user.
 *
 * @throws Exception if an error occurs.
 */
public void testOfflineStorageWithNegativePriority() throws Exception {
    // Set this connection with negative priority
    Presence presence = new Presence(Presence.Type.available);
    presence.setMode(Presence.Mode.available);
    presence.setPriority(-1);
    getConnection(0).sendStanza(presence);
    // Let the server process the change in presences
    Thread.sleep(200);
    // User0 listen for incoming traffic
    StanzaCollector collector = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    // User1 sends a message to the bare JID of User0
    Chat chat = getConnection(1).getChatManager().createChat(getBareJID(0), null);
    chat.sendMessage("Test 1");
    chat.sendMessage("Test 2");
    // Check that messages were sent to resource with highest priority
    Message message = (Message) collector.nextResult(2000);
    assertNull("Messages were not stored offline", message);
    // Set this connection with positive priority
    presence = new Presence(Presence.Type.available);
    presence.setMode(Presence.Mode.available);
    presence.setPriority(1);
    getConnection(0).sendStanza(presence);
    // Let the server process the change in presences
    Thread.sleep(200);
    message = (Message) collector.nextResult(2000);
    assertNotNull("Offline messages were not delivered", message);
    assertEquals("Test 1", message.getBody());
    message = (Message) collector.nextResult(1000);
    assertNotNull(message);
    assertEquals("Test 2", message.getBody());
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence)

Example 7 with MessageTypeFilter

use of org.jivesoftware.smack.filter.MessageTypeFilter in project Smack by igniterealtime.

the class MessageTest method testDirectPresence.

/**
 * Will a user recieve a message from another after only sending the user a directed presence,
 * or will Wildfire intercept for offline storage?
 *
 * User1 becomes lines. User0 never sent an available presence to the server but
 * instead sent one to User1. User1 sends a message to User0. Should User0 get the
 * message?
 */
public void testDirectPresence() {
    getConnection(1).sendStanza(new Presence(Presence.Type.available));
    Presence presence = new Presence(Presence.Type.available);
    presence.setTo(getBareJID(1));
    getConnection(0).sendStanza(presence);
    StanzaCollector collector = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    try {
        getConnection(1).getChatManager().createChat(getBareJID(0), null).sendMessage("Test 1");
    } catch (XMPPException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    Message message = (Message) collector.nextResult(2500);
    assertNotNull("Message not recieved from remote user", message);
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence)

Example 8 with MessageTypeFilter

use of org.jivesoftware.smack.filter.MessageTypeFilter 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 9 with MessageTypeFilter

use of org.jivesoftware.smack.filter.MessageTypeFilter in project Smack by igniterealtime.

the class MultipleRecipientManagerTest method testReplying.

/**
 * Ensures that replying to packets is ok.
 */
public void testReplying() throws XMPPException {
    StanzaCollector collector0 = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    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);
    // 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);
    assertFalse("Message 1 should be '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");
    MultipleRecipientManager.reply(getConnection(1), message1, reply1);
    // Get the reply and ensure it's ok
    reply1 = (Message) collector0.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection 0 never received the reply", reply1);
    info = MultipleRecipientManager.getMultipleRecipientInfo(reply1);
    assertNotNull("Replied message does not contain MultipleRecipientInfo", info);
    assertFalse("Replied message should be 'replyable'", info.shouldNotReply());
    assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
    assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
    // Send a reply to the reply
    Message reply2 = new Message();
    reply2.setBody("This is my reply to your reply");
    reply2.setFrom(getBareJID(0));
    MultipleRecipientManager.reply(getConnection(0), reply1, reply2);
    // Get the reply and ensure it's ok
    reply2 = (Message) collector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection 1 never received the reply", reply2);
    info = MultipleRecipientManager.getMultipleRecipientInfo(reply2);
    assertNotNull("Replied message does not contain MultipleRecipientInfo", info);
    assertFalse("Replied message should be 'replyable'", info.shouldNotReply());
    assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
    assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
    // Check that connection2 recevied 3 messages
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection2 didn't receive the 1 message", message1);
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection2 didn't receive the 2 message", message1);
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection2 didn't receive the 3 message", message1);
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNull("XMPPConnection2 received 4 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);
    collector0.cancel();
    collector1.cancel();
    collector2.cancel();
    collector3.cancel();
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 10 with MessageTypeFilter

use of org.jivesoftware.smack.filter.MessageTypeFilter 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

MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)14 Message (org.jivesoftware.smack.packet.Message)13 Presence (org.jivesoftware.smack.packet.Presence)11 StanzaCollector (org.jivesoftware.smack.StanzaCollector)5 ArrayList (java.util.ArrayList)3 XMPPException (org.jivesoftware.smack.XMPPException)3 Chat (org.jivesoftware.smack.Chat)2 AndFilter (org.jivesoftware.smack.filter.AndFilter)2 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)2 Packet (org.jivesoftware.smack.packet.Packet)2 OfflineMessageInfo (org.jivesoftware.smackx.packet.OfflineMessageInfo)2 List (java.util.List)1 PacketListener (org.jivesoftware.smack.PacketListener)1 SmackException (org.jivesoftware.smack.SmackException)1 FromMatchesFilter (org.jivesoftware.smack.filter.FromMatchesFilter)1 OrFilter (org.jivesoftware.smack.filter.OrFilter)1 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)1 DiscussionHistory (org.jivesoftware.smackx.muc.DiscussionHistory)1 MultiUserChat (org.jivesoftware.smackx.muc.MultiUserChat)1 MUCInitialPresence (org.jivesoftware.smackx.packet.MUCInitialPresence)1