Search in sources :

Example 1 with XmlPullParserException

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

the class XMPPBOSHConnection method connectInternal.

@SuppressWarnings("deprecation")
@Override
protected void connectInternal() throws SmackException, InterruptedException {
    done = false;
    notified = false;
    try {
        // Ensure a clean starting state
        if (client != null) {
            client.close();
            client = null;
        }
        sessionID = null;
        // Initialize BOSH client
        BOSHClientConfig.Builder cfgBuilder = BOSHClientConfig.Builder.create(config.getURI(), config.getXMPPServiceDomain().toString());
        if (config.isProxyEnabled()) {
            cfgBuilder.setProxy(config.getProxyAddress(), config.getProxyPort());
        }
        cfgBuilder.setCompressionEnabled(config.isCompressionEnabled());
        for (Map.Entry<String, String> h : config.getHttpHeaders().entrySet()) {
            cfgBuilder.addHttpHeader(h.getKey(), h.getValue());
        }
        client = BOSHClient.create(cfgBuilder.build());
        client.addBOSHClientConnListener(new BOSHConnectionListener());
        client.addBOSHClientResponseListener(new BOSHPacketReader());
        // Initialize the debugger
        if (debugger != null) {
            initDebugger();
        }
        // Send the session creation request
        client.send(ComposableBody.builder().setNamespaceDefinition("xmpp", XMPP_BOSH_NS).setAttribute(BodyQName.createWithPrefix(XMPP_BOSH_NS, "version", "xmpp"), "1.0").build());
    } catch (Exception e) {
        throw new GenericConnectionException(e);
    }
    // Wait for the response from the server
    synchronized (this) {
        if (!connected) {
            final long deadline = System.currentTimeMillis() + getReplyTimeout();
            while (!notified) {
                final long now = System.currentTimeMillis();
                if (now >= deadline)
                    break;
                wait(deadline - now);
            }
        }
    }
    // If there is no feedback, throw an remote server timeout error
    if (!connected && !done) {
        done = true;
        String errorMessage = "Timeout reached for the connection to " + getHost() + ":" + getPort() + ".";
        throw new SmackException.SmackMessageException(errorMessage);
    }
    try {
        XmlPullParser parser = PacketParserUtils.getParserFor("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'/>");
        onStreamOpen(parser);
    } catch (XmlPullParserException | IOException e) {
        throw new AssertionError("Failed to setup stream environment", e);
    }
}
Also used : XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) BOSHClientConfig(org.igniterealtime.jbosh.BOSHClientConfig) IOException(java.io.IOException) SmackException(org.jivesoftware.smack.SmackException) GenericConnectionException(org.jivesoftware.smack.SmackException.GenericConnectionException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) StreamErrorException(org.jivesoftware.smack.XMPPException.StreamErrorException) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) BOSHException(org.igniterealtime.jbosh.BOSHException) IOException(java.io.IOException) SmackWrappedException(org.jivesoftware.smack.SmackException.SmackWrappedException) XMPPException(org.jivesoftware.smack.XMPPException) GenericConnectionException(org.jivesoftware.smack.SmackException.GenericConnectionException) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) Map(java.util.Map)

Example 2 with XmlPullParserException

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

the class TestUtils method getParser.

private static XmlPullParser getParser(Reader reader, String startTag) {
    XmlPullParser parser;
    try {
        parser = SmackXmlParser.newXmlParser(reader);
        if (startTag == null) {
            while (parser.getEventType() != XmlPullParser.Event.START_ELEMENT) {
                parser.next();
            }
            return parser;
        }
        boolean found = false;
        while (!found) {
            if ((parser.next() == XmlPullParser.Event.START_ELEMENT) && parser.getName().equals(startTag))
                found = true;
        }
        if (!found)
            throw new IllegalArgumentException("Can not find start tag '" + startTag + "'");
    } catch (XmlPullParserException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return parser;
}
Also used : XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) IOException(java.io.IOException)

Example 3 with XmlPullParserException

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

the class AbstractXMPPConnection method parseAndProcessStanza.

protected void parseAndProcessStanza(XmlPullParser parser) throws XmlPullParserException, IOException, InterruptedException {
    ParserUtils.assertAtStartTag(parser);
    int parserDepth = parser.getDepth();
    Stanza stanza = null;
    try {
        stanza = PacketParserUtils.parseStanza(parser, incomingStreamXmlEnvironment);
    } catch (XmlPullParserException | SmackParsingException | IOException | IllegalArgumentException e) {
        CharSequence content = PacketParserUtils.parseContentDepth(parser, parserDepth);
        UnparseableStanza message = new UnparseableStanza(content, e);
        ParsingExceptionCallback callback = getParsingExceptionCallback();
        if (callback != null) {
            callback.handleUnparsableStanza(message);
        }
    }
    ParserUtils.assertAtEndTag(parser);
    if (stanza != null) {
        processStanza(stanza);
    }
}
Also used : Stanza(org.jivesoftware.smack.packet.Stanza) ParsingExceptionCallback(org.jivesoftware.smack.parsing.ParsingExceptionCallback) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) IOException(java.io.IOException) SmackParsingException(org.jivesoftware.smack.parsing.SmackParsingException)

Example 4 with XmlPullParserException

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

the class JivePropertiesExtensionProvider method parse.

/**
 * Parse a properties sub-packet. If any errors occur while de-serializing Java object
 * properties, an exception will be printed and not thrown since a thrown exception will shut
 * down the entire connection. ClassCastExceptions will occur when both the sender and receiver
 * of the stanza don't have identical versions of the same class.
 * <p>
 * Note that you have to explicitly enabled Java object deserialization with @{link
 * {@link JivePropertiesManager#setJavaObjectEnabled(boolean)}
 *
 * @param parser the XML parser, positioned at the start of a properties sub-packet.
 * @return a map of the properties.
 * @throws IOException if an I/O error occurred.
 * @throws XmlPullParserException if an error in the XML parser occurred.
 */
@SuppressWarnings("BanSerializableRead")
@Override
public JivePropertiesExtension parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
    Map<String, Object> properties = new HashMap<>();
    while (true) {
        XmlPullParser.Event eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getName().equals("property")) {
            // Parse a property
            boolean done = false;
            String name = null;
            String type = null;
            String valueText = null;
            Object value = null;
            while (!done) {
                eventType = parser.next();
                if (eventType == XmlPullParser.Event.START_ELEMENT) {
                    String elementName = parser.getName();
                    if (elementName.equals("name")) {
                        name = parser.nextText();
                    } else if (elementName.equals("value")) {
                        type = parser.getAttributeValue("", "type");
                        valueText = parser.nextText();
                    }
                } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
                    if (parser.getName().equals("property")) {
                        if ("integer".equals(type)) {
                            value = Integer.valueOf(valueText);
                        } else if ("long".equals(type)) {
                            value = Long.valueOf(valueText);
                        } else if ("float".equals(type)) {
                            value = Float.valueOf(valueText);
                        } else if ("double".equals(type)) {
                            value = Double.valueOf(valueText);
                        } else if ("boolean".equals(type)) {
                            // CHECKSTYLE:OFF
                            value = Boolean.valueOf(valueText);
                        // CHECKSTYLE:ON
                        } else if ("string".equals(type)) {
                            value = valueText;
                        } else if ("java-object".equals(type)) {
                            if (JivePropertiesManager.isJavaObjectEnabled()) {
                                try {
                                    byte[] bytes = Base64.decode(valueText);
                                    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
                                    value = in.readObject();
                                } catch (Exception e) {
                                    LOGGER.log(Level.SEVERE, "Error parsing java object", e);
                                }
                            } else {
                                LOG_OBJECT_NOT_ENABLED.once(() -> LOGGER.severe("JavaObject is not enabled. Enable with JivePropertiesManager.setJavaObjectEnabled(true)"));
                            }
                        }
                        if (name != null && value != null) {
                            properties.put(name, value);
                        }
                        done = true;
                    }
                }
            }
        } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
            if (parser.getName().equals(JivePropertiesExtension.ELEMENT)) {
                break;
            }
        }
    }
    return new JivePropertiesExtension(properties);
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) IOException(java.io.IOException) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) JivePropertiesExtension(org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension) ObjectInputStream(java.io.ObjectInputStream)

Example 5 with XmlPullParserException

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

the class MoodProvider method parse.

@Override
public MoodElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    String text = null;
    Mood mood = null;
    MoodConcretisation concretisation = null;
    outerloop: while (true) {
        XmlPullParser.Event tag = parser.next();
        switch(tag) {
            case START_ELEMENT:
                String name = parser.getName();
                String namespace = parser.getNamespace();
                if (MoodElement.ELEM_TEXT.equals(name)) {
                    text = parser.nextText();
                    continue outerloop;
                }
                if (!MoodElement.NAMESPACE.equals(namespace)) {
                    LOGGER.log(Level.FINE, "Foreign namespace " + namespace + " detected. Try to find suitable MoodConcretisationProvider.");
                    MoodConcretisationProvider<?> provider = (MoodConcretisationProvider<?>) ProviderManager.getExtensionProvider(name, namespace);
                    if (provider != null) {
                        concretisation = provider.parse(parser);
                    } else {
                        LOGGER.log(Level.FINE, "No provider for <" + name + " xmlns:'" + namespace + "'/> found. Ignore.");
                    }
                    continue outerloop;
                }
                try {
                    mood = Mood.valueOf(name);
                    continue outerloop;
                } catch (IllegalArgumentException e) {
                    throw new XmlPullParserException("Unknown mood value: " + name + " encountered.");
                }
            case END_ELEMENT:
                if (MoodElement.ELEMENT.equals(parser.getName())) {
                    MoodElement.MoodSubjectElement subjectElement = (mood == null && concretisation == null) ? null : new MoodElement.MoodSubjectElement(mood, concretisation);
                    return new MoodElement(subjectElement, text);
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
}
Also used : Mood(org.jivesoftware.smackx.mood.Mood) MoodConcretisation(org.jivesoftware.smackx.mood.element.MoodConcretisation) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) MoodElement(org.jivesoftware.smackx.mood.element.MoodElement)

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