Search in sources :

Example 31 with PacketCollector

use of org.jivesoftware.smack.PacketCollector 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 32 with PacketCollector

use of org.jivesoftware.smack.PacketCollector 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 33 with PacketCollector

use of org.jivesoftware.smack.PacketCollector 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)

Example 34 with PacketCollector

use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.

the class VCard method save.

/**
 * Save this vCard for the user connected by 'connection'. Connection should be authenticated
 * and not anonymous.<p>
 * <p/>
 * NOTE: the method is asynchronous and does not wait for the returned value.
 *
 * @param connection the Connection to use.
 * @throws XMPPException thrown if there was an issue setting the VCard in the server.
 */
public void save(Connection connection) throws XMPPException {
    checkAuthenticated(connection, true);
    setType(IQ.Type.SET);
    setFrom(connection.getUser());
    PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(getPacketID()));
    connection.sendPacket(this);
    Packet response = collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // 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());
    }
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) PacketCollector(org.jivesoftware.smack.PacketCollector) XMPPException(org.jivesoftware.smack.XMPPException) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 35 with PacketCollector

use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.

the class VCard method doLoad.

private void doLoad(Connection connection, String user) throws XMPPException {
    setType(Type.GET);
    PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(getPacketID()));
    connection.sendPacket(this);
    VCard result = null;
    try {
        result = (VCard) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
        if (result == null) {
            String errorMessage = "Timeout getting VCard information";
            throw new XMPPException(errorMessage, new XMPPError(XMPPError.Condition.request_timeout, errorMessage));
        }
        if (result.getError() != null) {
            throw new XMPPException(result.getError());
        }
    } catch (ClassCastException e) {
        System.out.println("No VCard for " + user);
    }
    copyFieldsFrom(result);
}
Also used : PacketCollector(org.jivesoftware.smack.PacketCollector) XMPPError(org.jivesoftware.smack.packet.XMPPError) XMPPException(org.jivesoftware.smack.XMPPException) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Aggregations

PacketCollector (org.jivesoftware.smack.PacketCollector)46 XMPPException (org.jivesoftware.smack.XMPPException)44 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)36 IQ (org.jivesoftware.smack.packet.IQ)24 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)21 Packet (org.jivesoftware.smack.packet.Packet)10 ArrayList (java.util.ArrayList)6 AndFilter (org.jivesoftware.smack.filter.AndFilter)6 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)6 MUCAdmin (org.jivesoftware.smackx.packet.MUCAdmin)6 MUCOwner (org.jivesoftware.smackx.packet.MUCOwner)6 FromMatchesFilter (org.jivesoftware.smack.filter.FromMatchesFilter)4 Message (org.jivesoftware.smack.packet.Message)4 OfflineMessageRequest (org.jivesoftware.smackx.packet.OfflineMessageRequest)4 PacketInterceptor (org.jivesoftware.smack.PacketInterceptor)3 Presence (org.jivesoftware.smack.packet.Presence)3 MUCInitialPresence (org.jivesoftware.smackx.packet.MUCInitialPresence)3 Registration (org.jivesoftware.smack.packet.Registration)2 StreamInitiation (org.jivesoftware.smackx.packet.StreamInitiation)2 AgentInfo (org.jivesoftware.smackx.workgroup.packet.AgentInfo)2