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();
}
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());
}
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());
}
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));
}
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);
}
Aggregations