aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/xmpp/utils/ErrorIqPacketExceptionHelper.java
blob: bcc5e9ddd517b94fa68d41b216241c47499bb0bc (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
package de.thedevstack.conversationsplus.xmpp.utils;

import de.thedevstack.conversationsplus.xml.Element;
import de.thedevstack.conversationsplus.xmpp.IqPacketParser;
import de.thedevstack.conversationsplus.xmpp.exceptions.BadRequestIqErrorException;
import de.thedevstack.conversationsplus.xmpp.exceptions.InternalServerErrorException;
import de.thedevstack.conversationsplus.xmpp.exceptions.IqPacketErrorException;
import de.thedevstack.conversationsplus.xmpp.exceptions.ServiceUnavailableException;
import de.thedevstack.conversationsplus.xmpp.exceptions.UndefinedConditionException;

/**
 * Created by steckbrief on 22.08.2016.
 */
public final class ErrorIqPacketExceptionHelper {
    private final static String ERROR_NAMESPACE = "urn:ietf:params:xml:ns:xmpp-stanzas";

    public static void throwIqErrorException(Element errorIqPacket) throws IqPacketErrorException {
        Element packet = IqPacketParser.findChild(errorIqPacket, "error", "jabber:client");
        if (null != packet) {
            if (hasErrorElement(packet, "bad-request")) {
                throw new BadRequestIqErrorException(errorIqPacket, getErrorText(packet));
            }
            if (hasErrorElement(packet, "service-unavailable")) {
                throw new ServiceUnavailableException(errorIqPacket, getErrorText(packet));
            }
            if (hasErrorElement(packet, "internal-server-error")) {
                throw new InternalServerErrorException(errorIqPacket, getErrorText(packet));
            }
            if (hasErrorElement(packet, "undefined-condition")) {
                throw new UndefinedConditionException(errorIqPacket, getErrorText(packet));
            }
        }
        throw new IqPacketErrorException(errorIqPacket, "Unknown error packet.");
    }

    private static boolean hasErrorElement(Element packet, String elementName) {
        return null != IqPacketParser.findChild(packet, elementName, ERROR_NAMESPACE);
    }

    private static String getErrorText(Element packet) {
        return IqPacketParser.findChildContent(packet, "text", ERROR_NAMESPACE);
    }

    private ErrorIqPacketExceptionHelper() {

    }
}