1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-29 09:42:06 +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,6 +156,7 @@ public class OfflineMessageManager {
} }
}); });
PacketCollector messageCollector = connection.createPacketCollector(messageFilter); PacketCollector messageCollector = connection.createPacketCollector(messageFilter);
try {
connection.createPacketCollectorAndSend(request).nextResultOrThrow(); connection.createPacketCollectorAndSend(request).nextResultOrThrow();
// Collect the received offline messages // Collect the received offline messages
Message message = (Message) messageCollector.nextResult(); Message message = (Message) messageCollector.nextResult();
@ -163,13 +164,16 @@ public class OfflineMessageManager {
messages.add(message); messages.add(message);
message = (Message) messageCollector.nextResult(); message = (Message) messageCollector.nextResult();
} }
}
finally {
// Stop queuing offline messages // Stop queuing offline messages
messageCollector.cancel(); 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);
try {
connection.createPacketCollectorAndSend(request).nextResultOrThrow();
// Collect the received offline messages // Collect the received offline messages
Message message = (Message) messageCollector.nextResult(); Message message = (Message) messageCollector.nextResult();
while (message != null) { while (message != null) {
messages.add(message); messages.add(message);
message = (Message) messageCollector.nextResult(); message = (Message) messageCollector.nextResult();
} }
}
finally {
// Stop queuing offline messages // Stop queuing offline messages
messageCollector.cancel(); messageCollector.cancel();
}
return messages; return messages;
} }