aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/xmpp/stanzas/IqPacketReceiver.java
blob: bfae03551019bd16291eb8db4d2a75509bf5eb03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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;
    }
}