/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.XmlUtil
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.message.Message
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.xml.RemoveElement
 *  com.l7tech.policy.assertion.xml.RemoveElement$ElementLocation
 *  com.l7tech.policy.variable.NoSuchVariableException
 *  com.l7tech.util.DomUtils
 */
package com.l7tech.server.policy.assertion.xml;

import com.l7tech.common.io.XmlUtil;
import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.Message;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.xml.RemoveElement;
import com.l7tech.policy.variable.NoSuchVariableException;
import com.l7tech.server.message.AuthenticationContext;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractMessageTargetableServerAssertion;
import com.l7tech.server.policy.assertion.AssertionStatusException;
import com.l7tech.server.policy.variable.ExpandVariables;
import com.l7tech.util.DomUtils;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class ServerRemoveElement
extends AbstractMessageTargetableServerAssertion<RemoveElement> {
    private final boolean d;
    private final RemoveElement.ElementLocation e;
    private final String f;
    private final String g;
    private final String[] h;
    static final /* synthetic */ boolean i;
    private static final String[] j;

    public ServerRemoveElement(RemoveElement removeElement) {
        super(removeElement);
        this.e = removeElement.getInsertedElementLocation();
        this.d = this.e != null;
        this.f = removeElement.getElementFromVariable();
        this.g = removeElement.getElementToInsertVariable();
        this.h = removeElement.getVariablesUsed();
    }

    /*
     * Loose catch block
     */
    @Override
    protected AssertionStatus doCheckRequest(PolicyEnforcementContext policyEnforcementContext, Message message, String string, AuthenticationContext authenticationContext) throws IOException, PolicyAssertionException {
        try {
            if (!message.isXml()) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.REMOVE_ELEMENT_NOT_XML);
                return AssertionStatus.NOT_APPLICABLE;
            }
        }
        catch (NoSuchVariableException noSuchVariableException) {
            throw ServerRemoveElement.a((Exception)((Object)noSuchVariableException));
        }
        try {
            block12: {
                Map<String, Object> map = policyEnforcementContext.getVariableMap(this.h, this.getAudit());
                Object object = ExpandVariables.processSingleVariableAsObject(j[1] + this.f + "}", map, this.getAudit());
                Element[] elementArray = this.a(object, this.f, false);
                if (this.d) {
                    return this.a(policyEnforcementContext, map, message, elementArray);
                }
                if (elementArray.length <= 0) break block12;
                Document document = message.getXmlKnob().getDocumentWritable();
                for (Element element : elementArray) {
                    if (document != element.getOwnerDocument()) continue;
                    try {
                        block13: {
                            if (element.getParentNode() == null) continue;
                            break block13;
                            catch (NoSuchVariableException noSuchVariableException) {
                                throw ServerRemoveElement.a((Exception)((Object)noSuchVariableException));
                            }
                        }
                        element.getParentNode().removeChild(element);
                    }
                    catch (NoSuchVariableException noSuchVariableException) {
                        throw ServerRemoveElement.a((Exception)((Object)noSuchVariableException));
                    }
                }
            }
            return AssertionStatus.NONE;
        }
        catch (NoSuchVariableException noSuchVariableException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.NO_SUCH_VARIABLE, this.f);
            return AssertionStatus.FAILED;
        }
        catch (SAXException sAXException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.REMOVE_ELEMENT_NOT_XML);
            return AssertionStatus.NOT_APPLICABLE;
        }
    }

    private Element[] a(Object object, String string, boolean bl2) {
        Element[] elementArray;
        block13: {
            elementArray = new Element[]{};
            if (object instanceof Element[]) {
                elementArray = (Element[])object;
            } else if (object instanceof Element) {
                elementArray = new Element[]{(Element)object};
            } else if (object instanceof Collection) {
                Collection collection = (Collection)object;
                if (!collection.isEmpty()) {
                    try {
                        elementArray = collection.toArray(elementArray);
                    }
                    catch (ArrayStoreException arrayStoreException) {
                        this.logAndAudit((AuditDetailMessage)AssertionMessages.VARIABLE_INVALID_VALUE, string, j[4]);
                        throw new AssertionStatusException(AssertionStatus.FAILED);
                    }
                }
            } else {
                try {
                    if (!bl2 || !(object instanceof CharSequence)) break block13;
                }
                catch (ArrayStoreException arrayStoreException) {
                    throw ServerRemoveElement.a(arrayStoreException);
                }
                CharSequence charSequence = (CharSequence)object;
                try {
                    Document document = XmlUtil.stringToDocument((String)charSequence.toString());
                    elementArray = new Element[]{document.getDocumentElement()};
                }
                catch (SAXException sAXException) {
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.INSERT_ELEMENT_BAD_FRAGMENT);
                    throw new AssertionStatusException(AssertionStatus.FAILED);
                }
            }
        }
        return elementArray;
    }

    private AssertionStatus a(PolicyEnforcementContext policyEnforcementContext, Map<String, Object> map, Message message, Element[] elementArray) throws NoSuchVariableException, SAXException, IOException {
        Element[] elementArray2;
        Document document = message.getXmlKnob().getDocumentWritable();
        try {
            if (elementArray.length < 1) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.INSERT_ELEMENT_EXISTING_NOT_FOUND);
                return AssertionStatus.FAILED;
            }
        }
        catch (NoSuchVariableException noSuchVariableException) {
            throw ServerRemoveElement.a((Exception)((Object)noSuchVariableException));
        }
        try {
            if (elementArray.length > 1) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.INSERT_ELEMENT_EXISTING_TOO_MANY);
                return AssertionStatus.FAILED;
            }
        }
        catch (NoSuchVariableException noSuchVariableException) {
            throw ServerRemoveElement.a((Exception)((Object)noSuchVariableException));
        }
        Element element = elementArray[0];
        try {
            if (element == null) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_WARNING_WITH_MORE_INFO, j[2]);
                return AssertionStatus.SERVER_ERROR;
            }
        }
        catch (NoSuchVariableException noSuchVariableException) {
            throw ServerRemoveElement.a((Exception)((Object)noSuchVariableException));
        }
        Document document2 = element.getOwnerDocument();
        try {
            if (document2 == null) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_WARNING_WITH_MORE_INFO, j[5]);
                return AssertionStatus.SERVER_ERROR;
            }
        }
        catch (NoSuchVariableException noSuchVariableException) {
            throw ServerRemoveElement.a((Exception)((Object)noSuchVariableException));
        }
        try {
            if (document2 != document) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_WARNING_WITH_MORE_INFO, j[0]);
                return AssertionStatus.SERVER_ERROR;
            }
        }
        catch (NoSuchVariableException noSuchVariableException) {
            throw ServerRemoveElement.a((Exception)((Object)noSuchVariableException));
        }
        Object object = ExpandVariables.processSingleVariableAsObject(j[7] + this.g + "}", map, this.getAudit());
        for (Element element2 : elementArray2 = this.a(object, this.g, true)) {
            this.a(element, element2, this.e);
        }
        return AssertionStatus.NONE;
    }

    private void a(Element element, Element element2, RemoveElement.ElementLocation elementLocation) {
        block25: {
            try {
                try {
                    if (i || element != null) break block25;
                }
                catch (ArrayStoreException arrayStoreException) {
                    throw ServerRemoveElement.a(arrayStoreException);
                }
                throw new AssertionError();
            }
            catch (ArrayStoreException arrayStoreException) {
                throw ServerRemoveElement.a(arrayStoreException);
            }
        }
        try {
            if (element2 == null) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_WARNING_WITH_MORE_INFO, j[3]);
                throw new AssertionStatusException(AssertionStatus.SERVER_ERROR);
            }
        }
        catch (ArrayStoreException arrayStoreException) {
            throw ServerRemoveElement.a(arrayStoreException);
        }
        if (element2.getOwnerDocument() != element.getOwnerDocument()) {
            element2 = (Element)element.getOwnerDocument().importNode(element2, true);
        }
        switch (elementLocation) {
            case FIRST_CHILD: {
                Element element3 = DomUtils.findFirstChildElement((Element)element);
                try {
                    if (element3 == null) {
                        element.appendChild(element2);
                        break;
                    }
                }
                catch (ArrayStoreException arrayStoreException) {
                    throw ServerRemoveElement.a(arrayStoreException);
                }
                element.insertBefore(element2, element3);
                break;
            }
            case LAST_CHILD: {
                element.appendChild(element2);
                break;
            }
            case PREVIOUS_SIBLING: 
            case NEXT_SIBLING: {
                Node node = element.getParentNode();
                try {
                    if (!(node instanceof Element)) {
                        this.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_WARNING_WITH_MORE_INFO, j[8]);
                        throw new AssertionStatusException(AssertionStatus.SERVER_ERROR);
                    }
                }
                catch (ArrayStoreException arrayStoreException) {
                    throw ServerRemoveElement.a(arrayStoreException);
                }
                Element element4 = (Element)node;
                try {
                    if (RemoveElement.ElementLocation.PREVIOUS_SIBLING == elementLocation) {
                        element4.insertBefore(element2, element);
                        break;
                    }
                }
                catch (ArrayStoreException arrayStoreException) {
                    throw ServerRemoveElement.a(arrayStoreException);
                }
                Node node2 = element.getNextSibling();
                try {
                    if (node2 == null) {
                        element4.appendChild(element2);
                        break;
                    }
                }
                catch (ArrayStoreException arrayStoreException) {
                    throw ServerRemoveElement.a(arrayStoreException);
                }
                element4.insertBefore(element2, node2);
                break;
            }
            default: {
                throw new IllegalStateException(j[6] + String.valueOf(elementLocation));
            }
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block12: {
            block11: {
                var5 = new String[9];
                var3_1 = 0;
                var2_2 = "8v]~U\u001aJ\u001a.QaD\u001eA\u0013z\u0014iN\u0016W]`[y\u0001\u0011A\u0011aZj\u0001\u0007K]z\\h\u0001\u0007E\u000fiQy\u0001\u001eA\u000e}UjDS@\u0012mA`D\u001dP\u0002Yu\u00198v]~U\u001aJ\u001a.QaD\u001eA\u0013z\u0014z@\u0000\u0004\u0013{Xa\u00143kC-D\u001fA\u0010kZy\u0001\u0004E\u000e.ZxM\u001f\u00078bQ`D\u001dP-8v]~U\u001aJ\u001a.QaD\u001eA\u0013z\u0014iH\u0017\u0004\u0013a@-C\u0016H\u0012`S-U\u001c\u0004\u001c.PbB\u0006I\u0018`@\u0019(`_cN\u0004J]kXhL\u0016J\tB[n@\u0007M\u0012`\u000e-";
                var4_3 = "8v]~U\u001aJ\u001a.QaD\u001eA\u0013z\u0014iN\u0016W]`[y\u0001\u0011A\u0011aZj\u0001\u0007K]z\\h\u0001\u0007E\u000fiQy\u0001\u001eA\u000e}UjDS@\u0012mA`D\u001dP\u0002Yu\u00198v]~U\u001aJ\u001a.QaD\u001eA\u0013z\u0014z@\u0000\u0004\u0013{Xa\u00143kC-D\u001fA\u0010kZy\u0001\u0004E\u000e.ZxM\u001f\u00078bQ`D\u001dP-8v]~U\u001aJ\u001a.QaD\u001eA\u0013z\u0014iH\u0017\u0004\u0013a@-C\u0016H\u0012`S-U\u001c\u0004\u001c.PbB\u0006I\u0018`@\u0019(`_cN\u0004J]kXhL\u0016J\tB[n@\u0007M\u0012`\u000e-".length();
                var1_4 = 63;
                var0_5 = -1;
lbl7:
                // 2 sources

                while (true) {
                    v0 = 116;
                    v1 = ++var0_5;
                    v2 = var2_2.substring(v1, v1 + var1_4);
                    v3 = -1;
                    break block11;
                    break;
                }
lbl13:
                // 1 sources

                while (true) {
                    var5[var3_1++] = ServerRemoveElement.c(v0, ServerRemoveElement.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "$\b^U\u001d(\u00120kyt\u001ci\u00192}<r\u0007i\u0011/.*i\u0011%\u00192iyo\u0015i\u0015$g*t\u001a'\u0017|k5e\u001e,\u001e(.;e\u0010(\u0005/kye\u000b \u0003(g7gS,\u001c9c<n\u0007i\u0018=}yn\u001ci\u0000=|<n\u0007i\u00150k4e\u001d=";
                    var4_3 = "$\b^U\u001d(\u00120kyt\u001ci\u00192}<r\u0007i\u0011/.*i\u0011%\u00192iyo\u0015i\u0015$g*t\u001a'\u0017|k5e\u001e,\u001e(.;e\u0010(\u0005/kye\u000b \u0003(g7gS,\u001c9c<n\u0007i\u0018=}yn\u001ci\u0000=|<n\u0007i\u00150k4e\u001d=".length();
                    var1_4 = 2;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 9;
                        v4 = ++var0_5;
                        v2 = var2_2.substring(v4, v4 + var1_4);
                        v3 = 0;
                        break block11;
                        break;
                    }
                    break;
                }
lbl28:
                // 1 sources

                while (true) {
                    var5[var3_1++] = ServerRemoveElement.c(v0, ServerRemoveElement.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    break block12;
                    break;
                }
            }
            switch (v3) {
                default: {
                    ** continue;
                }
                ** case 0:
lbl39:
                // 1 sources

                ** continue;
            }
        }
        ServerRemoveElement.j = var5;
        try {
            v5 = ServerRemoveElement.class.desiredAssertionStatus() == false;
        }
        catch (ArrayStoreException v6) {
            throw ServerRemoveElement.a(v6);
        }
        ServerRemoveElement.i = v5;
    }

    private static Exception a(Exception exception) {
        return exception;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x50);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String c(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 9;
                    case 1 -> 122;
                    case 2 -> 64;
                    case 3 -> 121;
                    case 4 -> 85;
                    case 5 -> 7;
                    default -> 80;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

