Use createPacketCollectorAndSend in FileTransferNegoiator

also fixes a potential PacketCollector leak.
This commit is contained in:
Florian Schmaus 2014-08-04 18:16:56 +02:00
parent 70f1c22bb2
commit d10ff92d44
3 changed files with 11 additions and 12 deletions

View File

@ -27,9 +27,9 @@ import java.util.Random;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Packet;
@ -298,10 +298,11 @@ public class FileTransferNegotiator extends Manager {
* @return Returns the stream negotiator selected by the peer. * @return Returns the stream negotiator selected by the peer.
* @throws XMPPErrorException Thrown if there is an error negotiating the file transfer. * @throws XMPPErrorException Thrown if there is an error negotiating the file transfer.
* @throws NotConnectedException * @throws NotConnectedException
* @throws NoResponseException
*/ */
public StreamNegotiator negotiateOutgoingTransfer(final String userID, public StreamNegotiator negotiateOutgoingTransfer(final String userID,
final String streamID, final String fileName, final long size, final String streamID, final String fileName, final long size,
final String desc, int responseTimeout) throws XMPPErrorException, NotConnectedException { final String desc, int responseTimeout) throws XMPPErrorException, NotConnectedException, NoResponseException {
StreamInitiation si = new StreamInitiation(); StreamInitiation si = new StreamInitiation();
si.setSessionID(streamID); si.setSessionID(streamID);
si.setMimeType(URLConnection.guessContentTypeFromName(fileName)); si.setMimeType(URLConnection.guessContentTypeFromName(fileName));
@ -316,9 +317,8 @@ public class FileTransferNegotiator extends Manager {
si.setTo(userID); si.setTo(userID);
si.setType(IQ.Type.set); si.setType(IQ.Type.set);
PacketCollector collector = connection().createPacketCollectorAndSend(si); Packet siResponse = connection().createPacketCollectorAndSend(si).nextResultOrThrow(
Packet siResponse = collector.nextResult(responseTimeout); responseTimeout);
collector.cancel();
if (siResponse instanceof IQ) { if (siResponse instanceof IQ) {
IQ iqResponse = (IQ) siResponse; IQ iqResponse = (IQ) siResponse;

View File

@ -373,12 +373,6 @@ public class OutgoingFileTransfer extends FileTransfer {
getPeer(), streamID, fileName, fileSize, description, getPeer(), streamID, fileName, fileSize, description,
RESPONSE_TIMEOUT); RESPONSE_TIMEOUT);
if (streamNegotiator == null) {
setStatus(Status.error);
setError(Error.no_response);
return null;
}
// Negotiate the stream // Negotiate the stream
if (!updateStatus(Status.negotiating_transfer, Status.negotiating_stream)) { if (!updateStatus(Status.negotiating_transfer, Status.negotiating_stream)) {
throw new IllegalStateChangeException(); throw new IllegalStateChangeException();

View File

@ -19,6 +19,7 @@ package org.jivesoftware.smackx.filetransfer;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.jivesoftware.smack.DummyConnection; import org.jivesoftware.smack.DummyConnection;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.junit.After; import org.junit.After;
@ -48,7 +49,11 @@ public class FileTransferNegotiatorTest {
@Test @Test
public void verifyForm() throws Exception { public void verifyForm() throws Exception {
FileTransferNegotiator fileNeg = FileTransferNegotiator.getInstanceFor(connection); FileTransferNegotiator fileNeg = FileTransferNegotiator.getInstanceFor(connection);
fileNeg.negotiateOutgoingTransfer("me", "streamid", "file", 1024, null, 10); try {
fileNeg.negotiateOutgoingTransfer("me", "streamid", "file", 1024, null, 10);
} catch (NoResponseException e) {
// Ignore
}
Packet packet = connection.getSentPacket(); Packet packet = connection.getSentPacket();
String xml = packet.toXML().toString(); String xml = packet.toXML().toString();
assertTrue(xml.indexOf("var='stream-method' type='list-single'") != -1); assertTrue(xml.indexOf("var='stream-method' type='list-single'") != -1);