/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.LicenseException
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.policy.assertion.Assertion
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.JdbcConnectionable
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.composite.CompositeAssertion
 *  com.l7tech.policy.assertion.composite.TransactionAssertion
 *  com.l7tech.util.ExceptionUtils
 *  jakarta.inject.Inject
 *  org.springframework.beans.factory.BeanFactory
 *  org.springframework.context.ApplicationContext
 *  org.springframework.dao.DataAccessException
 *  org.springframework.jdbc.datasource.DataSourceTransactionManager
 *  org.springframework.transaction.PlatformTransactionManager
 *  org.springframework.transaction.TransactionException
 *  org.springframework.transaction.support.TransactionCallback
 *  org.springframework.transaction.support.TransactionTemplate
 */
package com.l7tech.server.policy.assertion.composite;

import com.l7tech.gateway.common.LicenseException;
import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.policy.assertion.Assertion;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.JdbcConnectionable;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.composite.CompositeAssertion;
import com.l7tech.policy.assertion.composite.TransactionAssertion;
import com.l7tech.server.jdbc.JdbcConnectionPoolManager;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.composite.ServerCompositeAssertion;
import com.l7tech.server.policy.assertion.composite.f;
import com.l7tech.server.policy.assertion.composite.g;
import com.l7tech.server.policy.variable.ExpandVariables;
import com.l7tech.util.ExceptionUtils;
import jakarta.inject.Inject;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Iterator;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

public class ServerTransactionAssertion
extends ServerCompositeAssertion<TransactionAssertion> {
    @Inject
    private JdbcConnectionPoolManager jdbcConnectionPoolManager;
    private final String[] h;
    private final String i;
    private final ServerCompositeAssertion.AssertionResultListener j;
    private static final String[] k;

    /*
     * Unable to fully structure code
     */
    public ServerTransactionAssertion(TransactionAssertion var1_1, ApplicationContext var2_2) throws PolicyAssertionException, LicenseException {
        block3: {
            block2: {
                super(var1_1, (BeanFactory)var2_2);
                this.j = new f(this);
                this.h = ((TransactionAssertion)this.assertion).getVariablesUsed();
                if (((TransactionAssertion)this.assertion).getConnectionName() == null) ** GOTO lbl7
                try {
                    if (!((TransactionAssertion)this.assertion).getConnectionName().isEmpty()) break block2;
lbl7:
                    // 2 sources

                    this.i = this.a((Assertion)var1_1);
                    break block3;
                }
                catch (PolicyAssertionException v0) {
                    throw ServerTransactionAssertion.b((Exception)v0);
                }
            }
            this.i = null;
        }
    }

    /*
     * Loose catch block
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        Object object;
        String string;
        block14: {
            block13: {
                string = this.i;
                try {
                    if (((TransactionAssertion)this.assertion).getConnectionName() == null || ((TransactionAssertion)this.assertion).getConnectionName().isEmpty()) break block13;
                }
                catch (SQLException sQLException) {
                    throw ServerTransactionAssertion.b(sQLException);
                }
                object = policyEnforcementContext.getVariableMap(this.h, this.getAudit());
                string = ExpandVariables.process(((TransactionAssertion)this.assertion).getConnectionName(), object, this.getAudit());
            }
            if (string == null) return this.iterateChildren(policyEnforcementContext, this.j);
            try {
                if (!string.isEmpty()) break block14;
                return this.iterateChildren(policyEnforcementContext, this.j);
                catch (SQLException sQLException) {
                    throw ServerTransactionAssertion.b(sQLException);
                }
            }
            catch (SQLException sQLException) {
                throw ServerTransactionAssertion.b(sQLException);
            }
        }
        try {
            object = this.jdbcConnectionPoolManager.getDataSource(string);
        }
        catch (SQLException | NamingException exception) {
            String[] stringArray = new String[1];
            String[] stringArray2 = k;
            stringArray[0] = stringArray2[0] + string + stringArray2[4] + ExceptionUtils.getMessage((Throwable)exception);
            this.logAndAudit((AuditDetailMessage)AssertionMessages.JDBC_CONNECTION_ERROR, stringArray);
            return AssertionStatus.SERVER_ERROR;
        }
        try {
            if (object == null) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.JDBC_CONNECTION_ERROR, k[2] + string + k[3]);
                return AssertionStatus.SERVER_ERROR;
            }
        }
        catch (SQLException sQLException) {
            throw ServerTransactionAssertion.b(sQLException);
        }
        try {
            TransactionTemplate transactionTemplate = new TransactionTemplate((PlatformTransactionManager)new DataSourceTransactionManager((DataSource)object));
            return (AssertionStatus)transactionTemplate.execute((TransactionCallback)new g(this, policyEnforcementContext));
        }
        catch (DataAccessException | TransactionException throwable) {
            String string2 = ExceptionUtils.getMessage((Throwable)throwable);
            String[] stringArray = new String[1];
            String[] stringArray3 = k;
            stringArray[0] = stringArray3[2] + string + stringArray3[1] + string2;
            this.logAndAudit((AuditDetailMessage)AssertionMessages.JDBC_CONNECTION_ERROR, stringArray);
            return AssertionStatus.FAILED;
        }
    }

    private String a(Assertion assertion) {
        Iterator iterator = assertion.preorderIterator();
        while (iterator.hasNext()) {
            Assertion assertion2 = (Assertion)iterator.next();
            if (assertion2 == null || !assertion2.isEnabled()) continue;
            try {
                if (assertion.equals((Object)assertion2)) {
                    continue;
                }
            }
            catch (TransactionException transactionException) {
                throw ServerTransactionAssertion.b((Exception)((Object)transactionException));
            }
            try {
                if (assertion2 instanceof JdbcConnectionable) {
                    return ((JdbcConnectionable)assertion2).getConnectionName();
                }
            }
            catch (TransactionException transactionException) {
                throw ServerTransactionAssertion.b((Exception)((Object)transactionException));
            }
            if (!(assertion2 instanceof CompositeAssertion)) continue;
            String string = this.a(assertion2);
            try {
                if (string == null) continue;
                return string;
            }
            catch (TransactionException transactionException) {
                throw ServerTransactionAssertion.b((Exception)((Object)transactionException));
            }
        }
        return null;
    }

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

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[5];
                var3_1 = 0;
                var2_2 = "\u0015\rO.\b1R\u001f\rO`\u00033K\u0013B\u0003\u0017TX\u0001\u0014\u001f3H\u0005\u0003B4\u0004=HV\u0004@)\u00017BLB\u0011\u0015\rO.\b1R\u001f\rO`\u00033K\u0013B\u0003";
                var4_3 = "\u0015\rO.\b1R\u001f\rO`\u00033K\u0013B\u0003\u0017TX\u0001\u0014\u001f3H\u0005\u0003B4\u0004=HV\u0004@)\u00017BLB\u0011\u0015\rO.\b1R\u001f\rO`\u00033K\u0013B\u0003".length();
                var1_4 = 17;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerTransactionAssertion.c(v0, ServerTransactionAssertion.b(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "IE\u001c\u00174\rxK\u001cS3\u001e*X\u001f\u0016S3P+Z\u001f\u001e\u001c.\u001f:I\b\u001a\u001c3\u001f;\u001b\r\u0010I3\u0014\u0003IE\u001c";
                    var4_3 = "IE\u001c\u00174\rxK\u001cS3\u001e*X\u001f\u0016S3P+Z\u001f\u001e\u001c.\u001f:I\b\u001a\u001c3\u001f;\u001b\r\u0010I3\u0014\u0003IE\u001c".length();
                    var1_4 = 40;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 61;
                        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++] = ServerTransactionAssertion.c(v0, ServerTransactionAssertion.b(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;
            }
        }
        ServerTransactionAssertion.k = var5;
    }

    private static char[] b(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 6);
        }
        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 -> 86;
                    case 1 -> 66;
                    case 2 -> 1;
                    case 3 -> 96;
                    case 4 -> 77;
                    case 5 -> 114;
                    default -> 6;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

