Entity sender = extractSenderJID(stanza, sessionContext);
Entity subJID = null;
String iqStanzaID = stanza.getAttributeValue("id");
StanzaBuilder sb = StanzaBuilder.createIQStanza(serverJID, sender, IQStanzaType.RESULT, iqStanzaID);
sb.startInnerElement("pubsub", NamespaceURIs.XEP0060_PUBSUB);
XMLElement unsub = stanza.getFirstInnerElement().getFirstInnerElement(); // pubsub/unsubscribe
String strSubJID = unsub.getAttributeValue("jid"); // MUST
String strSubID = unsub.getAttributeValue("subid"); // SHOULD (req. for more than one subscription)
try {
subJID = EntityImpl.parse(strSubJID);
} catch (EntityFormatException e) {
// return error stanza... (general error)
return errorStanzaGenerator.generateJIDMalformedErrorStanza(sender, serverJID, stanza);
}
if(!sender.getBareJID().equals(subJID.getBareJID())) {
// insufficient privileges (error condition 3 (6.2.3))
return errorStanzaGenerator.generateInsufficientPrivilegesErrorStanza(sender, serverJID, stanza);
}
String nodeName = extractNodeName(stanza);
LeafNode node = root.find(nodeName);
if(node == null) {
// no such node (error condition 4 (6.2.3))
return errorStanzaGenerator.generateNoNodeErrorStanza(sender, serverJID, stanza);
}
if(strSubID == null) {
try {
if(node.unsubscribe(subJID) == false) {
// has no subscription (6.2.3.2)
return errorStanzaGenerator.generateNoSuchSubscriberErrorStanza(sender, serverJID, stanza);
}
} catch(MultipleSubscriptionException e) {
// error case 6.2.3.1
return errorStanzaGenerator.generateSubIDRequiredErrorStanza(sender, serverJID, stanza);
}
} else {
if(node.unsubscribe(strSubID, subJID) == false) {
// subID not valid (6.2.3.5)
return errorStanzaGenerator.generateSubIDNotValidErrorStanza(sender, serverJID, stanza);
}
}
sb.endInnerElement(); // pubsub
return new IQStanza(sb.build());
}