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

📄 pubsubengine.java

📁 基于Jabber协议的即时消息服务器
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            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);        }        // 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;                        }                        parentNode = (CollectionNode) tempNode;                        // If requested new nodeID does not contain parent nodeID then add                        // the parent nodeID to the beginging of the new nodeID                        if (!newNodeID.startsWith(parentNodeID)) {                            newNodeID = parentNodeID + "/" + newNodeID;                        }                    }                }            }        }        // If no parent was defined then use the root collection node        if (parentNode == null && service.isCollectionNodesSupported()) {            parentNode = service.getRootCollectionNode();            // Calculate new nodeID for the new node            if (!newNodeID.startsWith(parentNode.getNodeID() + "/")) {                newNodeID = parentNode.getNodeID() + "/" + newNodeID;            }        }        // Check that the requested nodeID does not exist        Node existingNode = service.getNode(newNodeID);        if (existingNode != null) {            // There is a conflict since a node with the same ID already exists            sendErrorPacket(iq, PacketError.Condition.conflict, null);            return;        }        // Check if user requested to create a new collection node        boolean collectionType = "collection".equals(createElement.attributeValue("type"));        if (collectionType && !service.isCollectionNodesSupported()) {            // Cannot create a collection node since the service doesn't support it            Element pubsubError = DocumentHelper.createElement(                    QName.get("unsupported", "http://jabber.org/protocol/pubsub#errors"));            pubsubError.addAttribute("feature", "collections");            sendErrorPacket(iq, PacketError.Condition.feature_not_implemented, pubsubError);            return;        }        if (parentNode != null && !collectionType) {            // Check if requester is allowed to add a new leaf child node to the parent node            if (!parentNode.isAssociationAllowed(from)) {                // User is not allowed to add child leaf node to parent node. Return an error.                sendErrorPacket(iq, PacketError.Condition.forbidden, null);                return;            }            // Check if number of child leaf nodes has not been exceeded            if (parentNode.isMaxLeafNodeReached()) {                // Max number of child leaf nodes has been reached. Return an error.                Element pubsubError = DocumentHelper.createElement(QName.get("max-nodes-exceeded",                        "http://jabber.org/protocol/pubsub#errors"));                sendErrorPacket(iq, PacketError.Condition.conflict, pubsubError);                return;            }        }        // Create and configure the node        boolean conflict = false;        Node newNode = null;        try {            // 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());            synchronized (newNodeID.intern()) {                if (service.getNode(newNodeID) == null) {                    // Create the node                    if (collectionType) {                        newNode = new CollectionNode(service, parentNode, newNodeID, from);                    }                    else {                        newNode = new LeafNode(service, parentNode, newNodeID, from);                    }                    // Add the creator as the node owner                    newNode.addOwner(owner);                    // Configure and save the node to the backend store                    if (completedForm != null) {                        newNode.configure(completedForm);                    }                    else {                        newNode.saveToDB();                    }                }                else {                    conflict = true;                }            }            if (conflict) {                // There is a conflict since a node with the same ID already exists                sendErrorPacket(iq, PacketError.Condition.conflict, null);            }            else {                // Return success to the node owner                IQ reply = IQ.createResultIQ(iq);                // Include new nodeID if it has changed from the original nodeID                if (!newNode.getNodeID().equals(nodeID)) {                    Element elem =                            reply.setChildElement("pubsub", "http://jabber.org/protocol/pubsub");                    elem.addElement("create").addAttribute("node", newNode.getNodeID());                }                router.route(reply);            }        }        catch (NotAcceptableException e) {            // Node should have at least one owner. Return not-acceptable error.            sendErrorPacket(iq, PacketError.Condition.not_acceptable, null);        }    }    private void getNodeConfiguration(IQ iq, Element childElement, String nodeID) {        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.isAdmin(iq.getFrom())) {            // Requesting entity is prohibited from configuring this node. Return forbidden error            sendErrorPacket(iq, PacketError.Condition.forbidden, null);            return;        }        // Return data form containing node configuration to the owner        IQ reply = IQ.createResultIQ(iq);        Element replyChildElement = childElement.createCopy();        reply.setChildElement(replyChildElement);        replyChildElement.element("configure").add(node.getConfigurationForm().getElement());        router.route(reply);    }    private void getDefaultNodeConfiguration(IQ iq, Element childElement, Element defaultElement) {        String type = defaultElement.attributeValue("type");        type = type == null ? "leaf" : type;        boolean isLeafType = "leaf".equals(type);        DefaultNodeConfiguration config = service.getDefaultNodeConfiguration(isLeafType);        if (config == null) {            // Service does not support the requested node type so return an error            Element pubsubError = DocumentHelper.createElement(                    QName.get("unsupported", "http://jabber.org/protocol/pubsub#errors"));            pubsubError.addAttribute("feature", isLeafType ? "leaf" : "collections");            sendErrorPacket(iq, PacketError.Condition.feature_not_implemented, pubsubError);            return;        }        // Return data form containing default node configuration        IQ reply = IQ.createResultIQ(iq);        Element replyChildElement = childElement.createCopy();        reply.setChildElement(replyChildElement);        replyChildElement.element("default").add(config.getConfigurationForm().getElement());        router.route(reply);    }    private void configureNode(IQ iq, Element configureElement, String nodeID) {        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.isAdmin(iq.getFrom())) {            // Requesting entity is not allowed to get node configuration. Return forbidden error            sendErrorPacket(iq, PacketError.Condition.forbidden, null);            return;        }        // Get the data form that contains the parent nodeID        DataForm completedForm = getSentConfigurationForm(configureElement);        if (completedForm != null) {            try {                // Update node configuration with the provided data form                // (and update the backend store)                node.configure(completedForm);                // Return that node configuration was successful                router.route(IQ.createResultIQ(iq));            }            catch (NotAcceptableException e) {                // Node should have at least one owner. Return not-acceptable error.                sendErrorPacket(iq, PacketError.Condition.not_acceptable, null);            }        }        else {            // No data form was included so return bad-request error            sendErrorPacket(iq, PacketError.Condition.bad_request, null);        }    }    private void deleteNode(IQ iq, Element deleteElement) {        String nodeID = deleteElement.attributeValue("node");        if (nodeID == null) {            // NodeID was not provided. Return bad-request error            sendErrorPacket(iq, PacketError.Condition.bad_request, null);            return;        }        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.isAdmin(iq.getFrom())) {            // Requesting entity is prohibited from deleting this node. Return forbidden error            sendErrorPacket(iq, PacketError.Condition.forbidden, null);            return;        }        if (node.isRootCollectionNode()) {            // Root collection node cannot be deleted. Return not-allowed error            sendErrorPacket(iq, PacketError.Condition.not_allowed, null);            return;        }        // Delete the node        if (node.delete()) {            // Return that node was deleted successfully            router.route(IQ.createResultIQ(iq));        }        else {            // Some error occured while trying to delete the node            sendErrorPacket(iq, PacketError.Condition.internal_server_error, null);        }    }    private void purgeNode(IQ iq, Element purgeElement) {        String nodeID = purgeElement.attributeValue("node");        if (nodeID == null) {            // NodeID was not provided. Return bad-request error            sendErrorPacket(iq, PacketError.Condition.bad_request, null);            return;        }        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.isAdmin(iq.getFrom())) {            // Requesting entity is prohibited from configuring this node. Return forbidden error            sendErrorPacket(iq, PacketError.Condition.forbidden, null);            return;        }        if (!((LeafNode) node).isPersistPublishedItems()) {            // Node does not persist items. Return feature-not-implemented error            Ele

⌨️ 快捷键说明

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