/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.AuditRecord
 *  com.l7tech.gateway.common.audit.SystemAuditRecord
 *  com.l7tech.gateway.common.service.PublishedService
 *  com.l7tech.policy.assertion.Assertion
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.JdbcConnectionable
 *  com.l7tech.policy.assertion.composite.CompositeAssertion
 *  com.l7tech.util.Config
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.ResourceUtils
 *  org.springframework.context.ApplicationEvent
 *  org.springframework.jdbc.datasource.DataSourceTransactionManager
 *  org.springframework.transaction.PlatformTransactionManager
 *  org.springframework.transaction.support.TransactionCallback
 *  org.springframework.transaction.support.TransactionTemplate
 */
package com.l7tech.server.audit;

import com.l7tech.gateway.common.audit.AuditRecord;
import com.l7tech.gateway.common.audit.SystemAuditRecord;
import com.l7tech.gateway.common.service.PublishedService;
import com.l7tech.policy.assertion.Assertion;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.JdbcConnectionable;
import com.l7tech.policy.assertion.composite.CompositeAssertion;
import com.l7tech.server.audit.AuditContextFactory;
import com.l7tech.server.audit.AuditSinkPolicyEnforcementContext;
import com.l7tech.server.audit.ab;
import com.l7tech.server.audit.ac;
import com.l7tech.server.event.PolicyCacheEvent;
import com.l7tech.server.jdbc.JdbcConnectionPoolManager;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.message.PolicyEnforcementContextFactory;
import com.l7tech.server.policy.PolicyCache;
import com.l7tech.server.policy.PolicyManager;
import com.l7tech.server.policy.ServerPolicyHandle;
import com.l7tech.server.util.PostStartupApplicationListener;
import com.l7tech.util.Config;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.ResourceUtils;
import java.io.Closeable;
import java.io.IOException;
import java.util.Iterator;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.springframework.context.ApplicationEvent;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

public class AuditPolicyEvaluator
implements PostStartupApplicationListener {
    private static final Logger a;
    private final Config b;
    private final PolicyCache c;
    private final JdbcConnectionPoolManager d;
    private final AtomicBoolean e = new AtomicBoolean(false);
    private final Queue<SystemAuditRecord> f = new ConcurrentLinkedQueue<SystemAuditRecord>();
    private static final String[] g;

    public AuditPolicyEvaluator(Config config, PolicyCache policyCache, PolicyManager policyManager, JdbcConnectionPoolManager jdbcConnectionPoolManager) {
        this.b = config;
        this.c = policyCache;
        this.d = jdbcConnectionPoolManager;
    }

    private String a() {
        return this.b.getProperty(g[4]);
    }

    public AssertionStatus outputRecordToPolicyAuditSink(AuditRecord auditRecord, PolicyEnforcementContext policyEnforcementContext) {
        try {
            return AuditContextFactory.doWithCustomAuditContext(AuditContextFactory.createLogOnlyAuditContext(), new ab(this, auditRecord, policyEnforcementContext));
        }
        catch (Exception exception) {
            a.log(Level.WARNING, g[3] + ExceptionUtils.getMessage((Throwable)exception), exception);
            return AssertionStatus.SERVER_ERROR;
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private AssertionStatus a(AuditRecord auditRecord, PolicyEnforcementContext policyEnforcementContext) {
        AssertionStatus assertionStatus;
        AuditSinkPolicyEnforcementContext auditSinkPolicyEnforcementContext = null;
        try {
            String string;
            block15: {
                block16: {
                    block14: {
                        string = this.a();
                        try {
                            if (string != null && string.trim().length() >= 1) break block14;
                        }
                        catch (Exception exception) {
                            throw AuditPolicyEvaluator.a(exception);
                        }
                        String[] stringArray = g;
                        a.log(Level.FINEST, stringArray[12]);
                        AssertionStatus assertionStatus2 = null;
                        return assertionStatus2;
                    }
                    try {
                        if (this.e.get()) break block15;
                        if (!(auditRecord instanceof SystemAuditRecord)) break block16;
                    }
                    catch (Exception exception) {
                        throw AuditPolicyEvaluator.a(exception);
                    }
                    SystemAuditRecord systemAuditRecord = (SystemAuditRecord)auditRecord;
                    this.f.add(systemAuditRecord);
                    AssertionStatus assertionStatus3 = AssertionStatus.NONE;
                    ResourceUtils.closeQuietly((Closeable)auditSinkPolicyEnforcementContext);
                    return assertionStatus3;
                }
                String[] stringArray = g;
                throw new IllegalStateException(stringArray[8] + auditRecord.getClass().getName() + stringArray[5] + auditRecord.getMessage());
            }
            auditSinkPolicyEnforcementContext = new AuditSinkPolicyEnforcementContext(auditRecord, PolicyEnforcementContextFactory.createPolicyEnforcementContext(null, null), policyEnforcementContext);
            auditSinkPolicyEnforcementContext.setAuditLevel(Level.INFO);
            PublishedService publishedService = new PublishedService();
            String[] stringArray = g;
            publishedService.setName(stringArray[1]);
            publishedService.setSoap(false);
            auditSinkPolicyEnforcementContext.setService(publishedService);
            AssertionStatus assertionStatus4 = this.a(string, (PolicyEnforcementContext)auditSinkPolicyEnforcementContext);
            try {
                if (!AssertionStatus.NONE.equals(assertionStatus4)) {
                    a.log(Level.WARNING, stringArray[10] + String.valueOf(assertionStatus4));
                }
            }
            catch (Exception exception) {
                throw AuditPolicyEvaluator.a(exception);
            }
            assertionStatus = assertionStatus4;
            ResourceUtils.closeQuietly((Closeable)auditSinkPolicyEnforcementContext);
        }
        catch (Exception exception) {
            a.log(Level.WARNING, g[13] + ExceptionUtils.getMessage((Throwable)exception), ExceptionUtils.getDebugException((Throwable)exception));
            AssertionStatus assertionStatus5 = AssertionStatus.SERVER_ERROR;
            return assertionStatus5;
        }
        finally {
            ResourceUtils.closeQuietly(auditSinkPolicyEnforcementContext);
        }
        return assertionStatus;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private AssertionStatus a(String string, PolicyEnforcementContext policyEnforcementContext) throws Exception {
        ServerPolicyHandle serverPolicyHandle = this.c.getServerPolicy(string);
        try {
            if (serverPolicyHandle == null) {
                a.log(Level.WARNING, g[6], string);
                return AssertionStatus.SERVER_ERROR;
            }
        }
        catch (NamingException namingException) {
            throw AuditPolicyEvaluator.a(namingException);
        }
        DataSource dataSource = null;
        String string2 = "";
        try {
            Assertion assertion;
            try {
                assertion = serverPolicyHandle.getPolicyAssertion();
                try {
                    String string3 = string2 = assertion != null ? this.a(assertion) : null;
                }
                catch (NamingException namingException) {
                    throw AuditPolicyEvaluator.a(namingException);
                }
                if (string2 != null) {
                    dataSource = this.d.getDataSource(string2);
                }
            }
            catch (NamingException namingException) {
                String[] stringArray = g;
                a.warning(stringArray[7] + string2);
            }
            catch (IOException iOException) {
                a.warning(g[9] + iOException.getMessage());
            }
            if (dataSource != null) {
                assertion = new TransactionTemplate((PlatformTransactionManager)new DataSourceTransactionManager(dataSource));
                AssertionStatus assertionStatus = (AssertionStatus)assertion.execute((TransactionCallback)new ac(this, serverPolicyHandle, policyEnforcementContext));
                return assertionStatus;
            }
            assertion = serverPolicyHandle.checkRequest(policyEnforcementContext);
            return assertion;
        }
        finally {
            ResourceUtils.closeQuietly((Closeable)serverPolicyHandle);
        }
    }

    /*
     * Loose catch block
     */
    private void b() {
        try {
            if (this.f.isEmpty()) {
                return;
            }
        }
        catch (Throwable throwable) {
            throw AuditPolicyEvaluator.a(throwable);
        }
        String[] stringArray = g;
        a.info(stringArray[14]);
        while (!this.f.isEmpty()) {
            SystemAuditRecord systemAuditRecord = this.f.remove();
            try {
                try {
                    if (a.isLoggable(Level.FINE)) {
                        a.fine(g[17] + String.valueOf(systemAuditRecord));
                    }
                }
                catch (Throwable throwable) {
                    throw AuditPolicyEvaluator.a(throwable);
                }
                AssertionStatus assertionStatus = this.outputRecordToPolicyAuditSink((AuditRecord)systemAuditRecord, null);
                if (assertionStatus == null) continue;
                try {
                    block13: {
                        if (AssertionStatus.NONE.equals(assertionStatus)) continue;
                        break block13;
                        catch (Throwable throwable) {
                            throw AuditPolicyEvaluator.a(throwable);
                        }
                    }
                    a.log(Level.SEVERE, g[15] + assertionStatus.getNumeric() + g[2] + String.valueOf(systemAuditRecord));
                }
                catch (Throwable throwable) {
                    throw AuditPolicyEvaluator.a(throwable);
                }
            }
            catch (Throwable throwable) {
                stringArray = g;
                a.log(Level.SEVERE, stringArray[11] + String.valueOf(systemAuditRecord) + stringArray[0] + ExceptionUtils.getMessage((Throwable)throwable), throwable);
            }
        }
    }

    private void c() {
        block4: {
            block5: {
                try {
                    try {
                        if (this.e.getAndSet(true)) break block4;
                        if (this.c.isStarted()) break block5;
                    }
                    catch (IllegalStateException illegalStateException) {
                        throw AuditPolicyEvaluator.a(illegalStateException);
                    }
                    throw new IllegalStateException(g[16]);
                }
                catch (IllegalStateException illegalStateException) {
                    throw AuditPolicyEvaluator.a(illegalStateException);
                }
            }
            this.b();
        }
    }

    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 (IllegalStateException illegalStateException) {
                throw AuditPolicyEvaluator.a(illegalStateException);
            }
            try {
                if (assertion2 instanceof JdbcConnectionable) {
                    return ((JdbcConnectionable)assertion2).getConnectionName();
                }
            }
            catch (IllegalStateException illegalStateException) {
                throw AuditPolicyEvaluator.a(illegalStateException);
            }
            if (!(assertion2 instanceof CompositeAssertion)) continue;
            String string = this.a(assertion2);
            try {
                if (string == null) continue;
                return string;
            }
            catch (IllegalStateException illegalStateException) {
                throw AuditPolicyEvaluator.a(illegalStateException);
            }
        }
        return null;
    }

    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        try {
            if (applicationEvent instanceof PolicyCacheEvent.Started) {
                this.c();
            }
        }
        catch (IllegalStateException illegalStateException) {
            throw AuditPolicyEvaluator.a(illegalStateException);
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[18];
                var3_1 = 0;
                var2_2 = "5\t+T`S?ss!nE\u001d*ce&{\tN\"xjo\u007fFQ\"uxo\u007fZX>rnb|LO=\u007fb*R9/^U\"zdo|HK\"xfo|]\\9bt?/ZD8bd\"/LK.xuo{F\u001d*ce&{\tN\"xjo\u007fFQ\"uxu/%IHT'seo{F\u001d.nd,z]Xkwt+f]\u001d8\u007fo$/YR'\u007fb65\t\u0016n\\Y\"b/<fGVefn#fJDeqt&k\u00025\tuZG\\)zdo{F\u001d*ub*|Z\u001d(yo)fNH9seon\\Y\"b!<fGVkfn#fJDk;,oaF\u001d;ym&lP\u001d<\u007fu'/nh\u0002R!4?T\u001d\"e!?}LN.xuofG\u001d;ym&lP\u001d(wb'j\t\u0015\"xw.c@Ykfn#fJDt?*ZG\\)zdo{F\u001d,suoeM_(6b aGX(bh a\tY*b`<`\\O(s;oHN\tS$x,\u001cvZI.{!.zMT?6s*lFO/6v.|\tZ.xd=n]X/6c*iFO.6C `]m9yb*|Z\u001d<wro|]\\9bd+5\t&ZG\\)zdo{F\u001d9su=fLK.6`:k@Ikeh!d\tM$zh,v\u0013\u001d5N\\Y\"b!<fGVkfn#fJDkun\"\u007fEX?seox@I#6`<|LO?\u007fn!/ZI*bt</F[k:ZG\\)zdo{F\u001d8ww*/ZI*du:\u007f\tN2eu*b\tX=so;/]Rkwt+f]\u001d8\u007fo$/YR'\u007fb65\t\"AF\u001d*ce&{\tN\"xjo\u007fFQ\"uxofZ\u001d(yo)fNH9se%IHT'seo{F\u001d.nd,z]Xkwt+f]\u001d8\u007fo$/YR'\u007fb65\t9\\LS/\u007fo(/ZI*du:\u007f\tN2eu*b\t\\>rh;/[X(ys+|\tI$6`:k@Ikeh!d\tM$zh,v\"N\\Y\"b!<fGVkfn#fJDkdd;z[S.r!<{HI>e!";
                var4_3 = "5\t+T`S?ss!nE\u001d*ce&{\tN\"xjo\u007fFQ\"uxo\u007fZX>rnb|LO=\u007fb*R9/^U\"zdo|HK\"xfo|]\\9bt?/ZD8bd\"/LK.xuo{F\u001d*ce&{\tN\"xjo\u007fFQ\"uxu/%IHT'seo{F\u001d.nd,z]Xkwt+f]\u001d8\u007fo$/YR'\u007fb65\t\u0016n\\Y\"b/<fGVefn#fJDeqt&k\u00025\tuZG\\)zdo{F\u001d*ub*|Z\u001d(yo)fNH9seon\\Y\"b!<fGVkfn#fJDk;,oaF\u001d;ym&lP\u001d<\u007fu'/nh\u0002R!4?T\u001d\"e!?}LN.xuofG\u001d;ym&lP\u001d(wb'j\t\u0015\"xw.c@Ykfn#fJDt?*ZG\\)zdo{F\u001d,suoeM_(6b aGX(bh a\tY*b`<`\\O(s;oHN\tS$x,\u001cvZI.{!.zMT?6s*lFO/6v.|\tZ.xd=n]X/6c*iFO.6C `]m9yb*|Z\u001d<wro|]\\9bd+5\t&ZG\\)zdo{F\u001d9su=fLK.6`:k@Ikeh!d\tM$zh,v\u0013\u001d5N\\Y\"b!<fGVkfn#fJDkun\"\u007fEX?seox@I#6`<|LO?\u007fn!/ZI*bt</F[k:ZG\\)zdo{F\u001d8ww*/ZI*du:\u007f\tN2eu*b\tX=so;/]Rkwt+f]\u001d8\u007fo$/YR'\u007fb65\t\"AF\u001d*ce&{\tN\"xjo\u007fFQ\"uxofZ\u001d(yo)fNH9se%IHT'seo{F\u001d.nd,z]Xkwt+f]\u001d8\u007fo$/YR'\u007fb65\t9\\LS/\u007fo(/ZI*du:\u007f\tN2eu*b\t\\>rh;/[X(ys+|\tI$6`:k@Ikeh!d\tM$zh,v\"N\\Y\"b!<fGVkfn#fJDkdd;z[S.r!<{HI>e!".length();
                var1_4 = 2;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = AuditPolicyEvaluator.a(v0, AuditPolicyEvaluator.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "3!$_\u001f,]\u001e=#O.\nS\u001e!!B\u0004\u000e\u0012\u000553\u0016\u0018\bS\u0000 %RK\u001eW\u0014;2SK\bZ\u0017t\u0010Y\u0007\u0015Q\u000b\u0017!U\u0003\u00192!56_\u0005\u001b\u0012\u0001 !D\u001f\tBR'9E\u001f\u0019_R16S\u0005\b\u0012\u0006;`W\u001e\u0018[\u0006t3_\u0005\u0017\u0012\u0002;,_\b\u0005\bR";
                    var4_3 = "3!$_\u001f,]\u001e=#O.\nS\u001e!!B\u0004\u000e\u0012\u000553\u0016\u0018\bS\u0000 %RK\u001eW\u0014;2SK\bZ\u0017t\u0010Y\u0007\u0015Q\u000b\u0017!U\u0003\u00192!56_\u0005\u001b\u0012\u0001 !D\u001f\tBR'9E\u001f\u0019_R16S\u0005\b\u0012\u0006;`W\u001e\u0018[\u0006t3_\u0005\u0017\u0012\u0002;,_\b\u0005\bR".length();
                    var1_4 = 55;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 82;
                        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++] = AuditPolicyEvaluator.a(v0, AuditPolicyEvaluator.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;
            }
        }
        AuditPolicyEvaluator.g = var5;
        AuditPolicyEvaluator.a = Logger.getLogger(AuditPolicyEvaluator.class.getName());
    }

    private static Throwable a(Throwable throwable) {
        return throwable;
    }

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

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String a(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 -> 32;
                    case 1 -> 6;
                    case 2 -> 18;
                    case 3 -> 100;
                    case 4 -> 57;
                    case 5 -> 46;
                    default -> 96;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

