Search in sources :

Example 36 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class FromMatchesFilterTest method bareCompareMatchingBaseJid.

@Test
public void bareCompareMatchingBaseJid() {
    FromMatchesFilter filter = FromMatchesFilter.createBare(BASE_JID1);
    Stanza packet = new Message();
    packet.setFrom(BASE_JID1);
    assertTrue(filter.accept(packet));
    packet.setFrom(FULL_JID1_R1);
    assertTrue(filter.accept(packet));
    packet.setFrom(FULL_JID1_R2);
    assertTrue(filter.accept(packet));
    packet.setFrom(BASE_JID2);
    assertFalse(filter.accept(packet));
    packet.setFrom(FULL_JID2);
    assertFalse(filter.accept(packet));
    packet.setFrom(BASE_JID3);
    assertFalse(filter.accept(packet));
}
Also used : Message(org.jivesoftware.smack.packet.Message) Stanza(org.jivesoftware.smack.packet.Stanza) Test(org.junit.Test)

Example 37 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class MessageEventManager method sendDisplayedNotification.

/**
     * Sends the notification that the message was displayed to the sender of the original message.
     * 
     * @param to the recipient of the notification.
     * @param packetID the id of the message to send.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public void sendDisplayedNotification(Jid to, String packetID) throws NotConnectedException, InterruptedException {
    // Create the message to send
    Message msg = new Message(to);
    // Create a MessageEvent Package and add it to the message
    MessageEvent messageEvent = new MessageEvent();
    messageEvent.setDisplayed(true);
    messageEvent.setStanzaId(packetID);
    msg.addExtension(messageEvent);
    // Send the packet
    connection().sendStanza(msg);
}
Also used : Message(org.jivesoftware.smack.packet.Message) MessageEvent(org.jivesoftware.smackx.xevent.packet.MessageEvent)

Example 38 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class RosterExchangeManager method send.

/**
     * Sends a roster entry to userID.
     * 
     * @param rosterEntry the roster entry to send
     * @param targetUserID the user that will receive the roster entries
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public void send(RosterEntry rosterEntry, Jid targetUserID) throws NotConnectedException, InterruptedException {
    // Create a new message to send the roster
    Message msg = new Message(targetUserID);
    // Create a RosterExchange Package and add it to the message
    RosterExchange rosterExchange = new RosterExchange();
    rosterExchange.addRosterEntry(rosterEntry);
    msg.addExtension(rosterExchange);
    XMPPConnection connection = weakRefConnection.get();
    // Send the message that contains the roster
    connection.sendStanza(msg);
}
Also used : RosterExchange(org.jivesoftware.smackx.xroster.packet.RosterExchange) Message(org.jivesoftware.smack.packet.Message) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Example 39 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class MultiUserChatTest method testInvitationWithMessage.

public void testInvitationWithMessage() {
    final String[] answer = new String[2];
    try {
        // User2 joins the new room
        MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
        muc2.join("testbot2");
        // User3 is listening to MUC invitations
        MultiUserChat.addInvitationListener(getConnection(2), new InvitationListener() {

            public void invitationReceived(XMPPConnection conn, String room, String inviter, String reason, String password, Message message) {
                // Indicate that the invitation was received
                answer[0] = reason;
                XHTMLExtension extension = (XHTMLExtension) message.getExtension("html", "http://jabber.org/protocol/xhtml-im");
                assertNotNull("An extension was not found in the invitation", extension);
                answer[1] = (String) extension.getBodies().next();
            }
        });
        // User2 invites user3 to join to the room
        Message msg = new Message();
        XHTMLExtension xhtmlExtension = new XHTMLExtension();
        xhtmlExtension.addBody("<body>Meet me in this excellent room</body>");
        msg.addExtension(xhtmlExtension);
        muc2.invite(msg, getFullJID(2), "Meet me in this excellent room");
        Thread.sleep(350);
        assertEquals("Invitation was not received", "Meet me in this excellent room", answer[0]);
        assertEquals("Rejection was not received", "<body>Meet me in this excellent room</body>", answer[1]);
        // User2 leaves the room
        muc2.leave();
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XHTMLExtension(org.jivesoftware.smackx.packet.XHTMLExtension) XMPPException(org.jivesoftware.smack.XMPPException)

Example 40 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class XHTMLExtensionTest method testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage.

/**
     * Low level API test. Test a message with two XHTML bodies and several XHTML tags.
     * 1. User_1 will send a message with XHTML to user_2
     * 2. User_2 will receive the message and iterate over the XHTML bodies to check if everything
     * is fine
     * 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
     * something is wrong
     */
public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
    // Create a chat for each connection
    Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
    final StanzaCollector chat2 = getConnection(1).createStanzaCollector(new ThreadFilter(chat1.getThreadID()));
    // Create a Listener that listens for Messages with the extension 
    //"http://jabber.org/protocol/xhtml-im"
    // This listener will listen on the conn2 and answer an ACK if everything is ok
    PacketFilter packetFilter = new StanzaExtensionFilter("html", "http://jabber.org/protocol/xhtml-im");
    PacketListener packetListener = new PacketListener() {

        @Override
        public void processStanza(Packet packet) {
        }
    };
    getConnection(1).addAsyncPacketListener(packetListener, packetFilter);
    // User1 creates a message to send to user2
    Message msg = new Message();
    msg.setSubject("Any subject you want");
    msg.setBody("awesome! As Emerson once said: A foolish consistency is the hobgoblin of little minds.");
    // Create an XHTMLExtension and add it to the message
    XHTMLExtension xhtmlExtension = new XHTMLExtension();
    xhtmlExtension.addBody("<body xml:lang=\"es-ES\"><h1>impresionante!</h1><p>Como Emerson dijo una vez:</p><blockquote><p>Una consistencia ridicula es el espantajo de mentes pequenas.</p></blockquote></body>");
    xhtmlExtension.addBody("<body xml:lang=\"en-US\"><h1>awesome!</h1><p>As Emerson once said:</p><blockquote><p>A foolish consistency is the hobgoblin of little minds.</p></blockquote></body>");
    msg.addExtension(xhtmlExtension);
    // User1 sends the message that contains the XHTML to user2
    try {
        bodiesSent = xhtmlExtension.getBodiesCount();
        bodiesReceived = 0;
        chat1.sendMessage(msg);
    } catch (Exception e) {
        fail("An error occured sending the message with XHTML");
    }
    Packet packet = chat2.nextResult(2000);
    int received = 0;
    Message message = (Message) packet;
    assertNotNull("Body is null", message.getBody());
    try {
        xhtmlExtension = (XHTMLExtension) message.getExtension("html", "http://jabber.org/protocol/xhtml-im");
        assertNotNull("Message without extension \"http://jabber.org/protocol/xhtml-im\"", xhtmlExtension);
        assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0);
        for (Iterator<String> it = xhtmlExtension.getBodies(); it.hasNext(); ) {
            received++;
            System.out.println(it.next());
        }
        bodiesReceived = received;
    } catch (ClassCastException e) {
        fail("ClassCastException - Most probable cause is that smack providers is " + "misconfigured");
    }
    // Wait half second so that the complete test can run
    assertEquals("Number of sent and received XHTMP bodies does not match", bodiesSent, bodiesReceived);
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) Message(org.jivesoftware.smack.packet.Message) StanzaExtensionFilter(org.jivesoftware.smack.filter.StanzaExtensionFilter) PacketListener(org.jivesoftware.smack.PacketListener) ThreadFilter(org.jivesoftware.smack.filter.ThreadFilter) Chat(org.jivesoftware.smack.Chat) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Aggregations

Message (org.jivesoftware.smack.packet.Message)166 Test (org.junit.Test)57 Presence (org.jivesoftware.smack.packet.Presence)21 XMPPException (org.jivesoftware.smack.XMPPException)15 StanzaCollector (org.jivesoftware.smack.StanzaCollector)14 NetworkException (com.xabber.android.data.NetworkException)13 Stanza (org.jivesoftware.smack.packet.Stanza)13 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)13 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)12 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)12 AccountItem (com.xabber.android.data.account.AccountItem)11 XMPPConnection (org.jivesoftware.smack.XMPPConnection)11 Date (java.util.Date)10 Jid (org.jxmpp.jid.Jid)10 Chat (org.jivesoftware.smack.Chat)9 AccountJid (com.xabber.android.data.entity.AccountJid)7 ArrayList (java.util.ArrayList)7 XmlPullParser (org.xmlpull.v1.XmlPullParser)7 InputStream (java.io.InputStream)6 Forwarded (org.jivesoftware.smackx.forward.packet.Forwarded)6