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

📄 pubsubengine.java

📁 基于Jabber协议的即时消息服务器
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                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 submits        // new subscription options        if (!subscription.canModify(iq.getFrom())) {            // Requestor is prohibited from setting new subscription options            sendErrorPacket(iq, PacketError.Condition.forbidden, null);            return;        }        Element formElement = optionsElement.element(QName.get("x", "jabber:x:data"));        if (formElement != null) {            // Change the subscription configuration based on the completed form            subscription.configure(iq, new DataForm(formElement));        }        else {            // No data form was included so return bad request error            sendErrorPacket(iq, PacketError.Condition.bad_request, null);        }    }    private void getSubscriptions(IQ iq, Element childElement) {        // TODO Assuming that owner is the bare JID (as defined in the JEP). This can be replaced with an explicit owner specified in the packet        JID owner = new JID(iq.getFrom().toBareJID());        // Collect subscriptions of owner for all nodes at the service        Collection<NodeSubscription> subscriptions = new ArrayList<NodeSubscription>();        for (Node node : service.getNodes()) {            subscriptions.addAll(node.getSubscriptions(owner));        }        // Create reply to send        IQ reply = IQ.createResultIQ(iq);        Element replyChildElement = childElement.createCopy();        reply.setChildElement(replyChildElement);        if (subscriptions.isEmpty()) {            // User does not have any affiliation or subscription with the pubsub service            reply.setError(PacketError.Condition.item_not_found);        }        else {            Element affiliationsElement = replyChildElement.element("subscriptions");            // Add information about subscriptions including existing affiliations            for (NodeSubscription subscription : subscriptions) {                Element subElement = affiliationsElement.addElement("subscription");                Node node = subscription.getNode();                NodeAffiliate nodeAffiliate = subscription.getAffiliate();                // Do not include the node id when node is the root collection node                if (!node.isRootCollectionNode()) {                    subElement.addAttribute("node", node.getNodeID());                }                subElement.addAttribute("jid", subscription.getJID().toString());                subElement.addAttribute("affiliation", nodeAffiliate.getAffiliation().name());                subElement.addAttribute("subscription", subscription.getState().name());                if (node.isMultipleSubscriptionsEnabled()) {                    subElement.addAttribute("subid", subscription.getID());                }            }        }        // Send reply        router.route(reply);    }    private  void getAffiliations(IQ iq, Element childElement) {        // TODO Assuming that owner is the bare JID (as defined in the JEP). This can be replaced with an explicit owner specified in the packet        JID owner = new JID(iq.getFrom().toBareJID());        // Collect affiliations of owner for all nodes at the service        Collection<NodeAffiliate> affiliations = new ArrayList<NodeAffiliate>();        for (Node node : service.getNodes()) {            NodeAffiliate nodeAffiliate = node.getAffiliate(owner);            if (nodeAffiliate != null) {                affiliations.add(nodeAffiliate);            }        }        // Create reply to send        IQ reply = IQ.createResultIQ(iq);        Element replyChildElement = childElement.createCopy();        reply.setChildElement(replyChildElement);        if (affiliations.isEmpty()) {            // User does not have any affiliation or subscription with the pubsub service            reply.setError(PacketError.Condition.item_not_found);        }        else {            Element affiliationsElement = replyChildElement.element("affiliations");            // Add information about affiliations without subscriptions            for (NodeAffiliate affiliate : affiliations) {                Element affiliateElement = affiliationsElement.addElement("affiliation");                // Do not include the node id when node is the root collection node                if (!affiliate.getNode().isRootCollectionNode()) {                    affiliateElement.addAttribute("node", affiliate.getNode().getNodeID());                }                affiliateElement.addAttribute("jid", affiliate.getJID().toString());                affiliateElement.addAttribute("affiliation", affiliate.getAffiliation().name());            }        }        // Send reply        router.route(reply);    }    private void getPublishedItems(IQ iq, Element itemsElement) {        String nodeID = itemsElement.attributeValue("node");        String subID = itemsElement.attributeValue("subid");        Node node;        if (nodeID == null) {            // User must specify a leaf node ID 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;            }        }        if (node.isCollectionNode()) {            // Node is a collection node. Return feature-not-implemented error            Element pubsubError = DocumentHelper.createElement(                    QName.get("unsupported", "http://jabber.org/protocol/pubsub#errors"));            pubsubError.addAttribute("feature", "retrieve-items");            sendErrorPacket(iq, PacketError.Condition.feature_not_implemented, pubsubError);            return;        }        // Check if sender and subscriber JIDs match or if a valid "trusted proxy" is being used        JID subscriberJID = iq.getFrom();        // 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(subscriberJID.toBareJID());        // Check if the node's access model allows the subscription to proceed        AccessModel accessModel = node.getAccessModel();        if (!accessModel.canAccessItems(node, owner, subscriberJID)) {            sendErrorPacket(iq, accessModel.getSubsriptionError(),                    accessModel.getSubsriptionErrorDetail());            return;        }        // Check that the requester is not an outcast        NodeAffiliate affiliate = node.getAffiliate(owner);        if (affiliate != null && affiliate.getAffiliation() == NodeAffiliate.Affiliation.outcast) {            sendErrorPacket(iq, PacketError.Condition.forbidden, null);            return;        }        // Get the user's subscription        NodeSubscription subscription = null;        if (node.isMultipleSubscriptionsEnabled() && !node.getSubscriptions(owner).isEmpty()) {            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;                }            }        }        if (subscription != null && !subscription.isActive()) {            Element pubsubError = DocumentHelper.createElement(                    QName.get("not-subscribed", "http://jabber.org/protocol/pubsub#errors"));            sendErrorPacket(iq, PacketError.Condition.not_authorized, pubsubError);            return;        }        LeafNode leafNode = (LeafNode) node;        // Get list of items to send to the user        boolean forceToIncludePayload = false;        List<PublishedItem> items = null;        String max_items = itemsElement.attributeValue("max_items");        int recentItems = 0;        if (max_items != null) {            try {                // Parse the recent number of items requested                recentItems = Integer.parseInt(max_items);            }            catch (NumberFormatException e) {                // There was an error parsing the number so assume that all items were requested                Log.warn("Assuming that all items were requested", e);                max_items = null;            }        }        if (max_items != null) {            // Get the N most recent published items            items = new ArrayList<PublishedItem>(leafNode.getPublishedItems(recentItems));        }        else {            List requestedItems = itemsElement.elements("item");            if (requestedItems.isEmpty()) {                // Get all the active items that were published to the node                items = new ArrayList<PublishedItem>(leafNode.getPublishedItems());            }            else {                items = new ArrayList<PublishedItem>();                // Indicate that payload should be included (if exists) no matter                // the node configuration                forceToIncludePayload = true;                // Get the items as requested by the user                for (Iterator it = requestedItems.iterator(); it.hasNext();) {                    Element element = (Element) it.next();                    String itemID = element.attributeValue("id");                    PublishedItem item = leafNode.getPublishedItem(itemID);                    if (item != null) {                        items.add(item);                    }                }            }        }        if (subscription != null && subscription.getKeyword() != null) {            // Filter items that do not match the subscription keyword            for (Iterator<PublishedItem> it = items.iterator(); it.hasNext();) {                PublishedItem item = it.next();                if (!subscription.isKeywordMatched(item)) {                    // Remove item that does not match keyword                    it.remove();                }            }        }        // Send items to the user        leafNode.sendPublishedItems(iq, items, forceToIncludePayload);    }    private void createNode(IQ iq, Element childElement, Element createElement) {        // Get sender of the IQ packet        JID from = iq.getFrom();        // Verify that sender has permissions to create nodes        if (!service.canCreateNode(from) || !UserManager.getInstance().isRegisteredUser(from)) {            // The user is not allowed to create nodes so return an error            sendErrorPacket(iq, PacketError.Condition.forbidden, null);            return;        }        DataForm completedForm = null;        CollectionNode parentNode = null;        String nodeID = createElement.attributeValue("node");        String newNodeID = nodeID;        if (nodeID == null) {            // User requested an instant node

⌨️ 快捷键说明

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