Search in sources :

Example 6 with Chat

use of org.jivesoftware.smack.Chat in project Smack by igniterealtime.

the class MultiUserChatTest method testPrivateChat.

public void testPrivateChat() {
    try {
        // User2 joins the new room
        MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
        muc2.join("testbot2");
        getConnection(0).getChatManager().addChatListener(new ChatManagerListener() {

            public void chatCreated(Chat chat2, boolean createdLocally) {
                assertEquals("Sender of chat is incorrect", room + "/testbot2", chat2.getParticipant());
                try {
                    chat2.sendMessage("ACK");
                } catch (XMPPException e) {
                    fail(e.getMessage());
                }
            }
        });
        // Start a private chat with another participant            
        Chat chat = muc2.createPrivateChat(room + "/testbot", null);
        StanzaCollector collector = chat.createCollector();
        chat.sendMessage("Hello there");
        Message response = (Message) collector.nextResult(2000);
        assertNotNull("No response", response);
        assertEquals("Sender of response is incorrect", room + "/testbot", response.getFrom());
        assertEquals("Body of response is incorrect", "ACK", response.getBody());
        // User2 leaves the room
        muc2.leave();
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) Chat(org.jivesoftware.smack.Chat) XMPPException(org.jivesoftware.smack.XMPPException) StanzaCollector(org.jivesoftware.smack.StanzaCollector) ChatManagerListener(org.jivesoftware.smack.ChatManagerListener) XMPPException(org.jivesoftware.smack.XMPPException)

Example 7 with Chat

use of org.jivesoftware.smack.Chat in project Smack by igniterealtime.

the class XHTMLExtensionTest method testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage.

/**
     * Low level API test.
     * 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 testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
    // 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()));
    // User1 creates a message to send to user2
    Message msg = new Message();
    msg.setSubject("Any subject you want");
    msg.setBody("Hey John, this is my new green!!!!");
    // Create a XHTMLExtension Package and add it to the message
    XHTMLExtension xhtmlExtension = new XHTMLExtension();
    xhtmlExtension.addBody("<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
    msg.addExtension(xhtmlExtension);
    // User1 sends the message that contains the XHTML to user2
    try {
        chat1.sendMessage(msg);
    } catch (Exception e) {
        fail("An error occured sending the message with XHTML");
    }
    Packet packet = chat2.nextResult(2000);
    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(); ) {
            String body = it.next();
            System.out.println(body);
        }
    } catch (ClassCastException e) {
        fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
    }
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) Message(org.jivesoftware.smack.packet.Message) ThreadFilter(org.jivesoftware.smack.filter.ThreadFilter) Chat(org.jivesoftware.smack.Chat) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 8 with Chat

use of org.jivesoftware.smack.Chat in project Smack by igniterealtime.

the class XHTMLExtensionTest method testSendSimpleXHTMLMessage.

/**
     * Low level API test.
     * This is a simple test to use with an XMPP client and check if the client receives the message
     * 1. User_1 will send a message with formatted text (XHTML) to user_2
     */
public void testSendSimpleXHTMLMessage() {
    // User1 creates a chat with user2
    Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
    // User1 creates a message to send to user2
    Message msg = new Message();
    msg.setSubject("Any subject you want");
    msg.setBody("Hey John, this is my new green!!!!");
    // Create a XHTMLExtension Package and add it to the message
    XHTMLExtension xhtmlExtension = new XHTMLExtension();
    xhtmlExtension.addBody("<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
    msg.addExtension(xhtmlExtension);
    // User1 sends the message that contains the XHTML to user2
    try {
        chat1.sendMessage(msg);
        Thread.sleep(200);
    } catch (Exception e) {
        fail("An error occured sending the message with XHTML");
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) Chat(org.jivesoftware.smack.Chat)

Aggregations

Chat (org.jivesoftware.smack.Chat)8 Message (org.jivesoftware.smack.packet.Message)7 StanzaCollector (org.jivesoftware.smack.StanzaCollector)5 XMPPException (org.jivesoftware.smack.XMPPException)5 ArrayList (java.util.ArrayList)2 ChatManager (org.jivesoftware.smack.ChatManager)2 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)2 ThreadFilter (org.jivesoftware.smack.filter.ThreadFilter)2 Packet (org.jivesoftware.smack.packet.Packet)2 Presence (org.jivesoftware.smack.packet.Presence)2 OfflineMessageInfo (org.jivesoftware.smackx.packet.OfflineMessageInfo)2 IOException (java.io.IOException)1 RuntimeExchangeException (org.apache.camel.RuntimeExchangeException)1 ChatManagerListener (org.jivesoftware.smack.ChatManagerListener)1 PacketListener (org.jivesoftware.smack.PacketListener)1 SmackException (org.jivesoftware.smack.SmackException)1 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)1 StanzaExtensionFilter (org.jivesoftware.smack.filter.StanzaExtensionFilter)1