use of org.jivesoftware.smack.roster.SubscribeListener 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);
}
}
Aggregations