Search in sources :

Example 6 with XmlPullParserException

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

the class PacketParserUtils method parsePresence.

/**
 * Parses a presence packet.
 *
 * @param parser the XML parser, positioned at the start of a presence packet.
 * @param outerXmlEnvironment the outer XML environment (optional).
 * @return a Presence packet.
 * @throws IOException if an I/O error occurred.
 * @throws XmlPullParserException if an error in the XML parser occurred.
 * @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
 */
public static Presence parsePresence(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    ParserUtils.assertAtStartTag(parser);
    final int initialDepth = parser.getDepth();
    XmlEnvironment presenceXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
    PresenceBuilder presence = parseCommonStanzaAttributes(stanzaId -> StanzaBuilder.buildPresence(stanzaId), parser, outerXmlEnvironment);
    Presence.Type type = Presence.Type.available;
    String typeString = parser.getAttributeValue("", "type");
    if (typeString != null && !typeString.equals("")) {
        type = Presence.Type.fromString(typeString);
    }
    presence.ofType(type);
    // Parse 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 "status":
                        presence.setStatus(parser.nextText());
                        break;
                    case "priority":
                        Byte priority = ParserUtils.getByteAttributeFromNextText(parser);
                        presence.setPriority(priority);
                        break;
                    case "show":
                        String modeText = parser.nextText();
                        if (StringUtils.isNotEmpty(modeText)) {
                            presence.setMode(Presence.Mode.fromString(modeText));
                        } else {
                            // Some implementations send presence stanzas with a
                            // '<show />' element, which is a invalid XMPP presence
                            // stanza according to RFC 6121 4.7.2.1
                            LOGGER.warning("Empty or null mode text in presence show element form " + presence + "' which is invalid according to RFC6121 4.7.2.1");
                        }
                        break;
                    case "error":
                        presence.setError(parseError(parser, presenceXmlEnvironment));
                        break;
                    default:
                        // failing completely here. See SMACK-390 for more information.
                        try {
                            XmlElement extensionElement = parseExtensionElement(elementName, namespace, parser, presenceXmlEnvironment);
                            presence.addExtension(extensionElement);
                        } catch (Exception e) {
                            LOGGER.log(Level.WARNING, "Failed to parse extension element in Presence stanza: " + presence, e);
                        }
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    return presence.build();
}
Also used : PresenceBuilder(org.jivesoftware.smack.packet.PresenceBuilder) Presence(org.jivesoftware.smack.packet.Presence) XmlElement(org.jivesoftware.smack.packet.XmlElement) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) SmackParsingException(org.jivesoftware.smack.parsing.SmackParsingException) IOException(java.io.IOException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) XmlEnvironment(org.jivesoftware.smack.packet.XmlEnvironment)

Example 7 with XmlPullParserException

use of org.jivesoftware.smack.xml.XmlPullParserException 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 8 with XmlPullParserException

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

the class DirectoryRosterStore method readEntry.

@SuppressWarnings("DefaultCharset")
private static Item readEntry(File file) {
    Reader reader;
    try {
        // TODO: Use Files.newBufferedReader() once Smack's minimum Android API level is 26 or higher.
        reader = new FileReader(file);
    } catch (FileNotFoundException e) {
        LOGGER.log(Level.FINE, "Roster entry file not found", e);
        return null;
    }
    try {
        XmlPullParser parser = PacketParserUtils.getParserFor(reader);
        Item item = RosterPacketProvider.parseItem(parser);
        reader.close();
        return item;
    } catch (XmlPullParserException | IOException | IllegalArgumentException e) {
        boolean deleted = file.delete();
        String message = "Exception while parsing roster entry.";
        if (deleted) {
            message += " File was deleted.";
        }
        LOGGER.log(Level.SEVERE, message, e);
        return null;
    }
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) FileNotFoundException(java.io.FileNotFoundException) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) Reader(java.io.Reader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) IOException(java.io.IOException)

Example 9 with XmlPullParserException

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

the class ModularXmppClientToServerConnection method parseAndProcessElement.

private void parseAndProcessElement(String element) {
    try {
        XmlPullParser parser = PacketParserUtils.getParserFor(element);
        // Skip the enclosing stream open what is guaranteed to be there.
        parser.next();
        XmlPullParser.Event event = parser.getEventType();
        outerloop: while (true) {
            switch(event) {
                case START_ELEMENT:
                    final String name = parser.getName();
                    // Note that we don't handle "stream" here as it's done in the splitter.
                    switch(name) {
                        case Message.ELEMENT:
                        case IQ.IQ_ELEMENT:
                        case Presence.ELEMENT:
                            try {
                                parseAndProcessStanza(parser);
                            } finally {
                            // TODO: Here would be the following stream management code.
                            // clientHandledStanzasCount = SMUtils.incrementHeight(clientHandledStanzasCount);
                            }
                            break;
                        case "error":
                            StreamError streamError = PacketParserUtils.parseStreamError(parser, null);
                            StreamErrorException streamErrorException = new StreamErrorException(streamError);
                            currentXmppException = streamErrorException;
                            notifyWaitingThreads();
                            throw streamErrorException;
                        case "features":
                            parseFeatures(parser);
                            afterFeaturesReceived();
                            break;
                        default:
                            parseAndProcessNonza(parser);
                            break;
                    }
                    break;
                case END_DOCUMENT:
                    break outerloop;
                // fall out
                default:
            }
            event = parser.next();
        }
    } catch (XmlPullParserException | IOException | InterruptedException | StreamErrorException | SmackParsingException e) {
        notifyConnectionError(e);
    }
}
Also used : StreamErrorException(org.jivesoftware.smack.XMPPException.StreamErrorException) StreamError(org.jivesoftware.smack.packet.StreamError) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) IOException(java.io.IOException) SmackParsingException(org.jivesoftware.smack.parsing.SmackParsingException)

Example 10 with XmlPullParserException

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

the class ModularXmppClientToServerConnectionInternal method onStreamOpen.

public final String onStreamOpen(String streamOpen) {
    XmlPullParser streamOpenParser;
    try {
        streamOpenParser = PacketParserUtils.getParserFor(streamOpen);
    } catch (XmlPullParserException | IOException e) {
        // Should never happen.
        throw new AssertionError(e);
    }
    String streamClose = onStreamOpen(streamOpenParser);
    return streamClose;
}
Also used : XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) IOException(java.io.IOException)

Aggregations

XmlPullParserException (org.jivesoftware.smack.xml.XmlPullParserException)15 IOException (java.io.IOException)10 XmlPullParser (org.jivesoftware.smack.xml.XmlPullParser)7 XMLStreamException (javax.xml.stream.XMLStreamException)3 SmackParsingException (org.jivesoftware.smack.parsing.SmackParsingException)3 ArrayList (java.util.ArrayList)2 StreamErrorException (org.jivesoftware.smack.XMPPException.StreamErrorException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 ObjectInputStream (java.io.ObjectInputStream)1 Reader (java.io.Reader)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BOSHClientConfig (org.igniterealtime.jbosh.BOSHClientConfig)1 BOSHException (org.igniterealtime.jbosh.BOSHException)1 SmackException (org.jivesoftware.smack.SmackException)1 GenericConnectionException (org.jivesoftware.smack.SmackException.GenericConnectionException)1