/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.http.GenericHttpClientFactory
 *  com.l7tech.common.io.XmlUtil
 *  com.l7tech.common.io.XmlUtil$BadSchemaException
 *  com.l7tech.gateway.common.audit.Audit
 *  com.l7tech.util.CausedIOException
 *  com.l7tech.util.ExceptionUtils
 *  org.springframework.context.ApplicationEvent
 */
package com.l7tech.server.communityschemas;

import com.l7tech.common.http.GenericHttpClientFactory;
import com.l7tech.common.io.XmlUtil;
import com.l7tech.gateway.common.audit.Audit;
import com.l7tech.server.communityschemas.SchemaConfiguration;
import com.l7tech.server.communityschemas.SchemaSourceResolver;
import com.l7tech.server.communityschemas.b;
import com.l7tech.server.communityschemas.c;
import com.l7tech.server.url.HttpObjectCache;
import com.l7tech.server.util.PostStartupApplicationListener;
import com.l7tech.util.CausedIOException;
import com.l7tech.util.ExceptionUtils;
import java.io.IOException;
import java.text.ParseException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
import org.springframework.context.ApplicationEvent;
import org.xml.sax.EntityResolver;

public class HttpSchemaSourceResolver
implements PostStartupApplicationListener,
SchemaSourceResolver {
    private static final Logger a;
    private final AtomicReference<SchemaSourceResolver.SchemaInvalidationListener> b = new AtomicReference();
    private final SchemaConfiguration c;
    private final GenericHttpClientFactory d;
    private final EntityResolver e;
    private final AtomicReference<HttpObjectCache<String>> f = new AtomicReference();
    private static final String[] g;

    public HttpSchemaSourceResolver(SchemaConfiguration schemaConfiguration, GenericHttpClientFactory genericHttpClientFactory, EntityResolver entityResolver) {
        this.c = schemaConfiguration;
        this.d = genericHttpClientFactory;
        this.e = entityResolver;
        this.a();
    }

    @Override
    public String getId() {
        return g[3];
    }

    @Override
    public boolean isTransient() {
        return true;
    }

    @Override
    public boolean isRemote() {
        return true;
    }

    @Override
    public SchemaSourceResolver.SchemaSource getSchemaByTargetNamespace(Audit audit, String string) {
        return null;
    }

    @Override
    public SchemaSourceResolver.SchemaSource getSchemaByUri(Audit audit, String string) throws IOException {
        SchemaSourceResolver.DefaultSchemaSource defaultSchemaSource;
        String string2;
        block6: {
            string2 = null;
            try {
                if (!string.startsWith(g[6]) && !string.startsWith(g[8])) break block6;
            }
            catch (ParseException parseException) {
                throw HttpSchemaSourceResolver.a(parseException);
            }
            try {
                string2 = (String)this.f.get().resolveUrl(audit, string);
            }
            catch (ParseException parseException) {
                String[] stringArray = g;
                throw new CausedIOException(stringArray[1] + string + stringArray[4] + ExceptionUtils.getMessage((Throwable)parseException), (Throwable)parseException);
            }
        }
        try {
            defaultSchemaSource = string2 == null ? null : new SchemaSourceResolver.DefaultSchemaSource(string, string2, this);
        }
        catch (ParseException parseException) {
            throw HttpSchemaSourceResolver.a(parseException);
        }
        return defaultSchemaSource;
    }

    @Override
    public void refreshSchemaByUri(Audit audit, String string) throws IOException {
        this.getSchemaByUri(audit, string);
    }

    @Override
    public void registerInvalidationListener(SchemaSourceResolver.SchemaInvalidationListener schemaInvalidationListener) {
        this.b.set(schemaInvalidationListener);
    }

    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        if (applicationEvent instanceof SchemaConfiguration.SchemaConfigurationReloadedEvent) {
            this.a();
        }
    }

    private void a() {
        GenericHttpClientFactory genericHttpClientFactory = this.a(this.d, this.c.getMaxSchemaSize());
        b b10 = new b(this);
        String[] stringArray = g;
        this.f.set(new HttpObjectCache<String>(stringArray[5], this.c.getMaxCacheEntries(), this.c.getMaxCacheAge(), this.c.getMaxStaleAge(), genericHttpClientFactory, b10, HttpObjectCache.WAIT_LATEST, stringArray[2]));
    }

    private void a(String string, String string2) {
        block10: {
            boolean bl2;
            String string3;
            SchemaSourceResolver.SchemaInvalidationListener schemaInvalidationListener;
            XmlUtil.BadSchemaException badSchemaException;
            block12: {
                block11: {
                    badSchemaException = null;
                    if (string2 != null) {
                        try {
                            XmlUtil.getSchemaTNS((String)string, (String)string2, (EntityResolver)this.e);
                        }
                        catch (XmlUtil.BadSchemaException badSchemaException2) {
                            badSchemaException = badSchemaException2;
                        }
                    }
                    SchemaSourceResolver.SchemaInvalidationListener schemaInvalidationListener2 = this.b.get();
                    try {
                        try {
                            if (schemaInvalidationListener2 == null) break block10;
                            schemaInvalidationListener = schemaInvalidationListener2;
                            string3 = string;
                            if (badSchemaException != null) break block11;
                        }
                        catch (XmlUtil.BadSchemaException badSchemaException3) {
                            throw HttpSchemaSourceResolver.a((Exception)((Object)badSchemaException3));
                        }
                        bl2 = true;
                        break block12;
                    }
                    catch (XmlUtil.BadSchemaException badSchemaException4) {
                        throw HttpSchemaSourceResolver.a((Exception)((Object)badSchemaException4));
                    }
                }
                bl2 = false;
            }
            try {
                if (!schemaInvalidationListener.invalidateSchemaByUri(string3, bl2)) {
                    a.warning(g[0] + string + g[7] + ExceptionUtils.getMessage((Throwable)badSchemaException));
                }
            }
            catch (XmlUtil.BadSchemaException badSchemaException5) {
                throw HttpSchemaSourceResolver.a((Exception)((Object)badSchemaException5));
            }
        }
    }

    private GenericHttpClientFactory a(GenericHttpClientFactory genericHttpClientFactory, long l2) {
        return new c(this, genericHttpClientFactory, l2);
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[9];
                var3_1 = 0;
                var2_2 = "&\u0002\bC@p2O\u001f\u001dJIt7O\b\u0011UBu9\u000e\b\u001bF\f4{O\u0002\u0011V\fp8\u0019\r\u0012KHx\"\u0006\u0002\u0019\u0002Ox5\u0007\t^DCkv:>2\u0002\u000b!:\u0002\u001f@@|v\u001b\u0003^FCn8\u0003\u0003\u001fF\fk3\u0002\u0003\nG\fj5\u0007\t\u0013C\f\u0018\u001c\u000f\u0016GAx\u0015\u000e\u000f\u0016Gax.<\u000f\u0016GAx\u0005\u0006\u0016\u001b\r'8*r\fK3\u001c\u0003\u0012TIk\u0003OV^\n7!2\u0002\u007fz>\n\u0001\u001f\u0005\u0007\u0018\nR\u0016";
                var4_3 = "&\u0002\bC@p2O\u001f\u001dJIt7O\b\u0011UBu9\u000e\b\u001bF\f4{O\u0002\u0011V\fp8\u0019\r\u0012KHx\"\u0006\u0002\u0019\u0002Ox5\u0007\t^DCkv:>2\u0002\u000b!:\u0002\u001f@@|v\u001b\u0003^FCn8\u0003\u0003\u001fF\fk3\u0002\u0003\nG\fj5\u0007\t\u0013C\f\u0018\u001c\u000f\u0016GAx\u0015\u000e\u000f\u0016Gax.<\u000f\u0016GAx\u0005\u0006\u0016\u001b\r'8*r\fK3\u001c\u0003\u0012TIk\u0003OV^\n7!2\u0002\u007fz>\n\u0001\u001f\u0005\u0007\u0018\nR\u0016".length();
                var1_4 = 61;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = HttpSchemaSourceResolver.a(v0, HttpSchemaSourceResolver.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "S[E\\Ep\"\u0006W\fJ\r\"\u0006\u001c\u0003\u0011ID8";
                    var4_3 = "S[E\\Ep\"\u0006W\fJ\r\"\u0006\u001c\u0003\u0011ID8".length();
                    var1_4 = 13;
                    var0_5 = -1;
lbl22:
                    // 2 sources

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

    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] ^ 0x53);
        }
        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 -> 106;
                    case 1 -> 105;
                    case 2 -> 123;
                    case 3 -> 39;
                    case 4 -> 41;
                    case 5 -> 28;
                    default -> 83;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

