/** * Send geolocation through the PubSub node. * * @param geoLocation * @throws InterruptedException * @throws NotConnectedException * @throws XMPPErrorException * @throws NoResponseException * @throws NotALeafNodeException */ public void sendGeolocation(GeoLocation geoLocation) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotALeafNodeException { getNode().publish(new PayloadItem<GeoLocation>(geoLocation)); }
@Test public void verifyPayloadItem() throws Exception { SimplePayload payload = new SimplePayload("<data xmlns='https://example.org'>This is the payload</data>"); PayloadItem<SimplePayload> simpleItem = new PayloadItem<>(payload); String simpleCtrl = "<item xmlns='http://jabber.org/protocol/pubsub'>" + payload.toXML(null) + "</item>"; assertXMLEqual(simpleCtrl, simpleItem.toXML(null).toString()); PayloadItem<SimplePayload> idItem = new PayloadItem<>("uniqueid", payload); String idCtrl = "<item xmlns='http://jabber.org/protocol/pubsub' id='uniqueid'>" + payload.toXML(null) + "</item>"; assertXMLEqual(idCtrl, idItem.toXML(null).toString()); PayloadItem<SimplePayload> itemWithNodeId = new PayloadItem<>("testId", "testNode", payload); String nodeIdCtrl = "<item xmlns='http://jabber.org/protocol/pubsub' id='testId' node='testNode'>" + payload.toXML(null) + "</item>"; assertXMLEqual(nodeIdCtrl, itemWithNodeId.toXML(null).toString()); }
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."); if (!metadataItems.isEmpty() && metadataItems.get(0).getPayload() != null) { PublicKeysListElement publishedList = metadataItems.get(0).getPayload(); for (PublicKeysListElement.PubkeyMetadataElement meta : publishedList.getMetadata().values()) { builder.addMetadata(meta); metadataNode.publish(new PayloadItem<>(builder.build()));
@Test public void parseSimplePayloadItem() throws Exception { String itemContent = "<foo xmlns='smack:test'>Some text</foo>"; 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' >" + itemContent + "</item>" + "</items>" + "</event>" + "</message>"); Stanza message = PacketParserUtils.parseMessage(parser); ExtensionElement eventExt = message.getExtension(PubSubNamespace.event.getXmlns()); EventElement event = (EventElement) eventExt; NamedElement itemExt = ((ItemsExtension) event.getExtensions().get(0)).items.get(0); assertTrue(itemExt instanceof PayloadItem<?>); PayloadItem<?> item = (PayloadItem<?>) itemExt; assertEquals("testid1", item.getId()); assertTrue(item.getPayload() instanceof SimplePayload); SimplePayload payload = (SimplePayload) item.getPayload(); assertEquals("foo", payload.getElementName()); assertEquals("smack:test", payload.getNamespace()); assertXMLEqual(itemContent, payload.toXML(null).toString()); }
@Override public String toXML() { StringBuilder builder = new StringBuilder("<item"); if (getId() != null) { builder.append(" id='"); builder.append(getId()); builder.append("'"); } if (getNode() != null) { builder.append(" node='"); builder.append(getNode()); builder.append("'"); } builder.append(">"); builder.append(payload.toXML()); builder.append("</item>"); return builder.toString(); }
@Override public String toString() { return getClass().getName() + " | Content [" + toXML(null) + "]"; } }
/** * Retrieve the OMEMO device list of a contact. * * @param connection authenticated XMPP connection. * @param contact BareJid of the contact of which we want to retrieve the device list from. * @return * @throws InterruptedException * @throws PubSubException.NotALeafNodeException * @throws SmackException.NoResponseException * @throws SmackException.NotConnectedException * @throws XMPPException.XMPPErrorException * @throws PubSubException.NotAPubSubNodeException */ private static OmemoDeviceListElement fetchDeviceList(XMPPConnection connection, BareJid contact) throws InterruptedException, PubSubException.NotALeafNodeException, SmackException.NoResponseException, SmackException.NotConnectedException, XMPPException.XMPPErrorException, PubSubException.NotAPubSubNodeException { PubSubManager pm = PubSubManager.getInstance(connection, contact); String nodeName = OmemoConstants.PEP_NODE_DEVICE_LIST; LeafNode node = pm.getLeafNode(nodeName); if (node == null) { return null; } List<PayloadItem<OmemoDeviceListElement>> items = node.getItems(); if (items.isEmpty()) { return null; } return items.get(items.size() - 1).getPayload(); }
Jid bridgeId = JidCreate.from(item.getId()); verifyJvbJid(bridgeId);
PayloadItem<?> item = (PayloadItem<?>) itemExt; assertEquals("testid1", item.getId()); assertTrue(item.getPayload() instanceof SimplePayload); assertXMLEqual(itemContent, item.getPayload().toXML(null).toString());
@Override public String toXML() { StringBuilder builder = new StringBuilder("<item"); if (getId() != null) { builder.append(" id='"); builder.append(getId()); builder.append("'"); } if (getNode() != null) { builder.append(" node='"); builder.append(getNode()); builder.append("'"); } builder.append(">"); builder.append(payload.toXML()); builder.append("</item>"); return builder.toString(); }
@Override public String toString() { return getClass().getName() + " | Content [" + toXML() + "]"; } }
/** * Fetch the latest {@link SecretkeyElement} from the private backup node. * * @see <a href="https://xmpp.org/extensions/xep-0373.html#synchro-pep"> * XEP-0373 §5. Synchronizing the Secret Key with a Private PEP Node</a> * * @param pepManager the PEP manager. * @return the secret key node or null, if it doesn't exist. * * @throws InterruptedException if the thread gets interrupted * @throws PubSubException.NotALeafNodeException if there is an issue with the PubSub node * @throws XMPPException.XMPPErrorException if there is an XMPP protocol related issue * @throws SmackException.NotConnectedException if we are not connected * @throws SmackException.NoResponseException /watch?v=7U0FzQzJzyI */ public static SecretkeyElement fetchSecretKey(PepManager pepManager) throws InterruptedException, PubSubException.NotALeafNodeException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException { PubSubManager pm = pepManager.getPepPubSubManager(); LeafNode secretKeyNode = pm.getOrCreateLeafNode(PEP_NODE_SECRET_KEY); List<PayloadItem<SecretkeyElement>> list = secretKeyNode.getItems(1); if (list.size() == 0) { LOGGER.log(Level.INFO, "No secret key published!"); return null; } SecretkeyElement secretkeyElement = list.get(0).getPayload(); return secretkeyElement; }
@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)); } } }
PayloadItem<?> item = (PayloadItem<?>) itemExt; assertEquals("testid1", item.getId()); assertTrue(item.getPayload() instanceof SimplePayload); SimplePayload payload = (SimplePayload) item.getPayload(); assertEquals("entry", payload.getElementName()); assertEquals("http://www.w3.org/2005/Atom", payload.getNamespace());
@Override public String toXML() { StringBuilder builder = new StringBuilder("<item"); if (getId() != null) { builder.append(" id='"); builder.append(getId()); builder.append("'"); } if (getNode() != null) { builder.append(" node='"); builder.append(getNode()); builder.append("'"); } builder.append(">"); builder.append(payload.toXML()); builder.append("</item>"); return builder.toString(); }
@Override public String toString() { return getClass().getName() + " | Content [" + toXML() + "]"; } }
/** * Consult the public key metadata node of {@code contact} to fetch the list of their published OpenPGP public keys. * * @see <a href="https://xmpp.org/extensions/xep-0373.html#discover-pubkey-list"> * XEP-0373 §4.3: Discovering Public Keys of a User</a> * * @param connection XMPP connection * @param contact {@link BareJid} of the user we want to fetch the list from. * @return content of {@code contact}'s metadata node. * * @throws InterruptedException if the thread gets interrupted. * @throws XMPPException.XMPPErrorException in case of an XMPP protocol exception. * @throws SmackException.NoResponseException in case the server doesn't respond * @throws PubSubException.NotALeafNodeException in case the queried node is not a {@link LeafNode} * @throws SmackException.NotConnectedException in case we are not connected * @throws PubSubException.NotAPubSubNodeException in case the queried entity is not a PubSub node */ public static PublicKeysListElement fetchPubkeysList(XMPPConnection connection, BareJid contact) throws InterruptedException, XMPPException.XMPPErrorException, SmackException.NoResponseException, PubSubException.NotALeafNodeException, SmackException.NotConnectedException, PubSubException.NotAPubSubNodeException { PubSubManager pm = PubSubManager.getInstance(connection, contact); LeafNode node = getLeafNode(pm, PEP_NODE_PUBLIC_KEYS); List<PayloadItem<PublicKeysListElement>> list = node.getItems(1); if (list.isEmpty()) { return null; } return list.get(0).getPayload(); }
/** * Send empty geolocation through the PubSub node. * * @throws InterruptedException * @throws NotConnectedException * @throws XMPPErrorException * @throws NoResponseException * @throws NotALeafNodeException */ public void stopPublishingGeolocation() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotALeafNodeException { GeoLocation emptyGeolocation = new GeoLocation.Builder().build(); getNode().publish(new PayloadItem<GeoLocation>(emptyGeolocation)); }
/** * Notifies all <tt>SubscriptionListener</tt>s about published * <tt>PayloadItem</tt>. * @param payloadItem new <tt>PayloadItem</tt> published to the PubSub * node observed by this subscription. */ void notifyListeners(PayloadItem payloadItem) { for (SubscriptionListener l : listeners) { l.onSubscriptionUpdate( node, payloadItem.getId(), payloadItem.getPayload()); } }
@Override public String toString() { return getClass().getName() + " | Content [" + toXML() + "]"; } }