/*
 * 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.gateway.common.audit.AuditFactory
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.MapValueAssertion
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.variable.VariableNotSettableException
 *  com.l7tech.util.NameValuePair
 *  org.jetbrains.annotations.NotNull
 *  org.jetbrains.annotations.Nullable
 */
package com.l7tech.server.policy.assertion;

import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.gateway.common.audit.AuditFactory;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.MapValueAssertion;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.variable.VariableNotSettableException;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractServerAssertion;
import com.l7tech.server.policy.assertion.AssertionStatusException;
import com.l7tech.server.policy.assertion.ad;
import com.l7tech.server.policy.variable.ExpandVariables;
import com.l7tech.util.NameValuePair;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ServerMapValueAssertion
extends AbstractServerAssertion<MapValueAssertion> {
    private static final int d = 40;
    private final List<ad> e;
    private final String[] f;
    private static final String[] g;

    public ServerMapValueAssertion(@NotNull MapValueAssertion mapValueAssertion) {
        if (mapValueAssertion == null) {
            ServerMapValueAssertion.b(0);
        }
        this(mapValueAssertion, (AuditFactory)null);
    }

    public ServerMapValueAssertion(@NotNull MapValueAssertion mapValueAssertion, @Nullable AuditFactory auditFactory) {
        if (mapValueAssertion == null) {
            ServerMapValueAssertion.b(1);
        }
        super(mapValueAssertion, auditFactory);
        ArrayList<ad> arrayList = new ArrayList<ad>();
        if (mapValueAssertion.getMappings() != null) {
            for (NameValuePair nameValuePair : mapValueAssertion.getMappings()) {
                arrayList.add(new ad(nameValuePair));
            }
        }
        this.e = arrayList;
        this.f = mapValueAssertion.getVariablesUsed();
    }

    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        try {
            if (((MapValueAssertion)this.assertion).getInputExpr() == null) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.ASSERTION_MISCONFIGURED, g[0]);
                return AssertionStatus.SERVER_ERROR;
            }
        }
        catch (IOException iOException) {
            throw ServerMapValueAssertion.a(iOException);
        }
        Map<String, Object> map = policyEnforcementContext.getVariableMap(this.f, this.getAudit());
        String string = ExpandVariables.process(((MapValueAssertion)this.assertion).getInputExpr(), map, this.getAudit());
        for (ad ad2 : this.e) {
            Pattern pattern = ad2.a(map, this.getAudit());
            Matcher matcher = pattern.matcher(string);
            if (matcher.find()) {
                int n2 = matcher.groupCount();
                for (int i2 = 0; i2 <= n2; ++i2) {
                    String string2 = matcher.group(i2);
                    map.put(String.valueOf(i2), string2);
                }
                String string3 = ExpandVariables.process(ad2.b, map, this.getAudit());
                this.a(policyEnforcementContext, ((MapValueAssertion)this.assertion).getOutputVar(), string3);
                this.getAudit().logAndAudit((AuditDetailMessage)AssertionMessages.MAP_VALUE_PATTERN_MATCHED, new String[]{pattern.toString()});
                return AssertionStatus.NONE;
            }
            this.getAudit().logAndAudit((AuditDetailMessage)AssertionMessages.MAP_VALUE_PATTERN_NOT_MATCHED, new String[]{pattern.toString()});
        }
        this.getAudit().logAndAudit((AuditDetailMessage)AssertionMessages.MAP_VALUE_NO_PATTERNS_MATCHED);
        return AssertionStatus.FAILED;
    }

    private void a(PolicyEnforcementContext policyEnforcementContext, String string, Object object) throws AssertionStatusException {
        try {
            policyEnforcementContext.setVariable(string, object);
        }
        catch (VariableNotSettableException variableNotSettableException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.VARIABLE_NOTSET, variableNotSettableException.getVariable());
            throw new AssertionStatusException(AssertionStatus.SERVER_ERROR, (Throwable)variableNotSettableException);
        }
    }

    private static /* synthetic */ void b(int n2) {
        String[] stringArray = g;
        throw new IllegalArgumentException(String.format(stringArray[1], stringArray[4], stringArray[3], stringArray[2]));
    }

    private static IOException a(IOException iOException) {
        return iOException;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[5];
                var3_1 = 0;
                var2_2 = "T \u0004Z+\u0003\u0007noAK5\u0001\u0017i<M\\+S\u001bioG\\+\u0015\u001b}:VV!]>[=CF(\u0016\u001cnoB\\7S2T P}0\u001f\u001e:?EA$\u001e\u0017n*V\u0013bV\u0001=oKUeV\u00014jW\u0013(\u0006\u0001noJ\\1S\u0010\u007foJF)\u001f\u0006&&JZ1M";
                var4_3 = "T \u0004Z+\u0003\u0007noAK5\u0001\u0017i<M\\+S\u001bioG\\+\u0015\u001b}:VV!]>[=CF(\u0016\u001cnoB\\7S2T P}0\u001f\u001e:?EA$\u001e\u0017n*V\u0013bV\u0001=oKUeV\u00014jW\u0013(\u0006\u0001noJ\\1S\u0010\u007foJF)\u001f\u0006&&JZ1M".length();
                var1_4 = 34;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerMapValueAssertion.b(v0, ServerMapValueAssertion.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u0007^7bW:x\u0001R2bHh~\u0012T(bKb`\rR#bZ~\u007f\u0001C.$Tc#7T(;^\u007fA\u0005A\f,Wxi%B)(Iye\u000b_\t\u0005B)(Iye\u000b_";
                    var4_3 = "\u0007^7bW:x\u0001R2bHh~\u0012T(bKb`\rR#bZ~\u007f\u0001C.$Tc#7T(;^\u007fA\u0005A\f,Wxi%B)(Iye\u000b_\t\u0005B)(Iye\u000b_".length();
                    var1_4 = 58;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 27;
                        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++] = ServerMapValueAssertion.b(v0, ServerMapValueAssertion.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;
            }
        }
        ServerMapValueAssertion.g = var5;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x17);
        }
        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 -> 127;
                    case 1 -> 42;
                    case 2 -> 65;
                    case 3 -> 86;
                    case 4 -> 32;
                    case 5 -> 22;
                    default -> 23;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

