Search in sources :

Example 1 with ThreadFilter

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

the class XHTMLManagerTest method testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage.

/**
    * High 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 an XHTMLText to send with the message
    XHTMLText xhtmlText = new XHTMLText(null, null);
    xhtmlText.appendOpenParagraphTag("font-size:large");
    xhtmlText.append("Hey John, this is my new ");
    xhtmlText.appendOpenSpanTag("color:green");
    xhtmlText.append("green");
    xhtmlText.appendCloseSpanTag();
    xhtmlText.appendOpenEmTag();
    xhtmlText.append("!!!!");
    xhtmlText.appendCloseEmTag();
    xhtmlText.appendCloseParagraphTag();
    // Add the XHTML text to the message
    XHTMLManager.addBody(msg, xhtmlText.toString());
    // 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;
    assertTrue("The received message is not an XHTML Message", XHTMLManager.isXHTMLMessage(message));
    try {
        assertTrue("Message without XHTML bodies", XHTMLManager.getBodies(message).hasNext());
        for (Iterator<String> it = XHTMLManager.getBodies(message); it.hasNext(); ) {
            String body = it.next();
            System.out.println(body);
        }
    } catch (ClassCastException e) {
        fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
    }
    assertNotNull("No reply received", msg);
}
Also used : ThreadFilter(org.jivesoftware.smack.filter.ThreadFilter)

Example 2 with ThreadFilter

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

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

the class XHTMLManagerTest 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()));
    // 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 XHTMLText to send with the message (in Spanish)
    XHTMLText xhtmlText = new XHTMLText(null, "es-ES");
    xhtmlText.appendOpenHeaderTag(1, null);
    xhtmlText.append("impresionante!");
    xhtmlText.appendCloseHeaderTag(1);
    xhtmlText.appendOpenParagraphTag(null);
    xhtmlText.append("Como Emerson dijo una vez:");
    xhtmlText.appendCloseParagraphTag();
    xhtmlText.appendOpenBlockQuoteTag(null);
    xhtmlText.appendOpenParagraphTag(null);
    xhtmlText.append("Una consistencia rid&#237;cula es el espantajo de mentes peque&#241;as.");
    xhtmlText.appendCloseParagraphTag();
    xhtmlText.appendCloseBlockQuoteTag();
    // Add the XHTML text to the message
    XHTMLManager.addBody(msg, xhtmlText.toString());
    // Create an XHTMLText to send with the message (in English)
    xhtmlText = new XHTMLText(null, "en-US");
    xhtmlText.appendOpenHeaderTag(1, null);
    xhtmlText.append("awesome!");
    xhtmlText.appendCloseHeaderTag(1);
    xhtmlText.appendOpenParagraphTag(null);
    xhtmlText.append("As Emerson once said:");
    xhtmlText.appendCloseParagraphTag();
    xhtmlText.appendOpenBlockQuoteTag(null);
    xhtmlText.appendOpenParagraphTag(null);
    xhtmlText.append("A foolish consistency is the hobgoblin of little minds.");
    xhtmlText.appendCloseParagraphTag();
    xhtmlText.appendCloseBlockQuoteTag();
    // Add the XHTML text to the message
    XHTMLManager.addBody(msg, xhtmlText.toString());
    // User1 sends the message that contains the XHTML to user2
    try {
        bodiesSent = 2;
        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;
    assertTrue("The received message is not an XHTML Message", XHTMLManager.isXHTMLMessage(message));
    try {
        assertTrue("Message without XHTML bodies", XHTMLManager.getBodies(message).hasNext());
        for (Iterator<String> it = XHTMLManager.getBodies(message); it.hasNext(); ) {
            received++;
            String body = it.next();
            System.out.println(body);
        }
        bodiesReceived = received;
    } catch (ClassCastException e) {
        fail("ClassCastException - Most probable cause is that smack providers" + "is misconfigured");
    }
    assertEquals("Number of sent and received XHTMP bodies does not match", bodiesSent, bodiesReceived);
}
Also used : ThreadFilter(org.jivesoftware.smack.filter.ThreadFilter)

Example 4 with ThreadFilter

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

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

the class FormTest method testFilloutForm.

/**
     * 1. Create a form to fill out and send it to the other user
     * 2. Retrieve the form to fill out, complete it and return it to the requestor
     * 3. Retrieve the completed form and check that everything is OK
     *
     * @throws InterruptedException 
     * @throws NotConnectedException 
     */
@SuppressWarnings("deprecation")
@SmackIntegrationTest
public void testFilloutForm() throws NotConnectedException, InterruptedException {
    Form formToSend = new Form(DataForm.Type.form);
    formToSend.setInstructions("Fill out this form to report your case.\nThe case will be created automatically.");
    formToSend.setTitle("Case configurations");
    // Add a hidden variable
    FormField field = new FormField("hidden_var");
    field.setType(FormField.Type.hidden);
    field.addValue("Some value for the hidden variable");
    formToSend.addField(field);
    // Add a fixed variable
    field = new FormField();
    field.addValue("Section 1: Case description");
    formToSend.addField(field);
    // Add a text-single variable
    field = new FormField("name");
    field.setLabel("Enter a name for the case");
    field.setType(FormField.Type.text_single);
    formToSend.addField(field);
    // Add a text-multi variable
    field = new FormField("description");
    field.setLabel("Enter a description");
    field.setType(FormField.Type.text_multi);
    formToSend.addField(field);
    // Add a boolean variable
    field = new FormField("time");
    field.setLabel("Is this your first case?");
    field.setType(FormField.Type.bool);
    formToSend.addField(field);
    // Add a text variable where an int value is expected
    field = new FormField("age");
    field.setLabel("How old are you?");
    field.setType(FormField.Type.text_single);
    formToSend.addField(field);
    // Create the chats between the two participants
    org.jivesoftware.smack.chat.Chat chat = org.jivesoftware.smack.chat.ChatManager.getInstanceFor(conOne).createChat(conTwo.getUser(), null);
    StanzaCollector collector = conOne.createStanzaCollector(new ThreadFilter(chat.getThreadID()));
    StanzaCollector collector2 = conTwo.createStanzaCollector(new ThreadFilter(chat.getThreadID()));
    Message msg = new Message();
    msg.setBody("To enter a case please fill out this form and send it back to me");
    msg.addExtension(formToSend.getDataFormToSend());
    try {
        // Send the message with the form to fill out
        chat.sendMessage(msg);
        // Get the message with the form to fill out
        Message msg2 = (Message) collector2.nextResult();
        assertNotNull("Messge not found", msg2);
        // Retrieve the form to fill out
        Form formToRespond = Form.getFormFrom(msg2);
        assertNotNull(formToRespond);
        assertNotNull(formToRespond.getField("name"));
        assertNotNull(formToRespond.getField("description"));
        // Obtain the form to send with the replies
        Form completedForm = formToRespond.createAnswerForm();
        assertNotNull(completedForm.getField("hidden_var"));
        // Check that a field of type String does not accept booleans
        try {
            completedForm.setAnswer("name", true);
            fail("A boolean value was set to a field of type String");
        } catch (IllegalArgumentException e) {
        // This exception is expected.
        }
        completedForm.setAnswer("name", "Credit card number invalid");
        completedForm.setAnswer("description", "The ATM says that my credit card number is invalid. What's going on?");
        completedForm.setAnswer("time", true);
        completedForm.setAnswer("age", 20);
        // Create a new message to send with the completed form
        msg2 = new Message();
        msg2.setTo(conOne.getUser().asBareJid());
        msg2.setThread(msg.getThread());
        msg2.setType(Message.Type.chat);
        msg2.setBody("To enter a case please fill out this form and send it back to me");
        // Add the completed form to the message
        msg2.addExtension(completedForm.getDataFormToSend());
        // Send the message with the completed form
        conTwo.sendStanza(msg2);
        // Get the message with the completed form
        Message msg3 = (Message) collector.nextResult();
        assertNotNull("Messge not found", msg3);
        // Retrieve the completed form
        completedForm = Form.getFormFrom(msg3);
        assertNotNull(completedForm);
        assertNotNull(completedForm.getField("name"));
        assertNotNull(completedForm.getField("description"));
        assertEquals(completedForm.getField("name").getValues().get(0), "Credit card number invalid");
        assertNotNull(completedForm.getField("time"));
        assertNotNull(completedForm.getField("age"));
        assertEquals("The age is bad", "20", completedForm.getField("age").getValues().get(0));
    } finally {
        collector.cancel();
        collector2.cancel();
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) ThreadFilter(org.jivesoftware.smack.filter.ThreadFilter) StanzaCollector(org.jivesoftware.smack.StanzaCollector) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.SmackIntegrationTest)

Aggregations

ThreadFilter (org.jivesoftware.smack.filter.ThreadFilter)8 Message (org.jivesoftware.smack.packet.Message)4 StanzaCollector (org.jivesoftware.smack.StanzaCollector)3 AbstractSmackIntegrationTest (org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)2 SmackIntegrationTest (org.igniterealtime.smack.inttest.SmackIntegrationTest)2 Chat (org.jivesoftware.smack.Chat)2 Packet (org.jivesoftware.smack.packet.Packet)2 Date (java.util.Date)1 PacketListener (org.jivesoftware.smack.PacketListener)1 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)1 StanzaExtensionFilter (org.jivesoftware.smack.filter.StanzaExtensionFilter)1 DataForm (org.jivesoftware.smackx.xdata.packet.DataForm)1