/*
 * 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.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.AuditDetailAssertion
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.variable.Syntax
 *  org.springframework.context.ApplicationContext
 */
package com.l7tech.server.policy.assertion;

import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.AuditDetailAssertion;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.variable.Syntax;
import com.l7tech.server.audit.AuditLogFormatter;
import com.l7tech.server.audit.Auditor;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractServerAssertion;
import com.l7tech.server.policy.variable.ExpandVariables;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.springframework.context.ApplicationContext;

public class ServerAuditDetailAssertion
extends AbstractServerAssertion<AuditDetailAssertion> {
    private static final AuditLogFormatter d;
    private Logger e;
    private Auditor f;
    private final String[] g;
    private final AuditDetailMessage h;
    private final ApplicationContext i;
    private static final String[] j;

    public ServerAuditDetailAssertion(AuditDetailAssertion auditDetailAssertion, ApplicationContext applicationContext) {
        super(auditDetailAssertion);
        this.i = applicationContext;
        this.g = auditDetailAssertion.getVariablesUsed();
        this.h = this.a(Level.parse(auditDetailAssertion.getLevel()));
    }

    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        Object object;
        StringBuilder stringBuilder;
        StringBuilder stringBuilder2;
        String string;
        block29: {
            block28: {
                Object object2;
                String string2 = ((AuditDetailAssertion)this.assertion).getCustomLoggerSuffix();
                try {
                    object2 = string2 != null ? j[0] + string2 : ServerAuditDetailAssertion.class.getName();
                }
                catch (IOException iOException) {
                    throw ServerAuditDetailAssertion.a(iOException);
                }
                string = object2;
                String[] stringArray = Syntax.getReferencedNamesIndexedVarsNotOmitted((String)string);
                stringBuilder2 = new StringBuilder(0);
                stringBuilder = new StringBuilder(0);
                for (int i2 = 0; i2 < stringArray.length; ++i2) {
                    block27: {
                        String string3;
                        block25: {
                            block26: {
                                object = stringArray[i2];
                                String[] stringArray2 = j;
                                string3 = ExpandVariables.process(stringArray2[3] + (String)object + "}", policyEnforcementContext.getVariableMap(this.g, this.getAudit()), this.getAudit()).trim();
                                try {
                                    try {
                                        try {
                                            if (!string3.isEmpty()) break block25;
                                            if (i2 <= 0) break block26;
                                        }
                                        catch (IOException iOException) {
                                            throw ServerAuditDetailAssertion.a(iOException);
                                        }
                                        if (stringBuilder2.length() <= 0) break block26;
                                    }
                                    catch (IOException iOException) {
                                        throw ServerAuditDetailAssertion.a(iOException);
                                    }
                                    stringBuilder2.append(j[2]);
                                }
                                catch (IOException iOException) {
                                    throw ServerAuditDetailAssertion.a(iOException);
                                }
                            }
                            stringBuilder2.append((String)object);
                            continue;
                        }
                        try {
                            try {
                                try {
                                    if (string3.matches(AuditDetailAssertion.CUSTOM_LOGGER_NAME_PATTERN)) continue;
                                    if (i2 <= 0) break block27;
                                }
                                catch (IOException iOException) {
                                    throw ServerAuditDetailAssertion.a(iOException);
                                }
                                if (stringBuilder2.length() <= 0) break block27;
                            }
                            catch (IOException iOException) {
                                throw ServerAuditDetailAssertion.a(iOException);
                            }
                            stringBuilder2.append(j[1]);
                        }
                        catch (IOException iOException) {
                            throw ServerAuditDetailAssertion.a(iOException);
                        }
                    }
                    stringBuilder.append((String)object);
                }
                try {
                    if (stringBuilder2.length() <= 0 && stringBuilder.length() <= 0) break block28;
                }
                catch (IOException iOException) {
                    throw ServerAuditDetailAssertion.a(iOException);
                }
                string = ServerAuditDetailAssertion.class.getName();
                break block29;
            }
            string = ExpandVariables.process(string, policyEnforcementContext.getVariableMap(this.g, this.getAudit()), this.getAudit());
        }
        try {
            this.e = Logger.getLogger(string);
            this.f = new Auditor(this, this.i, this.e);
            if (stringBuilder2.length() > 0) {
                this.f.logAndAudit((AuditDetailMessage)AssertionMessages.CUSTOM_LOGGER_NAME_FALLBACK_DUE_TO_VARIABLES_NOT_EXIST, stringBuilder2.toString(), ServerAuditDetailAssertion.class.getName());
            }
        }
        catch (IOException iOException) {
            throw ServerAuditDetailAssertion.a(iOException);
        }
        try {
            if (stringBuilder.length() > 0) {
                this.f.logAndAudit((AuditDetailMessage)AssertionMessages.CUSTOM_LOGGER_NAME_FALLBACK_DUE_TO_INVALID_PACKAGE_NAME, stringBuilder.toString(), ServerAuditDetailAssertion.class.getName());
            }
        }
        catch (IOException iOException) {
            throw ServerAuditDetailAssertion.a(iOException);
        }
        String string4 = ((AuditDetailAssertion)this.assertion).getDetail();
        string4 = ExpandVariables.process(string4, policyEnforcementContext.getVariableMap(this.g, this.getAudit()), this.getAudit());
        if (((AuditDetailAssertion)this.assertion).isLoggingOnly()) {
            object = new LogRecord(this.h.getLevel(), d.formatDetail(this.h, new String[]{string4}));
            ((LogRecord)object).setSourceClassName("");
            ((LogRecord)object).setSourceMethodName("");
            ((LogRecord)object).setLoggerName(this.e.getName());
            this.e.log((LogRecord)object);
        } else {
            this.f.logAndAudit(this.h, string4);
        }
        return AssertionStatus.NONE;
    }

    public Logger getLogger() {
        return this.e;
    }

    private AuditDetailMessage a(Level level) {
        try {
            if (Level.FINEST.equals(level)) {
                return AssertionMessages.USERDETAIL_FINEST;
            }
        }
        catch (RuntimeException runtimeException) {
            throw ServerAuditDetailAssertion.a(runtimeException);
        }
        try {
            if (Level.FINER.equals(level)) {
                return AssertionMessages.USERDETAIL_FINER;
            }
        }
        catch (RuntimeException runtimeException) {
            throw ServerAuditDetailAssertion.a(runtimeException);
        }
        try {
            if (Level.FINE.equals(level)) {
                return AssertionMessages.USERDETAIL_FINE;
            }
        }
        catch (RuntimeException runtimeException) {
            throw ServerAuditDetailAssertion.a(runtimeException);
        }
        try {
            if (Level.INFO.equals(level)) {
                return AssertionMessages.USERDETAIL_INFO;
            }
        }
        catch (RuntimeException runtimeException) {
            throw ServerAuditDetailAssertion.a(runtimeException);
        }
        try {
            if (Level.WARNING.equals(level)) {
                return AssertionMessages.USERDETAIL_WARNING;
            }
        }
        catch (RuntimeException runtimeException) {
            throw ServerAuditDetailAssertion.a(runtimeException);
        }
        throw new RuntimeException(j[4] + ((AuditDetailAssertion)this.assertion).getLevel());
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[5];
                var3_1 = 0;
                var2_2 = "#\u001f\u001a5\nw,%\u0013\u001f5\n/?n\u0013\u0002h\u0012/5n\u0002lP\u0002lP";
                var4_3 = "#\u001f\u001a5\nw,%\u0013\u001f5\n/?n\u0013\u0002h\u0012/5n\u0002lP\u0002lP".length();
                var1_4 = 22;
                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++] = ServerAuditDetailAssertion.b(v0, ServerAuditDetailAssertion.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = ";T\u0011jAM<IzdkJLdUzqzC\b";
                    var4_3 = ";T\u0011jAM<IzdkJLdUzqzC\b".length();
                    var1_4 = 2;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 112;
                        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++] = ServerAuditDetailAssertion.b(v0, ServerAuditDetailAssertion.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;
            }
        }
        ServerAuditDetailAssertion.j = var5;
        ServerAuditDetailAssertion.d = new AuditLogFormatter<REC>();
    }

    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] ^ 0x77);
        }
        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 -> 111;
                    case 1 -> 95;
                    case 2 -> 88;
                    case 3 -> 52;
                    case 4 -> 73;
                    case 5 -> 111;
                    default -> 119;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

