Search in sources :

Example 21 with AndFilter

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

the class MultiUserChat method changeSubject.

/**
 * Changes the subject within the room. As a default, only users with a role of "moderator"
 * are allowed to change the subject in a room. Although some rooms may be configured to
 * allow a mere participant or even a visitor to change the subject.
 *
 * @param subject the new room's subject to set.
 * @throws XMPPException if someone without appropriate privileges attempts to change the
 *          room subject will throw an error with code 403 (i.e. Forbidden)
 */
public void changeSubject(final String subject) throws XMPPException {
    Message message = new Message(room, Message.Type.groupchat);
    message.setSubject(subject);
    // Wait for an error or confirmation message back from the server.
    PacketFilter responseFilter = new AndFilter(new FromMatchesFilter(room), new PacketTypeFilter(Message.class));
    responseFilter = new AndFilter(responseFilter, new PacketFilter() {

        public boolean accept(Packet packet) {
            Message msg = (Message) packet;
            return subject.equals(msg.getSubject());
        }
    });
    PacketCollector response = connection.createPacketCollector(responseFilter);
    // Send change subject packet.
    connection.sendPacket(message);
    // Wait up to a certain number of seconds for a reply.
    Message answer = (Message) 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 : AndFilter(org.jivesoftware.smack.filter.AndFilter) Packet(org.jivesoftware.smack.packet.Packet) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) Message(org.jivesoftware.smack.packet.Message) FromMatchesFilter(org.jivesoftware.smack.filter.FromMatchesFilter) PacketCollector(org.jivesoftware.smack.PacketCollector) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) XMPPException(org.jivesoftware.smack.XMPPException)

Example 22 with AndFilter

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

the class MultiUserChat method join.

/**
 * Joins the chat room using the specified nickname and password. If already joined
 * using another nickname, this method will first leave the room and then
 * re-join using the new nickname.<p>
 *
 * To control the amount of history to receive while joining a room you will need to provide
 * a configured DiscussionHistory object.<p>
 *
 * A password is required when joining password protected rooms. If the room does
 * not require a password there is no need to provide one.<p>
 *
 * If the room does not already exist when the user seeks to enter it, the server will
 * decide to create a new room or not.
 *
 * @param nickname the nickname to use.
 * @param password the password to use.
 * @param history the amount of discussion history to receive while joining a room.
 * @param timeout the amount of time to wait for a reply from the MUC service(in milleseconds).
 * @throws XMPPException if an error occurs joining the room. In particular, a
 *      401 error can occur if no password was provided and one is required; or a
 *      403 error can occur if the user is banned; or a
 *      404 error can occur if the room does not exist or is locked; or a
 *      407 error can occur if user is not on the member list; or a
 *      409 error can occur if someone is already in the group chat with the same nickname.
 */
public synchronized void join(String nickname, String password, DiscussionHistory history, long timeout) throws XMPPException {
    if (nickname == null || nickname.equals("")) {
        throw new IllegalArgumentException("Nickname must not be null or blank.");
    }
    // nickname.
    if (joined) {
        leave();
    }
    // We join a room by sending a presence packet where the "to"
    // field is in the form "roomName@service/nickname"
    Presence joinPresence = new Presence(Presence.Type.available);
    joinPresence.setTo(room + "/" + nickname);
    // Indicate the the client supports MUC
    MUCInitialPresence mucInitialPresence = new MUCInitialPresence();
    if (password != null) {
        mucInitialPresence.setPassword(password);
    }
    if (history != null) {
        mucInitialPresence.setHistory(history.getMUCHistory());
    }
    joinPresence.addExtension(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 = null;
    Presence presence;
    try {
        response = connection.createPacketCollector(responseFilter);
        // Send join packet.
        connection.sendPacket(joinPresence);
        // Wait up to a certain number of seconds for a reply.
        presence = (Presence) response.nextResult(timeout);
    } finally {
        // Stop queuing results
        if (response != null) {
            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;
    joined = true;
    userHasJoined();
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) MUCInitialPresence(org.jivesoftware.smackx.packet.MUCInitialPresence) FromMatchesFilter(org.jivesoftware.smack.filter.FromMatchesFilter) PacketCollector(org.jivesoftware.smack.PacketCollector) MUCInitialPresence(org.jivesoftware.smackx.packet.MUCInitialPresence) Presence(org.jivesoftware.smack.packet.Presence) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) XMPPException(org.jivesoftware.smack.XMPPException) PacketInterceptor(org.jivesoftware.smack.PacketInterceptor)

Example 23 with AndFilter

use of org.jivesoftware.smack.filter.AndFilter 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 24 with AndFilter

use of org.jivesoftware.smack.filter.AndFilter 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 25 with AndFilter

use of org.jivesoftware.smack.filter.AndFilter in project Smack by igniterealtime.

the class IQTest method testInvalidNamespace.

/**
 * Check that the server responds a 503 error code when the client sends an IQ stanza with an
 * invalid namespace.
 */
public void testInvalidNamespace() {
    IQ iq = new IQ() {

        public String getChildElementXML() {
            StringBuilder buf = new StringBuilder();
            buf.append("<query xmlns=\"jabber:iq:anything\">");
            buf.append("</query>");
            return buf.toString();
        }
    };
    PacketFilter filter = new AndFilter(new PacketIDFilter(iq.getStanzaId()), new StanzaTypeFilter(IQ.class));
    StanzaCollector collector = getConnection(0).createStanzaCollector(filter);
    // Send the iq packet with an invalid namespace
    getConnection(0).sendStanza(iq);
    IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    collector.cancel();
    if (result == null) {
        fail("No response from server");
    } else if (result.getType() != IQ.Type.error) {
        fail("The server didn't reply with an error packet");
    } else {
        assertEquals("Server answered an incorrect error code", 503, result.getError().getCode());
    }
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) IQ(org.jivesoftware.smack.packet.IQ) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Aggregations

AndFilter (org.jivesoftware.smack.filter.AndFilter)34 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)15 Presence (org.jivesoftware.smack.packet.Presence)15 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)13 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)9 StanzaCollector (org.jivesoftware.smack.StanzaCollector)8 Message (org.jivesoftware.smack.packet.Message)8 XMPPException (org.jivesoftware.smack.XMPPException)7 FromMatchesFilter (org.jivesoftware.smack.filter.FromMatchesFilter)7 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)7 IQ (org.jivesoftware.smack.packet.IQ)7 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)6 Registration (org.jivesoftware.smack.packet.Registration)6 PacketCollector (org.jivesoftware.smack.PacketCollector)5 Packet (org.jivesoftware.smack.packet.Packet)5 Stanza (org.jivesoftware.smack.packet.Stanza)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)4 MUCInitialPresence (org.jivesoftware.smackx.packet.MUCInitialPresence)4