Search in sources :

Example 1 with JingleReason

use of org.jivesoftware.smackx.jingle.element.JingleReason in project Smack by igniterealtime.

the class JingleProvider method parse.

@Override
public Jingle parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    Jingle.Builder builder = Jingle.builder(iqData);
    String actionString = parser.getAttributeValue("", Jingle.ACTION_ATTRIBUTE_NAME);
    if (actionString != null) {
        JingleAction action = JingleAction.fromString(actionString);
        builder.setAction(action);
    }
    FullJid initiator = ParserUtils.getFullJidAttribute(parser, Jingle.INITIATOR_ATTRIBUTE_NAME);
    builder.setInitiator(initiator);
    FullJid responder = ParserUtils.getFullJidAttribute(parser, Jingle.RESPONDER_ATTRIBUTE_NAME);
    builder.setResponder(responder);
    String sessionId = parser.getAttributeValue("", Jingle.SESSION_ID_ATTRIBUTE_NAME);
    builder.setSessionId(sessionId);
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch(eventType) {
            case START_ELEMENT:
                String tagName = parser.getName();
                switch(tagName) {
                    case JingleContent.ELEMENT:
                        JingleContent content = parseJingleContent(parser, parser.getDepth());
                        builder.addJingleContent(content);
                        break;
                    case JingleReason.ELEMENT:
                        JingleReason reason = parseJingleReason(parser);
                        builder.setReason(reason);
                        break;
                    default:
                        LOGGER.severe("Unknown Jingle element: " + tagName);
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    return builder.build();
}
Also used : Jingle(org.jivesoftware.smackx.jingle.element.Jingle) JingleAction(org.jivesoftware.smackx.jingle.element.JingleAction) JingleReason(org.jivesoftware.smackx.jingle.element.JingleReason) FullJid(org.jxmpp.jid.FullJid) JingleContent(org.jivesoftware.smackx.jingle.element.JingleContent)

Example 2 with JingleReason

use of org.jivesoftware.smackx.jingle.element.JingleReason 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 3 with JingleReason

use of org.jivesoftware.smackx.jingle.element.JingleReason in project Smack by igniterealtime.

the class JingleProvider method parseJingleReason.

public static JingleReason parseJingleReason(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
    ParserUtils.assertAtStartTag(parser);
    final int initialDepth = parser.getDepth();
    final String jingleNamespace = parser.getNamespace();
    JingleReason.Reason reason = null;
    XmlElement element = null;
    String text = null;
    // 'sid' is only set if the reason is 'alternative-session'.
    String sid = null;
    outerloop: while (true) {
        XmlPullParser.TagEvent event = parser.nextTag();
        switch(event) {
            case START_ELEMENT:
                String elementName = parser.getName();
                String namespace = parser.getNamespace();
                if (namespace.equals(jingleNamespace)) {
                    switch(elementName) {
                        case "text":
                            text = parser.nextText();
                            break;
                        case "alternative-session":
                            parser.next();
                            sid = parser.nextText();
                            break;
                        default:
                            reason = Reason.fromString(elementName);
                            break;
                    }
                } else {
                    element = PacketParserUtils.parseExtensionElement(elementName, namespace, parser, null);
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
        }
    }
    JingleReason res;
    if (sid != null) {
        res = new JingleReason.AlternativeSession(sid, text, element);
    } else {
        res = new JingleReason(reason, text, element);
    }
    return res;
}
Also used : Reason(org.jivesoftware.smackx.jingle.element.JingleReason.Reason) JingleReason(org.jivesoftware.smackx.jingle.element.JingleReason) XmlElement(org.jivesoftware.smack.packet.XmlElement)

Aggregations

JingleReason (org.jivesoftware.smackx.jingle.element.JingleReason)3 XmlElement (org.jivesoftware.smack.packet.XmlElement)2 Jingle (org.jivesoftware.smackx.jingle.element.Jingle)2 StandardExtensionElement (org.jivesoftware.smack.packet.StandardExtensionElement)1 JingleAction (org.jivesoftware.smackx.jingle.element.JingleAction)1 JingleContent (org.jivesoftware.smackx.jingle.element.JingleContent)1 Reason (org.jivesoftware.smackx.jingle.element.JingleReason.Reason)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 EnumSource (org.junit.jupiter.params.provider.EnumSource)1 FullJid (org.jxmpp.jid.FullJid)1