use of org.jivesoftware.smackx.offline.packet.OfflineMessageRequest in project Smack by igniterealtime.
the class OfflineMessageManager method getMessages.
/**
* Returns a List of the offline <tt>Messages</tt> whose stamp matches the specified
* request. The request will include the list of stamps that uniquely identifies
* the offline messages to retrieve. The returned offline messages will not be deleted
* from the server. Use {@link #deleteMessages(java.util.List)} to delete the messages.
*
* @param nodes the list of stamps that uniquely identifies offline message.
* @return a List with the offline <tt>Messages</tt> that were received as part of
* this request.
* @throws XMPPErrorException If the user is not allowed to make this request or the server does
* not support offline message retrieval.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
* @throws InterruptedException
*/
public List<Message> getMessages(final List<String> nodes) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
List<Message> messages = new ArrayList<Message>();
OfflineMessageRequest request = new OfflineMessageRequest();
for (String node : nodes) {
OfflineMessageRequest.Item item = new OfflineMessageRequest.Item(node);
item.setAction("view");
request.addItem(item);
}
// Filter offline messages that were requested by this request
StanzaFilter messageFilter = new AndFilter(PACKET_FILTER, new StanzaFilter() {
@Override
public boolean accept(Stanza packet) {
OfflineMessageInfo info = (OfflineMessageInfo) packet.getExtension("offline", namespace);
return nodes.contains(info.getNode());
}
});
int pendingNodes = nodes.size();
StanzaCollector messageCollector = connection.createStanzaCollector(messageFilter);
try {
connection.createStanzaCollectorAndSend(request).nextResultOrThrow();
// Collect the received offline messages
Message message = messageCollector.nextResult();
while (message != null && pendingNodes > 0) {
pendingNodes--;
messages.add(message);
message = messageCollector.nextResult();
}
} finally {
// Stop queuing offline messages
messageCollector.cancel();
}
return messages;
}
use of org.jivesoftware.smackx.offline.packet.OfflineMessageRequest in project Smack by igniterealtime.
the class OfflineMessageManager method deleteMessages.
/**
* Deletes the specified list of offline messages. The request will include the list of
* stamps that uniquely identifies the offline messages to delete.
*
* @param nodes the list of stamps that uniquely identifies offline message.
* @throws XMPPErrorException If the user is not allowed to make this request or the server does
* not support offline message retrieval.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
* @throws InterruptedException
*/
public void deleteMessages(List<String> nodes) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
OfflineMessageRequest request = new OfflineMessageRequest();
request.setType(IQ.Type.set);
for (String node : nodes) {
OfflineMessageRequest.Item item = new OfflineMessageRequest.Item(node);
item.setAction("remove");
request.addItem(item);
}
connection.createStanzaCollectorAndSend(request).nextResultOrThrow();
}
use of org.jivesoftware.smackx.offline.packet.OfflineMessageRequest in project Smack by igniterealtime.
the class OfflineMessageManager method deleteMessages.
/**
* Deletes all offline messages of the user.
*
* @throws XMPPErrorException If the user is not allowed to make this request or the server does
* not support offline message retrieval.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
* @throws InterruptedException
*/
public void deleteMessages() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
OfflineMessageRequest request = new OfflineMessageRequest();
request.setType(IQ.Type.set);
request.setPurge(true);
connection.createStanzaCollectorAndSend(request).nextResultOrThrow();
}
use of org.jivesoftware.smackx.offline.packet.OfflineMessageRequest in project Smack by igniterealtime.
the class OfflineMessageManager method getMessages.
/**
* 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)}
* to delete the messages.
*
* @return a List with all the offline <tt>Messages</tt> of the user.
* @throws XMPPErrorException If the user is not allowed to make this request or the server does
* not support offline message retrieval.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
* @throws InterruptedException
*/
public List<Message> getMessages() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
OfflineMessageRequest request = new OfflineMessageRequest();
request.setFetch(true);
StanzaCollector resultCollector = connection.createStanzaCollectorAndSend(request);
StanzaCollector.Configuration messageCollectorConfiguration = StanzaCollector.newConfiguration().setStanzaFilter(PACKET_FILTER).setCollectorToReset(resultCollector);
StanzaCollector messageCollector = connection.createStanzaCollector(messageCollectorConfiguration);
List<Message> messages = null;
try {
resultCollector.nextResultOrThrow();
// Be extra safe, cancel the message collector right here so that it does not collector
// other messages that eventually match (although I've no idea how this could happen in
// case of XEP-13).
messageCollector.cancel();
messages = new ArrayList<>(messageCollector.getCollectedCount());
Message message;
while ((message = messageCollector.pollResult()) != null) {
messages.add(message);
}
} finally {
// Ensure that the message collector is canceled even if nextResultOrThrow threw. It
// doesn't matter if we cancel the message collector twice
messageCollector.cancel();
}
return messages;
}
Aggregations