package de.thedevstack.conversationsplus.xmpp.utils; 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; import eu.siacs.conversations.xml.Element; /** * 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() { } }