if (!service.canCreateNode(from) || (!UserManager.getInstance().isRegisteredUser(from) && !isComponent(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;
}
parentNode = (CollectionNode) tempNode;
}
}
field = completedForm.getField("pubsub#node_type");
if (field != null) {
// Check if user requested to create a new collection node
List<String> values = field.getValues();
if (!values.isEmpty()) {
collectionType = "collection".equals(values.get(0));