Search in sources :

Example 61 with XmlPullParser

use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.

the class MessageEventProvider method parse.

/**
 * Parses a MessageEvent stanza (extension sub-packet).
 *
 * @param parser the XML parser, positioned at the starting element of the extension.
 * @return a PacketExtension.
 * @throws IOException if an I/O error occurred.
 * @throws XmlPullParserException if an error in the XML parser occurred.
 */
@Override
public MessageEvent parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
    MessageEvent messageEvent = new MessageEvent();
    boolean done = false;
    while (!done) {
        XmlPullParser.Event eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT) {
            if (parser.getName().equals("id"))
                messageEvent.setStanzaId(parser.nextText());
            if (parser.getName().equals(MessageEvent.COMPOSING))
                messageEvent.setComposing(true);
            if (parser.getName().equals(MessageEvent.DELIVERED))
                messageEvent.setDelivered(true);
            if (parser.getName().equals(MessageEvent.DISPLAYED))
                messageEvent.setDisplayed(true);
            if (parser.getName().equals(MessageEvent.OFFLINE))
                messageEvent.setOffline(true);
        } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
            if (parser.getName().equals("x")) {
                done = true;
            }
        }
    }
    return messageEvent;
}
Also used : MessageEvent(org.jivesoftware.smackx.xevent.packet.MessageEvent) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser)

Example 62 with XmlPullParser

use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.

the class OfferRevokeProvider method parse.

@Override
public OfferRevokePacket parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
    // The parser will be positioned on the opening IQ tag, so get the JID attribute.
    Jid userJID = ParserUtils.getJidAttribute(parser);
    // Default the userID to the JID.
    Jid userID = userJID;
    String reason = null;
    String sessionID = null;
    boolean done = false;
    while (!done) {
        XmlPullParser.Event eventType = parser.next();
        if ((eventType == XmlPullParser.Event.START_ELEMENT) && parser.getName().equals("reason")) {
            reason = parser.nextText();
        } else if ((eventType == XmlPullParser.Event.START_ELEMENT) && parser.getName().equals(SessionID.ELEMENT_NAME)) {
            sessionID = parser.getAttributeValue("", "id");
        } else if ((eventType == XmlPullParser.Event.START_ELEMENT) && parser.getName().equals(UserID.ELEMENT_NAME)) {
            userID = ParserUtils.getJidAttribute(parser, "id");
        } else if ((eventType == XmlPullParser.Event.END_ELEMENT) && parser.getName().equals("offer-revoke")) {
            done = true;
        }
    }
    return new OfferRevokePacket(userJID, userID, reason, sessionID);
}
Also used : Jid(org.jxmpp.jid.Jid) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser)

Example 63 with XmlPullParser

use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.

the class OmemoDeviceListVAxolotlElementTest method serializationTest.

@Test
public void serializationTest() throws Exception {
    HashSet<Integer> ids = new HashSet<>();
    ids.add(1234);
    ids.add(9876);
    OmemoDeviceListElement_VAxolotl element = new OmemoDeviceListElement_VAxolotl(ids);
    String xml = element.toXML().toString();
    XmlPullParser parser = TestUtils.getParser(xml);
    OmemoDeviceListElement_VAxolotl parsed = new OmemoDeviceListVAxolotlProvider().parse(parser);
    assertTrue("Parsed element must equal the original.", parsed.getDeviceIds().equals(element.getDeviceIds()));
    assertEquals("Generated XML must match.", "<list xmlns='eu.siacs.conversations.axolotl'>" + "<device id='1234'/>" + "<device id='9876'/>" + "</list>", xml);
}
Also used : OmemoDeviceListVAxolotlProvider(org.jivesoftware.smackx.omemo.provider.OmemoDeviceListVAxolotlProvider) OmemoDeviceListElement_VAxolotl(org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement_VAxolotl) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 64 with XmlPullParser

use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.

the class OmemoBundleVAxolotlElementTest method emptyPreKeysShouldFailTest.

@Test(expected = IllegalArgumentException.class)
public void emptyPreKeysShouldFailTest() throws Exception {
    String s = "<bundle xmlns='eu.siacs.conversations.axolotl'><signedPreKeyPublic signedPreKeyId='1'>BU4bJ18+rqbSnBblZU8pR/s+impyhoL9AJssJIE59fZb</signedPreKeyPublic><signedPreKeySignature>MaQtv7ySqHpPr0gkVtMp4KmWC61Hnfs5a7/cKEhrX8n12evGdkg4fNf3Q/ufgmJu5dnup9pkTA1pj00dTbtXjw==</signedPreKeySignature><identityKey>BWO0QOem1YXIJuT61cxXpG/mKlvISDwZxQJHW2/7eVki</identityKey><prekeys></prekeys></bundle>";
    XmlPullParser parser = TestUtils.getParser(s);
    new OmemoBundleVAxolotlProvider().parse(parser);
}
Also used : OmemoBundleVAxolotlProvider(org.jivesoftware.smackx.omemo.provider.OmemoBundleVAxolotlProvider) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) Test(org.junit.Test)

Example 65 with XmlPullParser

use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.

the class PubkeyElementTest method providerTest.

@SuppressWarnings("UndefinedEquals")
@ParameterizedTest
@EnumSource(SmackTestUtil.XmlPullParserKind.class)
public void providerTest(SmackTestUtil.XmlPullParserKind parserKind) throws ParseException, XmlPullParserException, IOException, SmackParsingException {
    String base64EncodedOpenPgpPublicKey = "VGhpcyBpcyBqdXN0IGEgdGVzdA==";
    String pubkeyElement = "<pubkey xmlns='urn:xmpp:openpgp:0' date='2018-01-21T10:46:21.000+00:00'>" + "<data>" + base64EncodedOpenPgpPublicKey + "</data>" + "</pubkey>";
    Date date = XmppDateTime.parseXEP0082Date("2018-01-21T10:46:21.000+00:00");
    PubkeyElement element = new PubkeyElement(new PubkeyElement.PubkeyDataElement(base64EncodedOpenPgpPublicKey), date);
    assertXmlSimilar(pubkeyElement, element.toXML().toString());
    XmlPullParser parser = SmackTestUtil.getParserFor(pubkeyElement, parserKind);
    PubkeyElement parsed = PubkeyElementProvider.INSTANCE.parse(parser);
    assertEquals(element.getDate(), parsed.getDate());
    assertEquals(element.getDataElement().getB64Data(), parsed.getDataElement().getB64Data());
}
Also used : PubkeyElement(org.jivesoftware.smackx.ox.element.PubkeyElement) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) Date(java.util.Date) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

XmlPullParser (org.jivesoftware.smack.xml.XmlPullParser)139 Test (org.junit.jupiter.api.Test)69 Message (org.jivesoftware.smack.packet.Message)15 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 Date (java.util.Date)12 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)11 Jid (org.jxmpp.jid.Jid)11 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 XmlPullParserException (org.jivesoftware.smack.xml.XmlPullParserException)10 EnumSource (org.junit.jupiter.params.provider.EnumSource)9 IQ (org.jivesoftware.smack.packet.IQ)8 Test (org.junit.Test)8 MarkupElement (org.jivesoftware.smackx.message_markup.element.MarkupElement)7 MarkupElementProvider (org.jivesoftware.smackx.message_markup.provider.MarkupElementProvider)7 XmlElement (org.jivesoftware.smack.packet.XmlElement)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 NamedElement (org.jivesoftware.smack.packet.NamedElement)4 HttpOverXmppResp (org.jivesoftware.smackx.hoxt.packet.HttpOverXmppResp)4