diff options
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/thedevstack/conversationsplus/xmpp/stanzas/IqPacketReceiver.java | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/stanzas/IqPacketReceiver.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/stanzas/IqPacketReceiver.java new file mode 100644 index 00000000..bfae0355 --- /dev/null +++ b/src/main/java/de/thedevstack/conversationsplus/xmpp/stanzas/IqPacketReceiver.java @@ -0,0 +1,96 @@ +package de.thedevstack.conversationsplus.xmpp.stanzas; + +import java.util.Hashtable; + +import de.thedevstack.conversationsplus.entities.Account; +import de.thedevstack.conversationsplus.parser.IqParser; +import de.thedevstack.conversationsplus.utils.XmppConnectionServiceAccessor; +import de.thedevstack.conversationsplus.utils.XmppSendUtil; +import de.thedevstack.conversationsplus.xml.Element; +import de.thedevstack.conversationsplus.xmpp.IqPacketHandler; +import de.thedevstack.conversationsplus.xmpp.OnIqPacketReceived; +import de.thedevstack.conversationsplus.xmpp.Xep; +import de.thedevstack.conversationsplus.xmpp.exceptions.IqPacketErrorException; +import de.thedevstack.conversationsplus.xmpp.exceptions.NotAllowedIqException; + +/** + */ +public final class IqPacketReceiver implements OnIqPacketReceived { + private final Hashtable<String, IqPacketHandler> iqPacketHandlers = new Hashtable<>(); + private final IqParser legacyIqParser; + private static IqPacketReceiver INSTANCE; + + public synchronized static IqPacketReceiver getInstance() { + if (null == INSTANCE) { + IqPacketReceiver.INSTANCE = new IqPacketReceiver(); + } + return INSTANCE; + } + + private IqPacketReceiver() { + this.legacyIqParser = new IqParser(XmppConnectionServiceAccessor.xmppConnectionService); + } + + @Override + public void onIqPacketReceived(Account account, IqPacket packet) { + String key = generateKey(packet); + if (null != key && this.iqPacketHandlers.containsKey(key)) { + try { + IqPacketHandler iqPacketHandler = this.iqPacketHandlers.get(key); + iqPacketHandler.handleIqPacket(account, packet); + } catch (NotAllowedIqException e) { + ErrorIqPacket errorIqPacket = IqPacketGenerator.generateIqErrorPacketResponse(IqErrorCondition.NOT_ALLOWED, packet); + XmppSendUtil.sendIqPacket(account, errorIqPacket); + } catch (IqPacketErrorException e) { + + } + } else { + this.legacyIqParser.onIqPacketReceived(account, packet); + } + } + + public static String generateKey(IqPacket packet) { + Element childElement = packet.getChildElement(); + return generateKey(childElement.getName(), childElement.getNamespace()); + } + + public static String generateKey(String elementName, String xmlns) { + if (null == elementName && null == xmlns) { + return null; + } + return ((null != elementName) ? elementName : "") + ((null != xmlns) ? ("-" + xmlns) : ""); + } + + public void registerIqPacketHandler(String elementName, String xmlns, IqPacketHandler iqPacketHandler) { + String key = generateKey(elementName, xmlns); + if (null != key) { + this.iqPacketHandlers.put(key, iqPacketHandler); + } + } + + public void unregisterIqPacketHandler(String elementName, String xmlns) { + String key = generateKey(elementName, xmlns); + if (null != key && this.iqPacketHandlers.containsKey(key)) { + this.iqPacketHandlers.remove(key); + } + } + + public void registerIqPacketHandler(Xep xep) { + String key = generateKey(xep.elementName(), xep.namespace()); + if (null != key && null != xep.handler()) { + this.iqPacketHandlers.put(key, xep.handler()); + } + } + + public void unregisterIqPacketHandler(Xep xep) { + String key = generateKey(xep.elementName(), xep.namespace()); + if (null != key && this.iqPacketHandlers.containsKey(key)) { + this.iqPacketHandlers.remove(key); + } + } + + @Deprecated + public IqParser getLegacyIqParser() { + return this.legacyIqParser; + } +} |