Search in sources :

Example 6 with XmlElement

use of org.jivesoftware.smack.packet.XmlElement 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 7 with XmlElement

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

the class Node method getSubscriptions.

private List<Subscription> getSubscriptions(SubscriptionsNamespace subscriptionsNamespace, List<XmlElement> additionalExtensions, Collection<XmlElement> returnedExtensions) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    PubSubElementType pubSubElementType = subscriptionsNamespace.type;
    PubSub pubSub = createPubsubPacket(IQ.Type.get, new NodeExtension(pubSubElementType, getId()));
    if (additionalExtensions != null) {
        for (XmlElement pe : additionalExtensions) {
            pubSub.addExtension(pe);
        }
    }
    PubSub reply = sendPubsubPacket(pubSub);
    if (returnedExtensions != null) {
        returnedExtensions.addAll(reply.getExtensions());
    }
    SubscriptionsExtension subElem = reply.getExtension(pubSubElementType);
    return subElem.getSubscriptions();
}
Also used : PubSub(org.jivesoftware.smackx.pubsub.packet.PubSub) XmlElement(org.jivesoftware.smack.packet.XmlElement)

Example 8 with XmlElement

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

the class RSMManager method page.

Collection<XmlElement> page(int max) {
    List<XmlElement> packetExtensions = new LinkedList<>();
    packetExtensions.add(new RSMSet(max));
    return packetExtensions;
}
Also used : RSMSet(org.jivesoftware.smackx.rsm.packet.RSMSet) XmlElement(org.jivesoftware.smack.packet.XmlElement) LinkedList(java.util.LinkedList)

Example 9 with XmlElement

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

the class JingleProviderTest method testReasonElementWithExtraElement.

@ParameterizedTest
@EnumSource(SmackTestUtil.XmlPullParserKind.class)
public void testReasonElementWithExtraElement(SmackTestUtil.XmlPullParserKind parserKind) throws XmlPullParserException, IOException, SmackParsingException {
    String xml = "<iq from='juliet@capulet.lit/balcony'" + " id='le71fa63'" + " to='romeo@montague.lit/orchard'" + " type='set'>" + "<jingle xmlns='urn:xmpp:jingle:1'" + " action='session-terminate'" + " sid='a73sjjvkla37jfea'>" + "<reason>" + "<success/>" + "<my-element xmlns='https://example.org' foo='bar'/>" + "</reason>" + "</jingle>" + "</iq>";
    Jingle jingle = SmackTestUtil.parse(xml, JingleProvider.class, parserKind);
    JingleReason jingleReason = jingle.getReason();
    assertEquals(JingleReason.Reason.success, jingleReason.asEnum());
    XmlElement element = jingleReason.getElement();
    // TODO: Use JUnit 5.8's assertInstanceOf when possible
    // assertInstanceOf(StandardExtesionElement.class, extraElement);
    assertTrue(element instanceof StandardExtensionElement);
    StandardExtensionElement extraElement = (StandardExtensionElement) element;
    assertEquals("https://example.org", extraElement.getNamespace());
    assertEquals("bar", extraElement.getAttributes().get("foo"));
}
Also used : Jingle(org.jivesoftware.smackx.jingle.element.Jingle) StandardExtensionElement(org.jivesoftware.smack.packet.StandardExtensionElement) JingleReason(org.jivesoftware.smackx.jingle.element.JingleReason) XmlElement(org.jivesoftware.smack.packet.XmlElement) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with XmlElement

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

the class ItemValidationTest method parseSimplePayloadItem.

@Test
public void parseSimplePayloadItem() throws Exception {
    String itemContent = "<foo xmlns='smack:test'>Some text</foo>";
    XmlPullParser parser = PacketParserUtils.getParserFor("<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" + "<event xmlns='http://jabber.org/protocol/pubsub#event'>" + "<items node='testNode'>" + "<item id='testid1' >" + itemContent + "</item>" + "</items>" + "</event>" + "</message>");
    Stanza message = PacketParserUtils.parseMessage(parser);
    XmlElement eventExt = message.getExtension(PubSubNamespace.event.getXmlns());
    EventElement event = (EventElement) eventExt;
    NamedElement itemExt = ((ItemsExtension) event.getExtensions().get(0)).items.get(0);
    assertTrue(itemExt instanceof PayloadItem<?>);
    PayloadItem<?> item = (PayloadItem<?>) itemExt;
    assertEquals("testid1", item.getId());
    assertTrue(item.getPayload() instanceof SimplePayload);
    SimplePayload payload = (SimplePayload) item.getPayload();
    assertEquals("foo", payload.getElementName());
    assertEquals("smack:test", payload.getNamespace());
    assertXmlSimilar(itemContent, payload.toXML().toString());
}
Also used : Stanza(org.jivesoftware.smack.packet.Stanza) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) XmlElement(org.jivesoftware.smack.packet.XmlElement) NamedElement(org.jivesoftware.smack.packet.NamedElement) Test(org.junit.jupiter.api.Test)

Aggregations

XmlElement (org.jivesoftware.smack.packet.XmlElement)28 XmlPullParser (org.jivesoftware.smack.xml.XmlPullParser)6 Test (org.junit.jupiter.api.Test)5 NamedElement (org.jivesoftware.smack.packet.NamedElement)4 Stanza (org.jivesoftware.smack.packet.Stanza)4 XmlEnvironment (org.jivesoftware.smack.packet.XmlEnvironment)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 QName (javax.xml.namespace.QName)2 Message (org.jivesoftware.smack.packet.Message)2 MessageBuilder (org.jivesoftware.smack.packet.MessageBuilder)2 JingleReason (org.jivesoftware.smackx.jingle.element.JingleReason)2 PubSub (org.jivesoftware.smackx.pubsub.packet.PubSub)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)1 Mechanisms (org.jivesoftware.smack.packet.Mechanisms)1