/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.Audit
 *  com.l7tech.gateway.common.service.PublishedService
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.variable.NoSuchVariableException
 *  com.l7tech.policy.variable.Syntax
 *  com.l7tech.util.Config
 *  io.opentelemetry.api.common.Attributes
 *  io.opentelemetry.api.common.AttributesBuilder
 *  io.opentelemetry.api.trace.Span
 *  io.opentelemetry.api.trace.StatusCode
 *  io.opentelemetry.context.Scope
 */
package com.l7tech.server.otel.mediator;

import com.l7tech.gateway.common.audit.Audit;
import com.l7tech.gateway.common.service.PublishedService;
import com.l7tech.objectmodel.Goid;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.variable.NoSuchVariableException;
import com.l7tech.policy.variable.Syntax;
import com.l7tech.server.MessageProcessorMediator;
import com.l7tech.server.audit.Auditor;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.otel.OtelContext;
import com.l7tech.server.otel.OtelContextFactory;
import com.l7tech.server.otel.util.OtelTraceConfigUtil;
import com.l7tech.server.policy.variable.ExpandVariables;
import com.l7tech.server.service.ServiceCache;
import com.l7tech.util.Config;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.context.Scope;
import java.util.HashMap;
import java.util.Map;

public class OtelMessageProcessorMediator
implements MessageProcessorMediator {
    private final OtelContextFactory a;
    private final ServiceCache b;
    private final Config c;
    private Span d;
    private Scope e;
    private static final String[] f;

    public OtelMessageProcessorMediator(OtelContextFactory otelContextFactory, ServiceCache serviceCache, Config config) {
        this.a = otelContextFactory;
        this.b = serviceCache;
        this.c = config;
    }

    @Override
    public void processMessageStart(PolicyEnforcementContext policyEnforcementContext) {
        OtelContext otelContext = this.a.create(policyEnforcementContext, this.b);
        policyEnforcementContext.setOtelContext(otelContext);
        this.d = otelContext.startServiceSpan(policyEnforcementContext);
        this.e = this.d.makeCurrent();
    }

    @Override
    public void processMessagePostReallyProcessMessage(AssertionStatus assertionStatus) {
        if (assertionStatus == AssertionStatus.NONE) {
            this.d.setStatus(StatusCode.OK, assertionStatus.getMessage());
        } else {
            this.d.setStatus(StatusCode.ERROR, assertionStatus.getMessage());
        }
    }

    @Override
    public void doRequestPostProcessing(PolicyEnforcementContext policyEnforcementContext, String string, Auditor auditor) {
        this.a(policyEnforcementContext, string, auditor);
    }

    private boolean a() {
        return this.c.getBooleanProperty(f[16], false);
    }

    private String b() {
        return this.c.getProperty(f[9], "");
    }

    private void a(PolicyEnforcementContext policyEnforcementContext, String string, Auditor auditor) {
        if (!policyEnforcementContext.isPolicyExecutionAttempted() || !this.a()) {
            return;
        }
        OtelContext otelContext = policyEnforcementContext.getOtelContext();
        Goid goid = policyEnforcementContext.getService() == null ? PublishedService.DEFAULT_GOID : policyEnforcementContext.getService().getGoid();
        AttributesBuilder attributesBuilder = Attributes.builder();
        String[] stringArray = f;
        attributesBuilder.put(stringArray[18], string);
        attributesBuilder.put(stringArray[17], policyEnforcementContext.getService().getName());
        attributesBuilder.put(stringArray[21], goid.toString());
        if (policyEnforcementContext.getRequest().isHttpRequest()) {
            attributesBuilder.put(stringArray[6], policyEnforcementContext.getService().getRoutingUri());
            attributesBuilder.put(stringArray[10], policyEnforcementContext.getRequest().getHttpRequestKnob().getMethodAsString());
        }
        this.a(policyEnforcementContext, attributesBuilder, auditor);
        this.a(policyEnforcementContext, attributesBuilder);
        Attributes attributes = attributesBuilder.build();
        stringArray = f;
        otelContext.recordCounter(stringArray[5], stringArray[0], stringArray[2], 1L, attributes);
        int n2 = (int)(policyEnforcementContext.getEndTime() - policyEnforcementContext.getStartTime());
        int n3 = (int)policyEnforcementContext.getRoutingTotalTime();
        otelContext.recordHistogram(stringArray[8], stringArray[14], stringArray[11], n2, attributes);
        otelContext.recordHistogram(stringArray[20], stringArray[4], stringArray[15], n3, attributes);
        if (!policyEnforcementContext.isAuthorizedRequest()) {
            otelContext.recordCounter(stringArray[19], stringArray[23], stringArray[3], 1L, attributes);
        }
        if (policyEnforcementContext.isAuthorizedRequest() && !policyEnforcementContext.isCompletedRequest()) {
            stringArray = f;
            otelContext.recordCounter(stringArray[12], stringArray[7], stringArray[3], 1L, attributes);
        }
        if (policyEnforcementContext.isAuthorizedRequest() && policyEnforcementContext.isCompletedRequest()) {
            stringArray = f;
            otelContext.recordCounter(stringArray[1], stringArray[22], stringArray[3], 1L, attributes);
        }
    }

    private void a(PolicyEnforcementContext policyEnforcementContext, AttributesBuilder attributesBuilder) {
        String string;
        Object object = null;
        try {
            object = policyEnforcementContext.getVariable(f[13]);
        }
        catch (NoSuchVariableException noSuchVariableException) {
            // empty catch block
        }
        if (object instanceof String && !(string = String.valueOf(object)).isBlank()) {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            OtelTraceConfigUtil.setCustomAttributesMap(string, hashMap);
            if (!hashMap.isEmpty()) {
                for (Map.Entry entry : hashMap.entrySet()) {
                    attributesBuilder.put((String)entry.getKey(), (String)entry.getValue());
                }
            }
        }
    }

    private void a(PolicyEnforcementContext policyEnforcementContext, AttributesBuilder attributesBuilder, Auditor auditor) {
        String string = this.b();
        if (!string.isBlank()) {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            OtelTraceConfigUtil.setCustomAttributesMap(string, hashMap);
            if (!hashMap.isEmpty()) {
                for (Map.Entry entry : hashMap.entrySet()) {
                    String string2 = (String)entry.getValue();
                    String[] stringArray = Syntax.getReferencedNames((String)string2);
                    if (stringArray.length > 0) {
                        String string3 = ExpandVariables.process(string2, policyEnforcementContext.getVariableMap(stringArray, (Audit)auditor), (Audit)auditor);
                        attributesBuilder.put((String)entry.getKey(), string3);
                        continue;
                    }
                    attributesBuilder.put((String)entry.getKey(), (String)entry.getValue());
                }
            }
        }
    }

    @Override
    public void handleException(Exception exception) {
        this.d.recordException((Throwable)exception);
        this.d.setStatus(StatusCode.ERROR, exception.getClass().getSimpleName());
    }

    @Override
    public void processMessageFinally(PolicyEnforcementContext policyEnforcementContext, AssertionStatus assertionStatus) {
        policyEnforcementContext.getOtelContext().endServiceSpan(this.d, assertionStatus, policyEnforcementContext);
        if (this.e != null) {
            this.e.close();
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[24];
                var3_1 = 0;
                var2_2 = "2\u0013(eL,~\u0003\u000e*mCi\r\u0005\u001d0hS\u000f\u0015\u0019.rIoH9\u000f)gCi^\u0015\u0005\u0005\u0013)jT\u0005\u0005\u0013)jT\r$\u001d?o\u0000IC\u0002\\(mMi\u0011\u0015\u0019.rIoH9\u001d(pEa]\u0012\u00198\n\u0015\u0019.rIoH3\u000e5\u001e2\u0013(eL,~\u0003\u000e*mCi\r4\u0013)pIbJF\u001a=mLy_\u0003\u000f\u000f\u0015\u0019.rIoH9\u0010=pEbN\u001f\u001e\t\b9h\u000e\u007fH\u0014\n5gEAH\u0012\u000e5gS\"L\u0012\b.mByY\u0003\u000f\u0006\u000b\u0019(lOh\u0002\u000b\u000f\u0018\u0015\u0019.rIoH9\u000e3qTeC\u0001#:eI`X\u0014\u0019/\u001e\t\b9h\u000e\u007fH\u0014\n5gEAH\u0012\u000e5gS\"L\u0012\b.mByY\u0003\u000f\u000e \u000e3jT,h\b\u0018|pIaH\u0002\u000b\u000f\u0019\t\b9h\u000e\u007fH\u0014\n5gEAH\u0012\u000e5gebL\u0004\u00109`\u000b\u0015\u0019.rIoH(\u001d1a\t\t\f9vAxD\t\u0012\u0019\u0015\u0019.rIoH9\f3hIoT9\n5kLmY\u000f\u00132w\u0017\u0015\u0019.rIoH9\u000e3qTeC\u0001#0eTiC\u0005\u0005\u0004\u0001\u00135`";
                var4_3 = "2\u0013(eL,~\u0003\u000e*mCi\r\u0005\u001d0hS\u000f\u0015\u0019.rIoH9\u000f)gCi^\u0015\u0005\u0005\u0013)jT\u0005\u0005\u0013)jT\r$\u001d?o\u0000IC\u0002\\(mMi\u0011\u0015\u0019.rIoH9\u001d(pEa]\u0012\u00198\n\u0015\u0019.rIoH3\u000e5\u001e2\u0013(eL,~\u0003\u000e*mCi\r4\u0013)pIbJF\u001a=mLy_\u0003\u000f\u000f\u0015\u0019.rIoH9\u0010=pEbN\u001f\u001e\t\b9h\u000e\u007fH\u0014\n5gEAH\u0012\u000e5gS\"L\u0012\b.mByY\u0003\u000f\u0006\u000b\u0019(lOh\u0002\u000b\u000f\u0018\u0015\u0019.rIoH9\u000e3qTeC\u0001#:eI`X\u0014\u0019/\u001e\t\b9h\u000e\u007fH\u0014\n5gEAH\u0012\u000e5gS\"L\u0012\b.mByY\u0003\u000f\u000e \u000e3jT,h\b\u0018|pIaH\u0002\u000b\u000f\u0019\t\b9h\u000e\u007fH\u0014\n5gEAH\u0012\u000e5gebL\u0004\u00109`\u000b\u0015\u0019.rIoH(\u001d1a\t\t\f9vAxD\t\u0012\u0019\u0015\u0019.rIoH9\f3hIoT9\n5kLmY\u000f\u00132w\u0017\u0015\u0019.rIoH9\u000e3qTeC\u0001#0eTiC\u0005\u0005\u0004\u0001\u00135`".length();
                var1_4 = 19;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = OtelMessageProcessorMediator.a(v0, OtelMessageProcessorMediator.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "5\u0014/bK+y\u0004\t-jDn\n2\u000e8`BxY\u001f5\u0014/bK+y\u0004\t-jDn\n1\u00147jDr\n7\u00124oF\u007fC\u000e\u0015(";
                    var4_3 = "5\u0014/bK+y\u0004\t-jDn\n2\u000e8`BxY\u001f5\u0014/bK+y\u0004\t-jDn\n1\u00147jDr\n7\u00124oF\u007fC\u000e\u0015(".length();
                    var1_4 = 21;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 79;
                        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++] = OtelMessageProcessorMediator.a(v0, OtelMessageProcessorMediator.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;
            }
        }
        OtelMessageProcessorMediator.f = var5;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x65);
        }
        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 -> 46;
                    case 1 -> 52;
                    case 2 -> 20;
                    case 3 -> 76;
                    case 4 -> 104;
                    case 5 -> 68;
                    default -> 101;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

