PubSubManager.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.pubsub;

  18. import java.util.Collections;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.WeakHashMap;
  23. import java.util.concurrent.ConcurrentHashMap;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;

  26. import javax.xml.namespace.QName;

  27. import org.jivesoftware.smack.Manager;
  28. import org.jivesoftware.smack.SmackException.NoResponseException;
  29. import org.jivesoftware.smack.SmackException.NotConnectedException;
  30. import org.jivesoftware.smack.XMPPConnection;
  31. import org.jivesoftware.smack.XMPPException.XMPPErrorException;
  32. import org.jivesoftware.smack.packet.EmptyResultIQ;
  33. import org.jivesoftware.smack.packet.IQ;
  34. import org.jivesoftware.smack.packet.Stanza;
  35. import org.jivesoftware.smack.packet.StanzaError;
  36. import org.jivesoftware.smack.packet.StanzaError.Condition;
  37. import org.jivesoftware.smack.packet.XmlElement;
  38. import org.jivesoftware.smack.util.StringUtils;

  39. import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
  40. import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
  41. import org.jivesoftware.smackx.disco.packet.DiscoverItems;
  42. import org.jivesoftware.smackx.pubsub.PubSubException.NotALeafNodeException;
  43. import org.jivesoftware.smackx.pubsub.PubSubException.NotAPubSubNodeException;
  44. import org.jivesoftware.smackx.pubsub.form.ConfigureForm;
  45. import org.jivesoftware.smackx.pubsub.form.FillableConfigureForm;
  46. import org.jivesoftware.smackx.pubsub.packet.PubSub;
  47. import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
  48. import org.jivesoftware.smackx.pubsub.util.NodeUtils;
  49. import org.jivesoftware.smackx.xdata.packet.DataForm;

  50. import org.jxmpp.jid.BareJid;
  51. import org.jxmpp.jid.DomainBareJid;
  52. import org.jxmpp.jid.Jid;
  53. import org.jxmpp.jid.impl.JidCreate;
  54. import org.jxmpp.stringprep.XmppStringprepException;

  55. /**
  56.  * This is the starting point for access to the pubsub service.  It
  57.  * will provide access to general information about the service, as
  58.  * well as create or retrieve pubsub {@link LeafNode} instances.  These
  59.  * instances provide the bulk of the functionality as defined in the
  60.  * pubsub specification <a href="http://xmpp.org/extensions/xep-0060.html">XEP-0060</a>.
  61.  *
  62.  * @author Robin Collier
  63.  */
  64. public final class PubSubManager extends Manager {

  65.     public static final String PLUS_NOTIFY = "+notify";

  66.     public static final String AUTO_CREATE_FEATURE = "http://jabber.org/protocol/pubsub#auto-create";

  67.     private static final Logger LOGGER = Logger.getLogger(PubSubManager.class.getName());
  68.     private static final Map<XMPPConnection, Map<BareJid, PubSubManager>> INSTANCES = new WeakHashMap<>();

  69.     /**
  70.      * The JID of the PubSub service this manager manages.
  71.      */
  72.     private final BareJid pubSubService;

  73.     /**
  74.      * A map of node IDs to Nodes, used to cache those Nodes. This does only cache the type of Node,
  75.      * i.e. {@link CollectionNode} or {@link LeafNode}.
  76.      */
  77.     private final Map<String, Node> nodeMap = new ConcurrentHashMap<>();

  78.     /**
  79.      * Get a PubSub manager for the default PubSub service of the connection.
  80.      *
  81.      * @param connection TODO javadoc me please
  82.      * @return the default PubSub manager.
  83.      */
  84.     // CHECKSTYLE:OFF:RegexpSingleline
  85.     public static PubSubManager getInstanceFor(XMPPConnection connection) {
  86.     // CHECKSTYLE:ON:RegexpSingleline
  87.         DomainBareJid pubSubService = null;
  88.         if (connection.isAuthenticated()) {
  89.             try {
  90.                 pubSubService = getPubSubService(connection);
  91.             }
  92.             catch (NoResponseException | XMPPErrorException | NotConnectedException e) {
  93.                 LOGGER.log(Level.WARNING, "Could not determine PubSub service", e);
  94.             }
  95.             catch (InterruptedException e) {
  96.                 LOGGER.log(Level.FINE, "Interrupted while trying to determine PubSub service", e);
  97.             }
  98.         }
  99.         if (pubSubService == null) {
  100.             try {
  101.                 // Perform an educated guess about what the PubSub service's domain bare JID may be
  102.                 pubSubService = JidCreate.domainBareFrom("pubsub." + connection.getXMPPServiceDomain());
  103.             }
  104.             catch (XmppStringprepException e) {
  105.                 throw new RuntimeException(e);
  106.             }
  107.         }
  108.         return getInstanceFor(connection, pubSubService);
  109.     }

  110.     /**
  111.      * Get the PubSub manager for the given connection and PubSub service. Use <code>null</code> as argument for
  112.      * pubSubService to retrieve a PubSubManager for the users PEP service.
  113.      *
  114.      * @param connection the XMPP connection.
  115.      * @param pubSubService the PubSub service, may be <code>null</code>.
  116.      * @return a PubSub manager for the connection and service.
  117.      */
  118.     // CHECKSTYLE:OFF:RegexpSingleline
  119.     public static PubSubManager getInstanceFor(XMPPConnection connection, BareJid pubSubService) {
  120.     // CHECKSTYLE:ON:RegexpSingleline
  121.         if (pubSubService != null && connection.isAuthenticated() && connection.getUser().asBareJid().equals(pubSubService)) {
  122.             // PEP service.
  123.             pubSubService = null;
  124.         }

  125.         PubSubManager pubSubManager;
  126.         Map<BareJid, PubSubManager> managers;
  127.         synchronized (INSTANCES) {
  128.             managers = INSTANCES.get(connection);
  129.             if (managers == null) {
  130.                 managers = new HashMap<>();
  131.                 INSTANCES.put(connection, managers);
  132.             }
  133.         }
  134.         synchronized (managers) {
  135.             pubSubManager = managers.get(pubSubService);
  136.             if (pubSubManager == null) {
  137.                 pubSubManager = new PubSubManager(connection, pubSubService);
  138.                 managers.put(pubSubService, pubSubManager);
  139.             }
  140.         }

  141.         return pubSubManager;
  142.     }

  143.     /**
  144.      * Create a pubsub manager associated to the specified connection where
  145.      * the pubsub requests require a specific to address for packets.
  146.      *
  147.      * @param connection The XMPP connection
  148.      * @param toAddress The pubsub specific to address (required for some servers)
  149.      */
  150.     PubSubManager(XMPPConnection connection, BareJid toAddress) {
  151.         super(connection);
  152.         pubSubService = toAddress;
  153.     }

  154.     private void checkIfXmppErrorBecauseOfNotLeafNode(String nodeId, XMPPErrorException xmppErrorException)
  155.                     throws XMPPErrorException, NotALeafNodeException {
  156.         Condition condition = xmppErrorException.getStanzaError().getCondition();
  157.         if (condition == Condition.feature_not_implemented) {
  158.             // XEP-0060 § 6.5.9.5: Item retrieval not supported, e.g. because node is a collection node
  159.             throw new PubSubException.NotALeafNodeException(nodeId, pubSubService);
  160.         }

  161.         throw xmppErrorException;
  162.     }

  163.     /**
  164.      * Creates an instant node, if supported.
  165.      *
  166.      * @return The node that was created
  167.      * @throws XMPPErrorException if there was an XMPP error returned.
  168.      * @throws NoResponseException if there was no response from the remote entity.
  169.      * @throws NotConnectedException if the XMPP connection is not connected.
  170.      * @throws InterruptedException if the calling thread was interrupted.
  171.      */
  172.     public LeafNode createNode() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  173.         PubSub reply = sendPubsubPacket(IQ.Type.set, new NodeExtension(PubSubElementType.CREATE), null);
  174.         QName qname = new QName(PubSubNamespace.basic.getXmlns(), "create");
  175.         NodeExtension elem = (NodeExtension) reply.getExtension(qname);

  176.         LeafNode newNode = new LeafNode(this, elem.getNode());
  177.         nodeMap.put(newNode.getId(), newNode);

  178.         return newNode;
  179.     }

  180.     /**
  181.      * Creates a node with default configuration.
  182.      *
  183.      * @param nodeId The id of the node, which must be unique within the
  184.      * pubsub service
  185.      * @return The node that was created
  186.      * @throws XMPPErrorException if there was an XMPP error returned.
  187.      * @throws NoResponseException if there was no response from the remote entity.
  188.      * @throws NotConnectedException if the XMPP connection is not connected.
  189.      * @throws InterruptedException if the calling thread was interrupted.
  190.      */
  191.     public LeafNode createNode(String nodeId) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  192.         return (LeafNode) createNode(nodeId, null);
  193.     }

  194.     /**
  195.      * Creates a node with specified configuration.
  196.      *
  197.      * Note: This is the only way to create a collection node.
  198.      *
  199.      * @param nodeId The name of the node, which must be unique within the
  200.      * pubsub service
  201.      * @param config The configuration for the node
  202.      * @return The node that was created
  203.      * @throws XMPPErrorException if there was an XMPP error returned.
  204.      * @throws NoResponseException if there was no response from the remote entity.
  205.      * @throws NotConnectedException if the XMPP connection is not connected.
  206.      * @throws InterruptedException if the calling thread was interrupted.
  207.      */
  208.     public Node createNode(String nodeId, FillableConfigureForm config) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  209.         PubSub request = PubSub.createPubsubPacket(pubSubService, IQ.Type.set, new NodeExtension(PubSubElementType.CREATE, nodeId));
  210.         boolean isLeafNode = true;

  211.         if (config != null) {
  212.             DataForm submitForm = config.getDataFormToSubmit();
  213.             request.addExtension(new FormNode(FormNodeType.CONFIGURE, submitForm));
  214.             NodeType nodeType = config.getNodeType();
  215.             // Note that some implementations do to have the pubsub#node_type field in their default configuration,
  216.             // which I believe to be a bug. However, since PubSub specifies the default node type to be 'leaf' we assume
  217.             // leaf if the field does not exist.
  218.             isLeafNode = nodeType == null || nodeType == NodeType.leaf;
  219.         }

  220.         // Errors will cause exceptions in getReply, so it only returns
  221.         // on success.
  222.         sendPubsubPacket(request);
  223.         Node newNode = isLeafNode ? new LeafNode(this, nodeId) : new CollectionNode(this, nodeId);
  224.         nodeMap.put(newNode.getId(), newNode);

  225.         return newNode;
  226.     }

  227.     /**
  228.      * Retrieves the requested node, if it exists.  It will throw an
  229.      * exception if it does not.
  230.      *
  231.      * @param id - The unique id of the node
  232.      *
  233.      * @return the node
  234.      * @throws XMPPErrorException The node does not exist
  235.      * @throws NoResponseException if there was no response from the server.
  236.      * @throws NotConnectedException if the XMPP connection is not connected.
  237.      * @throws InterruptedException if the calling thread was interrupted.
  238.      * @throws NotAPubSubNodeException if a involved node is not a PubSub node.
  239.      */
  240.     public Node getNode(String id) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotAPubSubNodeException {
  241.         StringUtils.requireNotNullNorEmpty(id, "The node ID can not be null or the empty string");
  242.         Node node = nodeMap.get(id);

  243.         if (node == null) {
  244.             XMPPConnection connection = connection();
  245.             DiscoverInfo info = DiscoverInfo.builder(connection)
  246.                     .to(pubSubService)
  247.                     .setNode(id)
  248.                     .build();

  249.             DiscoverInfo infoReply = connection.sendIqRequestAndWaitForResponse(info);

  250.             if (infoReply.hasIdentity(PubSub.ELEMENT, "leaf")) {
  251.                 node = new LeafNode(this, id);
  252.             }
  253.             else if (infoReply.hasIdentity(PubSub.ELEMENT, "collection")) {
  254.                 node = new CollectionNode(this, id);
  255.             }
  256.             else {
  257.                 throw new PubSubException.NotAPubSubNodeException(id, infoReply);
  258.             }
  259.             nodeMap.put(id, node);
  260.         }
  261.         return node;
  262.     }

  263.     /**
  264.      * Try to get a leaf node and create one if it does not already exist.
  265.      *
  266.      * @param id The unique ID of the node.
  267.      * @return the leaf node.
  268.      * @throws NoResponseException if there was no response from the remote entity.
  269.      * @throws NotConnectedException if the XMPP connection is not connected.
  270.      * @throws InterruptedException if the calling thread was interrupted.
  271.      * @throws XMPPErrorException if there was an XMPP error returned.
  272.      * @throws NotALeafNodeException in case the node already exists as collection node.
  273.      * @since 4.2.1
  274.      */
  275.     public LeafNode getOrCreateLeafNode(final String id)
  276.                     throws NoResponseException, NotConnectedException, InterruptedException, XMPPErrorException, NotALeafNodeException {
  277.         try {
  278.             return getLeafNode(id);
  279.         }
  280.         catch (NotAPubSubNodeException e) {
  281.             return createNode(id);
  282.         }
  283.         catch (XMPPErrorException e1) {
  284.             if (e1.getStanzaError().getCondition() == Condition.item_not_found) {
  285.                 try {
  286.                     return createNode(id);
  287.                 }
  288.                 catch (XMPPErrorException e2) {
  289.                     if (e2.getStanzaError().getCondition() == Condition.conflict) {
  290.                         // The node was created in the meantime, re-try getNode(). Note that this case should be rare.
  291.                         try {
  292.                             return getLeafNode(id);
  293.                         }
  294.                         catch (NotAPubSubNodeException e) {
  295.                             // Should not happen
  296.                             throw new IllegalStateException(e);
  297.                         }
  298.                     }
  299.                     throw e2;
  300.                 }
  301.             }
  302.             if (e1.getStanzaError().getCondition() == Condition.service_unavailable) {
  303.                 // This could be caused by Prosody bug #805 (see https://prosody.im/issues/issue/805). Prosody does not
  304.                 // answer to disco#info requests on the node ID, which makes it undecidable if a node is a leaf or
  305.                 // collection node.
  306.                 LOGGER.warning("The PubSub service " + pubSubService
  307.                         + " threw an DiscoInfoNodeAssertionError, trying workaround for Prosody bug #805 (https://prosody.im/issues/issue/805)");
  308.                 return getOrCreateLeafNodeProsodyWorkaround(id);
  309.             }
  310.             throw e1;
  311.         }
  312.     }

  313.     /**
  314.      * Try to get a leaf node with the given node ID.
  315.      *
  316.      * @param id the node ID.
  317.      * @return the requested leaf node.
  318.      * @throws NotALeafNodeException in case the node exists but is a collection node.
  319.      * @throws NoResponseException if there was no response from the remote entity.
  320.      * @throws NotConnectedException if the XMPP connection is not connected.
  321.      * @throws InterruptedException if the calling thread was interrupted.
  322.      * @throws XMPPErrorException if there was an XMPP error returned.
  323.      * @throws NotAPubSubNodeException if a involved node is not a PubSub node.
  324.      * @since 4.2.1
  325.      */
  326.     public LeafNode getLeafNode(String id) throws NotALeafNodeException, NoResponseException, NotConnectedException,
  327.                     InterruptedException, XMPPErrorException, NotAPubSubNodeException {
  328.         Node node;
  329.         try {
  330.             node = getNode(id);
  331.         }
  332.         catch (XMPPErrorException e) {
  333.             if (e.getStanzaError().getCondition() == Condition.service_unavailable) {
  334.                 // This could be caused by Prosody bug #805 (see https://prosody.im/issues/issue/805). Prosody does not
  335.                 // answer to disco#info requests on the node ID, which makes it undecidable if a node is a leaf or
  336.                 // collection node.
  337.                 return getLeafNodeProsodyWorkaround(id);
  338.             }
  339.             throw e;
  340.         }

  341.         if (node instanceof LeafNode) {
  342.             return (LeafNode) node;
  343.         }

  344.         throw new PubSubException.NotALeafNodeException(id, pubSubService);
  345.     }

  346.     private LeafNode getLeafNodeProsodyWorkaround(final String id) throws NoResponseException, NotConnectedException,
  347.                     InterruptedException, NotALeafNodeException, XMPPErrorException {
  348.         LeafNode leafNode = new LeafNode(this, id);
  349.         try {
  350.             // Try to ensure that this is not a collection node by asking for one item form the node.
  351.             leafNode.getItems(1);
  352.         } catch (XMPPErrorException e) {
  353.             checkIfXmppErrorBecauseOfNotLeafNode(id, e);
  354.         }

  355.         nodeMap.put(id, leafNode);

  356.         return leafNode;
  357.     }

  358.     private LeafNode getOrCreateLeafNodeProsodyWorkaround(final String id)
  359.                     throws XMPPErrorException, NoResponseException, NotConnectedException, InterruptedException, NotALeafNodeException {
  360.         try {
  361.             return createNode(id);
  362.         }
  363.         catch (XMPPErrorException e1) {
  364.             if (e1.getStanzaError().getCondition() == Condition.conflict) {
  365.                 return getLeafNodeProsodyWorkaround(id);
  366.             }
  367.             throw e1;
  368.         }
  369.     }

  370.     /**
  371.      * Try to publish an item and, if the node with the given ID does not exists, auto-create the node.
  372.      * <p>
  373.      * Not every PubSub service supports automatic node creation. You can discover if this service supports it by using
  374.      * {@link #supportsAutomaticNodeCreation()}.
  375.      * </p>
  376.      *
  377.      * @param id The unique id of the node.
  378.      * @param item The item to publish.
  379.      * @param <I> type of the item.
  380.      *
  381.      * @return the LeafNode on which the item was published.
  382.      * @throws NoResponseException if there was no response from the remote entity.
  383.      * @throws XMPPErrorException if there was an XMPP error returned.
  384.      * @throws NotConnectedException if the XMPP connection is not connected.
  385.      * @throws InterruptedException if the calling thread was interrupted.
  386.      * @throws NotALeafNodeException if a PubSub leaf node operation was attempted on a non-leaf node.
  387.      * @since 4.2.1
  388.      */
  389.     public <I extends Item> LeafNode tryToPublishAndPossibleAutoCreate(String id, I item)
  390.                     throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException,
  391.                     NotALeafNodeException {
  392.         LeafNode leafNode = new LeafNode(this, id);

  393.         try {
  394.             leafNode.publish(item);
  395.         } catch (XMPPErrorException e) {
  396.             checkIfXmppErrorBecauseOfNotLeafNode(id, e);
  397.         }

  398.         // If LeafNode.publish() did not throw then we have successfully published an item and possible auto-created
  399.         // (XEP-0163 § 3., XEP-0060 § 7.1.4) the node. So we can put the node into the nodeMap.
  400.         nodeMap.put(id, leafNode);

  401.         return leafNode;
  402.     }

  403.     /**
  404.      * Get all the nodes that currently exist as a child of the specified
  405.      * collection node.  If the service does not support collection nodes
  406.      * then all nodes will be returned.
  407.      *
  408.      * To retrieve contents of the root collection node (if it exists),
  409.      * or there is no root collection node, pass null as the nodeId.
  410.      *
  411.      * @param nodeId - The id of the collection node for which the child
  412.      * nodes will be returned.
  413.      * @return {@link DiscoverItems} representing the existing nodes
  414.      * @throws XMPPErrorException if there was an XMPP error returned.
  415.      * @throws NoResponseException if there was no response from the server.
  416.      * @throws NotConnectedException if the XMPP connection is not connected.
  417.      * @throws InterruptedException if the calling thread was interrupted.
  418.      */
  419.     public DiscoverItems discoverNodes(String nodeId) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  420.         DiscoverItems items = new DiscoverItems();

  421.         if (nodeId != null)
  422.             items.setNode(nodeId);
  423.         items.setTo(pubSubService);
  424.         DiscoverItems nodeItems = connection().sendIqRequestAndWaitForResponse(items);
  425.         return nodeItems;
  426.     }

  427.     /**
  428.      * Gets the subscriptions on the root node.
  429.      *
  430.      * @return List of exceptions
  431.      * @throws XMPPErrorException if there was an XMPP error returned.
  432.      * @throws NoResponseException if there was no response from the remote entity.
  433.      * @throws NotConnectedException if the XMPP connection is not connected.
  434.      * @throws InterruptedException if the calling thread was interrupted.
  435.      */
  436.     public List<Subscription> getSubscriptions() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  437.         Stanza reply = sendPubsubPacket(IQ.Type.get, new NodeExtension(PubSubElementType.SUBSCRIPTIONS), null);
  438.         SubscriptionsExtension subElem = (SubscriptionsExtension) reply.getExtensionElement(PubSubElementType.SUBSCRIPTIONS.getElementName(), PubSubElementType.SUBSCRIPTIONS.getNamespace().getXmlns());
  439.         return subElem.getSubscriptions();
  440.     }

  441.     /**
  442.      * Gets the affiliations on the root node.
  443.      *
  444.      * @return List of affiliations
  445.      * @throws XMPPErrorException if there was an XMPP error returned.
  446.      * @throws NoResponseException if there was no response from the remote entity.
  447.      * @throws NotConnectedException if the XMPP connection is not connected.
  448.      * @throws InterruptedException if the calling thread was interrupted.
  449.      *
  450.      */
  451.     public List<Affiliation> getAffiliations() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  452.         PubSub reply = sendPubsubPacket(IQ.Type.get, new NodeExtension(PubSubElementType.AFFILIATIONS), null);
  453.         AffiliationsExtension listElem = reply.getExtension(PubSubElementType.AFFILIATIONS);
  454.         return listElem.getAffiliations();
  455.     }

  456.     /**
  457.      * Delete the specified node.
  458.      *
  459.      * @param nodeId TODO javadoc me please
  460.      * @throws XMPPErrorException if there was an XMPP error returned.
  461.      * @throws NoResponseException if there was no response from the remote entity.
  462.      * @throws NotConnectedException if the XMPP connection is not connected.
  463.      * @throws InterruptedException if the calling thread was interrupted.
  464.      * @return <code>true</code> if this node existed and was deleted and <code>false</code> if this node did not exist.
  465.      */
  466.     public boolean deleteNode(String nodeId) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  467.         boolean res = true;
  468.         try {
  469.             sendPubsubPacket(IQ.Type.set, new NodeExtension(PubSubElementType.DELETE, nodeId), PubSubElementType.DELETE.getNamespace());
  470.         } catch (XMPPErrorException e) {
  471.             if (e.getStanzaError().getCondition() == StanzaError.Condition.item_not_found) {
  472.                 res = false;
  473.             } else {
  474.                 throw e;
  475.             }
  476.         }
  477.         nodeMap.remove(nodeId);
  478.         return res;
  479.     }

  480.     /**
  481.      * Returns the default settings for Node configuration.
  482.      *
  483.      * @return configuration form containing the default settings.
  484.      * @throws XMPPErrorException if there was an XMPP error returned.
  485.      * @throws NoResponseException if there was no response from the remote entity.
  486.      * @throws NotConnectedException if the XMPP connection is not connected.
  487.      * @throws InterruptedException if the calling thread was interrupted.
  488.      */
  489.     public ConfigureForm getDefaultConfiguration() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  490.         // Errors will cause exceptions in getReply, so it only returns
  491.         // on success.
  492.         PubSub reply = sendPubsubPacket(IQ.Type.get, new NodeExtension(PubSubElementType.DEFAULT), PubSubElementType.DEFAULT.getNamespace());
  493.         return NodeUtils.getFormFromPacket(reply, PubSubElementType.DEFAULT);
  494.     }

  495.     /**
  496.      * Get the JID of the PubSub service managed by this manager.
  497.      *
  498.      * @return the JID of the PubSub service.
  499.      */
  500.     public BareJid getServiceJid() {
  501.         return pubSubService;
  502.     }

  503.     /**
  504.      * Gets the supported features of the servers pubsub implementation
  505.      * as a standard {@link DiscoverInfo} instance.
  506.      *
  507.      * @return The supported features
  508.      * @throws XMPPErrorException if there was an XMPP error returned.
  509.      * @throws NoResponseException if there was no response from the remote entity.
  510.      * @throws NotConnectedException if the XMPP connection is not connected.
  511.      * @throws InterruptedException if the calling thread was interrupted.
  512.      */
  513.     public DiscoverInfo getSupportedFeatures() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  514.         ServiceDiscoveryManager mgr = ServiceDiscoveryManager.getInstanceFor(connection());
  515.         return mgr.discoverInfo(pubSubService);
  516.     }

  517.     /**
  518.      * Check if the PubSub service supports automatic node creation.
  519.      *
  520.      * @return true if the PubSub service supports automatic node creation.
  521.      * @throws NoResponseException if there was no response from the remote entity.
  522.      * @throws XMPPErrorException if there was an XMPP error returned.
  523.      * @throws NotConnectedException if the XMPP connection is not connected.
  524.      * @throws InterruptedException if the calling thread was interrupted.
  525.      * @since 4.2.1
  526.      * @see <a href="https://xmpp.org/extensions/xep-0060.html#publisher-publish-autocreate">XEP-0060 § 7.1.4 Automatic Node Creation</a>
  527.      */
  528.     public boolean supportsAutomaticNodeCreation()
  529.                     throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  530.         ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection());
  531.         return sdm.supportsFeature(pubSubService, AUTO_CREATE_FEATURE);
  532.     }

  533.     /**
  534.      * Check if it is possible to create PubSub nodes on this service. It could be possible that the
  535.      * PubSub service allows only certain XMPP entities (clients) to create nodes and publish items
  536.      * to them.
  537.      * <p>
  538.      * Note that since XEP-60 does not provide an API to determine if an XMPP entity is allowed to
  539.      * create nodes, therefore this method creates an instant node calling {@link #createNode()} to
  540.      * determine if it is possible to create nodes.
  541.      * </p>
  542.      *
  543.      * @return <code>true</code> if it is possible to create nodes, <code>false</code> otherwise.
  544.      * @throws NoResponseException if there was no response from the remote entity.
  545.      * @throws NotConnectedException if the XMPP connection is not connected.
  546.      * @throws InterruptedException if the calling thread was interrupted.
  547.      * @throws XMPPErrorException if there was an XMPP error returned.
  548.      */
  549.     public boolean canCreateNodesAndPublishItems() throws NoResponseException, NotConnectedException, InterruptedException, XMPPErrorException {
  550.         LeafNode leafNode = null;
  551.         try {
  552.             leafNode = createNode();
  553.         }
  554.         catch (XMPPErrorException e) {
  555.             if (e.getStanzaError().getCondition() == StanzaError.Condition.forbidden) {
  556.                 return false;
  557.             }
  558.             throw e;
  559.         } finally {
  560.             if (leafNode != null) {
  561.                 deleteNode(leafNode.getId());
  562.             }
  563.         }
  564.         return true;
  565.     }

  566.     private PubSub sendPubsubPacket(IQ.Type type, XmlElement ext, PubSubNamespace ns)
  567.                     throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  568.         return sendPubsubPacket(pubSubService, type, Collections.singletonList(ext), ns);
  569.     }

  570.     XMPPConnection getConnection() {
  571.         return connection();
  572.     }

  573.     PubSub sendPubsubPacket(Jid to, IQ.Type type, List<XmlElement> extList, PubSubNamespace ns)
  574.                     throws NoResponseException, XMPPErrorException, NotConnectedException,
  575.                     InterruptedException {
  576. // CHECKSTYLE:OFF
  577.         PubSub pubSub = new PubSub(to, type, ns);
  578.         for (XmlElement pe : extList) {
  579.             pubSub.addExtension(pe);
  580.         }
  581. // CHECKSTYLE:ON
  582.         return sendPubsubPacket(pubSub);
  583.     }

  584.     PubSub sendPubsubPacket(PubSub packet) throws NoResponseException, XMPPErrorException,
  585.                     NotConnectedException, InterruptedException {
  586.         IQ resultIQ = connection().sendIqRequestAndWaitForResponse(packet);
  587.         if (resultIQ instanceof EmptyResultIQ) {
  588.             return null;
  589.         }
  590.         return (PubSub) resultIQ;
  591.     }

  592.     /**
  593.      * Get the "default" PubSub service for a given XMPP connection. The default PubSub service is
  594.      * simply an arbitrary XMPP service with the PubSub feature and an identity of category "pubsub"
  595.      * and type "service".
  596.      *
  597.      * @param connection TODO javadoc me please
  598.      * @return the default PubSub service or <code>null</code>.
  599.      * @throws NoResponseException if there was no response from the remote entity.
  600.      * @throws XMPPErrorException if there was an XMPP error returned.
  601.      * @throws NotConnectedException if the XMPP connection is not connected.
  602.      * @throws InterruptedException if the calling thread was interrupted.
  603.      * @see <a href="http://xmpp.org/extensions/xep-0060.html#entity-features">XEP-60 § 5.1 Discover
  604.      *      Features</a>
  605.      */
  606.     public static DomainBareJid getPubSubService(XMPPConnection connection)
  607.                     throws NoResponseException, XMPPErrorException, NotConnectedException,
  608.                     InterruptedException {
  609.         return ServiceDiscoveryManager.getInstanceFor(connection).findService(PubSub.NAMESPACE,
  610.                         true, "pubsub", "service");
  611.     }
  612. }