/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.LicenseException
 *  com.l7tech.policy.assertion.Assertion
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.composite.CompositeAssertion
 *  com.l7tech.policy.variable.BuiltinVariables
 *  com.l7tech.policy.variable.PolicyVariableUtils
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.ResourceUtils
 *  com.l7tech.util.TimeSource
 *  jakarta.inject.Inject
 *  jakarta.inject.Named
 *  org.springframework.beans.factory.BeanFactory
 */
package com.l7tech.server.policy.assertion.composite;

import com.l7tech.gateway.common.LicenseException;
import com.l7tech.policy.assertion.Assertion;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.composite.CompositeAssertion;
import com.l7tech.policy.variable.BuiltinVariables;
import com.l7tech.policy.variable.PolicyVariableUtils;
import com.l7tech.server.NoopTelemetryMediator;
import com.l7tech.server.TelemetryMediator;
import com.l7tech.server.TelemetryMediatorFactory;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.message.metrics.GatewayMetricsUtils;
import com.l7tech.server.message.metrics.LatencyMetrics;
import com.l7tech.server.policy.ServerPolicyFactory;
import com.l7tech.server.policy.assertion.AbstractServerAssertion;
import com.l7tech.server.policy.assertion.AssertionStatusException;
import com.l7tech.server.policy.assertion.ServerAssertion;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.ResourceUtils;
import com.l7tech.util.TimeSource;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.BeanFactory;

public abstract class ServerCompositeAssertion<CT extends CompositeAssertion>
extends AbstractServerAssertion<CT> {
    private static final Logger d;
    private List<ServerAssertion> e;
    private final boolean[] f;
    protected final TimeSource timeSource;
    @Inject
    @Named(value="telemetryMediatorFactory")
    TelemetryMediatorFactory telemetryMediatorFactory;
    static final /* synthetic */ boolean g;
    private static final String[] o;

    /*
     * Loose catch block
     */
    public ServerCompositeAssertion(CT CT, BeanFactory beanFactory) throws PolicyAssertionException, LicenseException {
        super(CT);
        if (beanFactory == null) {
            String[] stringArray = o;
            throw new IllegalArgumentException(stringArray[0]);
        }
        ServerPolicyFactory serverPolicyFactory = (ServerPolicyFactory)beanFactory.getBean(o[2], ServerPolicyFactory.class);
        ArrayList<ServerAssertion> arrayList = new ArrayList<ServerAssertion>(CT.getChildren().size());
        this.timeSource = this.getTimeSource();
        this.f = new boolean[CT.getChildren().size()];
        try {
            Assertion assertion = null;
            int n2 = 0;
            Iterator iterator = CT.children();
            while (iterator.hasNext()) {
                ServerAssertion serverAssertion;
                block20: {
                    Assertion assertion2;
                    block19: {
                        assertion2 = (Assertion)iterator.next();
                        this.f[n2] = false;
                        if (assertion == null) break block19;
                        try {
                            block21: {
                                if (!PolicyVariableUtils.usesAnyVariable((Assertion)assertion2, (String[])new String[]{BuiltinVariables.ASSERTION_LATENCY_MS, BuiltinVariables.ASSERTION_LATENCY_NS})) break block19;
                                break block21;
                                catch (Exception exception) {
                                    throw ServerCompositeAssertion.a(exception);
                                }
                            }
                            this.f[n2 - 1] = true;
                        }
                        catch (Exception exception) {
                            throw ServerCompositeAssertion.a(exception);
                        }
                    }
                    assertion = assertion2;
                    ++n2;
                    serverAssertion = serverPolicyFactory.compileSubtree(assertion2);
                    if (g) break block20;
                    try {
                        block22: {
                            if (serverAssertion != null) break block20;
                            break block22;
                            catch (Exception exception) {
                                throw ServerCompositeAssertion.a(exception);
                            }
                        }
                        throw new AssertionError();
                    }
                    catch (Exception exception) {
                        throw ServerCompositeAssertion.a(exception);
                    }
                }
                arrayList.add(serverAssertion);
            }
        }
        catch (Exception exception) {
            for (ServerAssertion serverAssertion : arrayList) {
                ResourceUtils.closeQuietly((Closeable)serverAssertion);
            }
            try {
                if (exception instanceof PolicyAssertionException) {
                    throw (PolicyAssertionException)((Object)exception);
                }
            }
            catch (Exception exception2) {
                throw ServerCompositeAssertion.a(exception2);
            }
            try {
                if (exception instanceof LicenseException) {
                    throw (LicenseException)((Object)exception);
                }
            }
            catch (Exception exception3) {
                throw ServerCompositeAssertion.a(exception3);
            }
            throw ExceptionUtils.wrap((Throwable)exception);
        }
        this.e = Collections.unmodifiableList(arrayList);
    }

    protected TimeSource getTimeSource() {
        return new TimeSource();
    }

    public List<ServerAssertion> getChildren() {
        return this.e;
    }

    public String toString() {
        return "<" + this.getClass().getName() + o[1] + String.valueOf(this.e) + ">";
    }

    protected void seenAssertionStatus(PolicyEnforcementContext policyEnforcementContext, AssertionStatus assertionStatus) {
        try {
            if (policyEnforcementContext != null) {
                policyEnforcementContext.addSeenAssertionStatus(assertionStatus);
            }
        }
        catch (IllegalArgumentException illegalArgumentException) {
            throw ServerCompositeAssertion.a(illegalArgumentException);
        }
    }

    protected void rollbackDeferredAssertions(PolicyEnforcementContext policyEnforcementContext) {
        try {
            if (policyEnforcementContext != null) {
                policyEnforcementContext.removeDeferredAssertion(this);
            }
        }
        catch (IllegalArgumentException illegalArgumentException) {
            throw ServerCompositeAssertion.a(illegalArgumentException);
        }
        for (ServerAssertion serverAssertion : this.e) {
            try {
                if (serverAssertion instanceof ServerCompositeAssertion) {
                    ((ServerCompositeAssertion)serverAssertion).rollbackDeferredAssertions(policyEnforcementContext);
                }
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw ServerCompositeAssertion.a(illegalArgumentException);
            }
            try {
                if (policyEnforcementContext == null) continue;
                policyEnforcementContext.removeDeferredAssertion(serverAssertion);
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw ServerCompositeAssertion.a(illegalArgumentException);
            }
        }
    }

    protected final AssertionStatus iterateChildren(PolicyEnforcementContext policyEnforcementContext, AssertionResultListener assertionResultListener) throws IOException, PolicyAssertionException {
        TelemetryMediator telemetryMediator;
        List<ServerAssertion> list = this.getChildren();
        AssertionStatus assertionStatus = AssertionStatus.NONE;
        try {
            telemetryMediator = this.telemetryMediatorFactory != null ? this.telemetryMediatorFactory.createTelemetryMediator() : NoopTelemetryMediator.INSTANCE;
        }
        catch (AssertionStatusException assertionStatusException) {
            throw ServerCompositeAssertion.a(assertionStatusException);
        }
        NoopTelemetryMediator noopTelemetryMediator = telemetryMediator;
        int n2 = 0;
        long l2 = 0L;
        for (ServerAssertion serverAssertion : list) {
            long l3;
            policyEnforcementContext.assertionStarting(serverAssertion);
            if (this.f[n2]) {
                l2 = this.timeSource.nanoTime();
            }
            long l4 = this.timeSource.currentTimeMillis();
            noopTelemetryMediator.getStartAssertionEvent((Assertion)serverAssertion.getAssertion(), policyEnforcementContext);
            try {
                assertionStatus = serverAssertion.checkRequest(policyEnforcementContext);
                noopTelemetryMediator.getStatusMsg(assertionStatus);
            }
            catch (AssertionStatusException assertionStatusException) {
                noopTelemetryMediator.getStatusException(assertionStatusException);
                assertionStatus = assertionStatusException.getAssertionStatus();
            }
            catch (Throwable throwable) {
                noopTelemetryMediator.getThrowableInfo(throwable);
                GatewayMetricsUtils.publishAssertionFinish(policyEnforcementContext, serverAssertion, new LatencyMetrics(l4, this.timeSource.currentTimeMillis()));
                throw throwable;
            }
            finally {
                l3 = this.timeSource.currentTimeMillis();
                noopTelemetryMediator.getEndAssertionEvent((Assertion)serverAssertion.getAssertion(), policyEnforcementContext);
            }
            try {
                if (this.f[n2]) {
                    policyEnforcementContext.setAssertionLatencyNanos(this.timeSource.nanoTime() - l2);
                }
            }
            catch (AssertionStatusException assertionStatusException) {
                throw ServerCompositeAssertion.a(assertionStatusException);
            }
            ++n2;
            policyEnforcementContext.assertionFinished(serverAssertion, assertionStatus, new LatencyMetrics(l4, l3));
            if (assertionResultListener == null) continue;
            boolean bl2 = assertionResultListener.assertionFinished(policyEnforcementContext, assertionStatus);
            try {
                if (bl2) continue;
                return assertionStatus;
            }
            catch (AssertionStatusException assertionStatusException) {
                throw ServerCompositeAssertion.a(assertionStatusException);
            }
        }
        return assertionStatus;
    }

    @Override
    public void close() {
        for (ServerAssertion serverAssertion : this.e) {
            try {
                serverAssertion.close();
            }
            catch (Exception exception) {
                d.log(Level.WARNING, o[3], exception);
            }
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block12: {
            block11: {
                var5 = new String[4];
                var3_1 = 0;
                var2_2 = "JXG2b\u0012crYAsW\u000b|p\u0010a}M\u0016vfD\u0002{PBa{AW{Q\u0007w\n>SJ{O\u0006a{^\u001f";
                var4_3 = "JXG2b\u0012crYAsW\u000b|p\u0010a}M\u0016vfD\u0002{PBa{AW{Q\u0007w\n>SJ{O\u0006a{^\u001f".length();
                var1_4 = 35;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerCompositeAssertion.b(v0, ServerCompositeAssertion.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = " \u0011\u00005\u000eU\u001b1\u001d\u00183\u001fU%\u0013\u0016\u00050\t\f<#\r\t.\u0019E2>^\b5\t\f3?\nL?\u0001C.5^\u000f0\bM3<\u0007";
                    var4_3 = " \u0011\u00005\u000eU\u001b1\u001d\u00183\u001fU%\u0013\u0016\u00050\t\f<#\r\t.\u0019E2>^\b5\t\f3?\nL?\u0001C.5^\u000f0\bM3<\u0007".length();
                    var1_4 = 13;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 18;
                        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++] = ServerCompositeAssertion.b(v0, ServerCompositeAssertion.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;
            }
        }
        ServerCompositeAssertion.o = var5;
        try {
            v5 = ServerCompositeAssertion.class.desiredAssertionStatus() == false;
        }
        catch (IllegalArgumentException v6) {
            throw ServerCompositeAssertion.a(v6);
        }
        ServerCompositeAssertion.g = v5;
        ServerCompositeAssertion.d = Logger.getLogger(ServerCompositeAssertion.class.getName());
    }

    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] ^ 0x4F);
        }
        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 -> 66;
                    case 1 -> 108;
                    case 2 -> 126;
                    case 3 -> 78;
                    case 4 -> 127;
                    case 5 -> 62;
                    default -> 79;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }

    protected static interface AssertionResultListener {
        public boolean assertionFinished(PolicyEnforcementContext var1, AssertionStatus var2);
    }
}

