Search in sources :

Example 46 with PacketIDFilter

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

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

the class FileTransferNegotiator method negotiateOutgoingTransfer.

/**
 * Send a request to another user to send them a file. The other user has
 * the option of, accepting, rejecting, or not responding to a received file
 * transfer request.
 * <p/>
 * If they accept, the packet will contain the other user's chosen stream
 * type to send the file across. The two choices this implementation
 * provides to the other user for file transfer are <a
 * href="http://www.jabber.org/jeps/jep-0065.html">SOCKS5 Bytestreams</a>,
 * which is the preferred method of transfer, and <a
 * href="http://www.jabber.org/jeps/jep-0047.html">In-Band Bytestreams</a>,
 * which is the fallback mechanism.
 * <p/>
 * The other user may choose to decline the file request if they do not
 * desire the file, their client does not support JEP-0096, or if there are
 * no acceptable means to transfer the file.
 * <p/>
 * Finally, if the other user does not respond this method will return null
 * after the specified timeout.
 *
 * @param userID          The userID of the user to whom the file will be sent.
 * @param streamID        The unique identifier for this file transfer.
 * @param fileName        The name of this file. Preferably it should include an
 *                        extension as it is used to determine what type of file it is.
 * @param size            The size, in bytes, of the file.
 * @param desc            A description of the file.
 * @param responseTimeout The amount of time, in milliseconds, to wait for the remote
 *                        user to respond. If they do not respond in time, this
 * @return Returns the stream negotiator selected by the peer.
 * @throws XMPPException Thrown if there is an error negotiating the file transfer.
 */
public StreamNegotiator negotiateOutgoingTransfer(final String userID, final String streamID, final String fileName, final long size, final String desc, int responseTimeout) throws XMPPException {
    StreamInitiation si = new StreamInitiation();
    si.setSesssionID(streamID);
    si.setMimeType(URLConnection.guessContentTypeFromName(fileName));
    StreamInitiation.File siFile = new StreamInitiation.File(fileName, size);
    siFile.setDesc(desc);
    si.setFile(siFile);
    si.setFeatureNegotiationForm(createDefaultInitiationForm());
    si.setFrom(connection.getUser());
    si.setTo(userID);
    si.setType(IQ.Type.SET);
    PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(si.getPacketID()));
    connection.sendPacket(si);
    Packet siResponse = collector.nextResult(responseTimeout);
    collector.cancel();
    if (siResponse instanceof IQ) {
        IQ iqResponse = (IQ) siResponse;
        if (iqResponse.getType().equals(IQ.Type.RESULT)) {
            StreamInitiation response = (StreamInitiation) siResponse;
            return getOutgoingNegotiator(getStreamMethodField(response.getFeatureNegotiationForm()));
        } else if (iqResponse.getType().equals(IQ.Type.ERROR)) {
            throw new XMPPException(iqResponse.getError());
        } else {
            throw new XMPPException("File transfer response unreadable");
        }
    } else {
        return null;
    }
}
Also used : StreamInitiation(org.jivesoftware.smackx.packet.StreamInitiation) Packet(org.jivesoftware.smack.packet.Packet) PacketCollector(org.jivesoftware.smack.PacketCollector) IQ(org.jivesoftware.smack.packet.IQ) XMPPException(org.jivesoftware.smack.XMPPException) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 48 with PacketIDFilter

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

the class ServiceDiscoveryManager method publishItems.

/**
 * Publishes new items to a parent entity and node. The item elements to publish MUST have at
 * least a 'jid' attribute specifying the Entity ID of the item, and an action attribute which
 * specifies the action being taken for that item. Possible action values are: "update" and
 * "remove".
 *
 * @param entityID the address of the XMPP entity.
 * @param node the attribute that supplements the 'jid' attribute.
 * @param discoverItems the DiscoveryItems to publish.
 * @throws XMPPException if the operation failed for some reason.
 */
public void publishItems(String entityID, String node, DiscoverItems discoverItems) throws XMPPException {
    discoverItems.setType(IQ.Type.SET);
    discoverItems.setTo(entityID);
    discoverItems.setNode(node);
    // Create a packet collector to listen for a response.
    PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(discoverItems.getPacketID()));
    connection.sendPacket(discoverItems);
    // Wait up to 5 seconds for a result.
    IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    collector.cancel();
    if (result == null) {
        throw new XMPPException("No response from the server.");
    }
    if (result.getType() == IQ.Type.ERROR) {
        throw new XMPPException(result.getError());
    }
}
Also used : IQ(org.jivesoftware.smack.packet.IQ) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 49 with PacketIDFilter

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

the class ServiceDiscoveryManager method discoverInfo.

/**
 * Returns the discovered information of a given XMPP entity addressed by its JID and
 * note attribute. Use this message only when trying to query information which is not
 * directly addressable.
 *
 * @see <a href="http://xmpp.org/extensions/xep-0030.html#info-basic">XEP-30 Basic Protocol</a>
 * @see <a href="http://xmpp.org/extensions/xep-0030.html#info-nodes">XEP-30 Info Nodes</a>
 *
 * @param entityID the address of the XMPP entity.
 * @param node the optional attribute that supplements the 'jid' attribute.
 * @return the discovered information.
 * @throws XMPPException if the operation failed for some reason.
 */
public DiscoverInfo discoverInfo(String entityID, String node) throws XMPPException {
    // Discover the entity's info
    DiscoverInfo disco = new DiscoverInfo();
    disco.setType(IQ.Type.GET);
    disco.setTo(entityID);
    disco.setNode(node);
    // Create a packet collector to listen for a response.
    PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(disco.getPacketID()));
    connection.sendPacket(disco);
    // Wait up to 5 seconds for a result.
    IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    collector.cancel();
    if (result == null) {
        throw new XMPPException("No response from the server.");
    }
    if (result.getType() == IQ.Type.ERROR) {
        throw new XMPPException(result.getError());
    }
    return (DiscoverInfo) result;
}
Also used : DiscoverInfo(org.jivesoftware.smackx.packet.DiscoverInfo) IQ(org.jivesoftware.smack.packet.IQ) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 50 with PacketIDFilter

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

the class SharedGroupManager method getSharedGroups.

/**
 * Returns the collection that will contain the name of the shared groups where the user
 * logged in with the specified session belongs.
 *
 * @param connection connection to use to get the user's shared groups.
 * @return collection with the shared groups' name of the logged user.
 */
public static List<String> getSharedGroups(Connection connection) throws XMPPException {
    // Discover the shared groups of the logged user
    SharedGroupsInfo info = new SharedGroupsInfo();
    info.setType(IQ.Type.GET);
    // Create a packet collector to listen for a response.
    PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(info.getPacketID()));
    connection.sendPacket(info);
    // Wait up to 5 seconds for a result.
    IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    collector.cancel();
    if (result == null) {
        throw new XMPPException("No response from the server.");
    }
    if (result.getType() == IQ.Type.ERROR) {
        throw new XMPPException(result.getError());
    }
    return ((SharedGroupsInfo) result).getGroups();
}
Also used : SharedGroupsInfo(org.jivesoftware.smackx.packet.SharedGroupsInfo) PacketCollector(org.jivesoftware.smack.PacketCollector) IQ(org.jivesoftware.smack.packet.IQ) XMPPException(org.jivesoftware.smack.XMPPException) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Aggregations

PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)66 IQ (org.jivesoftware.smack.packet.IQ)38 PacketCollector (org.jivesoftware.smack.PacketCollector)36 XMPPException (org.jivesoftware.smack.XMPPException)34 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)22 Packet (org.jivesoftware.smack.packet.Packet)10 AndFilter (org.jivesoftware.smack.filter.AndFilter)7 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)6 Registration (org.jivesoftware.smack.packet.Registration)6 MUCAdmin (org.jivesoftware.smackx.packet.MUCAdmin)6 MUCOwner (org.jivesoftware.smackx.packet.MUCOwner)6 RosterPacket (org.jivesoftware.smack.packet.RosterPacket)4 ArrayList (java.util.ArrayList)3 XMPPConnection (org.jivesoftware.smack.XMPPConnection)3 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 HashMap (java.util.HashMap)2 StanzaCollector (org.jivesoftware.smack.StanzaCollector)2 StanzaListener (org.jivesoftware.smack.StanzaListener)2 Authentication (org.jivesoftware.smack.packet.Authentication)2