Search in sources :

Example 1 with IQProvider

use of org.jivesoftware.smack.provider.IQProvider in project ecf by eclipse.

the class SimpleDirectoryPersistentCache method restoreInfoFromFile.

/**
 * Tries to restore an DiscoverInfo packet from a file.
 *
 * @param file
 * @return
 * @throws IOException
 */
private static DiscoverInfo restoreInfoFromFile(File file) throws IOException {
    DataInputStream dis = new DataInputStream(new FileInputStream(file));
    String fileContent = null;
    String id;
    String from;
    String to;
    try {
        fileContent = dis.readUTF();
    } finally {
        dis.close();
    }
    if (fileContent == null)
        return null;
    Reader reader = new StringReader(fileContent);
    XmlPullParser parser;
    try {
        parser = new MXParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        parser.setInput(reader);
    } catch (XmlPullParserException xppe) {
        xppe.printStackTrace();
        return null;
    }
    DiscoverInfo iqPacket;
    IQProvider provider = new DiscoverInfoProvider();
    // Parse the IQ, we only need the id
    try {
        parser.next();
        id = parser.getAttributeValue("", "id");
        from = parser.getAttributeValue("", "from");
        to = parser.getAttributeValue("", "to");
        parser.next();
    } catch (XmlPullParserException e1) {
        return null;
    }
    try {
        iqPacket = (DiscoverInfo) provider.parseIQ(parser);
    } catch (Exception e) {
        return null;
    }
    iqPacket.setPacketID(id);
    iqPacket.setFrom(from);
    iqPacket.setTo(to);
    iqPacket.setType(IQ.Type.RESULT);
    return iqPacket;
}
Also used : DiscoverInfo(org.jivesoftware.smackx.packet.DiscoverInfo) MXParser(org.xmlpull.mxp1.MXParser) DiscoverInfoProvider(org.jivesoftware.smackx.provider.DiscoverInfoProvider) StringReader(java.io.StringReader) XmlPullParser(org.xmlpull.v1.XmlPullParser) Reader(java.io.Reader) StringReader(java.io.StringReader) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IQProvider(org.jivesoftware.smack.provider.IQProvider)

Example 2 with IQProvider

use of org.jivesoftware.smack.provider.IQProvider in project ecf by eclipse.

the class PacketParserUtils method parseIQ.

/**
 * Parses an IQ packet.
 *
 * @param parser the XML parser, positioned at the start of an IQ packet.
 * @return an IQ object.
 * @throws Exception if an exception occurs while parsing the packet.
 */
public static IQ parseIQ(XmlPullParser parser, Connection connection) throws Exception {
    IQ iqPacket = null;
    String id = parser.getAttributeValue("", "id");
    String to = parser.getAttributeValue("", "to");
    String from = parser.getAttributeValue("", "from");
    IQ.Type type = IQ.Type.fromString(parser.getAttributeValue("", "type"));
    XMPPError error = null;
    boolean done = false;
    while (!done) {
        int eventType = parser.next();
        if (eventType == XmlPullParser.START_TAG) {
            String elementName = parser.getName();
            String namespace = parser.getNamespace();
            if (elementName.equals("error")) {
                error = PacketParserUtils.parseError(parser);
            } else if (elementName.equals("query") && namespace.equals("jabber:iq:auth")) {
                iqPacket = parseAuthentication(parser);
            } else if (elementName.equals("query") && namespace.equals("jabber:iq:roster")) {
                iqPacket = parseRoster(parser);
            } else if (elementName.equals("query") && namespace.equals("jabber:iq:register")) {
                iqPacket = parseRegistration(parser);
            } else if (elementName.equals("bind") && namespace.equals("urn:ietf:params:xml:ns:xmpp-bind")) {
                iqPacket = parseResourceBinding(parser);
            } else // Otherwise, see if there is a registered provider for
            // this element name and namespace.
            {
                Object provider = ProviderManager.getInstance().getIQProvider(elementName, namespace);
                if (provider != null) {
                    if (provider instanceof IQProvider) {
                        iqPacket = ((IQProvider) provider).parseIQ(parser);
                    } else if (provider instanceof Class) {
                        iqPacket = (IQ) PacketParserUtils.parseWithIntrospection(elementName, (Class<?>) provider, parser);
                    }
                } else // have to be answered with an IQ error response. See the code a few lines below
                if (IQ.Type.RESULT == type) {
                    // No Provider found for the IQ stanza, parse it to an UnparsedIQ instance
                    // so that the content of the IQ can be examined later on
                    iqPacket = new UnparsedResultIQ(parseContent(parser));
                }
            }
        } else if (eventType == XmlPullParser.END_TAG) {
            if (parser.getName().equals("iq")) {
                done = true;
            }
        }
    }
    // Decide what to do when an IQ packet was not understood
    if (iqPacket == null) {
        if (IQ.Type.GET == type || IQ.Type.SET == type) {
            // If the IQ stanza is of type "get" or "set" containing a child element
            // qualified by a namespace it does not understand, then answer an IQ of
            // type "error" with code 501 ("feature-not-implemented")
            iqPacket = new IQ() {

                @Override
                public String getChildElementXML() {
                    return null;
                }
            };
            iqPacket.setPacketID(id);
            iqPacket.setTo(from);
            iqPacket.setFrom(to);
            iqPacket.setType(IQ.Type.ERROR);
            iqPacket.setError(new XMPPError(XMPPError.Condition.feature_not_implemented));
            connection.sendPacket(iqPacket);
            return null;
        } else {
            // If an IQ packet wasn't created above, create an empty IQ packet.
            iqPacket = new IQ() {

                @Override
                public String getChildElementXML() {
                    return null;
                }
            };
        }
    }
    // Set basic values on the iq packet.
    iqPacket.setPacketID(id);
    iqPacket.setTo(to);
    iqPacket.setFrom(from);
    iqPacket.setType(type);
    iqPacket.setError(error);
    return iqPacket;
}
Also used : IQ(org.jivesoftware.smack.packet.IQ) XMPPError(org.jivesoftware.smack.packet.XMPPError) IQProvider(org.jivesoftware.smack.provider.IQProvider)

Example 3 with IQProvider

use of org.jivesoftware.smack.provider.IQProvider in project Smack by igniterealtime.

the class PacketParserUtils method parseIQ.

/**
 * Parses an IQ packet.
 *
 * @param parser the XML parser, positioned at the start of an IQ packet.
 * @param outerXmlEnvironment the outer XML environment (optional).
 * @return an IQ object.
 * @throws XmlPullParserException if an error in the XML parser occurred.
 * @throws XmppStringprepException if the provided string is invalid.
 * @throws IOException if an I/O error occurred.
 * @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
 */
public static IQ parseIQ(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, XmppStringprepException, IOException, SmackParsingException {
    ParserUtils.assertAtStartTag(parser);
    final int initialDepth = parser.getDepth();
    XmlEnvironment iqXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
    IQ iqPacket = null;
    StanzaError error = null;
    IqData iqData = parseIqData(parser);
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch(eventType) {
            case START_ELEMENT:
                String elementName = parser.getName();
                String namespace = parser.getNamespace();
                switch(elementName) {
                    case "error":
                        error = PacketParserUtils.parseError(parser, iqXmlEnvironment);
                        break;
                    // this element name and namespace.
                    default:
                        IqProvider<IQ> provider = ProviderManager.getIQProvider(elementName, namespace);
                        if (provider != null) {
                            iqPacket = provider.parse(parser, iqData, outerXmlEnvironment);
                        } else // Note that if we reach this code, it is guaranteed that the result IQ contained a child element
                        // (RFC 6120 § 8.2.3 6) because otherwise we would have reached the END_ELEMENT first.
                        {
                            // No Provider found for the IQ stanza, parse it to an UnparsedIQ instance
                            // so that the content of the IQ can be examined later on
                            iqPacket = new UnparsedIQ(elementName, namespace, parseElement(parser));
                        }
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    // Decide what to do when an IQ packet was not understood
    if (iqPacket == null) {
        switch(iqData.getType()) {
            case error:
                // If an IQ packet wasn't created above, create an empty error IQ packet.
                iqPacket = new ErrorIQ(error);
                break;
            case result:
                iqPacket = new EmptyResultIQ();
                break;
            default:
                break;
        }
    }
    // Set basic values on the iq packet.
    iqPacket.setStanzaId(iqData.getStanzaId());
    iqPacket.setTo(iqData.getTo());
    iqPacket.setFrom(iqData.getFrom());
    iqPacket.setType(iqData.getType());
    iqPacket.setError(error);
    return iqPacket;
}
Also used : ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) UnparsedIQ(org.jivesoftware.smack.packet.UnparsedIQ) EmptyResultIQ(org.jivesoftware.smack.packet.EmptyResultIQ) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) UnparsedIQ(org.jivesoftware.smack.packet.UnparsedIQ) IQ(org.jivesoftware.smack.packet.IQ) IqData(org.jivesoftware.smack.packet.IqData) EmptyResultIQ(org.jivesoftware.smack.packet.EmptyResultIQ) IqProvider(org.jivesoftware.smack.provider.IqProvider) XmlEnvironment(org.jivesoftware.smack.packet.XmlEnvironment) StanzaError(org.jivesoftware.smack.packet.StanzaError)

Example 4 with IQProvider

use of org.jivesoftware.smack.provider.IQProvider in project Smack by igniterealtime.

the class SmackTestUtil method parse.

@SuppressWarnings("unchecked")
public static <E extends Element> E parse(Reader reader, AbstractProvider<E> abstractProvider, XmlPullParserKind parserKind) throws XmlPullParserException, IOException, SmackParsingException {
    XmlPullParser parser = getParserFor(reader, parserKind);
    final E element;
    if (abstractProvider instanceof Provider) {
        Provider<E> provider = (Provider<E>) abstractProvider;
        element = provider.parse(parser);
    } else if (abstractProvider instanceof IqProvider) {
        IqData iqData = PacketParserUtils.parseIqData(parser);
        parser.next();
        ParserUtils.forwardToStartElement(parser);
        IqProvider<?> iqProvider = (IqProvider<?>) abstractProvider;
        element = (E) iqProvider.parse(parser, iqData);
    } else {
        throw new AssertionError();
    }
    return element;
}
Also used : XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) IqData(org.jivesoftware.smack.packet.IqData) IqProvider(org.jivesoftware.smack.provider.IqProvider) Provider(org.jivesoftware.smack.provider.Provider) AbstractProvider(org.jivesoftware.smack.provider.AbstractProvider) IqProvider(org.jivesoftware.smack.provider.IqProvider)

Example 5 with IQProvider

use of org.jivesoftware.smack.provider.IQProvider in project Smack by igniterealtime.

the class PacketParserUtils method parseIQ.

/**
     * Parses an IQ packet.
     *
     * @param parser the XML parser, positioned at the start of an IQ packet.
     * @return an IQ object.
     * @throws Exception
     */
public static IQ parseIQ(XmlPullParser parser) throws Exception {
    ParserUtils.assertAtStartTag(parser);
    final int initialDepth = parser.getDepth();
    IQ iqPacket = null;
    XMPPError.Builder error = null;
    final String id = parser.getAttributeValue("", "id");
    final Jid to = ParserUtils.getJidAttribute(parser, "to");
    final Jid from = ParserUtils.getJidAttribute(parser, "from");
    final IQ.Type type = IQ.Type.fromString(parser.getAttributeValue("", "type"));
    outerloop: while (true) {
        int eventType = parser.next();
        switch(eventType) {
            case XmlPullParser.START_TAG:
                String elementName = parser.getName();
                String namespace = parser.getNamespace();
                switch(elementName) {
                    case "error":
                        error = PacketParserUtils.parseError(parser);
                        break;
                    // this element name and namespace.
                    default:
                        IQProvider<IQ> provider = ProviderManager.getIQProvider(elementName, namespace);
                        if (provider != null) {
                            iqPacket = provider.parse(parser);
                        } else // Note that if we reach this code, it is guranteed that the result IQ contained a child element
                        // (RFC 6120 § 8.2.3 6) because otherwhise we would have reached the END_TAG first.
                        {
                            // No Provider found for the IQ stanza, parse it to an UnparsedIQ instance
                            // so that the content of the IQ can be examined later on
                            iqPacket = new UnparsedIQ(elementName, namespace, parseElement(parser));
                        }
                        break;
                }
                break;
            case XmlPullParser.END_TAG:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
        }
    }
    // Decide what to do when an IQ packet was not understood
    if (iqPacket == null) {
        switch(type) {
            case error:
                // If an IQ packet wasn't created above, create an empty error IQ packet.
                iqPacket = new ErrorIQ(error);
                break;
            case result:
                iqPacket = new EmptyResultIQ();
                break;
            default:
                break;
        }
    }
    // Set basic values on the iq packet.
    iqPacket.setStanzaId(id);
    iqPacket.setTo(to);
    iqPacket.setFrom(from);
    iqPacket.setType(type);
    iqPacket.setError(error);
    return iqPacket;
}
Also used : ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) Jid(org.jxmpp.jid.Jid) UnparsedIQ(org.jivesoftware.smack.packet.UnparsedIQ) EmptyResultIQ(org.jivesoftware.smack.packet.EmptyResultIQ) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) UnparsedIQ(org.jivesoftware.smack.packet.UnparsedIQ) IQ(org.jivesoftware.smack.packet.IQ) EmptyResultIQ(org.jivesoftware.smack.packet.EmptyResultIQ) XMPPError(org.jivesoftware.smack.packet.XMPPError) IQProvider(org.jivesoftware.smack.provider.IQProvider)

Aggregations

IQProvider (org.jivesoftware.smack.provider.IQProvider)4 IQ (org.jivesoftware.smack.packet.IQ)3 EmptyResultIQ (org.jivesoftware.smack.packet.EmptyResultIQ)2 ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)2 IqData (org.jivesoftware.smack.packet.IqData)2 UnparsedIQ (org.jivesoftware.smack.packet.UnparsedIQ)2 XMPPError (org.jivesoftware.smack.packet.XMPPError)2 IqProvider (org.jivesoftware.smack.provider.IqProvider)2 DataInputStream (java.io.DataInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 StanzaError (org.jivesoftware.smack.packet.StanzaError)1 XmlEnvironment (org.jivesoftware.smack.packet.XmlEnvironment)1 AbstractProvider (org.jivesoftware.smack.provider.AbstractProvider)1 Provider (org.jivesoftware.smack.provider.Provider)1 XmlPullParser (org.jivesoftware.smack.xml.XmlPullParser)1 DiscoverInfo (org.jivesoftware.smackx.packet.DiscoverInfo)1 DiscoverInfoProvider (org.jivesoftware.smackx.provider.DiscoverInfoProvider)1