Search in sources :

Example 26 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 27 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 28 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 29 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)

Example 30 with Message

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

the class LastActivityManagerTest method testOnline.

/**
	 * This is a test to check if a LastActivity request for idle time is
	 * answered and correct.
	 */
public void testOnline() {
    TCPConnection conn0 = getConnection(0);
    TCPConnection conn1 = getConnection(1);
    // Send a message as the last activity action from connection 1 to
    // connection 0
    conn1.sendStanza(new Message(getBareJID(0)));
    // Wait 1 seconds to have some idle time
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
        fail("Thread sleep interrupted");
    }
    LastActivity lastActivity = null;
    try {
        lastActivity = LastActivityManager.getLastActivity(conn0, getFullJID(1));
    } catch (XMPPException e) {
        e.printStackTrace();
        fail("An error occurred requesting the Last Activity");
    }
    // Asserts that the last activity packet was received
    assertNotNull("No last activity packet", lastActivity);
    // Asserts that there is at least a 1 second of idle time
    assertTrue("The last activity idle time is less than expected: " + lastActivity.getIdleTime(), lastActivity.getIdleTime() >= 1);
}
Also used : Message(org.jivesoftware.smack.packet.Message) TCPConnection(org.jivesoftware.smack.TCPConnection) XMPPException(org.jivesoftware.smack.XMPPException) LastActivity(org.jivesoftware.smackx.packet.LastActivity)

Aggregations

Message (org.jivesoftware.smack.packet.Message)142 Test (org.junit.Test)57 Presence (org.jivesoftware.smack.packet.Presence)17 StanzaCollector (org.jivesoftware.smack.StanzaCollector)14 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)12 Stanza (org.jivesoftware.smack.packet.Stanza)12 XMPPException (org.jivesoftware.smack.XMPPException)11 XMPPConnection (org.jivesoftware.smack.XMPPConnection)9 Date (java.util.Date)8 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)8 AccountItem (com.xabber.android.data.account.AccountItem)7 Chat (org.jivesoftware.smack.Chat)7 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)7 XmlPullParser (org.xmlpull.v1.XmlPullParser)7 ArrayList (java.util.ArrayList)6 Jid (org.jxmpp.jid.Jid)6 NetworkException (com.xabber.android.data.NetworkException)5 Forwarded (org.jivesoftware.smackx.forward.packet.Forwarded)5 GeoLocation (org.jivesoftware.smackx.geoloc.packet.GeoLocation)5 IOException (java.io.IOException)4