/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.fasterxml.jackson.databind.ObjectMapper
 *  com.l7tech.common.http.HttpConstants
 *  com.l7tech.common.mime.ContentTypeHeader
 *  com.l7tech.common.mime.NoSuchPartException
 *  com.l7tech.gateway.common.service.PublishedService
 *  com.l7tech.message.Header
 *  com.l7tech.message.Message
 *  com.l7tech.util.Config
 *  com.l7tech.util.ConfigFactory
 *  com.l7tech.util.ConfigFactory$ConfigurationListener
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.xml.MessageNotSoapException
 *  io.opentelemetry.api.GlobalOpenTelemetry
 *  io.opentelemetry.context.propagation.ContextPropagators
 *  io.opentelemetry.context.propagation.TextMapPropagator
 *  org.jetbrains.annotations.Nullable
 *  org.springframework.beans.BeansException
 *  org.springframework.context.ApplicationContext
 *  org.springframework.context.ApplicationContextAware
 */
package com.l7tech.server.otel;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.l7tech.common.http.HttpConstants;
import com.l7tech.common.mime.ContentTypeHeader;
import com.l7tech.common.mime.NoSuchPartException;
import com.l7tech.gateway.common.service.PublishedService;
import com.l7tech.message.Header;
import com.l7tech.message.Message;
import com.l7tech.server.audit.Auditor;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.otel.OtelContext;
import com.l7tech.server.otel.OtelContextNoop;
import com.l7tech.server.otel.OtelMetricContextImpl;
import com.l7tech.server.otel.OtelTraceContextImpl;
import com.l7tech.server.otel.OtelTraceSamplingConfig;
import com.l7tech.server.otel.a;
import com.l7tech.server.otel.b;
import com.l7tech.server.service.ServiceCache;
import com.l7tech.server.service.resolution.ServiceResolutionException;
import com.l7tech.server.util.SoapFaultManager;
import com.l7tech.util.Config;
import com.l7tech.util.ConfigFactory;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.xml.MessageNotSoapException;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapPropagator;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.xml.sax.SAXException;

public class OtelContextFactory
implements ApplicationContextAware {
    private static final Logger a;
    private static final ObjectMapper b;
    private static final OtelContext c;
    private final Config d;
    private final SoapFaultManager e;
    private final ReentrantReadWriteLock f = new ReentrantReadWriteLock();
    private Auditor g;
    private boolean h;
    private boolean i;
    private boolean j;
    private OtelTraceSamplingConfig k;
    private String l;
    private String m;
    private ServiceCache.ResolutionListener n;
    private String o;
    private String p;
    private String q;
    private static final ContextPropagators r;
    private static final String[] s;

    public OtelContextFactory(Config config, SoapFaultManager soapFaultManager) {
        this.d = config;
        this.e = soapFaultManager;
        ArrayList<String> arrayList = new ArrayList<String>();
        String[] stringArray = s;
        arrayList.add(stringArray[14]);
        arrayList.add(stringArray[12]);
        arrayList.add(stringArray[24]);
        arrayList.add(stringArray[6]);
        arrayList.add(stringArray[0]);
        arrayList.add(stringArray[1]);
        arrayList.add(stringArray[5]);
        arrayList.add(stringArray[3]);
        arrayList.add(stringArray[2]);
        ConfigFactory.addListener((ConfigFactory.ConfigurationListener)new a(this, arrayList), (String[])new String[0]);
        this.n = new b(this);
        this.a(arrayList);
    }

    public OtelContext create() {
        OtelContext otelContext = c;
        if (this.j) {
            otelContext = new OtelMetricContextImpl(this.l, this.m, this.g);
        }
        return otelContext;
    }

    public OtelContext create(PolicyEnforcementContext policyEnforcementContext, ServiceCache serviceCache) {
        block14: {
            block13: {
                try {
                    if (!this.j) {
                        return new OtelContextNoop();
                    }
                }
                catch (NoSuchElementException noSuchElementException) {
                    throw OtelContextFactory.a(noSuchElementException);
                }
                try {
                    try {
                        if (this.h && policyEnforcementContext.getRequest().isHttpRequest()) break block13;
                    }
                    catch (NoSuchElementException noSuchElementException) {
                        throw OtelContextFactory.a(noSuchElementException);
                    }
                    return new OtelMetricContextImpl(this.l, this.m, this.g);
                }
                catch (NoSuchElementException noSuchElementException) {
                    throw OtelContextFactory.a(noSuchElementException);
                }
            }
            PublishedService publishedService = this.a(policyEnforcementContext, serviceCache);
            try {
                block15: {
                    try {
                        try {
                            if (publishedService == null) break block14;
                            if (publishedService.isTracingEnabled()) break block15;
                        }
                        catch (NoSuchElementException noSuchElementException) {
                            throw OtelContextFactory.a(noSuchElementException);
                        }
                        if (!this.k.isTraceEnabledForService(publishedService.getId(), policyEnforcementContext.getRequest().getHttpRequestKnob())) break block14;
                    }
                    catch (NoSuchElementException noSuchElementException) {
                        throw OtelContextFactory.a(noSuchElementException);
                    }
                }
                return new OtelTraceContextImpl(this.l, this.m, publishedService.getName(), this.k, this.o, this.q, this.i, this.g);
            }
            catch (NoSuchElementException noSuchElementException) {
                throw OtelContextFactory.a(noSuchElementException);
            }
        }
        return new OtelMetricContextImpl(this.l, this.m, this.g);
    }

    @Nullable
    private PublishedService a(PolicyEnforcementContext policyEnforcementContext, ServiceCache serviceCache) {
        PublishedService publishedService;
        Header header;
        String string;
        boolean bl2 = false;
        Message message = policyEnforcementContext.getRequest();
        Collection collection = message.getHeadersKnob().getHeaders(HttpConstants.HEADER_CONTENT_TYPE, s[11]);
        if (!collection.isEmpty() && (string = (String)(header = (Header)collection.iterator().next()).getValue()).contains(ContentTypeHeader.SOAP_1_2_DEFAULT.getMainValue())) {
            bl2 = true;
        }
        try {
            publishedService = bl2 ? this.b(policyEnforcementContext, serviceCache) : this.c(policyEnforcementContext, serviceCache);
        }
        catch (NoSuchElementException noSuchElementException) {
            throw OtelContextFactory.a(noSuchElementException);
        }
        header = publishedService;
        return header;
    }

    private PublishedService b(PolicyEnforcementContext policyEnforcementContext, ServiceCache serviceCache) {
        try {
            String string = policyEnforcementContext.getRequest().getHttpRequestKnob().getRequestUri();
            String string2 = policyEnforcementContext.getRequest().getHttpRequestKnob().getSoapAction();
            QName qName = policyEnforcementContext.getRequest().getSoapKnob().getPayloadNames()[0];
            String string3 = qName.getNamespaceURI();
            Collection<PublishedService> collection = serviceCache.resolve(string, string2, string3);
            if (!collection.isEmpty()) {
                return collection.iterator().next();
            }
        }
        catch (NoSuchPartException | ServiceResolutionException | MessageNotSoapException | IOException | NoSuchElementException | SAXException throwable) {
            try {
                if (a.isLoggable(Level.FINE)) {
                    a.log(Level.FINE, s[10] + ExceptionUtils.getMessage((Throwable)throwable), throwable);
                }
            }
            catch (IOException iOException) {
                throw OtelContextFactory.a(iOException);
            }
        }
        return null;
    }

    private PublishedService c(PolicyEnforcementContext policyEnforcementContext, ServiceCache serviceCache) {
        PublishedService publishedService = null;
        try {
            publishedService = serviceCache.resolve(policyEnforcementContext.getRequest(), this.n);
        }
        catch (ServiceResolutionException serviceResolutionException) {
            try {
                if (a.isLoggable(Level.FINE)) {
                    a.log(Level.FINE, s[4] + ExceptionUtils.getMessage((Throwable)serviceResolutionException), serviceResolutionException);
                }
            }
            catch (ServiceResolutionException serviceResolutionException2) {
                throw OtelContextFactory.a(serviceResolutionException2);
            }
        }
        return publishedService;
    }

    /*
     * Exception decompiling
     */
    private void a(List<String> var1_1) {
        /*
         * This method has failed to decompile.  When submitting a bug report, please provide this stack trace, and (if you hold appropriate legal rights) the relevant class file.
         * 
         * org.benf.cfr.reader.util.ConfusedCFRException: Tried to end blocks [1[TRYBLOCK]], but top level block is 4[SWITCH]
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.processEndingBlocks(Op04StructuredStatement.java:435)
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.buildNestedBlocks(Op04StructuredStatement.java:484)
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op03SimpleStatement.createInitialStructuredBlock(Op03SimpleStatement.java:736)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisInner(CodeAnalyser.java:850)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisOrWrapFail(CodeAnalyser.java:278)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysis(CodeAnalyser.java:201)
         *     at org.benf.cfr.reader.entities.attributes.AttributeCode.analyse(AttributeCode.java:94)
         *     at org.benf.cfr.reader.entities.Method.analyse(Method.java:531)
         *     at org.benf.cfr.reader.entities.ClassFile.analyseMid(ClassFile.java:1055)
         *     at org.benf.cfr.reader.entities.ClassFile.analyseTop(ClassFile.java:942)
         *     at org.benf.cfr.reader.Driver.doJarVersionTypes(Driver.java:257)
         *     at org.benf.cfr.reader.Driver.doJar(Driver.java:139)
         *     at org.benf.cfr.reader.CfrDriverImpl.analyse(CfrDriverImpl.java:76)
         *     at org.benf.cfr.reader.Main.main(Main.java:54)
         */
        throw new IllegalStateException("Decompilation failed");
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        try {
            if (this.g == null) {
                this.g = new Auditor(this, applicationContext, a);
            }
        }
        catch (BeansException beansException) {
            throw OtelContextFactory.a((Exception)((Object)beansException));
        }
    }

    public static TextMapPropagator getTextMapPropagator() {
        return r.getTextMapPropagator();
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[25];
                var3_1 = 0;
                var2_2 = "XA\u00009ph\u0013VV\u0000\u00161r\u0015RM\u0011\u0003?n\bVW\t0-2\u0004YT\u00079;x\u0010XA\u00009ph\u0013VV\u0000\u00161r\u0007^R\u001dXA\u00009p~\u0000PR\u00042;2\u0015ET\u00060p}\u0015CG\f7+h\u0004D\u001eXA\u00009po\u0004EC\f6;Q\u0004CG\f6-2\u0000CA\u0017<<i\u0015RF@tZ\u00109:<\u000fXAE37r\u0005\u0017E\u001072u\u0012_P\u0001u-y\u0013A\\\u00060~z\u000eE\u0015\u0011=;<\u0006^C\u0000;~r\u000eY\u0018\u0016:?lAEP\u0014 ;o\u0015\u0019\u0015XA\u00009ph\u0013VV\u0000{?h\u0015E\\\u0007 *y\u0012\u0011XA\u00009ph\u0013VV\u0000\u00100}\u0003[P\u0001\"XA\u00009ph\u0013VV\u0000\u00161r\u0015RM\u0011\u0003?n\bVW\t0-2\u0004YT\u00079;x\u001dXA\u00009p~\u0000PR\u00042;2\u0015ET\u00060p}\u0015CG\f7+h\u0004D\fXA\u00009py\u000fVW\t0:<tZ\u00109:<\u000fXAE37r\u0005\u0017E\u001072u\u0012_P\u0001u-y\u0013A\\\u00060~z\u000eE\u0015\u0011=;<\u0006^C\u0000;~O.veE';m\u0014RF\u0011{\u000b\u007fa1\u0005~T\u0004VQ\u0000'\u0011XA\u00009pq\u0004CG\f6\u000en\u0004Q\\\u001dZrG\u0017:,<\u0011VG\u0016<0{Axa\u00009~h\u0013VV\u0000u=s\u000fQ\\\u0002t~R\u000e\u0017z102<\u0015ET\u00060~k\b[YE7;<\u0006R[\u0000'?h\u0004S\u0019E\u00052y\u0000DPE37dAC]\u0000u=s\u000fQ\\\u0002ud<\fXA\u00009py\u000fVW\t0:\u0016bE\u00014*u\u000fP\u0015*\u0001;pADP\u0011!7r\u0006D\u0010XA\u00009ph\u0013VV\u0000\u00161r\u0007^R\u0011XA\u00009pq\u0004CG\f6\u000en\u0004Q\\\u001d\u0002LH\u0011XA\u00009ph\u0013VV\u0000\u00100}\u0003[P\u0001.\u0012FE<-<\u000fXAE';\u007f\u000eP[\f/;xAVFE\u001a.y\u000fcP\t03y\u0015ELE%,s\u0011RG\u0011,\u0015XA\u00009ph\u0013VV\u0000{?h\u0015E\\\u0007 *y\u0012\u001eXA\u00009po\u0004EC\f6;Q\u0004CG\f6-2\u0000CA\u0017<<i\u0015RF";
                var4_3 = "XA\u00009ph\u0013VV\u0000\u00161r\u0015RM\u0011\u0003?n\bVW\t0-2\u0004YT\u00079;x\u0010XA\u00009ph\u0013VV\u0000\u00161r\u0007^R\u001dXA\u00009p~\u0000PR\u00042;2\u0015ET\u00060p}\u0015CG\f7+h\u0004D\u001eXA\u00009po\u0004EC\f6;Q\u0004CG\f6-2\u0000CA\u0017<<i\u0015RF@tZ\u00109:<\u000fXAE37r\u0005\u0017E\u001072u\u0012_P\u0001u-y\u0013A\\\u00060~z\u000eE\u0015\u0011=;<\u0006^C\u0000;~r\u000eY\u0018\u0016:?lAEP\u0014 ;o\u0015\u0019\u0015XA\u00009ph\u0013VV\u0000{?h\u0015E\\\u0007 *y\u0012\u0011XA\u00009ph\u0013VV\u0000\u00100}\u0003[P\u0001\"XA\u00009ph\u0013VV\u0000\u00161r\u0015RM\u0011\u0003?n\bVW\t0-2\u0004YT\u00079;x\u001dXA\u00009p~\u0000PR\u00042;2\u0015ET\u00060p}\u0015CG\f7+h\u0004D\fXA\u00009py\u000fVW\t0:<tZ\u00109:<\u000fXAE37r\u0005\u0017E\u001072u\u0012_P\u0001u-y\u0013A\\\u00060~z\u000eE\u0015\u0011=;<\u0006^C\u0000;~O.veE';m\u0014RF\u0011{\u000b\u007fa1\u0005~T\u0004VQ\u0000'\u0011XA\u00009pq\u0004CG\f6\u000en\u0004Q\\\u001dZrG\u0017:,<\u0011VG\u0016<0{Axa\u00009~h\u0013VV\u0000u=s\u000fQ\\\u0002t~R\u000e\u0017z102<\u0015ET\u00060~k\b[YE7;<\u0006R[\u0000'?h\u0004S\u0019E\u00052y\u0000DPE37dAC]\u0000u=s\u000fQ\\\u0002ud<\fXA\u00009py\u000fVW\t0:\u0016bE\u00014*u\u000fP\u0015*\u0001;pADP\u0011!7r\u0006D\u0010XA\u00009ph\u0013VV\u0000\u00161r\u0007^R\u0011XA\u00009pq\u0004CG\f6\u000en\u0004Q\\\u001d\u0002LH\u0011XA\u00009ph\u0013VV\u0000\u00100}\u0003[P\u0001.\u0012FE<-<\u000fXAE';\u007f\u000eP[\f/;xAVFE\u001a.y\u000fcP\t03y\u0015ELE%,s\u0011RG\u0011,\u0015XA\u00009ph\u0013VV\u0000{?h\u0015E\\\u0007 *y\u0012\u001eXA\u00009po\u0004EC\f6;Q\u0004CG\f6-2\u0000CA\u0017<<i\u0015RF".length();
                var1_4 = 34;
                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++] = OtelContextFactory.a(v0, OtelContextFactory.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "_F\u0007>wz\u0012D@\u000b0,o\u0003`@\u000740c\u0014_F\u0007>wz\u0012D@\u000b0,o\u0003`@\u000740c";
                    var4_3 = "_F\u0007>wz\u0012D@\u000b0,o\u0003`@\u000740c\u0014_F\u0007>wz\u0012D@\u000b0,o\u0003`@\u000740c".length();
                    var1_4 = 20;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 40;
                        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++] = OtelContextFactory.a(v0, OtelContextFactory.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;
            }
        }
        OtelContextFactory.s = var5;
        OtelContextFactory.a = Logger.getLogger(OtelContextFactory.class.getName());
        OtelContextFactory.b = new ObjectMapper();
        OtelContextFactory.c = new OtelContextNoop();
        OtelContextFactory.r = GlobalOpenTelemetry.getPropagators();
    }

    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] ^ 0x4E);
        }
        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 -> 24;
                    case 1 -> 26;
                    case 2 -> 74;
                    case 3 -> 122;
                    case 4 -> 113;
                    case 5 -> 51;
                    default -> 78;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

