use of org.jivesoftware.smack.filter.PacketIDFilter in project ecf by eclipse.
the class RemoteCommand method executeAction.
/**
* Executes the <code>action</codo> with the <code>form</code>.
* The action could be any of the available actions. The form must
* be the anwser of the previous stage. It can be <tt>null</tt> if it is the first stage.
*
* @param action the action to execute.
* @param form the form with the information.
* @param timeout the amount of time to wait for a reply.
* @throws XMPPException if there is a problem executing the command.
*/
private void executeAction(Action action, Form form, long timeout) throws XMPPException {
// TODO: Check that all the required fields of the form were filled, if
// TODO: not throw the corresponding exeption. This will make a faster response,
// TODO: since the request is stoped before it's sent.
AdHocCommandData data = new AdHocCommandData();
data.setType(IQ.Type.SET);
data.setTo(getOwnerJID());
data.setNode(getNode());
data.setSessionID(sessionID);
data.setAction(action);
if (form != null) {
data.setForm(form.getDataFormToSend());
}
PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(data.getPacketID()));
connection.sendPacket(data);
Packet response = collector.nextResult(timeout);
// Cancel the collector.
collector.cancel();
if (response == null) {
throw new XMPPException("No response from server on status set.");
}
if (response.getError() != null) {
throw new XMPPException(response.getError());
}
AdHocCommandData responseData = (AdHocCommandData) response;
this.sessionID = responseData.getSessionID();
super.setData(responseData);
}
use of org.jivesoftware.smack.filter.PacketIDFilter in project ecf by eclipse.
the class Roster method removeEntry.
/**
* Removes a roster entry from the roster. The roster entry will also be removed from the
* unfiled entries or from any roster group where it could belong and will no longer be part
* of the roster. Note that this is an asynchronous call -- Smack must wait for the server
* to send an updated subscription status.
*
* @param entry a roster entry.
* @throws XMPPException if an XMPP error occurs.
* @throws IllegalStateException if connection is not logged in or logged in anonymously
*/
public void removeEntry(RosterEntry entry) throws XMPPException {
if (!connection.isAuthenticated()) {
throw new IllegalStateException("Not logged in to server.");
}
if (connection.isAnonymous()) {
throw new IllegalStateException("Anonymous users can't have a roster.");
}
// The actual removal logic takes place in RosterPacketListenerprocess>>Packet(Packet)
if (!entries.containsKey(entry.getUser())) {
return;
}
RosterPacket packet = new RosterPacket();
packet.setType(IQ.Type.SET);
RosterPacket.Item item = RosterEntry.toRosterItem(entry);
// Set the item type as REMOVE so that the server will delete the entry
item.setItemType(RosterPacket.ItemType.remove);
packet.addRosterItem(item);
PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(packet.getPacketID()));
connection.sendPacket(packet);
IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
collector.cancel();
if (response == null) {
throw new XMPPException("No response from the server.");
} else // If the server replied with an error, throw an exception.
if (response.getType() == IQ.Type.ERROR) {
throw new XMPPException(response.getError());
}
}
use of org.jivesoftware.smack.filter.PacketIDFilter in project ecf by eclipse.
the class Roster method createEntry.
/**
* Creates a new roster entry and presence subscription. The server will asynchronously
* update the roster with the subscription status.
*
* @param user the user. (e.g. johndoe@jabber.org)
* @param name the nickname of the user.
* @param groups the list of group names the entry will belong to, or <tt>null</tt> if the
* the roster entry won't belong to a group.
* @throws XMPPException if an XMPP exception occurs.
* @throws IllegalStateException if connection is not logged in or logged in anonymously
*/
public void createEntry(String user, String name, String[] groups) throws XMPPException {
if (!connection.isAuthenticated()) {
throw new IllegalStateException("Not logged in to server.");
}
if (connection.isAnonymous()) {
throw new IllegalStateException("Anonymous users can't have a roster.");
}
// Create and send roster entry creation packet.
RosterPacket rosterPacket = new RosterPacket();
rosterPacket.setType(IQ.Type.SET);
RosterPacket.Item item = new RosterPacket.Item(user, name);
if (groups != null) {
for (String group : groups) {
if (group != null && group.trim().length() > 0) {
item.addGroupName(group);
}
}
}
rosterPacket.addRosterItem(item);
// Wait up to a certain number of seconds for a reply from the server.
PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(rosterPacket.getPacketID()));
connection.sendPacket(rosterPacket);
IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
collector.cancel();
if (response == null) {
throw new XMPPException("No response from the server.");
} else // If the server replied with an error, throw an exception.
if (response.getType() == IQ.Type.ERROR) {
throw new XMPPException(response.getError());
}
// Create a presence subscription packet and send.
Presence presencePacket = new Presence(Presence.Type.subscribe);
presencePacket.setTo(user);
connection.sendPacket(presencePacket);
}
use of org.jivesoftware.smack.filter.PacketIDFilter in project ecf by eclipse.
the class RosterGroup method addEntry.
/**
* Adds a roster entry to this group. If the entry was unfiled then it will be removed from
* the unfiled list and will be added to this group.
* Note that this is an asynchronous call -- Smack must wait for the server
* to receive the updated roster.
*
* @param entry a roster entry.
* @throws XMPPException if an error occured while trying to add the entry to the group.
*/
public void addEntry(RosterEntry entry) throws XMPPException {
PacketCollector collector = null;
// Only add the entry if it isn't already in the list.
synchronized (entries) {
if (!entries.contains(entry)) {
RosterPacket packet = new RosterPacket();
packet.setType(IQ.Type.SET);
RosterPacket.Item item = RosterEntry.toRosterItem(entry);
item.addGroupName(getName());
packet.addRosterItem(item);
// Wait up to a certain number of seconds for a reply from the server.
collector = connection.createPacketCollector(new PacketIDFilter(packet.getPacketID()));
connection.sendPacket(packet);
}
}
if (collector != null) {
IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
collector.cancel();
if (response == null) {
throw new XMPPException("No response from the server.");
} else // If the server replied with an error, throw an exception.
if (response.getType() == IQ.Type.ERROR) {
throw new XMPPException(response.getError());
}
}
}
use of org.jivesoftware.smack.filter.PacketIDFilter in project ecf by eclipse.
the class RosterGroup method removeEntry.
/**
* Removes a roster entry from this group. If the entry does not belong to any other group
* then it will be considered as unfiled, therefore it will be added to the list of unfiled
* entries.
* Note that this is an asynchronous call -- Smack must wait for the server
* to receive the updated roster.
*
* @param entry a roster entry.
* @throws XMPPException if an error occured while trying to remove the entry from the group.
*/
public void removeEntry(RosterEntry entry) throws XMPPException {
PacketCollector collector = null;
// server.
synchronized (entries) {
if (entries.contains(entry)) {
RosterPacket packet = new RosterPacket();
packet.setType(IQ.Type.SET);
RosterPacket.Item item = RosterEntry.toRosterItem(entry);
item.removeGroupName(this.getName());
packet.addRosterItem(item);
// Wait up to a certain number of seconds for a reply from the server.
collector = connection.createPacketCollector(new PacketIDFilter(packet.getPacketID()));
connection.sendPacket(packet);
}
}
if (collector != null) {
IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
collector.cancel();
if (response == null) {
throw new XMPPException("No response from the server.");
} else // If the server replied with an error, throw an exception.
if (response.getType() == IQ.Type.ERROR) {
throw new XMPPException(response.getError());
}
}
}
Aggregations