Search in sources :

Example 6 with EntityFullJid

use of org.jxmpp.jid.EntityFullJid in project Smack by igniterealtime.

the class MultiUserChat method changeNickname.

/**
 * Changes the occupant's nickname to a new nickname within the room. Each room occupant
 * will receive two presence packets. One of type "unavailable" for the old nickname and one
 * indicating availability for the new nickname. The unavailable presence will contain the new
 * nickname and an appropriate status code (namely 303) as extended presence information. The
 * status code 303 indicates that the occupant is changing his/her nickname.
 *
 * @param nickname the new nickname within the room.
 * @throws XMPPErrorException if the new nickname is already in use by another occupant.
 * @throws NoResponseException if there was no response from the server.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws MucNotJoinedException if not joined to the Multi-User Chat.
 */
public synchronized void changeNickname(Resourcepart nickname) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, MucNotJoinedException {
    Objects.requireNonNull(nickname, "Nickname must not be null or blank.");
    // nickname.
    if (!isJoined()) {
        throw new MucNotJoinedException(this);
    }
    final EntityFullJid jid = JidCreate.entityFullFrom(room, nickname);
    // We change the nickname by sending a presence packet where the "to"
    // field is in the form "roomName@service/nickname"
    // We don't have to signal the MUC support again
    Presence joinPresence = connection.getStanzaFactory().buildPresenceStanza().to(jid).ofType(Presence.Type.available).build();
    // Wait for a presence packet back from the server.
    StanzaFilter responseFilter = new AndFilter(FromMatchesFilter.createFull(jid), new StanzaTypeFilter(Presence.class));
    StanzaCollector response = connection.createStanzaCollectorAndSend(responseFilter, joinPresence);
    // Wait up to a certain number of seconds for a reply. If there is a negative reply, an
    // exception will be thrown
    response.nextResultOrThrow();
    // TODO: Shouldn't this handle nickname rewriting by the MUC service?
    setNickname(nickname);
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) EntityFullJid(org.jxmpp.jid.EntityFullJid) Presence(org.jivesoftware.smack.packet.Presence) MUCInitialPresence(org.jivesoftware.smackx.muc.packet.MUCInitialPresence) MucNotJoinedException(org.jivesoftware.smackx.muc.MultiUserChatException.MucNotJoinedException) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 7 with EntityFullJid

use of org.jxmpp.jid.EntityFullJid in project Smack by igniterealtime.

the class MultiUserChatRolesAffiliationsPrivilegesIntegrationTest method mucTestDefaultRoleForAffiliationInModeratedRoom.

/**
 * Asserts that a moderated room assigns the correct default roles for a given affiliation
 *
 * <p>From XEP-0045 § 5.1.2:</p>
 * <blockquote>
 * ...the initial default roles that a service SHOULD set based on the user's affiliation...
 * </blockquote>
 *
 * @throws Exception when errors occur
 */
@SmackIntegrationTest
public void mucTestDefaultRoleForAffiliationInModeratedRoom() throws Exception {
    EntityBareJid mucAddress = getRandomRoom("smack-inttest-moderatedroles");
    MultiUserChat mucAsSeenByOne = mucManagerOne.getMultiUserChat(mucAddress);
    MultiUserChat mucAsSeenByTwo = mucManagerTwo.getMultiUserChat(mucAddress);
    MultiUserChat mucAsSeenByThree = mucManagerThree.getMultiUserChat(mucAddress);
    final Resourcepart nicknameOne = Resourcepart.from("one-" + randomString);
    final Resourcepart nicknameTwo = Resourcepart.from("two-" + randomString);
    final Resourcepart nicknameThree = Resourcepart.from("three-" + randomString);
    final ResultSyncPoint<String, Exception> resultSyncPoint = new ResultSyncPoint<>();
    mucAsSeenByOne.addParticipantStatusListener(new ParticipantStatusListener() {

        @Override
        public void adminGranted(EntityFullJid participant) {
            resultSyncPoint.signal("done");
        }
    });
    createModeratedMuc(mucAsSeenByOne, nicknameOne);
    final MUCRole threeRole;
    switch(sinttestConfiguration.compatibilityMode) {
        default:
            threeRole = MUCRole.visitor;
            break;
        case ejabberd:
            threeRole = MUCRole.participant;
            break;
    }
    try {
        mucAsSeenByTwo.join(nicknameTwo);
        mucAsSeenByThree.join(nicknameThree);
        mucAsSeenByOne.grantAdmin(conTwo.getUser().asBareJid());
        resultSyncPoint.waitForResult(timeout);
        assertEquals(mucAsSeenByOne.getOccupantsCount(), 3);
        assertEquals(MUCRole.moderator, mucAsSeenByOne.getOccupant(JidCreate.entityFullFrom(mucAddress, nicknameOne)).getRole());
        assertEquals(MUCRole.moderator, mucAsSeenByOne.getOccupant(JidCreate.entityFullFrom(mucAddress, nicknameTwo)).getRole());
        assertEquals(threeRole, mucAsSeenByOne.getOccupant(JidCreate.entityFullFrom(mucAddress, nicknameThree)).getRole());
    } finally {
        tryDestroy(mucAsSeenByOne);
    }
}
Also used : EntityFullJid(org.jxmpp.jid.EntityFullJid) ResultSyncPoint(org.igniterealtime.smack.inttest.util.ResultSyncPoint) EntityBareJid(org.jxmpp.jid.EntityBareJid) SmackException(org.jivesoftware.smack.SmackException) TestNotPossibleException(org.igniterealtime.smack.inttest.TestNotPossibleException) XMPPException(org.jivesoftware.smack.XMPPException) Resourcepart(org.jxmpp.jid.parts.Resourcepart) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 8 with EntityFullJid

use of org.jxmpp.jid.EntityFullJid in project Smack by igniterealtime.

the class MultiUserChatRolesAffiliationsPrivilegesIntegrationTest method mucAffiliationTestForWitnessingAdminRemoval.

/**
 * Asserts that a user who is present when another user undergoes an affiliation change receives that change as a presence update
 *
 * <p>From XEP-0045 § 5.2.2:</p>
 * <blockquote>
 * ...a MUC service implementation MUST change the user's affiliation to reflect the change and communicate that to
 * all occupants...
 * </blockquote>
 *
 * <p>From XEP-0045 § 10.7:</p>
 * <blockquote>
 * If the user is in the room, the service MUST then send updated presence from this individual to all occupants,
 * indicating the loss of admin status by sending a presence element...
 * </blockquote>
 *
 * @throws Exception when errors occur
 */
@SmackIntegrationTest
public void mucAffiliationTestForWitnessingAdminRemoval() throws Exception {
    EntityBareJid mucAddress = getRandomRoom("smack-inttest");
    MultiUserChat mucAsSeenByOne = mucManagerOne.getMultiUserChat(mucAddress);
    MultiUserChat mucAsSeenByTwo = mucManagerTwo.getMultiUserChat(mucAddress);
    MultiUserChat mucAsSeenByThree = mucManagerThree.getMultiUserChat(mucAddress);
    final ResultSyncPoint<String, Exception> resultSyncPoint = new ResultSyncPoint<>();
    mucAsSeenByThree.addParticipantStatusListener(new ParticipantStatusListener() {

        @Override
        public void adminRevoked(EntityFullJid participant) {
            resultSyncPoint.signal("done");
        }
    });
    createMuc(mucAsSeenByOne, "one-" + randomString);
    try {
        final Resourcepart nicknameTwo = Resourcepart.from("two-" + randomString);
        final Resourcepart nicknameThree = Resourcepart.from("three-" + randomString);
        mucAsSeenByTwo.join(nicknameTwo);
        mucAsSeenByThree.join(nicknameThree);
        mucAsSeenByOne.grantAdmin(conTwo.getUser().asBareJid());
        mucAsSeenByOne.revokeAdmin(conTwo.getUser().asEntityBareJid());
        resultSyncPoint.waitForResult(timeout);
    } finally {
        tryDestroy(mucAsSeenByOne);
    }
}
Also used : EntityFullJid(org.jxmpp.jid.EntityFullJid) ResultSyncPoint(org.igniterealtime.smack.inttest.util.ResultSyncPoint) EntityBareJid(org.jxmpp.jid.EntityBareJid) SmackException(org.jivesoftware.smack.SmackException) TestNotPossibleException(org.igniterealtime.smack.inttest.TestNotPossibleException) XMPPException(org.jivesoftware.smack.XMPPException) Resourcepart(org.jxmpp.jid.parts.Resourcepart) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 9 with EntityFullJid

use of org.jxmpp.jid.EntityFullJid 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)

Example 10 with EntityFullJid

use of org.jxmpp.jid.EntityFullJid in project Smack by igniterealtime.

the class MultiUserChatEntityIntegrationTest method mucTestForRejectingDiscoOnRoomOccupantByNonOccupant.

/**
 * Asserts that a non-occupant receives a Bad Request error when attempting to query an occupant by their
 * occupant JID.
 *
 * <p>From XEP-0045 § 6.6:</p>
 * <blockquote>
 * If a non-occupant attempts to send a disco request to an address of the form &lt;room@service/nick&gt;, a MUC service
 * MUST return a &lt;bad-request/&gt; error
 * </blockquote>
 *
 * @throws Exception when errors occur
 */
@SmackIntegrationTest
public void mucTestForRejectingDiscoOnRoomOccupantByNonOccupant() throws Exception {
    EntityBareJid mucAddress = getRandomRoom("smack-inttest-discoitems");
    MultiUserChat mucAsSeenByOne = mucManagerOne.getMultiUserChat(mucAddress);
    final Resourcepart nicknameOne = Resourcepart.from("one-" + randomString);
    createMuc(mucAsSeenByOne, nicknameOne);
    final EntityFullJid mucAsSeenByOneUserJid = mucAsSeenByOne.getMyRoomJid();
    // be non-null after we created and joined the room. But it can not hurt to explicitly test for it either.
    if (mucAsSeenByOneUserJid == null) {
        throw new AssertionError();
    }
    XMPPException.XMPPErrorException xe;
    try {
        xe = assertThrows(XMPPException.XMPPErrorException.class, () -> ServiceDiscoveryManager.getInstanceFor(conTwo).discoverItems(mucAsSeenByOneUserJid));
    } finally {
        tryDestroy(mucAsSeenByOne);
    }
    final StanzaError.Condition expectedCondition;
    switch(sinttestConfiguration.compatibilityMode) {
        default:
            expectedCondition = StanzaError.Condition.bad_request;
            break;
        case ejabberd:
            expectedCondition = StanzaError.Condition.not_acceptable;
            break;
    }
    assertEquals(xe.getStanzaError().getCondition(), expectedCondition);
}
Also used : EntityFullJid(org.jxmpp.jid.EntityFullJid) XMPPException(org.jivesoftware.smack.XMPPException) EntityBareJid(org.jxmpp.jid.EntityBareJid) Resourcepart(org.jxmpp.jid.parts.Resourcepart) StanzaError(org.jivesoftware.smack.packet.StanzaError) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Aggregations

EntityFullJid (org.jxmpp.jid.EntityFullJid)24 EntityBareJid (org.jxmpp.jid.EntityBareJid)12 SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)10 XMPPException (org.jivesoftware.smack.XMPPException)9 Resourcepart (org.jxmpp.jid.parts.Resourcepart)9 TestNotPossibleException (org.igniterealtime.smack.inttest.TestNotPossibleException)8 ResultSyncPoint (org.igniterealtime.smack.inttest.util.ResultSyncPoint)8 SmackException (org.jivesoftware.smack.SmackException)8 AndFilter (org.jivesoftware.smack.filter.AndFilter)4 Presence (org.jivesoftware.smack.packet.Presence)4 Jid (org.jxmpp.jid.Jid)4 ArrayList (java.util.ArrayList)3 MucNotJoinedException (org.jivesoftware.smackx.muc.MultiUserChatException.MucNotJoinedException)3 MUCInitialPresence (org.jivesoftware.smackx.muc.packet.MUCInitialPresence)3 StanzaListener (org.jivesoftware.smack.StanzaListener)2 XMPPConnection (org.jivesoftware.smack.XMPPConnection)2 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)2 Bind (org.jivesoftware.smack.packet.Bind)2 Message (org.jivesoftware.smack.packet.Message)2 Stanza (org.jivesoftware.smack.packet.Stanza)2