/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.Audit
 *  com.l7tech.gateway.common.resources.ResourceEntry
 *  com.l7tech.gateway.common.resources.ResourceEntryHeader
 *  com.l7tech.gateway.common.resources.ResourceType
 *  com.l7tech.objectmodel.EntityHeader
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.util.CausedIOException
 *  com.l7tech.util.Functions
 *  com.l7tech.util.Functions$Binary
 *  org.springframework.context.ApplicationEvent
 */
package com.l7tech.server.globalresources;

import com.l7tech.gateway.common.audit.Audit;
import com.l7tech.gateway.common.resources.ResourceEntry;
import com.l7tech.gateway.common.resources.ResourceEntryHeader;
import com.l7tech.gateway.common.resources.ResourceType;
import com.l7tech.objectmodel.EntityHeader;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.server.communityschemas.SchemaSourceResolver;
import com.l7tech.server.event.EntityInvalidationEvent;
import com.l7tech.server.globalresources.ResourceEntryManager;
import com.l7tech.server.globalresources.o;
import com.l7tech.server.globalresources.p;
import com.l7tech.server.util.PostStartupApplicationListener;
import com.l7tech.util.CausedIOException;
import com.l7tech.util.Functions;
import java.io.IOException;
import java.io.StringReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicReference;
import org.springframework.context.ApplicationEvent;
import org.xml.sax.InputSource;
import org.xml.sax.ext.EntityResolver2;

public class ResourceEntrySchemaSourceResolver
implements PostStartupApplicationListener,
EntityResolver2,
SchemaSourceResolver {
    public static final String RESOLVER_ID;
    private final ResourceEntryManager a;
    private final Object b = new Object();
    private final Map<Goid, Set<String>> c = new HashMap<Goid, Set<String>>();
    private final AtomicReference<SchemaSourceResolver.SchemaInvalidationListener> d = new AtomicReference();
    private static final String[] e;

    public ResourceEntrySchemaSourceResolver(ResourceEntryManager resourceEntryManager) {
        this.a = resourceEntryManager;
    }

    @Override
    public String getId() {
        return e[0];
    }

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

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

    @Override
    public SchemaSourceResolver.SchemaSource getSchemaByTargetNamespace(Audit audit, String string) throws IOException {
        SchemaSourceResolver.SchemaSource schemaSource = null;
        try {
            Collection<ResourceEntryHeader> collection = this.a.findHeadersByTNS(string);
            if (collection.size() == 1) {
                ResourceEntry resourceEntry = (ResourceEntry)this.a.findByPrimaryKey(collection.iterator().next().getGoid());
                schemaSource = this.a(resourceEntry);
            } else if (!collection.isEmpty()) {
                Set set = (Set)Functions.reduce(collection, new TreeSet(), (Functions.Binary)new o(this));
                throw new IOException(e[1] + String.valueOf(set));
            }
        }
        catch (FindException findException) {
            throw new CausedIOException((Throwable)findException);
        }
        return schemaSource;
    }

    @Override
    public SchemaSourceResolver.SchemaSource getSchemaByUri(Audit audit, String string) throws IOException {
        SchemaSourceResolver.SchemaSource schemaSource;
        try {
            ResourceEntry resourceEntry = this.a.findResourceByUriAndType(string, ResourceType.XML_SCHEMA);
            schemaSource = this.a(resourceEntry);
        }
        catch (FindException findException) {
            throw new CausedIOException((Throwable)findException);
        }
        return schemaSource;
    }

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

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

    @Override
    public InputSource getExternalSubset(String string, String string2) throws IOException {
        return null;
    }

    @Override
    public InputSource resolveEntity(String string, String string2, String string3, String string4) throws IOException {
        InputSource inputSource = null;
        try {
            ResourceEntry resourceEntry;
            block8: {
                URI uRI;
                resourceEntry = this.a(string2);
                if (resourceEntry == null) {
                    resourceEntry = this.a.findResourceByUriAndType(string4, ResourceType.DTD);
                }
                try {
                    if (resourceEntry != null || string3 == null) break block8;
                }
                catch (FindException findException) {
                    throw ResourceEntrySchemaSourceResolver.a(findException);
                }
                URI uRI2 = new URI(string4);
                if (!uRI2.isAbsolute() && (uRI = new URI(string3)).isAbsolute()) {
                    resourceEntry = this.a.findResourceByUriAndType(uRI.resolve(uRI2).toString(), ResourceType.DTD);
                }
            }
            if (resourceEntry != null) {
                inputSource = new InputSource();
                inputSource.setSystemId(resourceEntry.getUri());
                inputSource.setCharacterStream(new StringReader(resourceEntry.getContent()));
            }
        }
        catch (FindException findException) {
            throw new CausedIOException((Throwable)findException);
        }
        catch (URISyntaxException uRISyntaxException) {
            throw new CausedIOException((Throwable)uRISyntaxException);
        }
        return inputSource;
    }

    @Override
    public InputSource resolveEntity(String string, String string2) throws IOException {
        return this.resolveEntity(e[3], string, string2, string2);
    }

    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        EntityInvalidationEvent entityInvalidationEvent;
        if (applicationEvent instanceof EntityInvalidationEvent && ResourceEntry.class.isAssignableFrom((entityInvalidationEvent = (EntityInvalidationEvent)applicationEvent).getEntityClass())) {
            this.a(entityInvalidationEvent.getEntityIds());
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private SchemaSourceResolver.SchemaSource a(ResourceEntry resourceEntry) {
        SchemaSourceResolver.DefaultSchemaSource defaultSchemaSource = null;
        if (resourceEntry != null) {
            Object object = this.b;
            synchronized (object) {
                Set<String> set = this.c.get(resourceEntry.getGoid());
                if (set == null) {
                    set = new HashSet<String>();
                    this.c.put(resourceEntry.getGoid(), set);
                }
                set.add(resourceEntry.getUri());
            }
            defaultSchemaSource = new SchemaSourceResolver.DefaultSchemaSource(resourceEntry.getUri(), resourceEntry.getContent(), this);
        }
        return defaultSchemaSource;
    }

    private ResourceEntry a(String string) throws FindException {
        Collection<ResourceEntryHeader> collection;
        ResourceEntry resourceEntry = null;
        if (string != null && (collection = this.a.findHeadersByPublicIdentifier(string)).size() == 1) {
            resourceEntry = (ResourceEntry)this.a.findByHeader((EntityHeader)collection.iterator().next());
        }
        return resourceEntry;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void a(Goid[] goidArray) {
        SchemaSourceResolver.SchemaInvalidationListener schemaInvalidationListener = this.d.get();
        if (schemaInvalidationListener != null) {
            HashMap<Goid, Set<String>> hashMap;
            List<Goid> list = Arrays.asList(goidArray);
            Object object = this.b;
            synchronized (object) {
                hashMap = new HashMap<Goid, Set<String>>(this.c);
                this.c.keySet().removeAll(list);
            }
            hashMap.keySet().retainAll(list);
            object = (Set)Functions.reduce(hashMap.values(), new HashSet(), (Functions.Binary)new p(this));
            Iterator iterator = object.iterator();
            while (iterator.hasNext()) {
                String string = (String)iterator.next();
                this.d.get().invalidateSchemaByUri(string, true);
            }
        }
    }

    private static FindException a(FindException findException) {
        return findException;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[4];
                var3_1 = 0;
                var2_2 = " OhDaD]5FtIuZ\u001e\u0002\u0003UCsG\u0011\u0011FuD*VkRiX\u0011\u0002\u0003tEhM\u0010\u0006P'@o]\u0013\u0003\u0003aIr\b\t\u0006Q`Ct\b\u0013\u0006NbUpI\u001e\u0002\u000f'Uy[\t\u0002N'OdM\u0013\u0013JaOeZ\u000eGBuC ";
                var4_3 = " OhDaD]5FtIuZ\u001e\u0002\u0003UCsG\u0011\u0011FuD*VkRiX\u0011\u0002\u0003tEhM\u0010\u0006P'@o]\u0013\u0003\u0003aIr\b\t\u0006Q`Ct\b\u0013\u0006NbUpI\u001e\u0002\u000f'Uy[\t\u0002N'OdM\u0013\u0013JaOeZ\u000eGBuC ".length();
                var1_4 = 24;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ResourceEntrySchemaSourceResolver.a(v0, ResourceEntrySchemaSourceResolver.b(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "_0\u0017;\u001e;\"J9\u000b6\n%a}|*<\f8nn9\n\u0006}2\f0\u000b.";
                    var4_3 = "_0\u0017;\u001e;\"J9\u000b6\n%a}|*<\f8nn9\n\u0006}2\f0\u000b.".length();
                    var1_4 = 24;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 121;
                        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++] = ResourceEntrySchemaSourceResolver.a(v0, ResourceEntrySchemaSourceResolver.b(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;
            }
        }
        ResourceEntrySchemaSourceResolver.e = var5;
        ResourceEntrySchemaSourceResolver.RESOLVER_ID = ResourceEntrySchemaSourceResolver.e[2];
    }

    private static char[] b(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x7B);
        }
        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 -> 97;
                    case 1 -> 37;
                    case 2 -> 1;
                    case 3 -> 32;
                    case 4 -> 6;
                    case 5 -> 46;
                    default -> 123;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

