Search in sources :

Example 1 with MessageBuilder

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

the class PacketParserUtils method parseMessage.

/**
 * Parses a message packet.
 *
 * @param parser the XML parser, positioned at the start of a message packet.
 * @param outerXmlEnvironment the outer XML environment (optional).
 * @return a Message packet.
 * @throws XmlPullParserException if an error in the XML parser occurred.
 * @throws IOException if an I/O error occurred.
 * @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
 */
public static Message parseMessage(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    ParserUtils.assertAtStartTag(parser);
    assert parser.getName().equals(Message.ELEMENT);
    XmlEnvironment messageXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
    final int initialDepth = parser.getDepth();
    MessageBuilder message = parseCommonStanzaAttributes(id -> {
        return StanzaBuilder.buildMessage(id);
    }, parser, outerXmlEnvironment);
    String typeString = parser.getAttributeValue("", "type");
    if (typeString != null) {
        message.ofType(Message.Type.fromString(typeString));
    }
    // in arbitrary sub-elements.
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch(eventType) {
            case START_ELEMENT:
                String elementName = parser.getName();
                String namespace = parser.getNamespace();
                switch(elementName) {
                    case "error":
                        message.setError(parseError(parser, messageXmlEnvironment));
                        break;
                    default:
                        XmlElement extensionElement = parseExtensionElement(elementName, namespace, parser, messageXmlEnvironment);
                        message.addExtension(extensionElement);
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            // fall out
            default:
        }
    }
    return message.build();
}
Also used : MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) XmlElement(org.jivesoftware.smack.packet.XmlElement) XmlEnvironment(org.jivesoftware.smack.packet.XmlEnvironment)

Example 2 with MessageBuilder

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

the class MultiUserChatLight method sendMessage.

/**
 * Sends a message to the chat room.
 *
 * @param text TODO javadoc me please
 *            the text of the message to send.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void sendMessage(String text) throws NotConnectedException, InterruptedException {
    MessageBuilder message = buildMessage();
    message.setBody(text);
    connection.sendStanza(message.build());
}
Also used : MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder)

Example 3 with MessageBuilder

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

the class ExplicitMessageEncryptionElementTest method addToMessageTest.

@Test
public void addToMessageTest() {
    Message message = StanzaBuilder.buildMessage().build();
    // Check inital state (no elements)
    assertNull(ExplicitMessageEncryptionElement.from(message));
    assertFalse(ExplicitMessageEncryptionElement.hasProtocol(message, ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.omemoVAxolotl));
    List<XmlElement> extensions = message.getExtensions();
    assertEquals(0, extensions.size());
    MessageBuilder messageBuilder = StanzaBuilder.buildMessage();
    // Add OMEMO
    ExplicitMessageEncryptionElement.set(messageBuilder, ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.omemoVAxolotl);
    message = messageBuilder.build();
    extensions = message.getExtensions();
    assertEquals(1, extensions.size());
    assertTrue(ExplicitMessageEncryptionElement.hasProtocol(message, ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.omemoVAxolotl));
    assertTrue(ExplicitMessageEncryptionElement.hasProtocol(message, ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.omemoVAxolotl.getNamespace()));
    assertFalse(ExplicitMessageEncryptionElement.hasProtocol(message, ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0));
    assertFalse(ExplicitMessageEncryptionElement.hasProtocol(message, ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0.getNamespace()));
    ExplicitMessageEncryptionElement.set(messageBuilder, ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0);
    message = messageBuilder.build();
    extensions = message.getExtensions();
    assertEquals(2, extensions.size());
    assertTrue(ExplicitMessageEncryptionElement.hasProtocol(message, ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.openpgpV0));
    assertTrue(ExplicitMessageEncryptionElement.hasProtocol(message, ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.omemoVAxolotl));
    // Check, if adding additional OMEMO wont add another element
    ExplicitMessageEncryptionElement.set(messageBuilder, ExplicitMessageEncryptionElement.ExplicitMessageEncryptionProtocol.omemoVAxolotl);
    message = messageBuilder.build();
    extensions = message.getExtensions();
    assertEquals(2, extensions.size());
}
Also used : Message(org.jivesoftware.smack.packet.Message) MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) XmlElement(org.jivesoftware.smack.packet.XmlElement) Test(org.junit.jupiter.api.Test)

Example 4 with MessageBuilder

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

the class MessageFasteningElementsTest method ensureStanzaCanOnlyContainOneFasteningElement.

/**
 * Ensure, that {@link FasteningElement#applyTo(MessageBuilder)}
 * throws when trying to add an {@link FasteningElement} to a {@link MessageBuilder} that already contains one
 * such element.
 *
 * @see <a href="https://xmpp.org/extensions/xep-0422.html#rules">XEP-0422: ยง4. Business Rules</a>
 */
@Test
public void ensureStanzaCanOnlyContainOneFasteningElement() {
    MessageBuilder messageWithFastening = stanzaFactory.buildMessageStanza();
    FasteningElement.builder().setOriginId("origin-id").build().applyTo(messageWithFastening);
    // Adding a second fastening MUST result in exception
    Assertions.assertThrows(IllegalArgumentException.class, () -> FasteningElement.builder().setOriginId("another-apply-to").build().applyTo(messageWithFastening));
}
Also used : MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with MessageBuilder

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

the class MessageFasteningElementsTest method ensureAddFasteningElementToStanzaWorks.

@Test
public void ensureAddFasteningElementToStanzaWorks() {
    MessageBuilder message = stanzaFactory.buildMessageStanza();
    FasteningElement fasteningElement = FasteningElement.builder().setOriginId("another-apply-to").build();
    // Adding only one element is allowed
    fasteningElement.applyTo(message);
}
Also used : FasteningElement(org.jivesoftware.smackx.message_fastening.element.FasteningElement) MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

MessageBuilder (org.jivesoftware.smack.packet.MessageBuilder)34 Message (org.jivesoftware.smack.packet.Message)10 XMPPConnection (org.jivesoftware.smack.XMPPConnection)9 Test (org.junit.jupiter.api.Test)8 Test (org.junit.Test)7 SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)5 Stanza (org.jivesoftware.smack.packet.Stanza)3 RosterExchange (org.jivesoftware.smackx.xroster.packet.RosterExchange)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 Date (java.util.Date)2 DummyConnection (org.jivesoftware.smack.DummyConnection)2 AndFilter (org.jivesoftware.smack.filter.AndFilter)2 XmlElement (org.jivesoftware.smack.packet.XmlElement)2 OpenPgpMessage (org.jivesoftware.smackx.ox.OpenPgpMessage)2 EntityBareJid (org.jxmpp.jid.EntityBareJid)2 File (java.io.File)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Random (java.util.Random)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1