Search in sources :

Example 1 with XmlPullParser

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

the class AbstractXMPPConnection method parseFeatures.

protected final void parseFeatures(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
    streamFeatures.clear();
    final int initialDepth = parser.getDepth();
    while (true) {
        XmlPullParser.Event eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getDepth() == initialDepth + 1) {
            XmlElement streamFeature = null;
            String name = parser.getName();
            String namespace = parser.getNamespace();
            switch(name) {
                case StartTls.ELEMENT:
                    streamFeature = PacketParserUtils.parseStartTlsFeature(parser);
                    break;
                case Mechanisms.ELEMENT:
                    streamFeature = new Mechanisms(PacketParserUtils.parseMechanisms(parser));
                    break;
                case Bind.ELEMENT:
                    streamFeature = Bind.Feature.INSTANCE;
                    break;
                case Session.ELEMENT:
                    streamFeature = PacketParserUtils.parseSessionFeature(parser);
                    break;
                case Compress.Feature.ELEMENT:
                    streamFeature = PacketParserUtils.parseCompressionFeature(parser);
                    break;
                default:
                    ExtensionElementProvider<ExtensionElement> provider = ProviderManager.getStreamFeatureProvider(name, namespace);
                    if (provider != null) {
                        streamFeature = provider.parse(parser, incomingStreamXmlEnvironment);
                    }
                    break;
            }
            if (streamFeature != null) {
                addStreamFeature(streamFeature);
            }
        } else if (eventType == XmlPullParser.Event.END_ELEMENT && parser.getDepth() == initialDepth) {
            break;
        }
    }
}
Also used : Mechanisms(org.jivesoftware.smack.packet.Mechanisms) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) XmlElement(org.jivesoftware.smack.packet.XmlElement)

Example 2 with XmlPullParser

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

the class PrivacyProviderTest method getParserFromXML.

private XmlPullParser getParserFromXML(String xml) throws XmlPullParserException {
    XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    parser.setInput(new StringReader(xml));
    return parser;
}
Also used : XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) StringReader(java.io.StringReader)

Example 3 with XmlPullParser

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

the class PrivacyProviderTest method testDeclineLists.

/**
 * Check the parser with an xml with empty lists. It includes the active,
 * default and special list.
 * To create the xml string based from an xml file, replace:\n with:	"\n  + "
 */
public void testDeclineLists() {
    // Make the XML to test
    String xml = "" + "  <iq type='result' id='getlist1' to='romeo@example.net/orchard'>	" + "  <query xmlns='jabber:iq:privacy'>	" + "    <active/>	" + "    <default/>	" + "  </query>	" + " </iq>	";
    try {
        // Create the xml parser
        XmlPullParser parser = getParserFromXML(xml);
        // Create a packet from the xml
        Privacy packet = (Privacy) (new PrivacyProvider()).parse(parser);
        assertNotNull(packet);
        assertEquals(null, packet.getDefaultName());
        assertEquals(null, packet.getActiveName());
        assertEquals(true, packet.isDeclineActiveList());
        assertEquals(true, packet.isDeclineDefaultList());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : PrivacyProvider(org.jivesoftware.smack.provider.PrivacyProvider) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException)

Example 4 with XmlPullParser

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

the class SmackTestCase method parseURL.

/**
 * Returns true if the given URL was found and parsed without problems. The file provided
 * by the URL must contain information useful for the test case configuration, such us,
 * host and port of the server.
 *
 * @param url the url of the file to parse.
 * @return true if the given URL was found and parsed without problems.
 */
private boolean parseURL(URL url) {
    boolean parsedOK = false;
    InputStream systemStream = null;
    try {
        systemStream = url.openStream();
        XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        parser.setInput(systemStream, "UTF-8");
        XmlPullParser.Event eventType = parser.getEventType();
        do {
            if (eventType == START_ELEMENT) {
                if (parser.getName().equals("host")) {
                    host = parser.nextText();
                } else if (parser.getName().equals("port")) {
                    port = parseIntProperty(parser, port);
                } else if (parser.getName().equals("serviceName")) {
                    serviceName = parser.nextText();
                } else if (parser.getName().equals("chat")) {
                    chatDomain = parser.nextText();
                } else if (parser.getName().equals("muc")) {
                    mucDomain = parser.nextText();
                } else if (parser.getName().equals("username")) {
                    usernamePrefix = parser.nextText();
                } else if (parser.getName().equals("password")) {
                    samePassword = "true".equals(parser.getAttributeValue(0));
                    passwordPrefix = parser.nextText();
                } else if (parser.getName().equals("testAnonymousLogin")) {
                    testAnonymousLogin = "true".equals(parser.nextText());
                } else if (parser.getName().equals("accountCreationParameters")) {
                    int numAttributes = parser.getAttributeCount();
                    String key = null;
                    String value = null;
                    for (int i = 0; i < numAttributes; i++) {
                        key = parser.getAttributeName(i);
                        value = parser.getAttributeValue(i);
                        accountCreationParameters.put(key, value);
                    }
                } else if (parser.getName().equals("compressionEnabled")) {
                    compressionEnabled = "true".equals(parser.nextText());
                }
            }
            eventType = parser.next();
        } while (eventType != END_DOCUMENT);
        parsedOK = true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            systemStream.close();
        } catch (Exception e) {
        /* Do Nothing */
        }
    }
    return parsedOK;
}
Also used : InputStream(java.io.InputStream) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) XMPPException(org.jivesoftware.smack.XMPPException)

Example 5 with XmlPullParser

use of org.jivesoftware.smack.xml.XmlPullParser 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)

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