/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.message.Message
 *  com.l7tech.policy.AssertionResourceInfo
 *  com.l7tech.policy.MessageUrlResourceInfo
 *  com.l7tech.policy.StaticResourceInfo
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.GlobalResourceInfo
 *  com.l7tech.policy.assertion.MessageTargetable
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.RoutingStatus
 *  com.l7tech.policy.assertion.TargetMessageType
 *  com.l7tech.policy.assertion.xml.SchemaValidation
 *  com.l7tech.policy.variable.NoSuchVariableException
 *  com.l7tech.util.HexUtils
 *  com.l7tech.xml.ElementCursor
 *  org.springframework.context.ApplicationContext
 */
package com.l7tech.server.policy.assertion.xml;

import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.Message;
import com.l7tech.policy.AssertionResourceInfo;
import com.l7tech.policy.MessageUrlResourceInfo;
import com.l7tech.policy.StaticResourceInfo;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.GlobalResourceInfo;
import com.l7tech.policy.assertion.MessageTargetable;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.RoutingStatus;
import com.l7tech.policy.assertion.TargetMessageType;
import com.l7tech.policy.assertion.xml.SchemaValidation;
import com.l7tech.policy.variable.NoSuchVariableException;
import com.l7tech.server.communityschemas.SchemaManager;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.ServerPolicyException;
import com.l7tech.server.policy.assertion.AbstractServerAssertion;
import com.l7tech.server.policy.assertion.xml.d;
import com.l7tech.server.policy.assertion.xml.e;
import com.l7tech.server.policy.assertion.xml.f;
import com.l7tech.server.util.res.ResourceGetter;
import com.l7tech.util.HexUtils;
import com.l7tech.xml.ElementCursor;
import java.io.IOException;
import java.util.logging.Level;
import org.springframework.context.ApplicationContext;

public class ServerSchemaValidation
extends AbstractServerAssertion<SchemaValidation> {
    private final ResourceGetter<String, ElementCursor> d;
    private final String e;
    private final SchemaManager f;
    private final String[] g;
    private final String h;
    private boolean i = false;
    private static final String[] j;

    public ServerSchemaValidation(SchemaValidation schemaValidation, ApplicationContext applicationContext) throws ServerPolicyException {
        super(schemaValidation);
        Object object;
        Object object2;
        String[] stringArray = j;
        this.f = (SchemaManager)applicationContext.getBean(stringArray[2]);
        this.g = schemaValidation.getVariablesUsed();
        AssertionResourceInfo assertionResourceInfo = ((SchemaValidation)this.assertion).getResourceInfo();
        try {
            if (assertionResourceInfo instanceof GlobalResourceInfo) {
                this.h = ((GlobalResourceInfo)assertionResourceInfo).getId();
                this.f.registerUri(this.h);
                this.d = null;
                this.e = null;
                return;
            }
        }
        catch (ServerPolicyException serverPolicyException) {
            throw ServerSchemaValidation.a((Exception)((Object)serverPolicyException));
        }
        try {
            this.h = null;
            if (assertionResourceInfo instanceof MessageUrlResourceInfo) {
                throw new ServerPolicyException(this.assertion, j[13]);
            }
        }
        catch (ServerPolicyException serverPolicyException) {
            throw ServerSchemaValidation.a((Exception)((Object)serverPolicyException));
        }
        if (assertionResourceInfo instanceof StaticResourceInfo) {
            block13: {
                String string;
                String string2;
                String string3;
                block12: {
                    object2 = (StaticResourceInfo)assertionResourceInfo;
                    object = object2.getDocument();
                    string3 = object2.getOriginalUrl();
                    stringArray = j;
                    string2 = stringArray[5] + System.identityHashCode(this) + stringArray[4] + HexUtils.encodeBase64((byte[])HexUtils.getMd5Digest((byte[])((String)object).getBytes()), (boolean)true);
                    try {
                        if (string3 != null) break block12;
                        this.e = string2;
                        break block13;
                    }
                    catch (ServerPolicyException serverPolicyException) {
                        throw ServerSchemaValidation.a((Exception)((Object)serverPolicyException));
                    }
                }
                try {
                    string = string3.contains("#") ? "_" : "#";
                }
                catch (ServerPolicyException serverPolicyException) {
                    throw ServerSchemaValidation.a((Exception)((Object)serverPolicyException));
                }
                String string4 = string;
                this.e = string3 + string4 + string2;
            }
            this.f.registerSchema(this.e, null, (String)object);
            assertionResourceInfo = new StaticResourceInfo(this.e);
        } else {
            this.e = null;
        }
        object2 = new d(this);
        object = new e(this);
        this.d = ResourceGetter.createResourceGetter(this.assertion, assertionResourceInfo, object2, null, object, this.getAudit());
    }

    /*
     * Unable to fully structure code
     */
    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext var1_1) throws IOException, PolicyAssertionException {
        block19: {
            block22: {
                block20: {
                    block18: {
                        block17: {
                            var2_2 = ((SchemaValidation)this.assertion).getTarget();
                            if (var2_2 != null) break block20;
                            var5_3 = var1_1.getRoutingStatus();
                            if (var5_3 == RoutingStatus.ROUTED) break block17;
                            try {
                                block21: {
                                    if (var5_3 == RoutingStatus.ATTEMPTED) break block17;
                                    break block21;
                                    catch (NoSuchVariableException v0) {
                                        throw ServerSchemaValidation.a((Exception)v0);
                                    }
                                }
                                v1 = true;
                                break block18;
                            }
                            catch (NoSuchVariableException v2) {
                                throw ServerSchemaValidation.a((Exception)v2);
                            }
                        }
                        v1 = var6_5 = false;
                    }
                    if (var6_5) {
                        var3_6 = var1_1.getRequest();
                        var2_2 = TargetMessageType.REQUEST;
                    } else {
                        var3_6 = var1_1.getResponse();
                        var2_2 = TargetMessageType.RESPONSE;
                    }
                    var4_7 = var2_2.name().toLowerCase();
                    break block22;
                }
                try {
                    var3_6 = var1_1.getTargetMessage((MessageTargetable)this.assertion, true);
                    var4_7 = ((SchemaValidation)this.assertion).getTargetName();
                }
                catch (NoSuchVariableException var5_4) {
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.NO_SUCH_VARIABLE, new String[]{((SchemaValidation)this.assertion).getOtherTargetMessageVariable()});
                    return AssertionStatus.FAILED;
                }
            }
            try {
                if (!var3_6.isXml()) {
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.SCHEMA_VALIDATION_NOT_XML, new String[]{var4_7});
                    return AssertionStatus.NOT_APPLICABLE;
                }
            }
            catch (NoSuchVariableException v3) {
                throw ServerSchemaValidation.a((Exception)v3);
            }
            this.logAndAudit((AuditDetailMessage)AssertionMessages.SCHEMA_VALIDATION_VALIDATING, new String[]{var4_7});
            var5_3 = this.a(var3_6, var1_1);
            try {
                if (var5_3 != AssertionStatus.BAD_REQUEST) break block19;
            }
            catch (NoSuchVariableException v4) {
                throw ServerSchemaValidation.a((Exception)v4);
            }
            {
                ** switch (com.l7tech.server.policy.assertion.xml.f.a[var2_2.ordinal()])
            }
lbl-1000:
            // 1 sources

            {
                case 1: {
                    var5_3 = AssertionStatus.BAD_RESPONSE;
                    break;
                }
lbl54:
                // 1 sources

                case 2: {
                    var5_3 = AssertionStatus.FAILED;
                    break;
                }
            }
        }
        return var5_3;
    }

    /*
     * Exception decompiling
     */
    AssertionStatus a(Message var1_1, PolicyEnforcementContext var2_2) throws IOException {
        /*
         * This method has failed to decompile.  When submitting a bug report, please provide this stack trace, and (if you hold appropriate legal rights) the relevant class file.
         * 
         * org.benf.cfr.reader.util.ConfusedCFRException: Started 2 blocks at once
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.getStartingBlocks(Op04StructuredStatement.java:412)
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.buildNestedBlocks(Op04StructuredStatement.java:487)
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op03SimpleStatement.createInitialStructuredBlock(Op03SimpleStatement.java:736)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisInner(CodeAnalyser.java:850)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisOrWrapFail(CodeAnalyser.java:278)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysis(CodeAnalyser.java:201)
         *     at org.benf.cfr.reader.entities.attributes.AttributeCode.analyse(AttributeCode.java:94)
         *     at org.benf.cfr.reader.entities.Method.analyse(Method.java:531)
         *     at org.benf.cfr.reader.entities.ClassFile.analyseMid(ClassFile.java:1055)
         *     at org.benf.cfr.reader.entities.ClassFile.analyseTop(ClassFile.java:942)
         *     at org.benf.cfr.reader.Driver.doJarVersionTypes(Driver.java:257)
         *     at org.benf.cfr.reader.Driver.doJar(Driver.java:139)
         *     at org.benf.cfr.reader.CfrDriverImpl.analyse(CfrDriverImpl.java:76)
         *     at org.benf.cfr.reader.Main.main(Main.java:54)
         */
        throw new IllegalStateException("Decompilation failed");
    }

    private void a(PolicyEnforcementContext policyEnforcementContext, String string, Throwable throwable) {
        this.logger.log(Level.INFO, j[12] + string);
        this.logAndAudit((AuditDetailMessage)AssertionMessages.SCHEMA_VALIDATION_FAILED, new String[]{string}, throwable);
        policyEnforcementContext.setVariable(SchemaValidation.SCHEMA_FAILURE_VARIABLE, string);
    }

    private synchronized boolean a() {
        boolean bl2 = this.i;
        this.i = true;
        return bl2;
    }

    @Override
    public void close() {
        if (this.a()) {
            return;
        }
        if (this.d != null) {
            this.d.close();
        }
        if (this.h != null) {
            this.f.unregisterUri(this.h);
        }
        if (this.e != null) {
            this.f.unregisterSchema(this.e);
        }
    }

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

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[15];
                var3_1 = 0;
                var2_2 = "\t\u0011\u0015=RmA-\u0011\u0014;WgX)\u0011\u00037\\wX-_\u0013x\\m@$UG6Pv\u0015*TG<Pu[$^\u0006<Zf\u0015*T\u00049JqPhP\txlQyhR\b6KgM<\u0011\u00047JnQh_\b,\u001f`PhR\u0015=^vP,\u000bG\u0002r\u0011\r;R\u000f=Rcx)_\u0006?Zp\u008d\u001cY\u0002x[mV=\\\u00026K\"A'\u0011\u00119SkQ)E\u0002xRcQ-\u0011\u0012+Z\"Z.\u0011\t9RgF8P\u0004=L\"S'CG/WkV \u0011\u0010=\u001fjT>TG6P\"F+Y\u00025^q\u0015:T\u00001LvP:T\u0003t\u001fc[,\u0011\u00031[\"['EG1QaY=U\u0002xLa]-\\\u0006xjPy;\u0011\u00017M\"A T\u0014=\u001flT%T\u0014(^aP;\u0003rB\u0003\u001d8^\u000b1\\{\u000f)B\u0014=Mv\\'_]+\\jP%P\u00119S8F).\t\u0011\u0015=RmA-\u0011\u0014;WgX)\u0011\u00037\\wX-_\u0013x\\m@$UG6Pv\u0015*TG(^pF-U]xo\u001cY\u0002x[mV=\\\u00026K\"A'\u0011\u00119SkQ)E\u0002xVlV$D\u0003=[\"ThB\u00040ZoThU\u0002;ScG)E\u000e7Q\"E'X\t,VlRhP\u0013x^\"`\u001a}G,WcAhX\u0014xQmAhA\u0002*RkA<T\u0003x]{\u0015<Y\u0002xHj\\<T\u000b1Lv\f;R\u000f=Rc\u0015\u001dc+b\u001f&\u001d_\u0006:Sg\u0015<^G*ZvG!T\u0011=\u001fc\u0015;R\u000f=Rc\u0015,^\u0004-Rg[<\u000bG\u001f\t\u0011\u0015=Nw\\:T\u0003xrKx\r\u0011\u00179Mv\u0015?P\u0014xSmF<\u000bGQ\u001cY\u0002x[mV=\\\u00026K\"A'\u0011\u00119SkQ)E\u0002xVlV$D\u0003=[\"ThB\u00040ZoThU\u0002;ScG)E\u000e7Q\"E'X\t,VlRhP\u0013x^l\u0015!_\u00119SkQhd5\u0014\u0013>P\u000b1[cA!^\txYc\\$T\u0003b\u001f";
                var4_3 = "\t\u0011\u0015=RmA-\u0011\u0014;WgX)\u0011\u00037\\wX-_\u0013x\\m@$UG6Pv\u0015*TG<Pu[$^\u0006<Zf\u0015*T\u00049JqPhP\txlQyhR\b6KgM<\u0011\u00047JnQh_\b,\u001f`PhR\u0015=^vP,\u000bG\u0002r\u0011\r;R\u000f=Rcx)_\u0006?Zp\u008d\u001cY\u0002x[mV=\\\u00026K\"A'\u0011\u00119SkQ)E\u0002xRcQ-\u0011\u0012+Z\"Z.\u0011\t9RgF8P\u0004=L\"S'CG/WkV \u0011\u0010=\u001fjT>TG6P\"F+Y\u00025^q\u0015:T\u00001LvP:T\u0003t\u001fc[,\u0011\u00031[\"['EG1QaY=U\u0002xLa]-\\\u0006xjPy;\u0011\u00017M\"A T\u0014=\u001flT%T\u0014(^aP;\u0003rB\u0003\u001d8^\u000b1\\{\u000f)B\u0014=Mv\\'_]+\\jP%P\u00119S8F).\t\u0011\u0015=RmA-\u0011\u0014;WgX)\u0011\u00037\\wX-_\u0013x\\m@$UG6Pv\u0015*TG(^pF-U]xo\u001cY\u0002x[mV=\\\u00026K\"A'\u0011\u00119SkQ)E\u0002xVlV$D\u0003=[\"ThB\u00040ZoThU\u0002;ScG)E\u000e7Q\"E'X\t,VlRhP\u0013x^\"`\u001a}G,WcAhX\u0014xQmAhA\u0002*RkA<T\u0003x]{\u0015<Y\u0002xHj\\<T\u000b1Lv\f;R\u000f=Rc\u0015\u001dc+b\u001f&\u001d_\u0006:Sg\u0015<^G*ZvG!T\u0011=\u001fc\u0015;R\u000f=Rc\u0015,^\u0004-Rg[<\u000bG\u001f\t\u0011\u0015=Nw\\:T\u0003xrKx\r\u0011\u00179Mv\u0015?P\u0014xSmF<\u000bGQ\u001cY\u0002x[mV=\\\u00026K\"A'\u0011\u00119SkQ)E\u0002xVlV$D\u0003=[\"ThB\u00040ZoThU\u0002;ScG)E\u000e7Q\"E'X\t,VlRhP\u0013x^l\u0015!_\u00119SkQhd5\u0014\u0013>P\u000b1[cA!^\txYc\\$T\u0003b\u001f".length();
                var1_4 = 94;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerSchemaValidation.b(v0, ServerSchemaValidation.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "O\u001e^a\u0014/\u001aW\tA@\u0010;\u0010w\tNw<&\u0019m[DaU&\u0010v[Tw\u0001h\fw\u000b]}\u0007<\u001afU2V\u0013H2\u0011'\u001cw\u0016H|\u0001h\u000bm[[s\u0019!\u001bc\u000fH2\u0002)\f\"\u0015BfU?\u001an\u0017\u0000t\u001a:\u0012g\u001f\rJ8\u0004E\"";
                    var4_3 = "O\u001e^a\u0014/\u001aW\tA@\u0010;\u0010w\tNw<&\u0019m[DaU&\u0010v[Tw\u0001h\fw\u000b]}\u0007<\u001afU2V\u0013H2\u0011'\u001cw\u0016H|\u0001h\u000bm[[s\u0019!\u001bc\u000fH2\u0002)\f\"\u0015BfU?\u001an\u0017\u0000t\u001a:\u0012g\u001f\rJ8\u0004E\"".length();
                    var1_4 = 44;
                    var0_5 = -1;
lbl22:
                    // 2 sources

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

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

                ** continue;
            }
        }
        ServerSchemaValidation.j = var5;
    }

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

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String b(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 -> 92;
                    case 1 -> 37;
                    case 2 -> 115;
                    case 3 -> 76;
                    case 4 -> 43;
                    case 5 -> 22;
                    default -> 33;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

