1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 20:12:07 +01:00

Make getMessages() set up collector *before* sending the request

Similar flaw was also fixed in 2c7f1efe80.

Also ensure that OfflineMessagesManager doesn't leak collector by using
a try/finally block.

Fixes SMACK-592
This commit is contained in:
Florian Schmaus 2014-08-04 12:19:58 +02:00
parent a574e1d56d
commit d65d239550

View file

@ -156,20 +156,24 @@ public class OfflineMessageManager {
} }
}); });
PacketCollector messageCollector = connection.createPacketCollector(messageFilter); PacketCollector messageCollector = connection.createPacketCollector(messageFilter);
connection.createPacketCollectorAndSend(request).nextResultOrThrow(); try {
// Collect the received offline messages connection.createPacketCollectorAndSend(request).nextResultOrThrow();
Message message = (Message) messageCollector.nextResult(); // Collect the received offline messages
while (message != null) { Message message = (Message) messageCollector.nextResult();
messages.add(message); while (message != null) {
message = (Message) messageCollector.nextResult(); messages.add(message);
message = (Message) messageCollector.nextResult();
}
}
finally {
// Stop queuing offline messages
messageCollector.cancel();
} }
// Stop queuing offline messages
messageCollector.cancel();
return messages; return messages;
} }
/** /**
* Returns an Iterator with all the offline <tt>Messages</tt> of the user. The returned offline * Returns a List of Messages with all the offline <tt>Messages</tt> of the user. The returned offline
* messages will not be deleted from the server. Use {@link #deleteMessages(java.util.List)} * messages will not be deleted from the server. Use {@link #deleteMessages(java.util.List)}
* to delete the messages. * to delete the messages.
* *
@ -183,17 +187,22 @@ public class OfflineMessageManager {
List<Message> messages = new ArrayList<Message>(); List<Message> messages = new ArrayList<Message>();
OfflineMessageRequest request = new OfflineMessageRequest(); OfflineMessageRequest request = new OfflineMessageRequest();
request.setFetch(true); request.setFetch(true);
connection.createPacketCollectorAndSend(request).nextResultOrThrow();
PacketCollector messageCollector = connection.createPacketCollector(packetFilter); PacketCollector messageCollector = connection.createPacketCollector(packetFilter);
// Collect the received offline messages try {
Message message = (Message) messageCollector.nextResult(); connection.createPacketCollectorAndSend(request).nextResultOrThrow();
while (message != null) {
messages.add(message); // Collect the received offline messages
message = (Message) messageCollector.nextResult(); Message message = (Message) messageCollector.nextResult();
while (message != null) {
messages.add(message);
message = (Message) messageCollector.nextResult();
}
}
finally {
// Stop queuing offline messages
messageCollector.cancel();
} }
// Stop queuing offline messages
messageCollector.cancel();
return messages; return messages;
} }