Search in sources :

Example 1 with FeatureNotSupportedException

use of org.jivesoftware.smack.SmackException.FeatureNotSupportedException in project Smack by igniterealtime.

the class MultipleRecipientManager method send.

/**
 * Sends the specified stanza to the collection of specified recipients using the specified
 * connection. If the server has support for XEP-33 then only one stanza is going to be sent to
 * the server with the multiple recipient instructions. However, if XEP-33 is not supported by
 * the server then the client is going to send the stanza to each recipient.
 *
 * @param connection the connection to use to send the packet.
 * @param packet the stanza to send to the list of recipients.
 * @param to the collection of JIDs to include in the TO list or <code>null</code> if no TO list exists.
 * @param cc the collection of JIDs to include in the CC list or <code>null</code> if no CC list exists.
 * @param bcc the collection of JIDs to include in the BCC list or <code>null</code> if no BCC list
 *        exists.
 * @param replyTo address to which all replies are requested to be sent or <code>null</code>
 *        indicating that they can reply to any address.
 * @param replyRoom JID of a MUC room to which responses should be sent or <code>null</code>
 *        indicating that they can reply to any address.
 * @param noReply true means that receivers should not reply to the message.
 * @throws XMPPErrorException if server does not support XEP-33: Extended Stanza Addressing and
 *         some XEP-33 specific features were requested.
 * @throws NoResponseException if there was no response from the server.
 * @throws FeatureNotSupportedException if special XEP-33 features where requested, but the
 *         server does not support them.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public static void send(XMPPConnection connection, Stanza packet, Collection<? extends Jid> to, Collection<? extends Jid> cc, Collection<? extends Jid> bcc, Jid replyTo, Jid replyRoom, boolean noReply) throws NoResponseException, XMPPErrorException, FeatureNotSupportedException, NotConnectedException, InterruptedException {
    // required at all and we can send it just as normal stanza without needing to add the extension element
    if (to != null && to.size() == 1 && (cc == null || cc.isEmpty()) && (bcc == null || bcc.isEmpty()) && !noReply && StringUtils.isNullOrEmpty(replyTo) && StringUtils.isNullOrEmpty(replyRoom)) {
        Jid toJid = to.iterator().next();
        packet.setTo(toJid);
        connection.sendStanza(packet);
        return;
    }
    DomainBareJid serviceAddress = getMultipleRecipientServiceAddress(connection);
    if (serviceAddress != null) {
        // Send packet to target users using multiple recipient service provided by the server
        sendThroughService(connection, packet, to, cc, bcc, replyTo, replyRoom, noReply, serviceAddress);
    } else {
        // Server does not support XEP-33 so try to send the packet to each recipient
        if (noReply || replyTo != null || replyRoom != null) {
            // the user that this features are not available
            throw new FeatureNotSupportedException("Extended Stanza Addressing");
        }
        // Send the packet to each individual recipient
        sendToIndividualRecipients(connection, packet, to, cc, bcc);
    }
}
Also used : Jid(org.jxmpp.jid.Jid) EntityFullJid(org.jxmpp.jid.EntityFullJid) DomainBareJid(org.jxmpp.jid.DomainBareJid) EntityBareJid(org.jxmpp.jid.EntityBareJid) FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) DomainBareJid(org.jxmpp.jid.DomainBareJid)

Example 2 with FeatureNotSupportedException

use of org.jivesoftware.smack.SmackException.FeatureNotSupportedException in project Smack by igniterealtime.

the class Socks5BytestreamManager method establishSession.

/**
 * Establishes a SOCKS5 Bytestream with the given user using the given session ID and returns
 * the Socket to send/receive data to/from the user.
 *
 * @param targetJID the JID of the user a SOCKS5 Bytestream should be established
 * @param sessionID the session ID for the SOCKS5 Bytestream request
 * @return the Socket to send/receive data to/from the user
 * @throws IOException if the bytestream could not be established
 * @throws InterruptedException if the current thread was interrupted while waiting
 * @throws XMPPException if an XMPP protocol error was received.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws NoResponseException if there was no response from the remote entity.
 * @throws SmackMessageException if there was an error.
 * @throws FeatureNotSupportedException if a requested feature is not supported by the remote entity.
 */
@Override
public Socks5BytestreamSession establishSession(Jid targetJID, String sessionID) throws IOException, InterruptedException, XMPPException, NoResponseException, NotConnectedException, SmackMessageException, FeatureNotSupportedException {
    XMPPConnection connection = connection();
    XMPPErrorException discoveryException = null;
    // check if target supports SOCKS5 Bytestream
    if (!supportsSocks5(targetJID)) {
        throw new FeatureNotSupportedException("SOCKS5 Bytestream", targetJID);
    }
    List<Jid> proxies = new ArrayList<>();
    // determine SOCKS5 proxies from XMPP-server
    try {
        proxies.addAll(determineProxies());
    } catch (XMPPErrorException e) {
        // don't abort here, just remember the exception thrown by determineProxies()
        // determineStreamHostInfos() will at least add the local Socks5 proxy (if enabled)
        discoveryException = e;
    }
    // determine address and port of each proxy
    List<StreamHost> streamHosts = determineStreamHostInfos(proxies);
    if (streamHosts.isEmpty()) {
        if (discoveryException != null) {
            throw discoveryException;
        } else {
            throw new SmackException.SmackMessageException("no SOCKS5 proxies available");
        }
    }
    // compute digest
    String digest = Socks5Utils.createDigest(sessionID, connection.getUser(), targetJID);
    // prioritize last working SOCKS5 proxy if exists
    if (this.proxyPrioritizationEnabled && this.lastWorkingProxy != null) {
        StreamHost selectedStreamHost = null;
        for (StreamHost streamHost : streamHosts) {
            if (streamHost.getJID().equals(this.lastWorkingProxy)) {
                selectedStreamHost = streamHost;
                break;
            }
        }
        if (selectedStreamHost != null) {
            streamHosts.remove(selectedStreamHost);
            streamHosts.add(0, selectedStreamHost);
        }
    }
    Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
    try {
        // add transfer digest to local proxy to make transfer valid
        socks5Proxy.addTransfer(digest);
        // create initiation packet
        Bytestream initiation = createBytestreamInitiation(sessionID, targetJID, streamHosts);
        // send initiation packet
        Stanza response = connection.createStanzaCollectorAndSend(initiation).nextResultOrThrow(getTargetResponseTimeout());
        // extract used stream host from response
        StreamHostUsed streamHostUsed = ((Bytestream) response).getUsedHost();
        StreamHost usedStreamHost = initiation.getStreamHost(streamHostUsed.getJID());
        if (usedStreamHost == null) {
            throw new SmackException.SmackMessageException("Remote user responded with unknown host");
        }
        // build SOCKS5 client
        Socks5Client socks5Client = new Socks5ClientForInitiator(usedStreamHost, digest, connection, sessionID, targetJID);
        // establish connection to proxy
        Socket socket = socks5Client.getSocket(getProxyConnectionTimeout());
        // remember last working SOCKS5 proxy to prioritize it for next request
        this.lastWorkingProxy = usedStreamHost.getJID();
        // negotiation successful, return the output stream
        return new Socks5BytestreamSession(socket, usedStreamHost.getJID().equals(connection.getUser()));
    } catch (TimeoutException e) {
        throw new IOException("Timeout while connecting to SOCKS5 proxy", e);
    } finally {
        // remove transfer digest if output stream is returned or an exception
        // occurred
        socks5Proxy.removeTransfer(digest);
    }
}
Also used : StreamHostUsed(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHostUsed) SmackMessageException(org.jivesoftware.smack.SmackException.SmackMessageException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) Jid(org.jxmpp.jid.Jid) EntityFullJid(org.jxmpp.jid.EntityFullJid) Stanza(org.jivesoftware.smack.packet.Stanza) FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) ArrayList(java.util.ArrayList) XMPPConnection(org.jivesoftware.smack.XMPPConnection) IOException(java.io.IOException) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) StreamHost(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost) Socket(java.net.Socket) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with FeatureNotSupportedException

use of org.jivesoftware.smack.SmackException.FeatureNotSupportedException in project Smack by igniterealtime.

the class EntityTimeManager method getTime.

public Time getTime(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, FeatureNotSupportedException {
    if (!isTimeSupported(jid)) {
        throw new SmackException.FeatureNotSupportedException(Time.NAMESPACE);
    }
    XMPPConnection connection = connection();
    Time request = Time.builder(connection).to(jid).build();
    return connection.sendIqRequestAndWaitForResponse(request);
}
Also used : FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) Time(org.jivesoftware.smackx.time.packet.Time) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Example 4 with FeatureNotSupportedException

use of org.jivesoftware.smack.SmackException.FeatureNotSupportedException in project Smack by igniterealtime.

the class Socks5ByteStreamManagerTest method shouldFailIfTargetDoesNotSupportSocks5.

/**
 * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid)} should throw an exception
 * if the given target does not support SOCKS5 Bytestream.
 * @throws XMPPException if an XMPP protocol error was received.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws IOException if an I/O error occurred.
 */
@Test
public void shouldFailIfTargetDoesNotSupportSocks5() throws XMPPException, SmackException, InterruptedException, IOException {
    final Protocol protocol = new Protocol();
    final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    FeatureNotSupportedException e = assertThrows(FeatureNotSupportedException.class, () -> {
        // build empty discover info as reply if targets features are queried
        DiscoverInfo discoverInfo = DiscoverInfo.builder("disco-1").build();
        protocol.addResponse(discoverInfo);
        // start SOCKS5 Bytestream
        byteStreamManager.establishSession(targetJID);
    });
    assertTrue(e.getFeature().equals("SOCKS5 Bytestream"));
    assertTrue(e.getJid().equals(targetJID));
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Protocol(org.jivesoftware.util.Protocol) Test(org.junit.jupiter.api.Test)

Example 5 with FeatureNotSupportedException

use of org.jivesoftware.smack.SmackException.FeatureNotSupportedException in project Smack by igniterealtime.

the class Roster method preApprove.

/**
 * Pre-approve user presence subscription.
 *
 * @param user the user. (e.g. johndoe@jabber.org)
 * @throws NotLoggedInException if not logged in.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws FeatureNotSupportedException if pre-approving is not supported.
 * @since 4.2
 */
public void preApprove(BareJid user) throws NotLoggedInException, NotConnectedException, InterruptedException, FeatureNotSupportedException {
    final XMPPConnection connection = connection();
    if (!isSubscriptionPreApprovalSupported()) {
        throw new FeatureNotSupportedException("Pre-approving");
    }
    Presence presencePacket = connection.getStanzaFactory().buildPresenceStanza().ofType(Presence.Type.subscribed).to(user).build();
    connection.sendStanza(presencePacket);
}
Also used : FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) Presence(org.jivesoftware.smack.packet.Presence) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Aggregations

FeatureNotSupportedException (org.jivesoftware.smack.SmackException.FeatureNotSupportedException)5 XMPPConnection (org.jivesoftware.smack.XMPPConnection)4 EntityFullJid (org.jxmpp.jid.EntityFullJid)2 Jid (org.jxmpp.jid.Jid)2 IOException (java.io.IOException)1 Socket (java.net.Socket)1 ArrayList (java.util.ArrayList)1 TimeoutException (java.util.concurrent.TimeoutException)1 SmackMessageException (org.jivesoftware.smack.SmackException.SmackMessageException)1 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)1 Presence (org.jivesoftware.smack.packet.Presence)1 Stanza (org.jivesoftware.smack.packet.Stanza)1 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)1 StreamHost (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost)1 StreamHostUsed (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHostUsed)1 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)1 Time (org.jivesoftware.smackx.time.packet.Time)1 Protocol (org.jivesoftware.util.Protocol)1 Test (org.junit.jupiter.api.Test)1 DomainBareJid (org.jxmpp.jid.DomainBareJid)1