Java Code Examples for org.jivesoftware.smackx.pubsub.PubSubManager#getOrCreateLeafNode()

The following examples show how to use org.jivesoftware.smackx.pubsub.PubSubManager#getOrCreateLeafNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenPgpPubSubUtil.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * Publish the users OpenPGP public key to the public key node if necessary.
 * Also announce the key to other users by updating the metadata node.
 *
 * @see <a href="https://xmpp.org/extensions/xep-0373.html#annoucning-pubkey">XEP-0373 §4.1</a>
 *
 * @param pepManager The PEP manager.
 * @param pubkeyElement {@link PubkeyElement} containing the public key
 * @param fingerprint fingerprint of the public key
 *
 * @throws InterruptedException if the thread gets interrupted.
 * @throws PubSubException.NotALeafNodeException if either the metadata node or the public key node is not a
 *                                               {@link LeafNode}.
 * @throws XMPPException.XMPPErrorException in case of an XMPP protocol error.
 * @throws SmackException.NotConnectedException if we are not connected.
 * @throws SmackException.NoResponseException if the server doesn't respond.
 */
public static void publishPublicKey(PepManager pepManager, PubkeyElement pubkeyElement, OpenPgpV4Fingerprint fingerprint)
        throws InterruptedException, PubSubException.NotALeafNodeException,
        XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException {

    String keyNodeName = PEP_NODE_PUBLIC_KEY(fingerprint);
    PubSubManager pm = pepManager.getPepPubSubManager();

    // Check if key available at data node
    // If not, publish key to data node
    LeafNode keyNode = pm.getOrCreateLeafNode(keyNodeName);
    changeAccessModelIfNecessary(keyNode, AccessModel.open);
    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.");
    }

    // Fetch IDs from metadata node
    LeafNode metadataNode = pm.getOrCreateLeafNode(PEP_NODE_PUBLIC_KEYS);
    changeAccessModelIfNecessary(metadataNode, AccessModel.open);
    List<PayloadItem<PublicKeysListElement>> metadataItems = metadataNode.getItems(1);

    PublicKeysListElement.Builder builder = PublicKeysListElement.builder();
    if (!metadataItems.isEmpty() && metadataItems.get(0).getPayload() != null) {
        // Add old entries back to list.
        PublicKeysListElement publishedList = metadataItems.get(0).getPayload();
        for (PublicKeysListElement.PubkeyMetadataElement meta : publishedList.getMetadata().values()) {
            builder.addMetadata(meta);
        }
    }
    builder.addMetadata(new PublicKeysListElement.PubkeyMetadataElement(fingerprint, new Date()));

    // Publish IDs to metadata node
    metadataNode.publish(new PayloadItem<>(builder.build()));
}
 
Example 2
Source File: OpenPgpPubSubUtil.java    From Smack with Apache License 2.0 3 votes vote down vote up
/**
 * Publishes a {@link SecretkeyElement} to the secret key node.
 * The node will be configured to use the whitelist access model to prevent access from subscribers.
 *
 * @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 connection {@link XMPPConnection} of the user
 * @param element a {@link SecretkeyElement} containing the encrypted secret key of the user
 *
 * @throws InterruptedException if the thread gets interrupted.
 * @throws PubSubException.NotALeafNodeException if something is wrong with the PubSub node
 * @throws XMPPException.XMPPErrorException in case of an protocol related error
 * @throws SmackException.NotConnectedException if we are not connected
 * @throws SmackException.NoResponseException /watch?v=0peBq89ZTrc
 * @throws SmackException.FeatureNotSupportedException if the Server doesn't support the whitelist access model
 */
public static void depositSecretKey(XMPPConnection connection, SecretkeyElement element)
        throws InterruptedException, PubSubException.NotALeafNodeException,
        XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException,
        SmackException.FeatureNotSupportedException {
    if (!OpenPgpManager.serverSupportsSecretKeyBackups(connection)) {
        throw new SmackException.FeatureNotSupportedException("http://jabber.org/protocol/pubsub#access-whitelist");
    }
    PubSubManager pm = PepManager.getInstanceFor(connection).getPepPubSubManager();
    LeafNode secretKeyNode = pm.getOrCreateLeafNode(PEP_NODE_SECRET_KEY);
    OpenPgpPubSubUtil.changeAccessModelIfNecessary(secretKeyNode, AccessModel.whitelist);

    secretKeyNode.publish(new PayloadItem<>(element));
}
 
Example 3
Source File: OpenPgpPubSubUtil.java    From Smack with Apache License 2.0 3 votes vote down vote up
/**
 * 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;
}