/** * Creates a new PEP exchange manager. * * @param connection an XMPPConnection which is used to send and receive messages. */ private PepManager(XMPPConnection connection) { super(connection); StanzaListener packetListener = new StanzaListener() { @Override public void processStanza(Stanza stanza) { final Message message = (Message) stanza; final EventElement event = EventElement.from(stanza); assert (event != null); final EntityBareJid from = message.getFrom().asEntityBareJidIfPossible(); assert (from != null); asyncButOrdered.performAsyncButOrdered(from, new Runnable() { @Override public void run() { for (PepListener listener : pepListeners) { listener.eventReceived(from, event, message); } } }); } }; // TODO Add filter to check if from supports PubSub as per xep163 2 2.4 connection.addSyncStanzaListener(packetListener, FROM_BARE_JID_WITH_EVENT_EXTENSION_FILTER); pepPubSubManager = PubSubManager.getInstance(connection, null); }
private LeafNode getNode() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotALeafNodeException { return PubSubManager.getInstance(connection()).getOrCreateLeafNode(GeoLocation.NAMESPACE); }
/** * 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(); }
/** * 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(); }
/** * Get a PubSub manager for the default PubSub service of the connection. * * @param connection * @return the default PubSub manager. */ public static PubSubManager getInstance(XMPPConnection connection) { DomainBareJid pubSubService = null; if (connection.isAuthenticated()) { try { pubSubService = getPubSubService(connection); } catch (NoResponseException | XMPPErrorException | NotConnectedException e) { LOGGER.log(Level.WARNING, "Could not determine PubSub service", e); } catch (InterruptedException e) { LOGGER.log(Level.FINE, "Interrupted while trying to determine PubSub service", e); } } if (pubSubService == null) { try { // Perform an educated guess about what the PubSub service's domain bare JID may be pubSubService = JidCreate.domainBareFrom("pubsub." + connection.getXMPPServiceDomain()); } catch (XmppStringprepException e) { throw new RuntimeException(e); } } return getInstance(connection, pubSubService); }
public PubSubIntegrationTest(SmackIntegrationTestEnvironment environment) throws TestNotPossibleException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { super(environment); DomainBareJid pubSubService = PubSubManager.getPubSubService(conOne); if (pubSubService == null) { throw new TestNotPossibleException("No PubSub service found"); } pubSubManagerOne = PubSubManager.getInstance(conOne, pubSubService); if (!pubSubManagerOne.canCreateNodesAndPublishItems()) { throw new TestNotPossibleException("PubSub service does not allow node creation"); } }
/** * Fetch the OpenPGP public key of a {@code contact}, identified by its OpenPGP {@code v4_fingerprint}. * * @see <a href="https://xmpp.org/extensions/xep-0373.html#discover-pubkey">XEP-0373 §4.3</a> * * @param connection XMPP connection * @param contact {@link BareJid} of the contact we want to fetch a key from. * @param v4_fingerprint upper case, hex encoded v4 fingerprint of the contacts key. * @return {@link PubkeyElement} containing the requested public key. * * @throws InterruptedException if the thread gets interrupted.A * @throws XMPPException.XMPPErrorException in case of an XMPP protocol error. * @throws PubSubException.NotAPubSubNodeException in case the targeted entity is not a PubSub node. * @throws PubSubException.NotALeafNodeException in case the fetched node is not a {@link LeafNode}. * @throws SmackException.NotConnectedException in case we are not connected. * @throws SmackException.NoResponseException if the server doesn't respond. */ public static PubkeyElement fetchPubkey(XMPPConnection connection, BareJid contact, OpenPgpV4Fingerprint v4_fingerprint) throws InterruptedException, XMPPException.XMPPErrorException, PubSubException.NotAPubSubNodeException, PubSubException.NotALeafNodeException, SmackException.NotConnectedException, SmackException.NoResponseException { PubSubManager pm = PubSubManager.getInstance(connection, contact); String nodeName = PEP_NODE_PUBLIC_KEY(v4_fingerprint); LeafNode node = getLeafNode(pm, nodeName); List<PayloadItem<PubkeyElement>> list = node.getItems(1); if (list.isEmpty()) { return null; } return list.get(0).getPayload(); }
/** * Retrieve a users OMEMO bundle. * * @param connection authenticated XMPP connection. * @param contactsDevice device of which we want to retrieve the bundle. * @return OmemoBundle of the device or null, if it doesn't exist. * * @throws SmackException.NotConnectedException * @throws InterruptedException * @throws SmackException.NoResponseException * @throws XMPPException.XMPPErrorException * @throws PubSubException.NotALeafNodeException * @throws PubSubException.NotAPubSubNodeException */ private static OmemoBundleElement fetchBundle(XMPPConnection connection, OmemoDevice contactsDevice) throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException, PubSubException.NotAPubSubNodeException { PubSubManager pm = PubSubManager.getInstance(connection, contactsDevice.getJid()); LeafNode node = pm.getLeafNode(contactsDevice.getBundleNodeName()); if (node == null) { return null; } List<PayloadItem<OmemoBundleElement>> bundleItems = node.getItems(); if (bundleItems.isEmpty()) { return null; } return bundleItems.get(bundleItems.size() - 1).getPayload(); }
/** * Publish the given device list to the server. * @param connection authenticated XMPP connection. * @param deviceList users deviceList. * @throws InterruptedException * @throws XMPPException.XMPPErrorException * @throws SmackException.NotConnectedException * @throws SmackException.NoResponseException * @throws PubSubException.NotALeafNodeException */ static void publishDeviceList(XMPPConnection connection, OmemoDeviceListElement deviceList) throws InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException { PubSubManager.getInstance(connection, connection.getUser().asBareJid()) .tryToPublishAndPossibleAutoCreate(OmemoConstants.PEP_NODE_DEVICE_LIST, new PayloadItem<>(deviceList)); }
/** * Publish the given OMEMO bundle to the server using PubSub. * @param connection our connection. * @param userDevice our device * @param bundle the bundle we want to publish * @throws XMPPException.XMPPErrorException * @throws SmackException.NotConnectedException * @throws InterruptedException * @throws SmackException.NoResponseException */ static void publishBundle(XMPPConnection connection, OmemoDevice userDevice, OmemoBundleElement bundle) throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { PubSubManager pm = PubSubManager.getInstance(connection, connection.getUser().asBareJid()); pm.tryToPublishAndPossibleAutoCreate(userDevice.getBundleNodeName(), new PayloadItem<>(bundle)); }
private void publishMood(MoodElement moodElement) throws SmackException.NotLoggedInException, InterruptedException, PubSubException.NotALeafNodeException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException { if (pubSubManager == null) { pubSubManager = PubSubManager.getInstance(getAuthenticatedConnectionOrThrow(), connection().getUser().asBareJid()); } LeafNode node = pubSubManager.getOrCreateLeafNode(MOOD_NODE); node.publish(new PayloadItem<>(moodElement)); }
public static void cleanUpPubSub(OmemoManager omemoManager) { PubSubManager pm = PubSubManager.getInstance(omemoManager.getConnection(),omemoManager.getOwnJid()); try { omemoManager.requestDeviceListUpdateFor(omemoManager.getOwnJid());
/** * Lazy initializer for PubSub manager. */ private PubSubManager getManager() { return PubSubManager.getInstance( parentProvider.getConnection(), pubSubAddress); }