Search in sources :

Example 91 with XmlPullParser

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

the class STUNResolver method loadSTUNServers.

/**
 * Load the STUN configuration from a stream.
 *
 * @param stunConfigStream An InputStream with the configuration file.
 * @return A list of loaded servers
 */
public ArrayList<STUNService> loadSTUNServers(java.io.InputStream stunConfigStream) {
    ArrayList<STUNService> serversList = new ArrayList<>();
    String serverName;
    int serverPort;
    try {
        XmlPullParser parser = PacketParserUtils.getParserFor(stunConfigStream);
        XmlPullParser.Event eventType = parser.getEventType();
        do {
            if (eventType == XmlPullParser.Event.START_ELEMENT) {
                // Parse a STUN server definition
                if (parser.getName().equals("stunServer")) {
                    serverName = null;
                    serverPort = -1;
                    // Parse the hostname
                    parser.next();
                    parser.next();
                    serverName = parser.nextText();
                    // Parse the port
                    parser.next();
                    parser.next();
                    serverPort = Integer.parseInt(parser.nextText());
                    // it to the list.
                    if (serverName != null && serverPort != -1) {
                        STUNService service = new STUNService(serverName, serverPort);
                        serversList.add(service);
                    }
                }
            }
            eventType = parser.next();
        } while (eventType != XmlPullParser.Event.END_DOCUMENT);
    } catch (XmlPullParserException e) {
        LOGGER.log(Level.SEVERE, "Exception", e);
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, "Exception", e);
    }
    currentServer = bestSTUNServer(serversList);
    return serversList;
}
Also used : ArrayList(java.util.ArrayList) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) IOException(java.io.IOException)

Example 92 with XmlPullParser

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

the class JingleProvider method parse.

/**
 * Parse a iq/jingle element.
 * @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.
 */
@Override
public Jingle parse(XmlPullParser parser, int intialDepth, XmlEnvironment xmlEnvironment) throws IOException, XmlPullParserException, SmackParsingException {
    Jingle jingle = new Jingle();
    String sid;
    JingleActionEnum action;
    Jid initiator, responder;
    boolean done = false;
    JingleContent currentContent = null;
    // Sub-elements providers
    JingleContentProvider jcp = new JingleContentProvider();
    JingleDescriptionProvider jdpAudio = new JingleDescriptionProvider.Audio();
    JingleTransportProvider jtpRawUdp = new JingleTransportProvider.RawUdp();
    JingleTransportProvider jtpIce = new JingleTransportProvider.Ice();
    ExtensionElementProvider<?> jmipAudio = new JingleContentInfoProvider.Audio();
    XmlPullParser.Event eventType;
    String elementName;
    String namespace;
    // Get some attributes for the <jingle> element
    sid = parser.getAttributeValue("", "sid");
    action = JingleActionEnum.getAction(parser.getAttributeValue("", "action"));
    initiator = ParserUtils.getJidAttribute(parser, "initiator");
    responder = ParserUtils.getJidAttribute(parser, "responder");
    jingle.setSid(sid);
    jingle.setAction(action);
    jingle.setInitiator(initiator);
    jingle.setResponder(responder);
    // Start processing sub-elements
    while (!done) {
        eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT) {
            elementName = parser.getName();
            namespace = parser.getNamespace();
            if (elementName.equals(JingleContent.NODENAME)) {
                // Add a new <content> element to the jingle
                currentContent = jcp.parse(parser);
                jingle.addContent(currentContent);
            } else if (elementName.equals(JingleDescription.NODENAME) && namespace.equals(JingleDescription.Audio.NAMESPACE)) {
                // Set the <description> element of the <content>
                currentContent.setDescription(jdpAudio.parse(parser));
            } else if (elementName.equals(JingleTransport.NODENAME)) {
                // Parse the possible transport namespaces
                if (namespace.equals(JingleTransport.RawUdp.NAMESPACE)) {
                    currentContent.addJingleTransport(jtpRawUdp.parse(parser));
                } else if (namespace.equals(JingleTransport.Ice.NAMESPACE)) {
                    currentContent.addJingleTransport(jtpIce.parse(parser));
                } else {
                    // TODO: Should be SmackParseException.
                    throw new IOException("Unknown transport namespace \"" + namespace + "\" in Jingle packet.");
                }
            } else if (namespace.equals(JingleContentInfo.Audio.NAMESPACE)) {
                jingle.setContentInfo((JingleContentInfo) jmipAudio.parse(parser));
            } else {
                // TODO: Should be SmackParseException.
                throw new IOException("Unknown combination of namespace \"" + namespace + "\" and element name \"" + elementName + "\" in Jingle packet.");
            }
        } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
            if (parser.getName().equals(Jingle.NODENAME)) {
                done = true;
            }
        }
    }
    return jingle;
}
Also used : Jid(org.jxmpp.jid.Jid) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) IOException(java.io.IOException) JingleActionEnum(org.jivesoftware.smackx.jingleold.JingleActionEnum) Jingle(org.jivesoftware.smackx.jingleold.packet.Jingle) JingleContentInfo(org.jivesoftware.smackx.jingleold.packet.JingleContentInfo) JingleContent(org.jivesoftware.smackx.jingleold.packet.JingleContent)

Example 93 with XmlPullParser

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

the class TranscriptsProvider method parseSummary.

private static Transcripts.TranscriptSummary parseSummary(XmlPullParser parser) throws IOException, XmlPullParserException, ParseException {
    String sessionID = parser.getAttributeValue("", "sessionID");
    Date joinTime = null;
    Date leftTime = null;
    List<Transcripts.AgentDetail> agents = new ArrayList<>();
    boolean done = false;
    while (!done) {
        XmlPullParser.Event eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT) {
            if (parser.getName().equals("joinTime")) {
                synchronized (UTC_FORMAT) {
                    joinTime = UTC_FORMAT.parse(parser.nextText());
                }
            } else if (parser.getName().equals("leftTime")) {
                synchronized (UTC_FORMAT) {
                    leftTime = UTC_FORMAT.parse(parser.nextText());
                }
            } else if (parser.getName().equals("agents")) {
                agents = parseAgents(parser);
            }
        } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
            if (parser.getName().equals("transcript")) {
                done = true;
            }
        }
    }
    return new Transcripts.TranscriptSummary(sessionID, joinTime, leftTime, agents);
}
Also used : ArrayList(java.util.ArrayList) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) Date(java.util.Date)

Example 94 with XmlPullParser

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

the class TranscriptsProvider method parse.

@Override
public Transcripts parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, TextParseException, ParseException {
    Jid userID = ParserUtils.getJidAttribute(parser, "userID");
    List<Transcripts.TranscriptSummary> summaries = new ArrayList<>();
    boolean done = false;
    while (!done) {
        XmlPullParser.Event eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT) {
            if (parser.getName().equals("transcript")) {
                Transcripts.TranscriptSummary summary = parseSummary(parser);
                summaries.add(summary);
            }
        } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
            if (parser.getName().equals("transcripts")) {
                done = true;
            }
        }
    }
    return new Transcripts(userID, summaries);
}
Also used : Jid(org.jxmpp.jid.Jid) ArrayList(java.util.ArrayList) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser)

Example 95 with XmlPullParser

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

the class DiscoverItemsProvider method parse.

@Override
public DiscoverItems parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
    DiscoverItems discoverItems = new DiscoverItems();
    boolean done = false;
    DiscoverItems.Item item;
    Jid jid = null;
    String name = "";
    String action = "";
    String node = "";
    discoverItems.setNode(parser.getAttributeValue("", "node"));
    while (!done) {
        XmlPullParser.Event eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT && "item".equals(parser.getName())) {
            // Initialize the variables from the parsed XML
            jid = ParserUtils.getJidAttribute(parser);
            name = parser.getAttributeValue("", "name");
            node = parser.getAttributeValue("", "node");
            action = parser.getAttributeValue("", "action");
        } else if (eventType == XmlPullParser.Event.END_ELEMENT && "item".equals(parser.getName())) {
            // Create a new Item and add it to DiscoverItems.
            item = new DiscoverItems.Item(jid);
            item.setName(name);
            item.setNode(node);
            item.setAction(action);
            discoverItems.addItem(item);
        } else if (eventType == XmlPullParser.Event.END_ELEMENT && "query".equals(parser.getName())) {
            done = true;
        }
    }
    return discoverItems;
}
Also used : Jid(org.jxmpp.jid.Jid) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems)

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