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);
}
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);
}
}
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);
}
}
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);
}
}
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 <room@service/nick>, a MUC service
* MUST return a <bad-request/> 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);
}
Aggregations