Search in sources :

Example 16 with StanzaError

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

the class FileTransferNegotiator method selectStreamNegotiator.

/**
 * Selects an appropriate stream negotiator after examining the incoming file transfer request.
 *
 * @param request The related file transfer request.
 * @return The file transfer object that handles the transfer
 * @throws NoStreamMethodsOfferedException If there are either no stream methods contained in the packet, or
 *                       there is not an appropriate stream method.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws NoAcceptableTransferMechanisms if no acceptable transfer mechanisms are available
 * @throws InterruptedException if the calling thread was interrupted.
 */
public StreamNegotiator selectStreamNegotiator(FileTransferRequest request) throws NotConnectedException, NoStreamMethodsOfferedException, NoAcceptableTransferMechanisms, InterruptedException {
    StreamInitiation si = request.getStreamInitiation();
    ListSingleFormField streamMethodField = getStreamMethodField(si.getFeatureNegotiationForm());
    if (streamMethodField == null) {
        String errorMessage = "No stream methods contained in stanza.";
        StanzaError error = StanzaError.from(StanzaError.Condition.bad_request, errorMessage).build();
        IQ iqPacket = IQ.createErrorResponse(si, error);
        connection().sendStanza(iqPacket);
        throw new FileTransferException.NoStreamMethodsOfferedException();
    }
    // select the appropriate protocol
    StreamNegotiator selectedStreamNegotiator;
    try {
        selectedStreamNegotiator = getNegotiator(streamMethodField);
    } catch (NoAcceptableTransferMechanisms e) {
        IQ iqPacket = IQ.createErrorResponse(si, StanzaError.from(StanzaError.Condition.bad_request, "No acceptable transfer mechanism").build());
        connection().sendStanza(iqPacket);
        throw e;
    }
    return selectedStreamNegotiator;
}
Also used : StreamInitiation(org.jivesoftware.smackx.si.packet.StreamInitiation) NoStreamMethodsOfferedException(org.jivesoftware.smackx.filetransfer.FileTransferException.NoStreamMethodsOfferedException) ListSingleFormField(org.jivesoftware.smackx.xdata.ListSingleFormField) IQ(org.jivesoftware.smack.packet.IQ) NoAcceptableTransferMechanisms(org.jivesoftware.smackx.filetransfer.FileTransferException.NoAcceptableTransferMechanisms) StanzaError(org.jivesoftware.smack.packet.StanzaError)

Example 17 with StanzaError

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

the class FailureProvider method parse.

@Override
public Failure parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    Failure.CompressFailureError compressFailureError = null;
    StanzaError stanzaError = null;
    XmlEnvironment failureXmlEnvironment = XmlEnvironment.from(parser, xmlEnvironment);
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch(eventType) {
            case START_ELEMENT:
                String name = parser.getName();
                String namespace = parser.getNamespace();
                switch(namespace) {
                    case Failure.NAMESPACE:
                        compressFailureError = Failure.CompressFailureError.valueOf(name.replace("-", "_"));
                        if (compressFailureError == null) {
                            LOGGER.warning("Unknown element in " + Failure.NAMESPACE + ": " + name);
                        }
                        break;
                    case StreamOpen.CLIENT_NAMESPACE:
                    case StreamOpen.SERVER_NAMESPACE:
                        switch(name) {
                            case StanzaError.ERROR:
                                stanzaError = PacketParserUtils.parseError(parser, failureXmlEnvironment);
                                break;
                            default:
                                LOGGER.warning("Unknown element in " + namespace + ": " + name);
                                break;
                        }
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            // fall out
            default:
        }
    }
    return new Failure(compressFailureError, stanzaError);
}
Also used : Failure(org.jivesoftware.smack.compress.packet.Failure) StanzaError(org.jivesoftware.smack.packet.StanzaError) XmlEnvironment(org.jivesoftware.smack.packet.XmlEnvironment)

Example 18 with StanzaError

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

the class PacketParserUtilsTest method ensureNoNullLangInParsedDescriptiveTexts.

@Test
public void ensureNoNullLangInParsedDescriptiveTexts() throws Exception {
    final String text = "Dummy descriptive text";
    final String errorXml = XMLBuilder.create(StanzaError.ERROR).a("type", "cancel").up().element("internal-server-error", StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE).up().element("text", StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE).t(text).up().asString();
    XmlPullParser parser = TestUtils.getParser(errorXml);
    StanzaError error = PacketParserUtils.parseError(parser);
    assertEquals(text, error.getDescriptiveText());
}
Also used : XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) StanzaError(org.jivesoftware.smack.packet.StanzaError) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 19 with StanzaError

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

the class PacketParserUtilsTest method ensureNoEmptyLangInDescriptiveText.

@Test
public void ensureNoEmptyLangInDescriptiveText() throws Exception {
    final String text = "Dummy descriptive text";
    Map<String, String> texts = new HashMap<>();
    texts.put("", text);
    StanzaError error = StanzaError.getBuilder(StanzaError.Condition.internal_server_error).setDescriptiveTexts(texts).build();
    final String errorXml = XMLBuilder.create(StanzaError.ERROR).a("type", "cancel").up().element("internal-server-error", StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE).up().element("text", StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE).t(text).up().asString();
    assertXmlSimilar(errorXml, error.toXML(StreamOpen.CLIENT_NAMESPACE));
}
Also used : HashMap(java.util.HashMap) StanzaError(org.jivesoftware.smack.packet.StanzaError) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 20 with StanzaError

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

the class FailureProviderTest method withStanzaErrrorFailureTest.

@Test
public void withStanzaErrrorFailureTest() throws Exception {
    final String xml = "<failure xmlns='http://jabber.org/protocol/compress'>" + "<setup-failed/>" + "<error xmlns='jabber:client' type='modify'>" + "<bad-request xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" + "</error>" + "</failure>";
    final XmlPullParser parser = PacketParserUtils.getParserFor(xml);
    final Failure failure = FailureProvider.INSTANCE.parse(parser);
    assertEquals(Failure.CompressFailureError.setup_failed, failure.getCompressFailureError());
    final StanzaError error = failure.getStanzaError();
    assertEquals(Condition.bad_request, error.getCondition());
}
Also used : XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) Failure(org.jivesoftware.smack.compress.packet.Failure) StanzaError(org.jivesoftware.smack.packet.StanzaError) Test(org.junit.Test)

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