Search in sources :

Example 31 with PacketFilter

use of org.jivesoftware.smack.filter.PacketFilter in project ecf by eclipse.

the class MultiUserChat method destroy.

/**
 * Sends a request to the server to destroy the room. The sender of the request
 * should be the room's owner. If the sender of the destroy request is not the room's owner
 * then the server will answer a "Forbidden" error (403).
 *
 * @param reason the reason for the room destruction.
 * @param alternateJID the JID of an alternate location.
 * @throws XMPPException if an error occurs while trying to destroy the room.
 *      An error can occur which will be wrapped by an XMPPException --
 *      XMPP error code 403. The error code can be used to present more
 *      appropiate error messages to end-users.
 */
public void destroy(String reason, String alternateJID) throws XMPPException {
    MUCOwner iq = new MUCOwner();
    iq.setTo(room);
    iq.setType(IQ.Type.SET);
    // Create the reason for the room destruction
    MUCOwner.Destroy destroy = new MUCOwner.Destroy();
    destroy.setReason(reason);
    destroy.setJid(alternateJID);
    iq.setDestroy(destroy);
    // Wait for a presence packet back from the server.
    PacketFilter responseFilter = new PacketIDFilter(iq.getPacketID());
    PacketCollector response = connection.createPacketCollector(responseFilter);
    // Send the room destruction request.
    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());
    }
    // Reset occupant information.
    occupantsMap.clear();
    nickname = null;
    joined = false;
    userHasLeft();
}
Also used : PacketFilter(org.jivesoftware.smack.filter.PacketFilter) PacketCollector(org.jivesoftware.smack.PacketCollector) IQ(org.jivesoftware.smack.packet.IQ) MUCOwner(org.jivesoftware.smackx.packet.MUCOwner) XMPPException(org.jivesoftware.smack.XMPPException) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 32 with PacketFilter

use of org.jivesoftware.smack.filter.PacketFilter in project ecf by eclipse.

the class MultiUserChat method changeAffiliationByAdmin.

/**
 * Tries to change the affiliation with an 'muc#admin' namespace
 *
 * @param jid
 * @param affiliation
 * @param reason the reason for the affiliation change (optional)
 * @throws XMPPException
 */
private void changeAffiliationByAdmin(String jid, String affiliation, String reason) throws XMPPException {
    MUCAdmin iq = new MUCAdmin();
    iq.setTo(room);
    iq.setType(IQ.Type.SET);
    // Set the new affiliation.
    MUCAdmin.Item item = new MUCAdmin.Item(affiliation, null);
    item.setJid(jid);
    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());
    }
}
Also used : PacketFilter(org.jivesoftware.smack.filter.PacketFilter) MUCAdmin(org.jivesoftware.smackx.packet.MUCAdmin) PacketCollector(org.jivesoftware.smack.PacketCollector) IQ(org.jivesoftware.smack.packet.IQ) XMPPException(org.jivesoftware.smack.XMPPException) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 33 with PacketFilter

use of org.jivesoftware.smack.filter.PacketFilter in project ecf by eclipse.

the class AccountManager method changePassword.

/**
 * Changes the password of the currently logged-in account. This operation can only
 * be performed after a successful login operation has been completed. Not all servers
 * support changing passwords; an XMPPException will be thrown when that is the case.
 *
 * @throws IllegalStateException if not currently logged-in to the server.
 * @throws XMPPException if an error occurs when changing the password.
 */
public void changePassword(String newPassword) throws XMPPException {
    Registration reg = new Registration();
    reg.setType(IQ.Type.SET);
    reg.setTo(connection.getServiceName());
    Map<String, String> map = new HashMap<String, String>();
    map.put("username", StringUtils.parseName(connection.getUser()));
    map.put("password", newPassword);
    reg.setAttributes(map);
    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());
    // Stop queuing results
    collector.cancel();
    if (result == null) {
        throw new XMPPException("No response from server.");
    } else if (result.getType() == IQ.Type.ERROR) {
        throw new XMPPException(result.getError());
    }
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) HashMap(java.util.HashMap) Registration(org.jivesoftware.smack.packet.Registration) IQ(org.jivesoftware.smack.packet.IQ) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 34 with PacketFilter

use of org.jivesoftware.smack.filter.PacketFilter in project ecf by eclipse.

the class AccountManager method deleteAccount.

/**
 * Deletes the currently logged-in account from the server. This operation can only
 * be performed after a successful login operation has been completed. Not all servers
 * support deleting accounts; an XMPPException will be thrown when that is the case.
 *
 * @throws IllegalStateException if not currently logged-in to the server.
 * @throws XMPPException if an error occurs when deleting the account.
 */
public void deleteAccount() throws XMPPException {
    if (!connection.isAuthenticated()) {
        throw new IllegalStateException("Must be logged in to delete a account.");
    }
    Registration reg = new Registration();
    reg.setType(IQ.Type.SET);
    reg.setTo(connection.getServiceName());
    Map<String, String> attributes = new HashMap<String, String>();
    // To delete an account, we add a single attribute, "remove", that is blank.
    attributes.put("remove", "");
    reg.setAttributes(attributes);
    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());
    // Stop queuing results
    collector.cancel();
    if (result == null) {
        throw new XMPPException("No response from server.");
    } else if (result.getType() == IQ.Type.ERROR) {
        throw new XMPPException(result.getError());
    }
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) HashMap(java.util.HashMap) Registration(org.jivesoftware.smack.packet.Registration) IQ(org.jivesoftware.smack.packet.IQ) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 35 with PacketFilter

use of org.jivesoftware.smack.filter.PacketFilter in project ecf by eclipse.

the class KeepAliveManager method schedulePingServerTask.

/**
 * Cancels any existing periodic ping task if there is one and schedules a new ping task if pingInterval is greater
 * then zero.
 *
 * This is designed so only one executor is used for scheduling all pings on all connections.  This results in only 1 thread used for pinging.
 */
private synchronized void schedulePingServerTask() {
    enableExecutorService();
    stopPingServerTask();
    if (pingInterval > 0) {
        periodicPingTask = periodicPingExecutorService.schedule(new Runnable() {

            public void run() {
                Ping ping = new Ping();
                PacketFilter responseFilter = new PacketIDFilter(ping.getPacketID());
                final PacketCollector response = pingFailedListeners.isEmpty() ? null : connection.createPacketCollector(responseFilter);
                connection.sendPacket(ping);
                if (response != null) {
                    // Schedule a collector for the ping reply, notify listeners if none is received.
                    periodicPingExecutorService.schedule(new Runnable() {

                        public void run() {
                            Packet result = response.nextResult(1);
                            // Stop queuing results
                            response.cancel();
                            // The actual result of the reply can be ignored since we only care if we actually got one.
                            if (result == null) {
                                for (PingFailedListener listener : pingFailedListeners) {
                                    listener.pingFailed();
                                }
                            }
                        }
                    }, SmackConfiguration.getPacketReplyTimeout(), TimeUnit.MILLISECONDS);
                }
            }
        }, getPingInterval(), TimeUnit.MILLISECONDS);
    }
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) PingFailedListener(org.jivesoftware.smack.ping.PingFailedListener) Ping(org.jivesoftware.smack.ping.packet.Ping) PacketCollector(org.jivesoftware.smack.PacketCollector) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Aggregations

PacketFilter (org.jivesoftware.smack.filter.PacketFilter)40 XMPPException (org.jivesoftware.smack.XMPPException)23 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)22 PacketCollector (org.jivesoftware.smack.PacketCollector)21 IQ (org.jivesoftware.smack.packet.IQ)18 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)15 AndFilter (org.jivesoftware.smack.filter.AndFilter)13 Packet (org.jivesoftware.smack.packet.Packet)13 Registration (org.jivesoftware.smack.packet.Registration)6 MUCAdmin (org.jivesoftware.smackx.packet.MUCAdmin)6 MUCOwner (org.jivesoftware.smackx.packet.MUCOwner)6 ArrayList (java.util.ArrayList)5 PacketListener (org.jivesoftware.smack.PacketListener)5 FromMatchesFilter (org.jivesoftware.smack.filter.FromMatchesFilter)5 Presence (org.jivesoftware.smack.packet.Presence)5 PacketInterceptor (org.jivesoftware.smack.PacketInterceptor)4 XMPPConnection (org.jivesoftware.smack.XMPPConnection)4 Message (org.jivesoftware.smack.packet.Message)4 MUCInitialPresence (org.jivesoftware.smackx.packet.MUCInitialPresence)4 HashMap (java.util.HashMap)2