Search in sources :

Example 1 with AbstractPresenceEventListener

use of org.jivesoftware.smack.roster.AbstractPresenceEventListener in project Smack by igniterealtime.

the class IntegrationTestRosterUtil method ensureSubscribedTo.

public static void ensureSubscribedTo(final XMPPConnection presenceRequestReceiverConnection, final XMPPConnection presenceRequestingConnection, long timeout) throws TimeoutException, Exception {
    final Roster presenceRequestReceiverRoster = Roster.getInstanceFor(presenceRequestReceiverConnection);
    final Roster presenceRequestingRoster = Roster.getInstanceFor(presenceRequestingConnection);
    final EntityFullJid presenceRequestReceiverAddress = presenceRequestReceiverConnection.getUser();
    final EntityFullJid presenceRequestingAddress = presenceRequestingConnection.getUser();
    if (presenceRequestReceiverRoster.isSubscribedToMyPresence(presenceRequestingAddress)) {
        return;
    }
    final SubscribeListener subscribeListener = new SubscribeListener() {

        @Override
        public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) {
            if (from.equals(presenceRequestingConnection.getUser().asBareJid())) {
                return SubscribeAnswer.Approve;
            }
            return SubscribeAnswer.Deny;
        }
    };
    presenceRequestReceiverRoster.addSubscribeListener(subscribeListener);
    final SimpleResultSyncPoint syncPoint = new SimpleResultSyncPoint();
    final PresenceEventListener presenceEventListener = new AbstractPresenceEventListener() {

        @Override
        public void presenceSubscribed(BareJid address, Presence subscribedPresence) {
            if (!address.equals(presenceRequestReceiverAddress.asBareJid())) {
                return;
            }
            syncPoint.signal();
        }
    };
    presenceRequestingRoster.addPresenceEventListener(presenceEventListener);
    try {
        presenceRequestingRoster.sendSubscriptionRequest(presenceRequestReceiverAddress.asBareJid());
        syncPoint.waitForResult(timeout);
    } finally {
        presenceRequestReceiverRoster.removeSubscribeListener(subscribeListener);
        presenceRequestingRoster.removePresenceEventListener(presenceEventListener);
    }
}
Also used : AbstractPresenceEventListener(org.jivesoftware.smack.roster.AbstractPresenceEventListener) PresenceEventListener(org.jivesoftware.smack.roster.PresenceEventListener) AbstractPresenceEventListener(org.jivesoftware.smack.roster.AbstractPresenceEventListener) Roster(org.jivesoftware.smack.roster.Roster) Jid(org.jxmpp.jid.Jid) EntityFullJid(org.jxmpp.jid.EntityFullJid) BareJid(org.jxmpp.jid.BareJid) EntityFullJid(org.jxmpp.jid.EntityFullJid) BareJid(org.jxmpp.jid.BareJid) SubscribeListener(org.jivesoftware.smack.roster.SubscribeListener) Presence(org.jivesoftware.smack.packet.Presence)

Aggregations

Presence (org.jivesoftware.smack.packet.Presence)1 AbstractPresenceEventListener (org.jivesoftware.smack.roster.AbstractPresenceEventListener)1 PresenceEventListener (org.jivesoftware.smack.roster.PresenceEventListener)1 Roster (org.jivesoftware.smack.roster.Roster)1 SubscribeListener (org.jivesoftware.smack.roster.SubscribeListener)1 BareJid (org.jxmpp.jid.BareJid)1 EntityFullJid (org.jxmpp.jid.EntityFullJid)1 Jid (org.jxmpp.jid.Jid)1