DefaultNodeConfiguration.java 20.2 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/**
 * $RCSfile: $
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2006 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.wildfire.pubsub;

import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.wildfire.pubsub.models.AccessModel;
import org.jivesoftware.wildfire.pubsub.models.PublisherModel;
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;

/**
 * A DefaultNodeConfiguration keeps the default configuration values for leaf or collection
 * nodes of a particular publish-subscribe service. New nodes created for the service 
 * will be initialized with the values defined in the default configuration.
 *
 * @author Matt Tucker
 */
public class DefaultNodeConfiguration {

    /**
     * Flag indicating whether this default configutation belongs to a leaf node or not.
     */
    private boolean leaf;
    /**
     * Flag that indicates whether to deliver payloads with event notifications.
     */
    private boolean deliverPayloads;
    /**
     * The maximum payload size in bytes.
     */
    private int maxPayloadSize;
    /**
     * Flag that indicates whether to persist items to storage. Note that when the
     * variable is false then the last published item is the only items being saved
     * to the backend storage.
     */
    private boolean persistPublishedItems;
    /**
     * Maximum number of published items to persist. Note that all nodes are going to persist
     * their published items. The only difference is the number of the last published items
     * to be persisted. Even nodes that are configured to not use persitent items are going
     * to save the last published item.
     */
    private int maxPublishedItems;
    /**
     * Flag that indicates whether to notify subscribers when the node configuration changes.
     */
    private boolean notifyConfigChanges;
    /**
     * Flag that indicates whether to notify subscribers when the node is deleted.
     */
    private boolean notifyDelete;
    /**
     * Flag that indicates whether to notify subscribers when items are removed from the node.
     */
    private boolean notifyRetract;
    /**
     * Flag that indicates whether to deliver notifications to available users only.
     */
    private boolean presenceBasedDelivery;
    /**
     * Flag that indicates whether to send items to new subscribers.
     */
Matt Tucker's avatar
Matt Tucker committed
73
    private boolean sendItemSubscribe = false;
Matt Tucker's avatar
Matt Tucker committed
74 75 76 77 78 79 80 81 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 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 186 187 188 189 190 191 192 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 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 280 281 282 283 284 285 286 287 288 289 290 291 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 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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    /**
     * Publisher model that specifies who is allowed to publish items to the node.
     */
    private PublisherModel publisherModel = PublisherModel.open;
    /**
     * Flag that indicates that subscribing and unsubscribing are enabled.
     */
    private boolean subscriptionEnabled;
    /**
     * Access model that specifies who is allowed to subscribe and retrieve items.
     */
    private AccessModel accessModel = AccessModel.open;
    /**
     * The default language of the node.
     */
    private String language = "";
    /**
     * Policy that defines whether owners or publisher should receive replies to items.
     */
    private Node.ItemReplyPolicy replyPolicy = Node.ItemReplyPolicy.owner;
    /**
     * Policy that defines who may associate leaf nodes with a collection.
     */
    private CollectionNode.LeafNodeAssociationPolicy associationPolicy =
            CollectionNode.LeafNodeAssociationPolicy.all;
    /**
     * 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;

    public DefaultNodeConfiguration(boolean isLeafType) {
        this.leaf = isLeafType;
    }

    /**
     * Returns true if this default configutation belongs to a leaf node.
     *
     * @return true if this default configutation belongs to a leaf node.
     */
    public boolean isLeaf() {
        return leaf;
    }

    /**
     * Returns true if payloads are going to be delivered with event notifications.
     *
     * @return true if payloads are going to be delivered with event notifications.
     */
    public boolean isDeliverPayloads() {
        return deliverPayloads;
    }

    /**
     * Returns the maximum payload size in bytes.
     *
     * @return the maximum payload size in bytes.
     */
    public int getMaxPayloadSize() {
        return maxPayloadSize;
    }

    /**
     * Returns true if items are going to be persisted in a storage. Note that when the
     * variable is false then the last published item is the only items being saved
     * to the backend storage.
     *
     * @return true if items are going to be persisted in a storage.
     */
    public boolean isPersistPublishedItems() {
        return persistPublishedItems;
    }

    /**
     * Returns the maximum number of published items to persist. Note that all nodes are going
     * to persist their published items. The only difference is the number of the last published
     * items to be persisted. Even nodes that are configured to not use persitent items are going
     * to save the last published item.
     *
     * @return the maximum number of published items to persist.
     */
    public int getMaxPublishedItems() {
        return maxPublishedItems;
    }

    /**
     * Returns true if subscribers are going to be notified when node configuration changes.
     *
     * @return true if subscribers are going to be notified when node configuration changes.
     */
    public boolean isNotifyConfigChanges() {
        return notifyConfigChanges;
    }

    /**
     * Returns true if subscribers are going to be notified when node is deleted.
     *
     * @return true if subscribers are going to be notified when node is deleted.
     */
    public boolean isNotifyDelete() {
        return notifyDelete;
    }

    /**
     * Returns true if subscribers are going to be notified when items are removed from the node.
     *
     * @return true if subscribers are going to be notified when items are removed from the node.
     */
    public boolean isNotifyRetract() {
        return notifyRetract;
    }

    /**
     * Returns true if notifications are going to be delivered only to available users.
     *
     * @return true if notifications are going to be delivered only to available users.
     */
    public boolean isPresenceBasedDelivery() {
        return presenceBasedDelivery;
    }

    /**
     * Returns true if new subscribers are going to receive new items once subscribed.
     *
     * @return true if new subscribers are going to receive new items once subscribed.
     */
    public boolean isSendItemSubscribe() {
        return sendItemSubscribe;
    }

    /**
     * Returnes the publisher model that specifies who is allowed to publish items to the node.
     *
     * @return the publisher model that specifies who is allowed to publish items to the node.
     */
    public PublisherModel getPublisherModel() {
        return publisherModel;
    }

    /**
     * Returns true if subscribing and unsubscribing are enabled.
     *
     * @return true if subscribing and unsubscribing are enabled.
     */
    public boolean isSubscriptionEnabled() {
        return subscriptionEnabled;
    }

    /**
     * Returns the access model that specifies who is allowed to subscribe and retrieve items.
     *
     * @return the access model that specifies who is allowed to subscribe and retrieve items.
     */
    public AccessModel getAccessModel() {
        return accessModel;
    }

    /**
     * Returns the default language of the node.
     *
     * @return the default language of the node.
     */
    public String getLanguage() {
        return language;
    }

    /**
     * Returns the policy that defines whether owners or publisher should receive
     * replies to items.
     *
     * @return the policy that defines whether owners or publisher should receive 
     *        replies to items.
     */
    public Node.ItemReplyPolicy getReplyPolicy() {
        return replyPolicy;
    }

    /**
     * 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 CollectionNode.LeafNodeAssociationPolicy getAssociationPolicy() {
        return associationPolicy;
    }

    /**
     * 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 if payloads are going to be delivered with event notifications.
     *
     * @param deliverPayloads true if payloads are going to be delivered with event notifications.
     */
    public void setDeliverPayloads(boolean deliverPayloads) {
        this.deliverPayloads = deliverPayloads;
    }

    /**
     * Sets the maximum payload size in bytes.
     *
     * @param maxPayloadSize the maximum payload size in bytes.
     */
    public void setMaxPayloadSize(int maxPayloadSize) {
        this.maxPayloadSize = maxPayloadSize;
    }

    /**
     * Sets if items are going to be persisted in a storage. Note that when the
     * variable is false then the last published item is the only items being saved
     * to the backend storage.
     *
     * @param persistPublishedItems true if items are going to be persisted in a storage.
     */
    public void setPersistPublishedItems(boolean persistPublishedItems) {
        this.persistPublishedItems = persistPublishedItems;
    }

    /**
     * Sets the maximum number of published items to persist. Note that all nodes are going
     * to persist their published items. The only difference is the number of the last published
     * items to be persisted. Even nodes that are configured to not use persitent items are going
     * to save the last published item.
     *
     * @param maxPublishedItems the maximum number of published items to persist.
     */
    public void setMaxPublishedItems(int maxPublishedItems) {
        this.maxPublishedItems = maxPublishedItems;
    }

    /**
     * Sets if subscribers are going to be notified when node configuration changes.
     *
     * @param notifyConfigChanges true if subscribers are going to be notified when node
     *        configuration changes.
     */
    public void setNotifyConfigChanges(boolean notifyConfigChanges) {
        this.notifyConfigChanges = notifyConfigChanges;
    }

    /**
     * Sets if subscribers are going to be notified when node is deleted.
     *
     * @param notifyDelete true if subscribers are going to be notified when node is deleted.
     */
    public void setNotifyDelete(boolean notifyDelete) {
        this.notifyDelete = notifyDelete;
    }

    /**
     * Sets if subscribers are going to be notified when items are removed from the node.
     *
     * @param notifyRetract true if subscribers are going to be notified when items are removed
     *        from the node.
     */
    public void setNotifyRetract(boolean notifyRetract) {
        this.notifyRetract = notifyRetract;
    }

    /**
     * Sets if notifications are going to be delivered only to available users.
     *
     * @param presenceBasedDelivery true if notifications are going to be delivered only to
     *        available users.
     */
    public void setPresenceBasedDelivery(boolean presenceBasedDelivery) {
        this.presenceBasedDelivery = presenceBasedDelivery;
    }

    /**
     * Sets if new subscribers are going to receive new items once subscribed.
     *
     * @param sendItemSubscribe true if new subscribers are going to receive new items
     *        once subscribed.
     */
    public void setSendItemSubscribe(boolean sendItemSubscribe) {
        this.sendItemSubscribe = sendItemSubscribe;
    }

    /**
     * Sets the publisher model that specifies who is allowed to publish items to the node.
     *
     * @param publisherModel the publisher model that specifies who is allowed to publish
     *        items to the node.
     */
    public void setPublisherModel(PublisherModel publisherModel) {
        this.publisherModel = publisherModel;
    }

    /**
     * Sets if subscribing and unsubscribing are enabled.
     *
     * @param subscriptionEnabled true if subscribing and unsubscribing are enabled.
     */
    public void setSubscriptionEnabled(boolean subscriptionEnabled) {
        this.subscriptionEnabled = subscriptionEnabled;
    }

    /**
     * Sets the access model that specifies who is allowed to subscribe and retrieve items.
     *
     * @param accessModel the access model that specifies who is allowed to subscribe and
     *        retrieve items.
     */
    public void setAccessModel(AccessModel accessModel) {
        this.accessModel = accessModel;
    }

    /**
     * Sets the default language of the node.
     *
     * @param language the default language of the node.
     */
    public void setLanguage(String language) {
        this.language = language;
    }

    /**
     * Sets the policy that defines whether owners or publisher should receive replies to items.
     *
     * @param replyPolicy the policy that defines whether owners or publisher should receive
     *        replies to items.
     */
    public void setReplyPolicy(Node.ItemReplyPolicy replyPolicy) {
        this.replyPolicy = replyPolicy;
    }

    /**
     * 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.
     */
    public void setAssociationPolicy(CollectionNode.LeafNodeAssociationPolicy associationPolicy) {
        this.associationPolicy = associationPolicy;
    }

    /**
     * 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.
     */
    public void setMaxLeafNodes(int maxLeafNodes) {
        this.maxLeafNodes = maxLeafNodes;
    }


    public DataForm getConfigurationForm() {
        DataForm form = new DataForm(DataForm.Type.form);
        form.setTitle(LocaleUtils.getLocalizedString("pubsub.form.default.title"));
        form.addInstruction(LocaleUtils.getLocalizedString("pubsub.form.default.instruction"));
        // Add the form fields and configure them for edition

        FormField formField = form.addField();
        formField.setVariable("FORM_TYPE");
        formField.setType(FormField.Type.hidden);
        formField.addValue("http://jabber.org/protocol/pubsub#node_config");

        formField = form.addField();
        formField.setVariable("pubsub#subscribe");
        formField.setType(FormField.Type.boolean_type);
        formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.subscribe"));
        formField.addValue(subscriptionEnabled);

        formField = form.addField();
        formField.setVariable("pubsub#deliver_payloads");
        formField.setType(FormField.Type.boolean_type);
        formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.deliver_payloads"));
        formField.addValue(deliverPayloads);

        formField = form.addField();
        formField.setVariable("pubsub#notify_config");
        formField.setType(FormField.Type.boolean_type);
        formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.notify_config"));
        formField.addValue(notifyConfigChanges);

        formField = form.addField();
        formField.setVariable("pubsub#notify_delete");
        formField.setType(FormField.Type.boolean_type);
        formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.notify_delete"));
        formField.addValue(notifyDelete);

        formField = form.addField();
        formField.setVariable("pubsub#notify_retract");
        formField.setType(FormField.Type.boolean_type);
        formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.notify_retract"));
        formField.addValue(notifyRetract);

        formField = form.addField();
        formField.setVariable("pubsub#presence_based_delivery");
        formField.setType(FormField.Type.boolean_type);
        formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.presence_based"));
        formField.addValue(presenceBasedDelivery);

        if (leaf) {
Matt Tucker's avatar
Matt Tucker committed
476 477 478 479 480 481 482
            formField = form.addField();
            formField.setVariable("pubsub#send_item_subscribe");
            formField.setType(FormField.Type.boolean_type);
            formField.setLabel(
                    LocaleUtils.getLocalizedString("pubsub.form.conf.send_item_subscribe"));
            formField.addValue(sendItemSubscribe);

Matt Tucker's avatar
Matt Tucker committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
            formField = form.addField();
            formField.setVariable("pubsub#persist_items");
            formField.setType(FormField.Type.boolean_type);
            formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.persist_items"));
            formField.addValue(persistPublishedItems);

            formField = form.addField();
            formField.setVariable("pubsub#max_items");
            formField.setType(FormField.Type.text_single);
            formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.max_items"));
            formField.addValue(maxPublishedItems);

            formField = form.addField();
            formField.setVariable("pubsub#max_payload_size");
            formField.setType(FormField.Type.text_single);
            formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.max_payload_size"));
            formField.addValue(maxPayloadSize);
        }

        formField = form.addField();
        formField.setVariable("pubsub#access_model");
        formField.setType(FormField.Type.list_single);
        formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.access_model"));
        formField.addOption(null, AccessModel.authorize.getName());
        formField.addOption(null, AccessModel.open.getName());
        formField.addOption(null, AccessModel.presence.getName());
        formField.addOption(null, AccessModel.roster.getName());
        formField.addOption(null, AccessModel.whitelist.getName());
        formField.addValue(accessModel.getName());

        formField = form.addField();
        formField.setVariable("pubsub#publish_model");
        formField.setType(FormField.Type.list_single);
        formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.publish_model"));
        formField.addOption(null, PublisherModel.publishers.getName());
        formField.addOption(null, PublisherModel.subscribers.getName());
        formField.addOption(null, PublisherModel.open.getName());
        formField.addValue(publisherModel.getName());

        formField = form.addField();
        formField.setVariable("pubsub#language");
        formField.setType(FormField.Type.text_single);
        formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.language"));
        formField.addValue(language);

        formField = form.addField();
        formField.setVariable("pubsub#itemreply");
        formField.setType(FormField.Type.list_single);
        formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.itemreply"));
        if (replyPolicy != null) {
            formField.addValue(replyPolicy.name());
        }

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

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

        return form;
    }
}