/** * 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. * * @param item - The item being sent * @param <T> type of the items. * * @throws XMPPErrorException * @throws NoResponseException * @throws NotConnectedException * @throws InterruptedException * */ @SuppressWarnings("unchecked") public <T extends Item> void publish(T item) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { Collection<T> items = new ArrayList<>(1); items.add((item == null ? (T) new Item() : item)); publish(items); }
@Override public String toString() { return getClass().getName() + " | Content [" + toXML(null) + "]"; }
@Test public void verifyBasicItem() throws Exception { Item simpleItem = new Item(); String simpleCtrl = "<item xmlns='http://jabber.org/protocol/pubsub' />"; assertXMLEqual(simpleCtrl, simpleItem.toXML(null).toString()); Item idItem = new Item("uniqueid"); String idCtrl = "<item xmlns='http://jabber.org/protocol/pubsub' id='uniqueid'/>"; assertXMLEqual(idCtrl, idItem.toXML(null).toString()); Item itemWithNodeId = new Item("testId", "testNode"); String nodeIdCtrl = "<item xmlns='http://jabber.org/protocol/pubsub' id='testId' node='testNode' />"; assertXMLEqual(nodeIdCtrl, itemWithNodeId.toXML(null).toString()); }
@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); } } }
@Override public String toXML() { StringBuilder builder = new StringBuilder("<item"); if (id != null) { builder.append(" id='"); builder.append(id); builder.append("'"); } if (getNode() != null) { builder.append(" node='"); builder.append(getNode()); builder.append("'"); } builder.append("/>"); return builder.toString(); }
@Test public void parseBasicItem() throws Exception { XmlPullParser parser = PacketParserUtils.getParserFor( "<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" + "<event xmlns='http://jabber.org/protocol/pubsub#event'>" + "<items node='testNode'>" + "<item id='testid1' />" + "</items>" + "</event>" + "</message>"); Stanza message = PacketParserUtils.parseMessage(parser); ExtensionElement eventExt = message.getExtension(PubSubNamespace.event.getXmlns()); assertTrue(eventExt instanceof EventElement); EventElement event = (EventElement) eventExt; assertEquals(EventElementType.items, event.getEventType()); assertEquals(1, event.getExtensions().size()); assertTrue(event.getExtensions().get(0) instanceof ItemsExtension); assertEquals(1, ((ItemsExtension) event.getExtensions().get(0)).items.size()); NamedElement itemExt = ((ItemsExtension) event.getExtensions().get(0)).items.get(0); assertTrue(itemExt instanceof Item); assertEquals("testid1", ((Item) itemExt).getId()); }
@Override public XmlStringBuilder toXML(String enclosingNamespace) { XmlStringBuilder xml = getCommonXml(); xml.closeEmptyElement(); return xml; }
@Override public String toXML() { StringBuilder builder = new StringBuilder("<item"); if (id != null) { builder.append(" id='"); builder.append(id); builder.append("'"); } if (getNode() != null) { builder.append(" node='"); builder.append(getNode()); builder.append("'"); } builder.append("/>"); return builder.toString(); }
/** * Get the items specified from the node. This would typically be * used when the server does not return the payload due to size * constraints. The user would be required to retrieve the payload * after the items have been retrieved via {@link #getItems()} or an * event, that did not include the payload. * * @param ids Item ids of the items to retrieve * @param <T> type of the items. * * @return The list of {@link Item} with payload * @throws XMPPErrorException * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException * @throws InterruptedException */ public <T extends Item> List<T> getItems(Collection<String> ids) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { List<Item> itemList = new ArrayList<>(ids.size()); for (String id : ids) { itemList.add(new Item(id)); } PubSub request = createPubsubPacket(Type.get, new ItemsExtension(ItemsExtension.ItemsElementType.items, getId(), itemList)); return getItems(request); }
@Override public String toXML(String enclosingNamespace) { StringBuilder builder = new StringBuilder("<"); builder.append(getElementName()); builder.append(" node='"); builder.append(getNode()); builder.append("'>"); for (Item item : items) { builder.append(item.toXML(null)); } builder.append("</publish>"); return builder.toString(); } }
@Override public String toXML() { StringBuilder builder = new StringBuilder("<item"); if (id != null) { builder.append(" id='"); builder.append(id); builder.append("'"); } if (getNode() != null) { builder.append(" node='"); builder.append(getNode()); builder.append("'"); } builder.append("/>"); return builder.toString(); }
/** * 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(); } }
@Override public String toString() { return getClass().getName() + " | Content [" + toXML() + "]"; } }
@Override public Item parse(XmlPullParser parser, int initialDepth) throws Exception { String id = parser.getAttributeValue(null, "id"); String node = parser.getAttributeValue(null, "node"); String xmlns = parser.getNamespace(); ItemNamespace itemNamespace = ItemNamespace.fromXmlns(xmlns); int tag = parser.next(); if (tag == XmlPullParser.END_TAG) { return new Item(itemNamespace, id, node); } else { String payloadElemName = parser.getName(); String payloadNS = parser.getNamespace(); final ExtensionElementProvider<ExtensionElement> extensionProvider = ProviderManager.getExtensionProvider(payloadElemName, payloadNS); if (extensionProvider == null) { // TODO: Should we use StandardExtensionElement in this case? And probably remove SimplePayload all together. CharSequence payloadText = PacketParserUtils.parseElement(parser, true); return new PayloadItem<>(itemNamespace, id, node, new SimplePayload(payloadText.toString())); } else { return new PayloadItem<>(itemNamespace, id, node, extensionProvider.parse(parser)); } } }
@Override public String toString() { return getClass().getName() + " | Content [" + toXML() + "]"; } }
/** * Publishes an event to the node. This is a simple item * with no 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 an asynchronous call which returns as soon as the * packet has been sent. * * For synchronous calls use {@link #send(Item) send(Item))}. * * @param item - The item being sent */ public <T extends Item> void publish(T item) { Collection<T> items = new ArrayList<T>(1); items.add((T)(item == null ? new Item() : item)); publish(items); }
@Override public String toString() { return getClass().getName() + " | Content [" + toXML() + "]"; } }
/** * Publishes an event to the node. This is a simple item * with no 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 an asynchronous call which returns as soon as the * packet has been sent. * * For synchronous calls use {@link #send(Item) send(Item))}. * * @param item - The item being sent */ public <T extends Item> void publish(T item) { Collection<T> items = new ArrayList<T>(1); items.add((T)(item == null ? new Item() : item)); publish(items); }
@Override public String toXML() { StringBuilder builder = new StringBuilder("<"); builder.append(getElementName()); builder.append(" node='"); builder.append(getNode()); builder.append("'>"); for (Item item : items) { builder.append(item.toXML()); } builder.append("</publish>"); return builder.toString(); } }