aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/xmpp/utils/ErrorIqPacketExceptionHelper.java
blob: f02d8d46fb650217b3fb3a9fc8e243e2a89a27ae (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
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 packet) throws IqPacketErrorException {
        if (hasErrorElement(packet, "bad-request")) {
            throw new BadRequestIqErrorException(packet, getErrorText(packet));
        }
        if (hasErrorElement(packet, "service-unavailable")) {
            throw new ServiceUnavailableException(packet, getErrorText(packet));
        }
        if (hasErrorElement(packet, "internal-server-error")) {
            throw new InternalServerErrorException(packet, getErrorText(packet));
        }
        if (hasErrorElement(packet, "undefined-condition")) {
            throw new UndefinedConditionException(packet, getErrorText(packet));
        }
        throw new IqPacketErrorException(packet, "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() {

    }
}