AccessModel.java 4.34 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 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
Matt Tucker's avatar
Matt Tucker committed
19 20
 */

21
package org.jivesoftware.openfire.pubsub.models;
Matt Tucker's avatar
Matt Tucker committed
22 23

import org.dom4j.Element;
24
import org.jivesoftware.openfire.pubsub.Node;
Matt Tucker's avatar
Matt Tucker committed
25 26 27 28 29 30 31 32
import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError;

/**
 * Policy that defines who is allowed to subscribe and retrieve items.
 *
 * @author Matt Tucker
 */
33
public abstract class AccessModel {
Matt Tucker's avatar
Matt Tucker committed
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 73 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

    public final static AccessModel whitelist = new WhitelistAccess();
    public final static AccessModel open = new OpenAccess();
    public final static AccessModel authorize = new AuthorizeAccess();
    public final static AccessModel presence = new PresenceAccess();
    public final static AccessModel roster = new RosterAccess();

    /**
     * Returns the specific subclass of AccessModel as specified by the access
     * model name. If an unknown name is specified then an IllegalArgumentException
     * is going to be thrown.
     *
     * @param name the name of the subsclass.
     * @return the specific subclass of AccessModel as specified by the access
     *         model name.
     */
    public static AccessModel valueOf(String name) {
        if ("open".equals(name)) {
            return open;
        }
        else if ("whitelist".equals(name)) {
            return whitelist;
        }
        else if ("authorize".equals(name)) {
            return authorize;
        }
        else if ("presence".equals(name)) {
            return presence;
        }
        else if ("roster".equals(name)) {
            return roster;
        }
        throw new IllegalArgumentException("Unknown access model: " + name);
    }

    /**
     * Returns the name as defined by the JEP-60 spec.
     *
     * @return the name as defined by the JEP-60 spec.
     */
    public abstract String getName();

    /**
     * Returns true if the entity is allowed to subscribe to the specified node.
     *
     * @param node       the node that the subscriber is trying to subscribe to.
     * @param owner      the JID of the owner of the subscription.
     * @param subscriber the JID of the subscriber.
     * @return true if the subscriber is allowed to subscribe to the specified node.
     */
    public abstract boolean canSubscribe(Node node, JID owner, JID subscriber);

    /**
     * Returns true if the entity is allowed to get the node published items.
     *
     * @param node       the node that the entity is trying to get the node's items.
     * @param owner      the JID of the owner of the subscription.
     * @param subscriber the JID of the subscriber.
     * @return true if the subscriber is allowed to get the node's published items.
     */
    public abstract boolean canAccessItems(Node node, JID owner, JID subscriber);

    /**
     * Returns the error condition that should be returned to the subscriber when
     * subscription is not allowed.
     *
     * @return the error condition that should be returned to the subscriber when
     *         subscription is not allowed.
     */
    public abstract PacketError.Condition getSubsriptionError();

    /**
     * Returns the error element that should be returned to the subscriber as
     * error detail when subscription is not allowed. The returned element is created
     * each time this message is sent so it is safe to include the returned element in
     * the parent element.
     *
     * @return the error element that should be returned to the subscriber as
     *         error detail when subscription is not allowed.
     */
    public abstract Element getSubsriptionErrorDetail();

    /**
     * Returns true if the new subscription should be authorized by a node owner.
     *
     * @return true if the new subscription should be authorized by a node owner.
     */
    public abstract boolean isAuthorizationRequired();
}