Java Code Examples for org.jivesoftware.smack.packet.Message#ELEMENT

The following examples show how to use org.jivesoftware.smack.packet.Message#ELEMENT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PacketParserUtils.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to parse and return either a Message, IQ or Presence stanza.
 *
 * connection is optional and is used to return feature-not-implemented errors for unknown IQ stanzas.
 *
 * @param parser TODO javadoc me please
 * @param outerXmlEnvironment the outer XML environment (optional).
 * @return a stanza which is either a Message, IQ or Presence.
 * @throws XmlPullParserException if an error in the XML parser occurred.
 * @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
 * @throws IOException if an I/O error occurred.
 */
public static Stanza parseStanza(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, SmackParsingException, IOException {
    ParserUtils.assertAtStartTag(parser);
    final String name = parser.getName();
    switch (name) {
    case Message.ELEMENT:
        return parseMessage(parser, outerXmlEnvironment);
    case IQ.IQ_ELEMENT:
        return parseIQ(parser, outerXmlEnvironment);
    case Presence.ELEMENT:
        return parsePresence(parser, outerXmlEnvironment);
    default:
        throw new IllegalArgumentException("Can only parse message, iq or presence, not " + name);
    }
}
 
Example 2
Source File: ModularXmppClientToServerConnection.java    From Smack with Apache License 2.0 4 votes vote down vote up
protected 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;
            default: // fall out
            }
            event = parser.next();
        }
    } catch (XmlPullParserException | IOException | InterruptedException | StreamErrorException
                    | SmackParsingException e) {
        notifyConnectionError(e);
    }
}
 
Example 3
Source File: XMPPBOSHConnection.java    From Smack with Apache License 2.0 4 votes vote down vote up
/**
 * Parse the received packets and notify the corresponding connection.
 *
 * @param event the BOSH client response which includes the received packet.
 */
@Override
public void responseReceived(BOSHMessageEvent event) {
    AbstractBody body = event.getBody();
    if (body != null) {
        try {
            if (sessionID == null) {
                sessionID = body.getAttribute(BodyQName.create(XMPPBOSHConnection.BOSH_URI, "sid"));
            }
            if (streamId == null) {
                streamId = body.getAttribute(BodyQName.create(XMPPBOSHConnection.BOSH_URI, "authid"));
            }
            final XmlPullParser parser = PacketParserUtils.getParserFor(body.toXML());

            XmlPullParser.Event eventType = parser.getEventType();
            do {
                eventType = parser.next();
                switch (eventType) {
                case START_ELEMENT:
                    String name = parser.getName();
                    switch (name) {
                    case Message.ELEMENT:
                    case IQ.IQ_ELEMENT:
                    case Presence.ELEMENT:
                        parseAndProcessStanza(parser);
                        break;
                    case "features":
                        parseFeatures(parser);
                        break;
                    case "error":
                        // Some BOSH error isn't stream error.
                        if ("urn:ietf:params:xml:ns:xmpp-streams".equals(parser.getNamespace(null))) {
                            throw new StreamErrorException(PacketParserUtils.parseStreamError(parser));
                        } else {
                            StanzaError stanzaError = PacketParserUtils.parseError(parser);
                            throw new XMPPException.XMPPErrorException(null, stanzaError);
                        }
                    default:
                        parseAndProcessNonza(parser);
                        break;
                    }
                    break;
                default:
                    // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                    break;
                }
            }
            while (eventType != XmlPullParser.Event.END_DOCUMENT);
        }
        catch (Exception e) {
            if (isConnected()) {
                notifyConnectionError(e);
            }
        }
    }
}
 
Example 4
Source File: ForwardedProvider.java    From Smack with Apache License 2.0 4 votes vote down vote up
@Override
public Forwarded parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    DelayInformation di = null;
    Stanza packet = null;

    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch (eventType) {
        case START_ELEMENT:
            String name = parser.getName();
            String namespace = parser.getNamespace();
            switch (name) {
            case DelayInformation.ELEMENT:
                if (DelayInformation.NAMESPACE.equals(namespace)) {
                    di = DelayInformationProvider.INSTANCE.parse(parser, parser.getDepth(), null);
                } else {
                    LOGGER.warning("Namespace '" + namespace + "' does not match expected namespace '"
                                    + DelayInformation.NAMESPACE + "'");
                }
                break;
            case Message.ELEMENT:
                packet = PacketParserUtils.parseMessage(parser);
                break;
            default:
                LOGGER.warning("Unsupported forwarded packet type: " + name);
            }
            break;
        case END_ELEMENT:
            if (parser.getDepth() == initialDepth) {
                break outerloop;
            }
            break;
        default:
            // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
            break;
        }
    }

    if (packet == null) {
        // TODO: Should be SmackParseException.
        throw new IOException("forwarded extension must contain a packet");
    }
    return new Forwarded(di, packet);
}