Search in sources :

Example 1 with ErrorIQ

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

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

the class Socks5ClientForInitiatorTest method shouldFailIfActivateSocks5ProxyFails.

/**
 * If the initiator can connect to a SOCKS5 proxy but activating the stream fails an exception
 * should be thrown.
 *
 * @throws Exception should not happen
 */
@Test
public void shouldFailIfActivateSocks5ProxyFails() throws Exception {
    Protocol protocol = new Protocol();
    XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
    // build error response as reply to the stream activation
    IQ error = new ErrorIQ(StanzaError.getBuilder(StanzaError.Condition.internal_server_error).build());
    error.setFrom(proxyJID);
    error.setTo(initiatorJID);
    protocol.addResponse(error, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
    // start a local SOCKS5 proxy
    try (Socks5TestProxy socks5Proxy = new Socks5TestProxy()) {
        StreamHost streamHost = new StreamHost(proxyJID, loopbackAddress, socks5Proxy.getPort());
        // create digest to get the socket opened by target
        String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
        Socks5ClientForInitiator socks5Client = new Socks5ClientForInitiator(streamHost, digest, connection, sessionID, targetJID);
        try {
            socks5Client.getSocket(GET_SOCKET_TIMEOUT);
            fail("exception should be thrown");
        } catch (XMPPErrorException e) {
            assertTrue(StanzaError.Condition.internal_server_error.equals(e.getStanzaError().getCondition()));
            protocol.verifyAll();
        }
    }
}
Also used : ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) EmptyResultIQ(org.jivesoftware.smack.packet.EmptyResultIQ) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) IQ(org.jivesoftware.smack.packet.IQ) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Protocol(org.jivesoftware.util.Protocol) StreamHost(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost) Test(org.junit.jupiter.api.Test)

Example 3 with ErrorIQ

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

the class Socks5ByteStreamManagerTest method shouldFailIfTargetDoesNotAcceptSocks5Bytestream.

/**
 * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should fail if the
 * target does not accept a SOCKS5 Bytestream. See <a
 * href="http://xmpp.org/extensions/xep-0065.html#usecase-alternate">XEP-0065 Section 5.2 A2</a>
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws XMPPException if an XMPP protocol error was received.
 * @throws IOException if an I/O error occurred.
 */
@Test
public void shouldFailIfTargetDoesNotAcceptSocks5Bytestream() throws SmackException, InterruptedException, IOException, XMPPException {
    final Protocol protocol = new Protocol();
    final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
    final String sessionID = "session_id_shouldFailIfTargetDoesNotAcceptSocks5Bytestream";
    // get Socks5ByteStreamManager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    byteStreamManager.setAnnounceLocalStreamHost(false);
    /**
     * create responses in the order they should be queried specified by the XEP-0065
     * specification
     */
    // build discover info that supports the SOCKS5 feature
    DiscoverInfoBuilder discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID);
    discoverInfo.addFeature(Bytestream.NAMESPACE);
    // return that SOCKS5 is supported if target is queried
    protocol.addResponse(discoverInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build discover items containing a proxy item
    DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer, initiatorJID);
    Item item = new Item(proxyJID);
    discoverItems.addItem(item);
    // return the proxy item if XMPP server is queried
    protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build discover info for proxy containing information about being a SOCKS5 proxy
    DiscoverInfoBuilder proxyInfo = Socks5PacketUtils.createDiscoverInfo(proxyJID, initiatorJID);
    Identity identity = new Identity("proxy", proxyJID.toString(), "bytestreams");
    proxyInfo.addIdentity(identity);
    // return the socks5 bytestream proxy identity if proxy is queried
    protocol.addResponse(proxyInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build a socks5 stream host info containing the address and the port of the
    // proxy
    Bytestream streamHostInfo = Socks5PacketUtils.createBytestreamResponse(proxyJID, initiatorJID);
    streamHostInfo.addStreamHost(proxyJID, proxyAddress, 7778);
    // return stream host info if it is queried
    protocol.addResponse(streamHostInfo, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build error packet to reject SOCKS5 Bytestream
    StanzaError stanzaError = StanzaError.getBuilder(StanzaError.Condition.not_acceptable).build();
    IQ rejectPacket = new ErrorIQ(stanzaError);
    rejectPacket.setFrom(targetJID);
    rejectPacket.setTo(initiatorJID);
    // return error packet as response to the bytestream initiation
    protocol.addResponse(rejectPacket, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
    XMPPErrorException e = assertThrows(XMPPErrorException.class, () -> {
        // start SOCKS5 Bytestream
        byteStreamManager.establishSession(targetJID, sessionID);
    });
    protocol.verifyAll();
    assertEquals(rejectPacket.getError(), e.getStanzaError());
}
Also used : ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) DiscoverInfoBuilder(org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) IQ(org.jivesoftware.smack.packet.IQ) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) XMPPConnection(org.jivesoftware.smack.XMPPConnection) StanzaError(org.jivesoftware.smack.packet.StanzaError) Item(org.jivesoftware.smackx.disco.packet.DiscoverItems.Item) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) Protocol(org.jivesoftware.util.Protocol) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity) Test(org.junit.jupiter.api.Test)

Example 4 with ErrorIQ

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

the class IBBPacketUtils method createErrorIQ.

/**
 * Returns an error IQ.
 *
 * @param from the senders JID
 * @param to the recipients JID
 * @param condition the XMPP error condition
 * @return an error IQ
 */
public static IQ createErrorIQ(Jid from, Jid to, StanzaError.Condition condition) {
    StanzaError xmppError = StanzaError.getBuilder(condition).build();
    IQ errorIQ = new ErrorIQ(xmppError);
    errorIQ.setType(IQ.Type.error);
    errorIQ.setFrom(from);
    errorIQ.setTo(to);
    return errorIQ;
}
Also used : ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) EmptyResultIQ(org.jivesoftware.smack.packet.EmptyResultIQ) IQ(org.jivesoftware.smack.packet.IQ) StanzaError(org.jivesoftware.smack.packet.StanzaError)

Example 5 with ErrorIQ

use of org.jivesoftware.smack.packet.ErrorIQ 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

ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)8 IQ (org.jivesoftware.smack.packet.IQ)7 EmptyResultIQ (org.jivesoftware.smack.packet.EmptyResultIQ)5 StanzaError (org.jivesoftware.smack.packet.StanzaError)4 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)3 XMPPConnection (org.jivesoftware.smack.XMPPConnection)2 UnparsedIQ (org.jivesoftware.smack.packet.UnparsedIQ)2 XMPPError (org.jivesoftware.smack.packet.XMPPError)2 Protocol (org.jivesoftware.util.Protocol)2 Test (org.junit.jupiter.api.Test)2 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 Executor (java.util.concurrent.Executor)1 QName (javax.xml.namespace.QName)1 AlreadyConnectedException (org.jivesoftware.smack.SmackException.AlreadyConnectedException)1 AlreadyLoggedInException (org.jivesoftware.smack.SmackException.AlreadyLoggedInException)1 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)1 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)1 NotLoggedInException (org.jivesoftware.smack.SmackException.NotLoggedInException)1