CollectionNode.java 14.8 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile: $
 * $Revision: $
 * $Date: $
 *
6
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7 8
 *
 * This software is published under the terms of the GNU Public License (GPL),
9 10
 * a copy of which is included in this distribution, or a commercial license
 * agreement with Jive.
Matt Tucker's avatar
Matt Tucker committed
11 12
 */

13
package org.jivesoftware.openfire.pubsub;
Matt Tucker's avatar
Matt Tucker committed
14

15 16 17 18
import org.dom4j.Element;
import org.jivesoftware.util.LocaleUtils;
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;
Matt Tucker's avatar
Matt Tucker committed
19
import org.xmpp.packet.JID;
20
import org.xmpp.packet.Message;
Matt Tucker's avatar
Matt Tucker committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * A type of node that contains nodes and/or other collections but no published
 * items. Collections provide the foundation entity to provide a means of representing
 * hierarchical node structures.
 *
 * @author Matt Tucker
 */
public class CollectionNode extends Node {

    /**
     * Map that contains the child nodes of this node. The key is the child node ID and the
     * value is the child node. A map is used to ensure uniqueness and in particular
     * a ConcurrentHashMap for concurrency reasons.
     */
    private Map<String, Node> nodes = new ConcurrentHashMap<String, Node>();
    /**
     * Policy that defines who may associate leaf nodes with a collection.
     */
    private LeafNodeAssociationPolicy associationPolicy = LeafNodeAssociationPolicy.all;
    /**
     * Users that are allowed to associate leaf nodes with this collection node. This collection
     * is going to be used only when the associationPolicy is <tt>whitelist</tt>.
     */
    private Collection<JID> associationTrusted = new ArrayList<JID>();
    /**
     * Max number of leaf nodes that this collection node might have. A value of -1 means
     * that there is no limit.
     */
    private int maxLeafNodes = -1;

55
    public CollectionNode(PubSubService service, CollectionNode parentNode, String nodeID, JID creator) {
Matt Tucker's avatar
Matt Tucker committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        super(service, parentNode, nodeID, creator);
        // Configure node with default values (get them from the pubsub service)
        DefaultNodeConfiguration defaultConfiguration = service.getDefaultNodeConfiguration(false);
        this.associationPolicy = defaultConfiguration.getAssociationPolicy();
        this.maxLeafNodes = defaultConfiguration.getMaxLeafNodes();
    }


    void configure(FormField field) {
        List<String> values;
        if ("pubsub#leaf_node_association_policy".equals(field.getVariable())) {
            values = field.getValues();
            if (values.size() > 0)  {
                associationPolicy = LeafNodeAssociationPolicy.valueOf(values.get(0));
            }
        }
        else if ("pubsub#leaf_node_association_whitelist".equals(field.getVariable())) {
            // Get the new list of users that may add leaf nodes to this collection node
            associationTrusted = new ArrayList<JID>();
            for (String value : field.getValues()) {
                try {
77
                    addAssociationTrusted(new JID(value));
Matt Tucker's avatar
Matt Tucker committed
78
                }
79 80 81
                catch (Exception e) {
                    // Do nothing
                }
Matt Tucker's avatar
Matt Tucker committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
            }
        }
        else if ("pubsub#leaf_nodes_max".equals(field.getVariable())) {
            values = field.getValues();
            maxLeafNodes = values.size() > 0 ? Integer.parseInt(values.get(0)) : -1;
        }
    }

    void postConfigure(DataForm completedForm) {
        //Do nothing.
    }

    protected void addFormFields(DataForm form, boolean isEditing) {
        super.addFormFields(form, isEditing);

        FormField formField = form.addField();
        formField.setVariable("pubsub#leaf_node_association_policy");
        if (isEditing) {
            formField.setType(FormField.Type.list_single);
            formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.leaf_node_association"));
            formField.addOption(null, LeafNodeAssociationPolicy.all.name());
            formField.addOption(null, LeafNodeAssociationPolicy.owners.name());
            formField.addOption(null, LeafNodeAssociationPolicy.whitelist.name());
        }
        formField.addValue(associationPolicy.name());

        formField = form.addField();
        formField.setVariable("pubsub#leaf_node_association_whitelist");
        if (isEditing) {
            formField.setType(FormField.Type.jid_multi);
            formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.leaf_node_whitelist"));
        }
        for (JID contact : associationTrusted) {
            formField.addValue(contact.toString());
        }

        formField = form.addField();
        formField.setVariable("pubsub#leaf_nodes_max");
        if (isEditing) {
            formField.setType(FormField.Type.text_single);
            formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.leaf_nodes_max"));
        }
        formField.addValue(maxLeafNodes);
    }

127 128 129 130 131 132 133 134
    /**
     * Adds a child node to the list of child nodes. The new child node may just have been
     * created or just restored from the database. This method will not trigger notifications
     * to node subscribers since the node could be a node that has just been loaded from the
     * database.
     *
     * @param child the node to add to the list of child nodes.
     */
Matt Tucker's avatar
Matt Tucker committed
135 136 137 138
    void addChildNode(Node child) {
        nodes.put(child.getNodeID(), child);
    }

139 140 141 142 143 144 145

    /**
     * Removes a child node from the list of child nodes. This method will not trigger
     * notifications to node subscribers.
     *
     * @param child the node to remove from the list of child nodes.
     */
Matt Tucker's avatar
Matt Tucker committed
146 147 148 149
    void removeChildNode(Node child) {
        nodes.remove(child.getNodeID());
    }

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    /**
     * Notification that a new node was created and added to this node. Trigger notifications
     * to node subscribers whose subscription type is {@link NodeSubscription.Type#nodes} and
     * have the proper depth.
     *
     * @param child the newly created node that was added to this node.
     */
    void childNodeAdded(Node child) {
        // Build packet to broadcast to subscribers
        Message message = new Message();
        Element event = message.addChildElement("event", "http://jabber.org/protocol/pubsub#event");
        Element item = event.addElement("items").addElement("item");
        item.addAttribute("id", child.getNodeID());
        if (deliverPayloads) {
            item.add(child.getMetadataForm().getElement());
        }
        // Broadcast event notification to subscribers
        broadcastCollectionNodeEvent(child, message);
    }

    /**
     * Notification that a child node was deleted from this node. Trigger notifications
     * to node subscribers whose subscription type is {@link NodeSubscription.Type#nodes} and
     * have the proper depth.
     *
     * @param child the deleted node that was removed from this node.
     */
    void childNodeDeleted(Node child) {
        // Build packet to broadcast to subscribers
        Message message = new Message();
        Element event = message.addChildElement("event", "http://jabber.org/protocol/pubsub#event");
        event.addElement("delete").addAttribute("node", child.getNodeID());
        // Broadcast event notification to subscribers
        broadcastCollectionNodeEvent(child, message);
    }

Matt Tucker's avatar
Matt Tucker committed
186 187 188 189 190 191 192
    protected void deletingNode() {
        // Update child nodes to use the parent node of this node as the new parent node
        for (Node node : getNodes()) {
            node.changeParent(parent);
        }
    }

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    private void broadcastCollectionNodeEvent(Node child, Message notification) {
        // Get affected subscriptions (of this node and all parent nodes)
        Collection<NodeSubscription> subscriptions = new ArrayList<NodeSubscription>();
        subscriptions.addAll(getSubscriptions(child));
        for (CollectionNode parentNode : getParents()) {
            subscriptions.addAll(parentNode.getSubscriptions(child));
        }
        // TODO Possibly use a thread pool for sending packets (based on the jids size)
        for (NodeSubscription subscription : subscriptions) {
            service.sendNotification(subscription.getNode(), notification, subscription.getJID());
        }
    }

    /**
     * Returns a collection with the subscriptions to this node that should be notified
     * that a new child was added or deleted.
     *
     * @param child the added or deleted child.
     * @return a collection with the subscriptions to this node that should be notified
     *         that a new child was added or deleted.
     */
    private Collection<NodeSubscription> getSubscriptions(Node child) {
        Collection<NodeSubscription> subscriptions = new ArrayList<NodeSubscription>();
        for (NodeSubscription subscription : getSubscriptions()) {
            if (subscription.canSendChildNodeEvent(child)) {
                subscriptions.add(subscription);
            }
        }
        return subscriptions;
    }

Matt Tucker's avatar
Matt Tucker committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    public boolean isCollectionNode() {
        return true;
    }

    /**
     * Returns true if the specified node is a first-level children of this collection
     * node.
     *
     * @param child the node to check if it is a direct child of this node.
     * @return true if the specified node is a first-level children of this collection
     *         node.
     */
    public boolean isChildNode(Node child) {
        return nodes.containsKey(child.getNodeID());
    }

    /**
     * Returns true if the specified node is a direct child node of this collection node or
     * a descendant of the children nodes.
     *
     * @param child the node to check if it is a descendant of this node.
     * @return true if the specified node is a direct child node of this collection node or
     *         a descendant of the children nodes.
     */
    public boolean isDescendantNode(Node child) {
        if (isChildNode(child)) {
            return true;
        }
        for (Node node : getNodes()) {
            if (node.isDescendantNode(child)) {
                return true;
            }
        }
        return false;
    }

    public Collection<Node> getNodes() {
        return nodes.values();
    }

    /**
     * Returns the policy that defines who may associate leaf nodes with a collection.
     *
     * @return the policy that defines who may associate leaf nodes with a collection.
     */
    public LeafNodeAssociationPolicy getAssociationPolicy() {
        return associationPolicy;
    }

    /**
     * Returns the users that are allowed to associate leaf nodes with this collection node.
     * This collection is going to be used only when the associationPolicy is <tt>whitelist</tt>.
     *
     * @return the users that are allowed to associate leaf nodes with this collection node.
     */
    public Collection<JID> getAssociationTrusted() {
280 281 282 283 284 285 286 287 288 289 290 291
        return Collections.unmodifiableCollection(associationTrusted);
    }

    /**
     * Adds a new trusted user that is allowed to associate leaf nodes with this collection node.
     * The new user is not going to be added to the database. Instead it is just kept in memory.
     *
     * @param user the new trusted user that is allowed to associate leaf nodes with this
     *        collection node.
     */
    void addAssociationTrusted(JID user) {
        associationTrusted.add(user);
Matt Tucker's avatar
Matt Tucker committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    }

    /**
     * Returns the max number of leaf nodes that this collection node might have. A value of
     * -1 means that there is no limit.
     *
     * @return the max number of leaf nodes that this collection node might have.
     */
    public int getMaxLeafNodes() {
        return maxLeafNodes;
    }

    /**
     * Sets the policy that defines who may associate leaf nodes with a collection.
     *
     * @param associationPolicy the policy that defines who may associate leaf nodes
     *        with a collection.
     */
    void setAssociationPolicy(LeafNodeAssociationPolicy associationPolicy) {
        this.associationPolicy = associationPolicy;
    }

    /**
     * Sets the users that are allowed to associate leaf nodes with this collection node.
     * This collection is going to be used only when the associationPolicy is <tt>whitelist</tt>.
     *
     * @param associationTrusted the users that are allowed to associate leaf nodes with this
     *        collection node.
     */
    void setAssociationTrusted(Collection<JID> associationTrusted) {
        this.associationTrusted = associationTrusted;
    }

    /**
     * Sets the max number of leaf nodes that this collection node might have. A value of
     * -1 means that there is no limit.
     *
     * @param maxLeafNodes the max number of leaf nodes that this collection node might have.
     */
    void setMaxLeafNodes(int maxLeafNodes) {
        this.maxLeafNodes = maxLeafNodes;
    }

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    /**
     * Returns true if the specified user is allowed to associate a leaf node with this
     * node. The decision is taken based on the association policy that the node is
     * using.
     *
     * @param user the user trying to associate a leaf node with this node.
     * @return true if the specified user is allowed to associate a leaf node with this
     *         node.
     */
    public boolean isAssociationAllowed(JID user) {
        if (associationPolicy == LeafNodeAssociationPolicy.all) {
            // Anyone is allowed to associate leaf nodes with this node
            return true;
        }
        else if (associationPolicy == LeafNodeAssociationPolicy.owners) {
            // Only owners or sysadmins are allowed to associate leaf nodes with this node
            return isAdmin(user);
        }
        else {
            // Owners, sysadmins and a whitelist of usres are allowed to
            // associate leaf nodes with this node
            return isAdmin(user) || associationTrusted.contains(user);
        }
    }

    /**
     * Returns true if the max number of leaf nodes associated with this node has
     * reached to the maximum allowed.
     *
     * @return true if the max number of leaf nodes associated with this node has
     *         reached to the maximum allowed.
     */
    public boolean isMaxLeafNodeReached() {
        if (maxLeafNodes < 0) {
            // There is no maximum limit
            return false;
        }
        // Count number of child leaf nodes
        int counter = 0;
        for (Node node : getNodes()) {
            if (!node.isCollectionNode()) {
                counter = counter + 1;
            }
        }
        // Compare count with maximum allowed
        return counter >= maxLeafNodes;
    }

Matt Tucker's avatar
Matt Tucker committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    /**
     * Policy that defines who may associate leaf nodes with a collection.
     */
    public static enum LeafNodeAssociationPolicy {

        /**
         * Anyone may associate leaf nodes with the collection.
         */
        all,
        /**
         * Only collection node owners may associate leaf nodes with the collection.
         */
        owners,
        /**
         * Only those on a whitelist may associate leaf nodes with the collection.
         */
399
        whitelist
Matt Tucker's avatar
Matt Tucker committed
400 401
    }
}