Search in sources :

Example 11 with MessageTypeFilter

use of org.jivesoftware.smack.filter.MessageTypeFilter 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();
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) StanzaCollector(org.jivesoftware.smack.StanzaCollector) XMPPException(org.jivesoftware.smack.XMPPException)

Example 12 with MessageTypeFilter

use of org.jivesoftware.smack.filter.MessageTypeFilter 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();
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Packet(org.jivesoftware.smack.packet.Packet) MultipleAddresses(org.jivesoftware.smackx.packet.MultipleAddresses) Message(org.jivesoftware.smack.packet.Message) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 13 with MessageTypeFilter

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

the class MessageTest method testHighestShow.

/**
 * User0 is connected from 2 resources. User0 is available in both resources
 * but with different show values. User1 sends a message to the
 * bare JID of User0. Check that the resource with highest show value will get
 * the messages.
 *
 * @throws Exception if an error occurs.
 */
public void testHighestShow() throws Exception {
    // Create another connection for the same user of connection 1
    ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
    XMPPTCPConnection conn3 = new XMPPConnection(connectionConfiguration);
    conn3.connect();
    conn3.login(getUsername(0), getPassword(0), "Home");
    // Set this connection as highest priority
    Presence presence = new Presence(Presence.Type.available);
    presence.setMode(Presence.Mode.away);
    conn3.sendStanza(presence);
    // Set this connection as highest priority
    presence = new Presence(Presence.Type.available);
    presence.setMode(Presence.Mode.available);
    getConnection(0).sendStanza(presence);
    // Let the server process the change in presences
    Thread.sleep(200);
    // User0 listen in both connected clients
    StanzaCollector collector = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    StanzaCollector coll3 = conn3.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) coll3.nextResult(2000);
    assertNull("Resource with lowest show value got the message", message);
    message = (Message) collector.nextResult(2000);
    assertNotNull(message);
    assertEquals("Test 1", message.getBody());
    message = (Message) collector.nextResult(1000);
    assertNotNull(message);
    assertEquals("Test 2", message.getBody());
    conn3.disconnect();
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence)

Example 14 with MessageTypeFilter

use of org.jivesoftware.smack.filter.MessageTypeFilter in project ecf by eclipse.

the class MultiUserChat method init.

private void init() {
    // Create filters
    messageFilter = new AndFilter(new FromMatchesFilter(room), new MessageTypeFilter(Message.Type.groupchat));
    messageFilter = new AndFilter(messageFilter, new PacketFilter() {

        public boolean accept(Packet packet) {
            Message msg = (Message) packet;
            return msg.getBody() != null;
        }
    });
    presenceFilter = new AndFilter(new FromMatchesFilter(room), new PacketTypeFilter(Presence.class));
    // Create a collector for incoming messages.
    messageCollector = new ConnectionDetachedPacketCollector();
    // Create a listener for subject updates.
    PacketListener subjectListener = new PacketListener() {

        public void processPacket(Packet packet) {
            Message msg = (Message) packet;
            // Update the room subject
            subject = msg.getSubject();
            // Fire event for subject updated listeners
            fireSubjectUpdatedListeners(msg.getSubject(), msg.getFrom());
        }
    };
    // Create a listener for all presence updates.
    PacketListener presenceListener = new PacketListener() {

        public void processPacket(Packet packet) {
            Presence presence = (Presence) packet;
            String from = presence.getFrom();
            String myRoomJID = room + "/" + nickname;
            boolean isUserStatusModification = presence.getFrom().equals(myRoomJID);
            if (presence.getType() == Presence.Type.available) {
                Presence oldPresence = occupantsMap.put(from, presence);
                if (oldPresence != null) {
                    // Get the previous occupant's affiliation & role
                    MUCUser mucExtension = getMUCUserExtension(oldPresence);
                    String oldAffiliation = mucExtension.getItem().getAffiliation();
                    String oldRole = mucExtension.getItem().getRole();
                    // Get the new occupant's affiliation & role
                    mucExtension = getMUCUserExtension(presence);
                    String newAffiliation = mucExtension.getItem().getAffiliation();
                    String newRole = mucExtension.getItem().getRole();
                    // Fire role modification events
                    checkRoleModifications(oldRole, newRole, isUserStatusModification, from);
                    // Fire affiliation modification events
                    checkAffiliationModifications(oldAffiliation, newAffiliation, isUserStatusModification, from);
                } else {
                    // A new occupant has joined the room
                    if (!isUserStatusModification) {
                        List<String> params = new ArrayList<String>();
                        params.add(from);
                        fireParticipantStatusListeners("joined", params);
                    }
                }
            } else if (presence.getType() == Presence.Type.unavailable) {
                occupantsMap.remove(from);
                MUCUser mucUser = getMUCUserExtension(presence);
                if (mucUser != null && mucUser.getStatus() != null) {
                    // Fire events according to the received presence code
                    checkPresenceCode(mucUser.getStatus().getCode(), presence.getFrom().equals(myRoomJID), mucUser, from);
                } else {
                    // An occupant has left the room
                    if (!isUserStatusModification) {
                        List<String> params = new ArrayList<String>();
                        params.add(from);
                        fireParticipantStatusListeners("left", params);
                    }
                }
            }
        }
    };
    // Listens for all messages that include a MUCUser extension and fire the invitation
    // rejection listeners if the message includes an invitation rejection.
    PacketListener declinesListener = new PacketListener() {

        public void processPacket(Packet packet) {
            // Get the MUC User extension
            MUCUser mucUser = getMUCUserExtension(packet);
            // Check if the MUCUser informs that the invitee has declined the invitation
            if (mucUser.getDecline() != null && ((Message) packet).getType() != Message.Type.error) {
                // Fire event for invitation rejection listeners
                fireInvitationRejectionListeners(mucUser.getDecline().getFrom(), mucUser.getDecline().getReason());
            }
        }
    };
    PacketMultiplexListener packetMultiplexor = new PacketMultiplexListener(messageCollector, presenceListener, subjectListener, declinesListener);
    roomListenerMultiplexor = RoomListenerMultiplexor.getRoomMultiplexor(connection);
    roomListenerMultiplexor.addRoom(room, packetMultiplexor);
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) MUCUser(org.jivesoftware.smackx.packet.MUCUser) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) Message(org.jivesoftware.smack.packet.Message) ArrayList(java.util.ArrayList) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) PacketListener(org.jivesoftware.smack.PacketListener) AndFilter(org.jivesoftware.smack.filter.AndFilter) MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) FromMatchesFilter(org.jivesoftware.smack.filter.FromMatchesFilter) MUCInitialPresence(org.jivesoftware.smackx.packet.MUCInitialPresence) Presence(org.jivesoftware.smack.packet.Presence) ArrayList(java.util.ArrayList) List(java.util.List)

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