String iqStanzaID = stanza.getAttributeValue("id");
StanzaBuilder sb = StanzaBuilder.createIQStanza(serverJID, sender, IQStanzaType.RESULT, iqStanzaID);
sb.startInnerElement("pubsub", NamespaceURIs.XEP0060_PUBSUB_OWNER);
String nodeName = extractNodeName(stanza);
LeafNode node = root.find(nodeName);
if(node == null) {
return errorStanzaGenerator.generateNoNodeErrorStanza(sender, serverJID, stanza);
}
if(!node.isAuthorized(sender, PubSubPrivilege.MANAGE_AFFILIATIONS)) {
return errorStanzaGenerator.generateInsufficientPrivilegesErrorStanza(sender, serverJID, stanza);
}
XMLElement affiliationElement = null;
try {
if(stanza.getFirstInnerElement().getFirstInnerElement().getInnerElements().size() != 1) {
return errorStanzaGenerator.generateNotAcceptableErrorStanza(serverJID, sender, stanza);
}
affiliationElement = stanza.getFirstInnerElement().getFirstInnerElement().getFirstInnerElement();
Entity userJID = null;
try {
userJID = EntityImpl.parse(affiliationElement.getAttributeValue("jid"));
} catch (EntityFormatException e) {
return errorStanzaGenerator.generateJIDMalformedErrorStanza(serverJID, sender, stanza); // TODO not defined in the standard(?)
}
PubSubAffiliation newAffiliation = PubSubAffiliation.get(affiliationElement.getAttributeValue("affiliation"));
node.setAffiliation(userJID, newAffiliation);
} catch(LastOwnerResignedException e) {
// if the last owner tries to resign.
return errorStanzaGenerator.generateNotAcceptableErrorStanza(serverJID, sender, stanza);
} catch(Throwable t) { // possible null-pointer
return errorStanzaGenerator.generateBadRequestErrorStanza(serverJID, sender, stanza); // TODO not defined in the standard(?)