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 sub = stanza.getFirstInnerElement().getFirstInnerElement(); // pubsub/subscribe
String strSubJID = sub.getAttributeValue("jid"); // MUST
try {
subJID = EntityImpl.parse(strSubJID);
} catch (EntityFormatException e) {
return errorStanzaGenerator.generateJIDMalformedErrorStanza(sender, serverJID, stanza);
}
if(!sender.getBareJID().equals(subJID.getBareJID())) {
// error condition 1 (6.1.3)
return errorStanzaGenerator.generateJIDDontMatchErrorStanza(sender, serverJID, stanza);
}
String nodeName = extractNodeName(stanza);
LeafNode node = root.find(nodeName);
if(node == null) {
// no such node (error condition 11 (6.1.3))
return errorStanzaGenerator.generateNoNodeErrorStanza(sender, serverJID, stanza);
}
String id = idGenerator.create();
node.subscribe(id, subJID);
buildSuccessStanza(sb, nodeName, strSubJID, id);
sb.endInnerElement(); // pubsub
return new IQStanza(sb.build());
}