Search in sources :

Example 26 with StanzaCollector

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

the class MamManager method queryArchive.

private MamQueryResult queryArchive(MamQueryIQ mamQueryIq) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotLoggedInException {
    final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
    MamFinIQ mamFinIQ = null;
    StanzaCollector mamFinIQCollector = connection.createStanzaCollector(new IQReplyFilter(mamQueryIq, connection));
    StanzaCollector.Configuration resultCollectorConfiguration = StanzaCollector.newConfiguration().setStanzaFilter(new MamResultFilter(mamQueryIq)).setCollectorToReset(mamFinIQCollector);
    StanzaCollector resultCollector = connection.createStanzaCollector(resultCollectorConfiguration);
    try {
        connection.sendStanza(mamQueryIq);
        mamFinIQ = mamFinIQCollector.nextResultOrThrow();
    } finally {
        mamFinIQCollector.cancel();
        resultCollector.cancel();
    }
    List<Forwarded> forwardedMessages = new ArrayList<>(resultCollector.getCollectedCount());
    for (Message resultMessage = resultCollector.pollResult(); resultMessage != null; resultMessage = resultCollector.pollResult()) {
        MamElements.MamResultExtension mamResultExtension = MamElements.MamResultExtension.from(resultMessage);
        forwardedMessages.add(mamResultExtension.getForwarded());
    }
    return new MamQueryResult(forwardedMessages, mamFinIQ, mamQueryIq.getNode(), DataForm.from(mamQueryIq));
}
Also used : Message(org.jivesoftware.smack.packet.Message) IQReplyFilter(org.jivesoftware.smack.filter.IQReplyFilter) ArrayList(java.util.ArrayList) XMPPConnection(org.jivesoftware.smack.XMPPConnection) MamElements(org.jivesoftware.smackx.mam.element.MamElements) MamFinIQ(org.jivesoftware.smackx.mam.element.MamFinIQ) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded) MamResultFilter(org.jivesoftware.smackx.mam.filter.MamResultFilter) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 27 with StanzaCollector

use of org.jivesoftware.smack.StanzaCollector 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)

Example 28 with StanzaCollector

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

the class AbstractSmackIntTest method performActionAndWaitUntilStanzaReceived.

protected void performActionAndWaitUntilStanzaReceived(Runnable action, XMPPConnection connection, StanzaFilter filter) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    StanzaCollector.Configuration configuration = StanzaCollector.newConfiguration().setStanzaFilter(filter).setSize(1);
    StanzaCollector collector = connection.createStanzaCollector(configuration);
    try {
        action.run();
        collector.nextResultOrThrow(timeout);
    } finally {
        collector.cancel();
    }
}
Also used : StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Aggregations

StanzaCollector (org.jivesoftware.smack.StanzaCollector)28 Message (org.jivesoftware.smack.packet.Message)14 AndFilter (org.jivesoftware.smack.filter.AndFilter)7 Presence (org.jivesoftware.smack.packet.Presence)7 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)6 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)6 ArrayList (java.util.ArrayList)5 Chat (org.jivesoftware.smack.Chat)5 XMPPConnection (org.jivesoftware.smack.XMPPConnection)5 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)5 Packet (org.jivesoftware.smack.packet.Packet)5 XMPPException (org.jivesoftware.smack.XMPPException)4 ThreadFilter (org.jivesoftware.smack.filter.ThreadFilter)3 Stanza (org.jivesoftware.smack.packet.Stanza)3 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)2 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)2 IQ (org.jivesoftware.smack.packet.IQ)2 StandardExtensionElement (org.jivesoftware.smack.packet.StandardExtensionElement)2 OfflineMessageRequest (org.jivesoftware.smackx.offline.packet.OfflineMessageRequest)2 OfflineMessageInfo (org.jivesoftware.smackx.packet.OfflineMessageInfo)2