Search in sources :

Example 6 with StanzaError

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

the class OutgoingFileTransfer method handleXMPPException.

private void handleXMPPException(XMPPErrorException e) {
    StanzaError error = e.getStanzaError();
    if (error != null) {
        switch(error.getCondition()) {
            case forbidden:
                setStatus(Status.refused);
                return;
            case bad_request:
                setStatus(Status.error);
                setError(Error.not_acceptable);
                break;
            default:
                setStatus(FileTransfer.Status.error);
        }
    }
    setException(e);
}
Also used : StanzaError(org.jivesoftware.smack.packet.StanzaError)

Example 7 with StanzaError

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

the class PingManager method isValidErrorPong.

private boolean isValidErrorPong(Jid destinationJid, XMPPErrorException xmppErrorException) {
    // service responded, i.e. is up and pingable.
    if (destinationJid.equals(connection().getXMPPServiceDomain())) {
        return true;
    }
    final StanzaError xmppError = xmppErrorException.getStanzaError();
    // We may received an error response from an intermediate service returning an error like
    // 'remote-server-not-found' or 'remote-server-timeout' to us (which would fake the 'from' address,
    // see RFC 6120 § 8.3.1 2.). Or the recipient could became unavailable.
    // Sticking with the current rules of RFC 6120/6121, it is undecidable at this point whether we received an
    // error response from the pinged entity or not. This is because a service-unavailable error condition is
    // *required* (as per the RFCs) to be send back in both relevant cases:
    // 1. When the receiving entity is unaware of the IQ request type. RFC 6120 § 8.4.:
    // "If an intended recipient receives an IQ stanza of type "get" or
    // "set" containing a child element qualified by a namespace it does
    // not understand, then the entity MUST return an IQ stanza of type
    // "error" with an error condition of <service-unavailable/>.
    // 2. When the receiving resource is not available. RFC 6121 § 8.5.3.2.3.
    // Some clients don't obey the first rule and instead send back a feature-not-implement condition with type 'cancel',
    // which allows us to consider this response as valid "error response" pong.
    StanzaError.Type type = xmppError.getType();
    StanzaError.Condition condition = xmppError.getCondition();
    return type == StanzaError.Type.CANCEL && condition == StanzaError.Condition.feature_not_implemented;
}
Also used : StanzaError(org.jivesoftware.smack.packet.StanzaError)

Example 8 with StanzaError

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

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

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

the class ConfigureFormTest method getConfigFormWithInsufficientPrivileges.

@Test
public void getConfigFormWithInsufficientPrivileges() throws XMPPException, SmackException, IOException, InterruptedException {
    ThreadedDummyConnection con = ThreadedDummyConnection.newInstance();
    PubSubManager mgr = new PubSubManager(con, PubSubManagerTest.DUMMY_PUBSUB_SERVICE);
    DiscoverInfoBuilder info = DiscoverInfo.builder("disco-result").ofType(IQ.Type.result).from(PubSubManagerTest.DUMMY_PUBSUB_SERVICE);
    Identity ident = new Identity("pubsub", null, "leaf");
    info.addIdentity(ident);
    DiscoverInfo discoverInfo = info.build();
    con.addIQReply(discoverInfo);
    Node node = mgr.getNode("princely_musings");
    PubSub errorIq = new PubSub();
    errorIq.setType(IQ.Type.error);
    errorIq.setFrom(PubSubManagerTest.DUMMY_PUBSUB_SERVICE);
    StanzaError error = StanzaError.getBuilder(Condition.forbidden).build();
    errorIq.setError(error);
    con.addIQReply(errorIq);
    try {
        node.getNodeConfiguration();
        fail();
    } catch (XMPPErrorException e) {
        assertEquals(StanzaError.Type.AUTH, e.getStanzaError().getType());
    }
}
Also used : ThreadedDummyConnection(org.jivesoftware.smack.ThreadedDummyConnection) DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) PubSub(org.jivesoftware.smackx.pubsub.packet.PubSub) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) DiscoverInfoBuilder(org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity) StanzaError(org.jivesoftware.smack.packet.StanzaError) Test(org.junit.jupiter.api.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