use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.
the class MultiUserChat method getAffiliatesByAdmin.
/**
* Returns a collection of <code>Affiliate</code> that have the specified room affiliation
* sending a request in the admin namespace.
*
* @param affiliation the affiliation of the users in the room.
* @return a collection of <code>Affiliate</code> that have the specified room affiliation.
* @throws XMPPException if an error occured while performing the request to the server or you
* don't have enough privileges to get this information.
*/
private Collection<Affiliate> getAffiliatesByAdmin(String affiliation) throws XMPPException {
MUCAdmin iq = new MUCAdmin();
iq.setTo(room);
iq.setType(IQ.Type.GET);
// Set the specified affiliation. This may request the list of owners/admins/members/outcasts.
MUCAdmin.Item item = new MUCAdmin.Item(affiliation, null);
iq.addItem(item);
// Wait for a response packet back from the server.
PacketFilter responseFilter = new PacketIDFilter(iq.getPacketID());
PacketCollector response = connection.createPacketCollector(responseFilter);
// Send the request to the server.
connection.sendPacket(iq);
// Wait up to a certain number of seconds for a reply.
MUCAdmin answer = (MUCAdmin) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
response.cancel();
if (answer == null) {
throw new XMPPException("No response from server.");
} else if (answer.getError() != null) {
throw new XMPPException(answer.getError());
}
// Get the list of affiliates from the server's answer
List<Affiliate> affiliates = new ArrayList<Affiliate>();
for (Iterator<MUCAdmin.Item> it = answer.getItems(); it.hasNext(); ) {
affiliates.add(new Affiliate(it.next()));
}
return affiliates;
}
use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.
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 XMPPException if the new nickname is already in use by another occupant.
*/
public void changeNickname(String nickname) throws XMPPException {
if (nickname == null || nickname.equals("")) {
throw new IllegalArgumentException("Nickname must not be null or blank.");
}
// nickname.
if (!joined) {
throw new IllegalStateException("Must be logged into the room to change 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 = new Presence(Presence.Type.available);
joinPresence.setTo(room + "/" + nickname);
// Invoke presence interceptors so that extra information can be dynamically added
for (PacketInterceptor packetInterceptor : presenceInterceptors) {
packetInterceptor.interceptPacket(joinPresence);
}
// Wait for a presence packet back from the server.
PacketFilter responseFilter = new AndFilter(new FromMatchesFilter(room + "/" + nickname), new PacketTypeFilter(Presence.class));
PacketCollector response = connection.createPacketCollector(responseFilter);
// Send join packet.
connection.sendPacket(joinPresence);
// Wait up to a certain number of seconds for a reply.
Presence presence = (Presence) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
response.cancel();
if (presence == null) {
throw new XMPPException("No response from server.");
} else if (presence.getError() != null) {
throw new XMPPException(presence.getError());
}
this.nickname = nickname;
}
use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.
the class MultiUserChat method create.
/**
* Creates the room according to some default configuration, assign the requesting user
* as the room owner, and add the owner to the room but not allow anyone else to enter
* the room (effectively "locking" the room). The requesting user will join the room
* under the specified nickname as soon as the room has been created.<p>
*
* To create an "Instant Room", that means a room with some default configuration that is
* available for immediate access, the room's owner should send an empty form after creating
* the room. {@link #sendConfigurationForm(Form)}<p>
*
* To create a "Reserved Room", that means a room manually configured by the room creator
* before anyone is allowed to enter, the room's owner should complete and send a form after
* creating the room. Once the completed configutation form is sent to the server, the server
* will unlock the room. {@link #sendConfigurationForm(Form)}
*
* @param nickname the nickname to use.
* @throws XMPPException if the room couldn't be created for some reason
* (e.g. room already exists; user already joined to an existant room or
* 405 error if the user is not allowed to create the room)
*/
public synchronized void create(String nickname) throws XMPPException {
if (nickname == null || nickname.equals("")) {
throw new IllegalArgumentException("Nickname must not be null or blank.");
}
// nickname.
if (joined) {
throw new IllegalStateException("Creation failed - User already joined the room.");
}
// We create a room by sending a presence packet to room@service/nick
// and signal support for MUC. The owner will be automatically logged into the room.
Presence joinPresence = new Presence(Presence.Type.available);
joinPresence.setTo(room + "/" + nickname);
// Indicate the the client supports MUC
joinPresence.addExtension(new MUCInitialPresence());
// Invoke presence interceptors so that extra information can be dynamically added
for (PacketInterceptor packetInterceptor : presenceInterceptors) {
packetInterceptor.interceptPacket(joinPresence);
}
// Wait for a presence packet back from the server.
PacketFilter responseFilter = new AndFilter(new FromMatchesFilter(room + "/" + nickname), new PacketTypeFilter(Presence.class));
PacketCollector response = connection.createPacketCollector(responseFilter);
// Send create & join packet.
connection.sendPacket(joinPresence);
// Wait up to a certain number of seconds for a reply.
Presence presence = (Presence) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
response.cancel();
if (presence == null) {
throw new XMPPException("No response from server.");
} else if (presence.getError() != null) {
throw new XMPPException(presence.getError());
}
// Whether the room existed before or was created, the user has joined the room
this.nickname = nickname;
joined = true;
userHasJoined();
// Look for confirmation of room creation from the server
MUCUser mucUser = getMUCUserExtension(presence);
if (mucUser != null && mucUser.getStatus() != null) {
if ("201".equals(mucUser.getStatus().getCode())) {
// Room was created and the user has joined the room
return;
}
}
// We need to leave the room since it seems that the room already existed
leave();
throw new XMPPException("Creation failed - Missing acknowledge of room creation.");
}
use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.
the class MultiUserChat method changeRole.
private void changeRole(String nickname, String role, String reason) throws XMPPException {
MUCAdmin iq = new MUCAdmin();
iq.setTo(room);
iq.setType(IQ.Type.SET);
// Set the new role.
MUCAdmin.Item item = new MUCAdmin.Item(null, role);
item.setNick(nickname);
item.setReason(reason);
iq.addItem(item);
// Wait for a response packet back from the server.
PacketFilter responseFilter = new PacketIDFilter(iq.getPacketID());
PacketCollector response = connection.createPacketCollector(responseFilter);
// Send the change request to the server.
connection.sendPacket(iq);
// Wait up to a certain number of seconds for a reply.
IQ answer = (IQ) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
response.cancel();
if (answer == null) {
throw new XMPPException("No response from server.");
} else if (answer.getError() != null) {
throw new XMPPException(answer.getError());
}
}
use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.
the class MultiUserChat method sendRegistrationForm.
/**
* Sends the completed registration form to the server. After the user successfully submits
* the form, the room may queue the request for review by the room admins or may immediately
* add the user to the member list by changing the user's affiliation from "none" to "member.<p>
*
* If the desired room nickname is already reserved for that room, the room will return a
* "Conflict" error to the user (error code 409). If the room does not support registration,
* it will return a "Service Unavailable" error to the user (error code 503).
*
* @param form the completed registration form.
* @throws XMPPException if an error occurs submitting the registration form. In particular, a
* 409 error can occur if the desired room nickname is already reserved for that room;
* or a 503 error can occur if the room does not support registration.
*/
public void sendRegistrationForm(Form form) throws XMPPException {
Registration reg = new Registration();
reg.setType(IQ.Type.SET);
reg.setTo(room);
reg.addExtension(form.getDataFormToSend());
PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class));
PacketCollector collector = connection.createPacketCollector(filter);
connection.sendPacket(reg);
IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
collector.cancel();
if (result == null) {
throw new XMPPException("No response from server.");
} else if (result.getType() == IQ.Type.ERROR) {
throw new XMPPException(result.getError());
}
}
Aggregations