null
as argument for
* pubSubService to retrieve a PubSubManager for the users PEP service.
*
* @param connection the XMPP connection.
* @param pubSubService the PubSub service, may be null
.
* @return a PubSub manager for the connection and service.
*/
// CHECKSTYLE:OFF:RegexpSingleline
public static PubSubManager getInstanceFor(XMPPConnection connection, BareJid pubSubService) {
// CHECKSTYLE:ON:RegexpSingleline
if (pubSubService != null && connection.isAuthenticated() && connection.getUser().asBareJid().equals(pubSubService)) {
// PEP service.
pubSubService = null;
}
PubSubManager pubSubManager;
Map* Not every PubSub service supports automatic node creation. You can discover if this service supports it by using * {@link #supportsAutomaticNodeCreation()}. *
* * @param id The unique id of the node. * @param item The item to publish. * @param type of the item. * * @return the LeafNode on which the item was published. * @throws NoResponseException if there was no response from the remote entity. * @throws XMPPErrorException if there was an XMPP error returned. * @throws NotConnectedException if the XMPP connection is not connected. * @throws InterruptedException if the calling thread was interrupted. * @throws NotALeafNodeException if a PubSub leaf node operation was attempted on a non-leaf node. * @since 4.2.1 */ public LeafNode tryToPublishAndPossibleAutoCreate(String id, I item) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotALeafNodeException { LeafNode leafNode = new LeafNode(this, id); try { leafNode.publish(item); } catch (XMPPErrorException e) { checkIfXmppErrorBecauseOfNotLeafNode(id, e); } // If LeafNode.publish() did not throw then we have successfully published an item and possible auto-created // (XEP-0163 § 3., XEP-0060 § 7.1.4) the node. So we can put the node into the nodeMap. nodeMap.put(id, leafNode); return leafNode; } /** * Get all the nodes that currently exist as a child of the specified * collection node. If the service does not support collection nodes * then all nodes will be returned. * * To retrieve contents of the root collection node (if it exists), * or there is no root collection node, pass null as the nodeId. * * @param nodeId - The id of the collection node for which the child * nodes will be returned. * @return {@link DiscoverItems} representing the existing nodes * @throws XMPPErrorException if there was an XMPP error returned. * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException if the XMPP connection is not connected. * @throws InterruptedException if the calling thread was interrupted. */ public DiscoverItems discoverNodes(String nodeId) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { DiscoverItems items = new DiscoverItems(); if (nodeId != null) items.setNode(nodeId); items.setTo(pubSubService); DiscoverItems nodeItems = connection().createStanzaCollectorAndSend(items).nextResultOrThrow(); return nodeItems; } /** * Gets the subscriptions on the root node. * * @return List of exceptions * @throws XMPPErrorException if there was an XMPP error returned. * @throws NoResponseException if there was no response from the remote entity. * @throws NotConnectedException if the XMPP connection is not connected. * @throws InterruptedException if the calling thread was interrupted. */ public Listtrue
if this node existed and was deleted and false
if this node did not exist.
*/
public boolean deleteNode(String nodeId) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
boolean res = true;
try {
sendPubsubPacket(Type.set, new NodeExtension(PubSubElementType.DELETE, nodeId), PubSubElementType.DELETE.getNamespace());
} catch (XMPPErrorException e) {
if (e.getStanzaError().getCondition() == StanzaError.Condition.item_not_found) {
res = false;
} else {
throw e;
}
}
nodeMap.remove(nodeId);
return res;
}
/**
* Returns the default settings for Node configuration.
*
* @return configuration form containing the default settings.
* @throws XMPPErrorException if there was an XMPP error returned.
* @throws NoResponseException if there was no response from the remote entity.
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws InterruptedException if the calling thread was interrupted.
*/
public ConfigureForm getDefaultConfiguration() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
// Errors will cause exceptions in getReply, so it only returns
// on success.
PubSub reply = sendPubsubPacket(Type.get, new NodeExtension(PubSubElementType.DEFAULT), PubSubElementType.DEFAULT.getNamespace());
return NodeUtils.getFormFromPacket(reply, PubSubElementType.DEFAULT);
}
/**
* Get the JID of the PubSub service managed by this manager.
*
* @return the JID of the PubSub service.
*/
public BareJid getServiceJid() {
return pubSubService;
}
/**
* Gets the supported features of the servers pubsub implementation
* as a standard {@link DiscoverInfo} instance.
*
* @return The supported features
* @throws XMPPErrorException if there was an XMPP error returned.
* @throws NoResponseException if there was no response from the remote entity.
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws InterruptedException if the calling thread was interrupted.
*/
public DiscoverInfo getSupportedFeatures() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
ServiceDiscoveryManager mgr = ServiceDiscoveryManager.getInstanceFor(connection());
return mgr.discoverInfo(pubSubService);
}
/**
* Check if the PubSub service supports automatic node creation.
*
* @return true if the PubSub service supports automatic node creation.
* @throws NoResponseException if there was no response from the remote entity.
* @throws XMPPErrorException if there was an XMPP error returned.
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws InterruptedException if the calling thread was interrupted.
* @since 4.2.1
* @see XEP-0060 § 7.1.4 Automatic Node Creation
*/
public boolean supportsAutomaticNodeCreation()
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection());
return sdm.supportsFeature(pubSubService, AUTO_CREATE_FEATURE);
}
/**
* Check if it is possible to create PubSub nodes on this service. It could be possible that the
* PubSub service allows only certain XMPP entities (clients) to create nodes and publish items
* to them.
* * Note that since XEP-60 does not provide an API to determine if an XMPP entity is allowed to * create nodes, therefore this method creates an instant node calling {@link #createNode()} to * determine if it is possible to create nodes. *
* * @returntrue
if it is possible to create nodes, false
otherwise.
* @throws NoResponseException if there was no response from the remote entity.
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws InterruptedException if the calling thread was interrupted.
* @throws XMPPErrorException if there was an XMPP error returned.
*/
public boolean canCreateNodesAndPublishItems() throws NoResponseException, NotConnectedException, InterruptedException, XMPPErrorException {
LeafNode leafNode = null;
try {
leafNode = createNode();
}
catch (XMPPErrorException e) {
if (e.getStanzaError().getCondition() == StanzaError.Condition.forbidden) {
return false;
}
throw e;
} finally {
if (leafNode != null) {
deleteNode(leafNode.getId());
}
}
return true;
}
private PubSub sendPubsubPacket(Type type, ExtensionElement ext, PubSubNamespace ns)
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
return sendPubsubPacket(pubSubService, type, Collections.singletonList(ext), ns);
}
XMPPConnection getConnection() {
return connection();
}
PubSub sendPubsubPacket(Jid to, Type type, Listnull
.
* @throws NoResponseException if there was no response from the remote entity.
* @throws XMPPErrorException if there was an XMPP error returned.
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws InterruptedException if the calling thread was interrupted.
* @see XEP-60 § 5.1 Discover
* Features
*/
public static DomainBareJid getPubSubService(XMPPConnection connection)
throws NoResponseException, XMPPErrorException, NotConnectedException,
InterruptedException {
return ServiceDiscoveryManager.getInstanceFor(connection).findService(PubSub.NAMESPACE,
true, "pubsub", "service");
}
}