Search in sources :

Example 1 with StanzaError

use of org.jivesoftware.smack.packet.StanzaError in project Smack by igniterealtime.

the class PacketParserUtils method parseError.

/**
 * Parses error sub-packets.
 *
 * @param parser the XML parser.
 * @param outerXmlEnvironment the outer XML environment (optional).
 * @return an error sub-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 StanzaError parseError(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    final int initialDepth = parser.getDepth();
    Map<String, String> descriptiveTexts = null;
    XmlEnvironment stanzaErrorXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
    List<XmlElement> extensions = new ArrayList<>();
    StanzaError.Builder builder = StanzaError.getBuilder();
    // Parse the error header
    builder.setType(StanzaError.Type.fromString(parser.getAttributeValue("", "type")));
    builder.setErrorGenerator(parser.getAttributeValue("", "by"));
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch(eventType) {
            case START_ELEMENT:
                String name = parser.getName();
                String namespace = parser.getNamespace();
                switch(namespace) {
                    case StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE:
                        switch(name) {
                            case Stanza.TEXT:
                                descriptiveTexts = parseDescriptiveTexts(parser, descriptiveTexts);
                                break;
                            default:
                                builder.setCondition(StanzaError.Condition.fromString(name));
                                String conditionText = parser.nextText();
                                if (!conditionText.isEmpty()) {
                                    builder.setConditionText(conditionText);
                                }
                                break;
                        }
                        break;
                    default:
                        PacketParserUtils.addExtensionElement(extensions, parser, name, namespace, stanzaErrorXmlEnvironment);
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    builder.setExtensions(extensions).setDescriptiveTexts(descriptiveTexts);
    return builder.build();
}
Also used : ArrayList(java.util.ArrayList) XmlElement(org.jivesoftware.smack.packet.XmlElement) XmlEnvironment(org.jivesoftware.smack.packet.XmlEnvironment) StanzaError(org.jivesoftware.smack.packet.StanzaError)

Example 2 with StanzaError

use of org.jivesoftware.smack.packet.StanzaError 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 3 with StanzaError

use of org.jivesoftware.smack.packet.StanzaError in project Smack by igniterealtime.

the class JidPrepManager method requestJidPrep.

public String requestJidPrep(Jid jidPrepService, String jidToBePrepped) throws NoResponseException, NotConnectedException, InterruptedException, XMPPErrorException {
    JidPrepIq jidPrepRequest = new JidPrepIq(jidToBePrepped);
    jidPrepRequest.setTo(jidPrepService);
    JidPrepIq jidPrepResponse;
    try {
        jidPrepResponse = connection().sendIqRequestAndWaitForResponse(jidPrepRequest);
    } catch (XMPPErrorException e) {
        StanzaError stanzaError = e.getStanzaError();
        if (stanzaError.getCondition() == StanzaError.Condition.jid_malformed) {
            // or if the JID to prep was malformed. Assume the later is the case and return 'null'.
            return null;
        }
        throw e;
    }
    return jidPrepResponse.getJid();
}
Also used : XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) JidPrepIq(org.jivesoftware.smackx.jid_prep.element.JidPrepIq) StanzaError(org.jivesoftware.smack.packet.StanzaError)

Example 4 with StanzaError

use of org.jivesoftware.smack.packet.StanzaError in project Smack by igniterealtime.

the class Socks5BytestreamRequest method cancelRequest.

/**
 * Cancels the SOCKS5 Bytestream request by sending an error to the initiator and building a
 * XMPP exception.
 *
 * @param streamHostsExceptions the stream hosts and their exceptions.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws CouldNotConnectToAnyProvidedSocks5Host as expected result.
 * @throws NoSocks5StreamHostsProvided if no stream host was provided.
 */
private void cancelRequest(Map<StreamHost, Exception> streamHostsExceptions) throws NotConnectedException, InterruptedException, CouldNotConnectToAnyProvidedSocks5Host, NoSocks5StreamHostsProvided {
    final Socks5Exception.NoSocks5StreamHostsProvided noHostsProvidedException;
    final Socks5Exception.CouldNotConnectToAnyProvidedSocks5Host couldNotConnectException;
    final String errorMessage;
    if (streamHostsExceptions.isEmpty()) {
        noHostsProvidedException = new Socks5Exception.NoSocks5StreamHostsProvided();
        couldNotConnectException = null;
        errorMessage = noHostsProvidedException.getMessage();
    } else {
        noHostsProvidedException = null;
        couldNotConnectException = Socks5Exception.CouldNotConnectToAnyProvidedSocks5Host.construct(streamHostsExceptions);
        errorMessage = couldNotConnectException.getMessage();
    }
    StanzaError error = StanzaError.from(StanzaError.Condition.item_not_found, errorMessage).build();
    IQ errorIQ = IQ.createErrorResponse(this.bytestreamRequest, error);
    this.manager.getConnection().sendStanza(errorIQ);
    if (noHostsProvidedException != null) {
        throw noHostsProvidedException;
    } else {
        throw couldNotConnectException;
    }
}
Also used : NoSocks5StreamHostsProvided(org.jivesoftware.smackx.bytestreams.socks5.Socks5Exception.NoSocks5StreamHostsProvided) IQ(org.jivesoftware.smack.packet.IQ) CouldNotConnectToAnyProvidedSocks5Host(org.jivesoftware.smackx.bytestreams.socks5.Socks5Exception.CouldNotConnectToAnyProvidedSocks5Host) StanzaError(org.jivesoftware.smack.packet.StanzaError)

Example 5 with StanzaError

use of org.jivesoftware.smack.packet.StanzaError in project Smack by igniterealtime.

the class Socks5BytestreamManager method replyRejectPacket.

/**
 * Responses to the given packet's sender with an XMPP error that a SOCKS5 Bytestream is not
 * accepted.
 * <p>
 * Specified in XEP-65 5.3.1 (Example 13)
 * </p>
 *
 * @param packet Stanza that should be answered with a not-acceptable error
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
void replyRejectPacket(IQ packet) throws NotConnectedException, InterruptedException {
    StanzaError xmppError = StanzaError.getBuilder(StanzaError.Condition.not_acceptable).build();
    IQ errorIQ = IQ.createErrorResponse(packet, xmppError);
    connection().sendStanza(errorIQ);
}
Also used : IQ(org.jivesoftware.smack.packet.IQ) StanzaError(org.jivesoftware.smack.packet.StanzaError)

Aggregations

StanzaError (org.jivesoftware.smack.packet.StanzaError)21 IQ (org.jivesoftware.smack.packet.IQ)7 Test (org.junit.jupiter.api.Test)5 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)4 ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)3 XmlEnvironment (org.jivesoftware.smack.packet.XmlEnvironment)3 XmlPullParser (org.jivesoftware.smack.xml.XmlPullParser)3 AdHocCommandData (org.jivesoftware.smackx.commands.packet.AdHocCommandData)3 Failure (org.jivesoftware.smack.compress.packet.Failure)2 EmptyResultIQ (org.jivesoftware.smack.packet.EmptyResultIQ)2 Action (org.jivesoftware.smackx.commands.AdHocCommand.Action)2 Identity (org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity)2 DiscoverInfoBuilder (org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder)2 Test (org.junit.Test)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ThreadedDummyConnection (org.jivesoftware.smack.ThreadedDummyConnection)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1