/** * Try to publish an item and, if the node with the given ID does not exists, auto-create the node. * <p> * Not every PubSub service supports automatic node creation. You can discover if this service supports it by using * {@link #supportsAutomaticNodeCreation()}. * </p> * * @param id The unique id of the node. * @param item The item to publish. * @param <I> 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 <I extends Item> 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 items persisted on the node, limited to the specified number. * * @param maxItems Maximum number of items to return * @param <T> type of the items. * * @return List of {@link Item} * @throws XMPPErrorException * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException * @throws InterruptedException */ public <T extends Item> List<T> getItems(int maxItems) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { PubSub request = createPubsubPacket(Type.get, new GetItemsRequest(getId(), maxItems)); return getItems(request); }
/** * Delete the item with the specified id from the node. * * @param itemId The id of the item * @throws XMPPErrorException * @throws NoResponseException * @throws NotConnectedException * @throws InterruptedException */ public void deleteItem(String itemId) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { Collection<String> items = new ArrayList<>(1); items.add(itemId); deleteItem(items); }
private LeafNode getLeafNodeProsodyWorkaround(final String id) throws NoResponseException, NotConnectedException, InterruptedException, NotALeafNodeException, XMPPErrorException { LeafNode leafNode = new LeafNode(this, id); try { // Try to ensure that this is not a collection node by asking for one item form the node. leafNode.getItems(1); } catch (XMPPErrorException e) { Condition condition = e.getStanzaError().getCondition(); if (condition == Condition.feature_not_implemented) { // XEP-0060 § 6.5.9.5: Item retrieval not supported, e.g. because node is a collection node throw new PubSubException.NotALeafNodeException(id, pubSubService); } throw e; } nodeMap.put(id, leafNode); return leafNode; }
/** * Purges the node of all items. * * <p>Note: Some implementations may keep the last item * sent. * @throws XMPPErrorException * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException * @throws InterruptedException */ public void deleteAllItems() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { PubSub request = createPubsubPacket(Type.set, new NodeExtension(PubSubElementType.PURGE_OWNER, getId())); pubSubManager.getConnection().createStanzaCollectorAndSend(request).nextResultOrThrow(); }
/** * Creates an instant node, if supported. * * @return The node that was created * @exception XMPPException */ public LeafNode createNode() throws XMPPException { PubSub reply = (PubSub)sendPubsubPacket(Type.SET, new NodeExtension(PubSubElementType.CREATE)); NodeExtension elem = (NodeExtension)reply.getExtension("create", PubSubNamespace.BASIC.getXmlns()); LeafNode newNode = new LeafNode(con, elem.getNode()); newNode.setTo(to); nodeMap.put(newNode.getId(), newNode); return newNode; }
/** * Publishes an event to the node. This is an empty event * with no item. * * This is only acceptable for nodes with {@link ConfigureForm#isPersistItems()}=false * and {@link ConfigureForm#isDeliverPayloads()}=false. * * @throws NotConnectedException * @throws InterruptedException * @throws XMPPErrorException * @throws NoResponseException * @deprecated use {@link #publish()} instead. */ @Deprecated public void send() throws NotConnectedException, InterruptedException, NoResponseException, XMPPErrorException { publish(); }
/** * Creates a node with specified configuration. * * Note: This is the only way to create a collection node. * * @param nodeId The name of the node, which must be unique within the * pubsub service * @param config The configuration for the node * @return The node that was created * @throws XMPPErrorException * @throws NoResponseException * @throws NotConnectedException * @throws InterruptedException */ public Node createNode(String nodeId, Form config) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { PubSub request = PubSub.createPubsubPacket(pubSubService, Type.set, new NodeExtension(PubSubElementType.CREATE, nodeId)); boolean isLeafNode = true; if (config != null) { request.addExtension(new FormNode(FormNodeType.CONFIGURE, config)); FormField nodeTypeField = config.getField(ConfigureNodeFields.node_type.getFieldName()); if (nodeTypeField != null) isLeafNode = nodeTypeField.getValues().get(0).toString().equals(NodeType.leaf.toString()); } // Errors will cause exceptions in getReply, so it only returns // on success. sendPubsubPacket(request); Node newNode = isLeafNode ? new LeafNode(this, nodeId) : new CollectionNode(this, nodeId); nodeMap.put(newNode.getId(), newNode); return newNode; }
List<Item> items = keyNode.getItems(1); if (items.isEmpty()) { LOGGER.log(Level.FINE, "Node " + keyNodeName + " is empty. Publish."); keyNode.publish(new PayloadItem<>(pubkeyElement)); } else { LOGGER.log(Level.FINE, "Node " + keyNodeName + " already contains key. Skip."); List<PayloadItem<PublicKeysListElement>> metadataItems = metadataNode.getItems(1); metadataNode.publish(new PayloadItem<>(builder.build()));
/** * Get the current items stored in the node. * * @param <T> type of the items. * @return List of {@link Item} in the node * @throws XMPPErrorException * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException * @throws InterruptedException */ public <T extends Item> List<T> getItems() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { return getItems((List<ExtensionElement>) null, null); }
/** * Creates an instant node, if supported. * * @return The node that was created * @throws XMPPErrorException * @throws NoResponseException * @throws NotConnectedException * @throws InterruptedException */ public LeafNode createNode() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { PubSub reply = sendPubsubPacket(Type.set, new NodeExtension(PubSubElementType.CREATE), null); NodeExtension elem = reply.getExtension("create", PubSubNamespace.basic.getXmlns()); LeafNode newNode = new LeafNode(this, elem.getNode()); nodeMap.put(newNode.getId(), newNode); return newNode; }
} finally { if (leafNode != null) { deleteNode(leafNode.getId());
/** * Publishes an event to the node. This can be either a simple item * with no payload, or one with it. This is determined by the Node * configuration. * * If the node has <b>deliver_payload=false</b>, the Item must not * have a payload. * * If the id is null, an empty item (one without an id) will be sent. * Please note that this is not the same as {@link #send()}, which * publishes an event with NO item. * * This is a synchronous call which will throw an exception * on failure. * * For asynchronous calls, use {@link #publish(Item) publish(Item)}. * * @param item - The item being sent * * @throws XMPPException */ public <T extends Item> void send(T item) throws XMPPException { Collection<T> items = new ArrayList<T>(1); items.add((item == null ? (T)new Item() : item)); send(items); }
pm.getLeafNode(OmemoConstants.PEP_NODE_BUNDLE_FROM_DEVICE_ID(id)).deleteAllItems(); } catch (InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | PubSubException.NotALeafNodeException | XMPPException.XMPPErrorException | pm.getLeafNode(OmemoConstants.PEP_NODE_DEVICE_LIST).deleteAllItems(); } catch (InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | PubSubException.NotALeafNodeException | XMPPException.XMPPErrorException |
/** * Delete the items with the specified id's from the node. * * @param itemIds The list of id's of items to delete * @throws XMPPErrorException * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException * @throws InterruptedException */ public void deleteItem(Collection<String> itemIds) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { List<Item> items = new ArrayList<>(itemIds.size()); for (String id : itemIds) { items.add(new Item(id)); } PubSub request = createPubsubPacket(Type.set, new ItemsExtension(ItemsExtension.ItemsElementType.retract, getId(), items)); pubSubManager.getConnection().createStanzaCollectorAndSend(request).nextResultOrThrow(); } }
/** * Creates an instant node, if supported. * * @return The node that was created * @exception XMPPException */ public LeafNode createNode() throws XMPPException { PubSub reply = (PubSub)sendPubsubPacket(Type.SET, new NodeExtension(PubSubElementType.CREATE)); NodeExtension elem = (NodeExtension)reply.getExtension("create", PubSubNamespace.BASIC.getXmlns()); LeafNode newNode = new LeafNode(con, elem.getNode()); newNode.setTo(to); nodeMap.put(newNode.getId(), newNode); return newNode; }
/** * Publishes multiple events to the node. Same rules apply as in {@link #publish(Item)}. * * In addition, if {@link ConfigureForm#isPersistItems()}=false, only the last item in the input * list will get stored on the node, assuming it stores the last sent item. * * @param items - The collection of items being sent * @param <T> type of the items. * * @throws NotConnectedException * @throws InterruptedException * @throws XMPPErrorException * @throws NoResponseException * @deprecated use {@link #publish(Collection)} instead. */ @Deprecated public <T extends Item> void send(Collection<T> items) throws NotConnectedException, InterruptedException, NoResponseException, XMPPErrorException { publish(items); }
node = new LeafNode(this, id);
@SmackIntegrationTest public void simplePubSubNodeTest() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { final String nodename = "sinttest-simple-nodename-" + testRunId; final String itemId = "sintest-simple-itemid-" + testRunId; ConfigureForm defaultConfiguration = pubSubManagerOne.getDefaultConfiguration(); ConfigureForm config = new ConfigureForm(defaultConfiguration.createAnswerForm()); // Configure the node as "Notification-Only Node", which in turn means that // items do not need payload, to prevent payload-required error responses when // publishing the item. config.setDeliverPayloads(false); config.setPersistentItems(true); Node node = pubSubManagerOne.createNode(nodename, config); try { LeafNode leafNode = (LeafNode) node; leafNode.publish(new Item(itemId)); List<Item> items = leafNode.getItems(); assertEquals(1, items.size()); Item item = items.get(0); assertEquals(itemId, item.getId()); } finally { pubSubManagerOne.deleteNode(nodename); } } }
private <T extends Item> List<T> getItems(PubSub request) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { return getItems(request, null); }