* 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 * @throws XMPPErrorException * @throws NotConnectedException * @throws InterruptedException * @since 4.2.1 */ public LeafNode tryToPublishAndPossibleAutoCreate(String id, I item) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { LeafNode leafNode = new LeafNode(this, id); leafNode.publish(item); // 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 * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException * @throws InterruptedException */ 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 * @throws NoResponseException * @throws NotConnectedException * @throws InterruptedException */ public List* 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
* @throws NotConnectedException
* @throws InterruptedException
* @throws XMPPErrorException
*/
public boolean canCreateNodesAndPublishItems() throws NoResponseException, NotConnectedException, InterruptedException, XMPPErrorException {
LeafNode leafNode = null;
try {
leafNode = createNode();
}
catch (XMPPErrorException e) {
if (e.getXMPPError().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
* @throws XMPPErrorException
* @throws NotConnectedException
* @throws InterruptedException
* @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");
}
}