
rocks.xmpp.extensions.offline.OfflineMessageManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xmpp-extensions-client Show documentation
Show all versions of xmpp-extensions-client Show documentation
Client side business logic for XMPP extensions
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2016 Christian Schudt
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package rocks.xmpp.extensions.offline;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.session.Manager;
import rocks.xmpp.core.session.XmppSession;
import rocks.xmpp.core.stanza.model.IQ;
import rocks.xmpp.extensions.caps.EntityCapabilitiesManager;
import rocks.xmpp.extensions.data.model.DataForm;
import rocks.xmpp.extensions.disco.ServiceDiscoveryManager;
import rocks.xmpp.extensions.offline.model.OfflineMessage;
import rocks.xmpp.extensions.offline.model.OfflineMessageHeader;
import rocks.xmpp.util.concurrent.AsyncResult;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* This manager covers the use cases of XEP-0013: Flexible Offline Message Retrieval.
*
* Offline Message Retrieval must be used before sending initial presence, in order to tell the server, that it must not flood the client with offline messages later.
*
* Enabling or disabling this manager has no effect.
*
* @author Christian Schudt
*/
public final class OfflineMessageManager extends Manager {
private OfflineMessageManager(XmppSession xmppSession) {
super(xmppSession);
}
/**
* Discovers support for flexible offline message retrieval.
*
* @return The async result with true, if the server supports flexible offline message retrieval; otherwise false.
*/
public AsyncResult isSupported() {
EntityCapabilitiesManager entityCapabilitiesManager = xmppSession.getManager(EntityCapabilitiesManager.class);
return entityCapabilitiesManager.discoverCapabilities(xmppSession.getDomain())
.thenApply(infoNode -> infoNode.getFeatures().contains(OfflineMessage.NAMESPACE));
}
/**
* Gets the number of offline messages.
*
* @return The async result with the number of offline messages.
* @see 2.2 Requesting Number of Messages
*/
public AsyncResult requestNumberOfMessages() {
ServiceDiscoveryManager serviceDiscoveryManager = xmppSession.getManager(ServiceDiscoveryManager.class);
return serviceDiscoveryManager.discoverInformation(null, OfflineMessage.NAMESPACE).thenApply(infoDiscovery -> {
if (!infoDiscovery.getExtensions().isEmpty()) {
DataForm dataForm = infoDiscovery.getExtensions().get(0);
if (dataForm != null) {
for (DataForm.Field field : dataForm.getFields()) {
if ("number_of_messages".equals(field.getVar())) {
String numberOfMessages = field.getValues().get(0);
return Integer.valueOf(numberOfMessages);
}
}
}
}
return 0;
});
}
/**
* Gets the offline message headers.
*
* @return The async result with the list of message headers.
* @see 2.3 Requesting Message Headers
*/
public AsyncResult> requestMessageHeaders() {
ServiceDiscoveryManager serviceDiscoveryManager = xmppSession.getManager(ServiceDiscoveryManager.class);
return serviceDiscoveryManager.discoverItems(null, OfflineMessage.NAMESPACE).thenApply(itemNode ->
itemNode.getItems().stream()
.map(item -> new OfflineMessageHeader(Jid.of(item.getName()), item.getNode()))
.collect(Collectors.toList())
);
}
/**
* Requests a specific offline message. The message will be sent in a normal way and contains the {@link OfflineMessage} extension, which can be used to match the id {@link OfflineMessage#getId()}.
*
* @param id The offline message id, which corresponds to {@link OfflineMessageHeader#getId()}
* @return The async result with the server's IQ reply.
* @see 2.4 Retrieving Specific Messages
*/
public AsyncResult requestMessage(String id) {
return xmppSession.query(IQ.get(new OfflineMessage(new OfflineMessage.Item(id, OfflineMessage.Item.Action.VIEW))));
}
/**
* Removes specific offline messages.
*
* @param ids The offline message ids, which correspond to {@link OfflineMessageHeader#getId()}
* @return The async result with the server's IQ reply.
* @see 2.5 Removing Specific Messages
*/
public AsyncResult removeMessages(String... ids) {
List items = new ArrayList<>();
for (String id : ids) {
items.add(new OfflineMessage.Item(id, OfflineMessage.Item.Action.REMOVE));
}
return xmppSession.query(IQ.set(new OfflineMessage(items)));
}
/**
* Requests all offline messages.
*
* @return The async result with the server's IQ reply.
* @see 2.6 Retrieving All Messages
*/
public AsyncResult requestAllMessages() {
return xmppSession.query(IQ.get(new OfflineMessage(true, false)));
}
/**
* Removes all offline messages.
*
* @return The async result with the server's IQ reply.
* @see 2.7 Removing All Messages
*/
public AsyncResult removeAllMessages() {
return xmppSession.query(IQ.set(new OfflineMessage(false, true)));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy