📄 pubsubengine.java
字号:
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(PubSubService service, 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(PubSubService service, 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(PubSubService service, 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; 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(PubSubService service, 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 if (!service.isInstantNodeSupported()) { // Instant nodes creation is not allowed so return an error Element pubsubError = DocumentHelper.createElement( QName.get("nodeid-required", "http://jabber.org/protocol/pubsub#errors")); sendErrorPacket(iq, PacketError.Condition.not_acceptable, pubsubError); return; } do { // Create a new nodeID and make sure that the random generated string does not // match an existing node. Probability to match an existing node are very very low // but they exist :) newNodeID = StringUtils.randomString(15); } while (service.getNode(newNodeID) != null); } boolean collectionType = false; // Check if user requested to configure the node (using a data form) Element configureElement = childElement.element("configure"); if (configureElement != null) { // Get the data form that contains the parent nodeID completedForm = getSentConfigurationForm(configureElement); if (completedForm != null) { // Calculate newNodeID when new node is affiliated with a Collection FormField field = completedForm.getField("pubsub#collection"); if (field != null) { List<String> values = field.getValues(); if (!values.isEmpty()) { String parentNodeID = values.get(0); Node tempNode = service.getNode(parentNodeID); if (tempNode == null) { // Requested parent node was not found so return an error sendErrorPacket(iq, PacketError.Condition.item_not_found, null); return; } else if (!tempNode.isCollectionNode()) { // Requested parent node is not a collection node so return an error sendErrorPacket(iq, PacketError.Condition.not_acceptable, null); return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -