⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pubsubengine.java

📁 基于Jabber协议的即时消息服务器
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        NodeAffiliate nodeAffiliate = node.getAffiliate(owner);        if (nodeAffiliate != null &&                nodeAffiliate.getAffiliation() == NodeAffiliate.Affiliation.outcast) {            // Subscriber is an outcast. Return forbidden error            sendErrorPacket(iq, PacketError.Condition.forbidden, null);            return;        }        // Check that subscriptions to the node are enabled        if (!node.isSubscriptionEnabled() && !service.isServiceAdmin(from)) {            // Sender is not a sysadmin and subscription is disabled so return an error            sendErrorPacket(iq, PacketError.Condition.not_allowed, null);            return;        }        // Get any configuration form included in the options element (if any)        DataForm optionsForm = null;        Element options = childElement.element("options");        if (options != null) {            Element formElement = options.element(QName.get("x", "jabber:x:data"));            if (formElement != null) {                optionsForm = new DataForm(formElement);            }        }        // If leaf node does not support multiple subscriptions then check whether subscriber is        // creating another subscription or not        if (!node.isCollectionNode() && !node.isMultipleSubscriptionsEnabled()) {            NodeSubscription existingSubscription = node.getSubscription(subscriberJID);            if (existingSubscription != null) {                // User is trying to create another subscription so                // return current subscription state                existingSubscription.sendSubscriptionState(iq);                return;            }        }        // Check if subscribing twice to a collection node using same subscription type        if (node.isCollectionNode()) {            // By default assume that new subscription is of type node            boolean isNodeType = true;            if (optionsForm != null) {                FormField field = optionsForm.getField("pubsub#subscription_type");                if (field != null) {                    if ("items".equals(field.getValues().get(0))) {                        isNodeType = false;                    }                }            }            if (nodeAffiliate != null) {                for (NodeSubscription subscription : nodeAffiliate.getSubscriptions()) {                    if (isNodeType) {                        // User is requesting a subscription of type "nodes"                        if (NodeSubscription.Type.nodes == subscription.getType()) {                            // Cannot have 2 subscriptions of the same type. Return conflict error                            sendErrorPacket(iq, PacketError.Condition.conflict, null);                            return;                        }                    }                    else if (!node.isMultipleSubscriptionsEnabled()) {                        // User is requesting a subscription of type "items" and                        // multiple subscriptions is not allowed                        if (NodeSubscription.Type.items == subscription.getType()) {                            // User is trying to create another subscription so                            // return current subscription state                            subscription.sendSubscriptionState(iq);                            return;                        }                    }                }            }        }        // Create a subscription and an affiliation if the subscriber doesn't have one        node.createSubscription(iq, owner, subscriberJID, accessModel.isAuthorizationRequired(),                optionsForm);    }    private void unsubscribeNode(IQ iq, Element unsubscribeElement) {        String nodeID = unsubscribeElement.attributeValue("node");        String subID = unsubscribeElement.attributeValue("subid");        Node node;        if (nodeID == null) {            if (service.isCollectionNodesSupported()) {                // Entity unsubscribes from root collection node                node = service.getRootCollectionNode();            }            else {                // Service does not have a root collection node so return a nodeid-required error                Element pubsubError = DocumentHelper.createElement(QName.get(                        "nodeid-required", "http://jabber.org/protocol/pubsub#errors"));                sendErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);                return;            }        }        else {            // Look for the specified node            node = service.getNode(nodeID);            if (node == null) {                // Node does not exist. Return item-not-found error                sendErrorPacket(iq, PacketError.Condition.item_not_found, null);                return;            }        }        NodeSubscription subscription;        if (node.isMultipleSubscriptionsEnabled()) {            if (subID == null) {                // No subid was specified and the node supports multiple subscriptions                Element pubsubError = DocumentHelper.createElement(                        QName.get("subid-required", "http://jabber.org/protocol/pubsub#errors"));                sendErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);                return;            }            else {                // Check if the specified subID belongs to an existing node subscription                subscription = node.getSubscription(subID);                if (subscription == null) {                    Element pubsubError = DocumentHelper.createElement(                            QName.get("invalid-subid", "http://jabber.org/protocol/pubsub#errors"));                    sendErrorPacket(iq, PacketError.Condition.not_acceptable, pubsubError);                    return;                }            }        }        else {            String jidAttribute = unsubscribeElement.attributeValue("jid");            // Check if the specified JID has a subscription with the node            if (jidAttribute == null) {                // No JID was specified so return an error indicating that jid is required                Element pubsubError = DocumentHelper.createElement(                        QName.get("jid-required", "http://jabber.org/protocol/pubsub#errors"));                sendErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);                return;            }            JID subscriberJID = new JID(jidAttribute);            subscription = node.getSubscription(subscriberJID);            if (subscription == null) {                Element pubsubError = DocumentHelper.createElement(                        QName.get("not-subscribed", "http://jabber.org/protocol/pubsub#errors"));                sendErrorPacket(iq, PacketError.Condition.unexpected_request, pubsubError);                return;            }        }        JID from = iq.getFrom();        // Check that unsubscriptions to the node are enabled        if (!node.isSubscriptionEnabled() && !service.isServiceAdmin(from)) {            // Sender is not a sysadmin and unsubscription is disabled so return an error            sendErrorPacket(iq, PacketError.Condition.not_allowed, null);            return;        }        // TODO Assumed that the owner of the subscription is the bare JID of the subscription JID. Waiting StPeter answer for explicit field.        JID owner = new JID(from.toBareJID());        // A subscription was found so check if the user is allowed to cancel the subscription        if (!subscription.canModify(from) && !subscription.canModify(owner)) {            // Requestor is prohibited from unsubscribing entity            sendErrorPacket(iq, PacketError.Condition.forbidden, null);            return;        }        // Cancel subscription        node.cancelSubscription(subscription);        // Send reply with success        router.route(IQ.createResultIQ(iq));    }    private void getSubscriptionConfiguration(IQ iq, Element childElement, Element optionsElement) {        String nodeID = optionsElement.attributeValue("node");        String subID = optionsElement.attributeValue("subid");        Node node;        if (nodeID == null) {            if (service.isCollectionNodesSupported()) {                // Entity requests subscription options of root collection node                node = service.getRootCollectionNode();            }            else {                // Service does not have a root collection node so return a nodeid-required error                Element pubsubError = DocumentHelper.createElement(QName.get(                        "nodeid-required", "http://jabber.org/protocol/pubsub#errors"));                sendErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);                return;            }        }        else {            // Look for the specified node            node = service.getNode(nodeID);            if (node == null) {                // Node does not exist. Return item-not-found error                sendErrorPacket(iq, PacketError.Condition.item_not_found, null);                return;            }        }        NodeSubscription subscription;        if (node.isMultipleSubscriptionsEnabled()) {            if (subID == null) {                // No subid was specified and the node supports multiple subscriptions                Element pubsubError = DocumentHelper.createElement(                        QName.get("subid-required", "http://jabber.org/protocol/pubsub#errors"));                sendErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);                return;            }            else {                // Check if the specified subID belongs to an existing node subscription                subscription = node.getSubscription(subID);                if (subscription == null) {                    Element pubsubError = DocumentHelper.createElement(                            QName.get("invalid-subid", "http://jabber.org/protocol/pubsub#errors"));                    sendErrorPacket(iq, PacketError.Condition.not_acceptable, pubsubError);                    return;                }            }        }        else {            // Check if the specified JID has a subscription with the node            String jidAttribute = optionsElement.attributeValue("jid");            if (jidAttribute == null) {                // No JID was specified so return an error indicating that jid is required                Element pubsubError = DocumentHelper.createElement(                        QName.get("jid-required", "http://jabber.org/protocol/pubsub#errors"));                sendErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);                return;            }            JID subscriberJID = new JID(jidAttribute);            subscription = node.getSubscription(subscriberJID);            if (subscription == null) {                Element pubsubError = DocumentHelper.createElement(                        QName.get("not-subscribed", "http://jabber.org/protocol/pubsub#errors"));                sendErrorPacket(iq, PacketError.Condition.unexpected_request, pubsubError);                return;            }        }        // A subscription was found so check if the user is allowed to get the subscription options        if (!subscription.canModify(iq.getFrom())) {            // Requestor is prohibited from getting the subscription options            sendErrorPacket(iq, PacketError.Condition.forbidden, null);            return;        }        // Return data form containing subscription configuration to the subscriber        IQ reply = IQ.createResultIQ(iq);        Element replyChildElement = childElement.createCopy();        reply.setChildElement(replyChildElement);        replyChildElement.element("options").add(subscription.getConfigurationForm().getElement());        router.route(reply);    }    private void configureSubscription(IQ iq, Element optionsElement) {        String nodeID = optionsElement.attributeValue("node");        String subID = optionsElement.attributeValue("subid");        Node node;        if (nodeID == null) {            if (service.isCollectionNodesSupported()) {                // Entity submits new subscription options of root collection node                node = service.getRootCollectionNode();            }            else {                // Service does not have a root collection node so return a nodeid-required error                Element pubsubError = DocumentHelper.createElement(QName.get(                        "nodeid-required", "http://jabber.org/protocol/pubsub#errors"));                sendErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);                return;            }        }        else {            // Look for the specified node            node = service.getNode(nodeID);            if (node == null) {                // Node does not exist. Return item-not-found error                sendErrorPacket(iq, PacketError.Condition.item_not_found, null);                return;            }        }        NodeSubscription subscription;        if (node.isMultipleSubscriptionsEnabled()) {            if (subID == null) {                // No subid was specified and the node supports multiple subscriptions                Element pubsubError = DocumentHelper.createElement(                        QName.get("subid-required", "http://jabber.org/protocol/pubsub#errors"));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -