/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.AsyncAdminMethods$JobId
 *  com.l7tech.gateway.common.resources.HttpConfiguration
 *  com.l7tech.gateway.common.resources.HttpProxyConfiguration
 *  com.l7tech.gateway.common.resources.ResourceAdmin
 *  com.l7tech.gateway.common.resources.ResourceEntry
 *  com.l7tech.gateway.common.resources.ResourceEntryBag
 *  com.l7tech.gateway.common.resources.ResourceEntryHeader
 *  com.l7tech.gateway.common.resources.ResourceType
 *  com.l7tech.gateway.common.service.ServiceAdmin$DownloadDocumentType
 *  com.l7tech.objectmodel.DeleteException
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.PersistentEntity
 *  com.l7tech.objectmodel.SaveException
 *  com.l7tech.objectmodel.UpdateException
 *  com.l7tech.util.Background
 *  com.l7tech.util.Charsets
 *  com.l7tech.util.Config
 *  com.l7tech.util.IOUtils
 *  com.l7tech.util.ResourceUtils
 */
package com.l7tech.server.globalresources;

import com.l7tech.gateway.common.AsyncAdminMethods;
import com.l7tech.gateway.common.resources.HttpConfiguration;
import com.l7tech.gateway.common.resources.HttpProxyConfiguration;
import com.l7tech.gateway.common.resources.ResourceAdmin;
import com.l7tech.gateway.common.resources.ResourceEntry;
import com.l7tech.gateway.common.resources.ResourceEntryBag;
import com.l7tech.gateway.common.resources.ResourceEntryHeader;
import com.l7tech.gateway.common.resources.ResourceType;
import com.l7tech.gateway.common.service.ServiceAdmin;
import com.l7tech.objectmodel.DeleteException;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.PersistentEntity;
import com.l7tech.objectmodel.SaveException;
import com.l7tech.objectmodel.UpdateException;
import com.l7tech.server.admin.AsyncAdminMethodsImpl;
import com.l7tech.server.event.AdminInfo;
import com.l7tech.server.globalresources.DefaultHttpProxyManager;
import com.l7tech.server.globalresources.HttpConfigurationManager;
import com.l7tech.server.globalresources.ResourceEntryManager;
import com.l7tech.server.globalresources.SchemaResourceManager;
import com.l7tech.server.globalresources.n;
import com.l7tech.server.globalresources.u;
import com.l7tech.server.service.ServiceDocumentResolver;
import com.l7tech.util.Background;
import com.l7tech.util.Charsets;
import com.l7tech.util.Config;
import com.l7tech.util.IOUtils;
import com.l7tech.util.ResourceUtils;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TimerTask;
import java.util.concurrent.FutureTask;
import java.util.logging.Logger;

public class ResourceAdminImpl
extends AsyncAdminMethodsImpl
implements ResourceAdmin {
    private static final Logger e;
    private final Config f;
    private final ResourceEntryManager g;
    private final DefaultHttpProxyManager h;
    private final HttpConfigurationManager i;
    private final ServiceDocumentResolver j;
    private final SchemaResourceManager k;
    private final Map<String, ResourceEntryHeader> l;
    private static final String[] m;

    public ResourceAdminImpl(Config config, ResourceEntryManager resourceEntryManager, DefaultHttpProxyManager defaultHttpProxyManager, HttpConfigurationManager httpConfigurationManager, ServiceDocumentResolver serviceDocumentResolver, SchemaResourceManager schemaResourceManager, Map<String, String> map) {
        this.f = config;
        this.g = resourceEntryManager;
        this.h = defaultHttpProxyManager;
        this.i = httpConfigurationManager;
        this.j = serviceDocumentResolver;
        this.k = schemaResourceManager;
        this.l = ResourceAdminImpl.a(map);
    }

    public Collection<ResourceEntryHeader> findAllResources() throws FindException {
        return this.g.findAllHeaders();
    }

    public ResourceEntry findResourceEntryByPrimaryKey(Goid goid) throws FindException {
        return (ResourceEntry)this.g.findByPrimaryKey(goid);
    }

    public ResourceEntry findResourceEntryByUriAndType(String string, ResourceType resourceType) throws FindException {
        return this.g.findResourceByUriAndType(string, resourceType);
    }

    public Goid saveResourceEntry(ResourceEntry resourceEntry) throws SaveException, UpdateException {
        Goid goid;
        if (Goid.isDefault((Goid)resourceEntry.getGoid())) {
            goid = this.g.save((PersistentEntity)resourceEntry);
        } else {
            goid = resourceEntry.getGoid();
            this.g.update((PersistentEntity)resourceEntry);
        }
        return goid;
    }

    public void saveResourceEntryBag(ResourceEntryBag resourceEntryBag) throws SaveException, UpdateException {
        for (ResourceEntry resourceEntry : resourceEntryBag) {
            this.saveResourceEntry(resourceEntry);
        }
    }

    public void deleteResourceEntry(ResourceEntry resourceEntry) throws DeleteException {
        try {
            if (Goid.isDefault((Goid)resourceEntry.getGoid())) {
                throw new DeleteException(m[0]);
            }
        }
        catch (DeleteException deleteException) {
            throw ResourceAdminImpl.a((Exception)((Object)deleteException));
        }
        this.g.delete((PersistentEntity)resourceEntry);
    }

    public void deleteResourceEntry(Goid goid) throws FindException, DeleteException {
        try {
            if (Goid.isDefault((Goid)goid)) {
                throw new DeleteException(m[0]);
            }
        }
        catch (FindException findException) {
            throw ResourceAdminImpl.a((Exception)((Object)findException));
        }
        this.g.delete(goid);
    }

    public Collection<ResourceEntryHeader> findResourceHeadersByType(ResourceType resourceType) throws FindException {
        return this.g.findHeadersByType(resourceType);
    }

    public ResourceEntryHeader findResourceHeaderByUriAndType(String string, ResourceType resourceType) throws FindException {
        return this.g.findHeaderByUriAndType(string, resourceType);
    }

    public Collection<ResourceEntryHeader> findResourceHeadersByKeyAndType(String string, ResourceType resourceType) throws FindException {
        return this.g.findHeadersByKeyAndType(string, resourceType);
    }

    public Collection<ResourceEntryHeader> findResourceHeadersByTargetNamespace(String string) throws FindException {
        return this.g.findHeadersByTNS(string);
    }

    public Collection<ResourceEntryHeader> findResourceHeadersByPublicIdentifier(String string) throws FindException {
        return this.g.findHeadersByPublicIdentifier(string);
    }

    public ResourceEntry findDefaultResourceByUri(String string) throws FindException {
        ResourceEntry resourceEntry = null;
        for (Map.Entry<String, ResourceEntryHeader> entry : this.l.entrySet()) {
            InputStream inputStream;
            ResourceEntryHeader resourceEntryHeader;
            block6: {
                resourceEntryHeader = entry.getValue();
                if (!resourceEntryHeader.getUri().equals(string)) continue;
                inputStream = null;
                inputStream = ResourceAdminImpl.class.getResourceAsStream(entry.getKey());
                if (inputStream != null) break block6;
                String[] stringArray = m;
                e.warning(stringArray[7] + entry.getKey() + stringArray[5]);
                ResourceUtils.closeQuietly((Closeable)inputStream);
                continue;
            }
            try {
                byte[] byArray = IOUtils.slurpStream((InputStream)inputStream, (int)0x100000);
                resourceEntry = new ResourceEntry();
                resourceEntry.setType(resourceEntryHeader.getResourceType());
                resourceEntry.setContentType(resourceEntryHeader.getResourceType().getMimeType());
                resourceEntry.setUri(resourceEntryHeader.getUri());
                resourceEntry.setContent(new String(byArray, Charsets.UTF8));
                resourceEntry.setResourceKey1(resourceEntryHeader.getResourceKey1());
                resourceEntry.setResourceKey2(resourceEntryHeader.getResourceKey2());
                resourceEntry.setResourceKey3(resourceEntryHeader.getResourceKey3());
            }
            catch (IOException iOException) {
                try {
                    throw new FindException(m[4], (Throwable)iOException);
                }
                catch (Throwable throwable) {
                    ResourceUtils.closeQuietly(inputStream);
                    throw throwable;
                }
            }
            ResourceUtils.closeQuietly((Closeable)inputStream);
            break;
        }
        return resourceEntry;
    }

    public Collection<ResourceEntryHeader> findDefaultResources() throws FindException {
        return new ArrayList<ResourceEntryHeader>(this.l.values());
    }

    public int countRegisteredSchemas(Collection<Goid> collection) throws FindException {
        ArrayList<String> arrayList = new ArrayList<String>();
        for (Goid object : collection) {
            try {
                if (object == null) {
                    continue;
                }
            }
            catch (FindException findException) {
                throw ResourceAdminImpl.a((Exception)((Object)findException));
            }
            String string = (ResourceEntry)this.g.findByPrimaryKey(object);
            try {
                if (string == null) continue;
                arrayList.add(string.getUri());
            }
            catch (FindException findException) {
                throw ResourceAdminImpl.a((Exception)((Object)findException));
            }
        }
        int n2 = 0;
        for (String string : arrayList) {
            try {
                if (!this.k.isSchemaRequired(string)) continue;
                ++n2;
            }
            catch (FindException findException) {
                throw ResourceAdminImpl.a((Exception)((Object)findException));
            }
        }
        return n2;
    }

    public boolean allowSchemaDoctype() {
        return this.f.getBooleanProperty(m[2], false);
    }

    public HttpProxyConfiguration getDefaultHttpProxyConfiguration() throws FindException {
        return this.h.getDefaultHttpProxyConfiguration();
    }

    public void setDefaultHttpProxyConfiguration(HttpProxyConfiguration httpProxyConfiguration) throws SaveException, UpdateException {
        this.h.setDefaultHttpProxyConfiguration(httpProxyConfiguration);
    }

    public Collection<HttpConfiguration> findAllHttpConfigurations() throws FindException {
        return this.i.findAll();
    }

    public HttpConfiguration findHttpConfigurationByPrimaryKey(Goid goid) throws FindException {
        return (HttpConfiguration)this.i.findByPrimaryKey(goid);
    }

    public void deleteHttpConfiguration(HttpConfiguration httpConfiguration) throws DeleteException {
        try {
            if (HttpConfiguration.DEFAULT_GOID.equals((Object)httpConfiguration.getGoid())) {
                throw new DeleteException(m[12]);
            }
        }
        catch (DeleteException deleteException) {
            throw ResourceAdminImpl.a((Exception)((Object)deleteException));
        }
        this.i.delete((PersistentEntity)httpConfiguration);
    }

    public Goid saveHttpConfiguration(HttpConfiguration httpConfiguration) throws SaveException, UpdateException {
        Goid goid;
        if (HttpConfiguration.DEFAULT_GOID.equals((Object)httpConfiguration.getGoid())) {
            goid = this.i.save((PersistentEntity)httpConfiguration);
        } else {
            goid = httpConfiguration.getGoid();
            this.i.update((PersistentEntity)httpConfiguration);
        }
        return goid;
    }

    public String resolveResource(String string) throws IOException {
        ServiceAdmin.DownloadDocumentType downloadDocumentType;
        block10: {
            block9: {
                try {
                    if (string == null) {
                        throw new IOException(m[6]);
                    }
                }
                catch (IOException iOException) {
                    throw ResourceAdminImpl.a(iOException);
                }
                String string2 = string.toLowerCase();
                String[] stringArray = m;
                if (string2.endsWith(stringArray[14])) {
                    downloadDocumentType = ServiceAdmin.DownloadDocumentType.SCHEMA;
                } else {
                    stringArray = m;
                    if (string2.endsWith(stringArray[13])) {
                        downloadDocumentType = ServiceAdmin.DownloadDocumentType.XSL;
                    } else {
                        try {
                            if (!string2.endsWith(m[11]) && !string.endsWith(m[3])) break block9;
                        }
                        catch (IOException iOException) {
                            throw ResourceAdminImpl.a(iOException);
                        }
                        downloadDocumentType = ServiceAdmin.DownloadDocumentType.WSDL;
                    }
                }
                break block10;
            }
            downloadDocumentType = ServiceAdmin.DownloadDocumentType.UNKNOWN;
        }
        return this.j.resolveDocumentTarget(string, downloadDocumentType);
    }

    public AsyncAdminMethods.JobId<String> resolveResourceAsync(String string) {
        FutureTask<String> futureTask = new FutureTask<String>(AdminInfo.find(false).wrapCallable(new n(this, string)));
        Background.scheduleOneShot((TimerTask)new u(this, futureTask), (long)0L);
        return this.registerJob(futureTask, String.class);
    }

    private static Map<String, ResourceEntryHeader> a(Map<String, String> map) {
        HashMap<String, ResourceEntryHeader> hashMap = new HashMap<String, ResourceEntryHeader>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String[] stringArray;
            ResourceType resourceType = null;
            for (ResourceType resourceType2 : ResourceType.values()) {
                if (!entry.getKey().endsWith(resourceType2.getFilenameSuffix())) continue;
                resourceType = resourceType2;
                break;
            }
            if (resourceType == null) {
                stringArray = m;
                e.warning(stringArray[8] + entry.getKey() + stringArray[1]);
                continue;
            }
            stringArray = m;
            String[] stringArray2 = entry.getValue().split(stringArray[10]);
            if (stringArray2.length > 2) {
                e.warning(stringArray[9] + entry.getKey() + stringArray[1]);
            }
            ResourceEntryHeader resourceEntryHeader = new ResourceEntryHeader(Goid.toString((Goid)ResourceEntry.DEFAULT_GOID), stringArray2[0], null, resourceType, stringArray2.length > 1 ? stringArray2[1] : null, null, null, Integer.valueOf(0), null);
            hashMap.put(entry.getKey(), resourceEntryHeader);
        }
        return Collections.unmodifiableMap(hashMap);
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[15];
                var3_1 = 0;
                var2_2 = "q\u0013\u0003]fgOV\u0017\u0001V}vC\u0012\u0017\u0003G`g\u0016\u0012\u001c\u0002G)c\n@\u0001\u0004@}v\u0001F\u0002\u0015\\\u0013A\u0011\u0005VdrAS\u001e\u0001\\~W\u0000Q\u0006\u0014Cl\u0005\r\u0005\u001eWe\u0016w\u0000\u001f\\{3\u0003]\u0013\tZgtO@\u0017\u001e\\|a\fW\u0002\u0015\\\b\\\u0007\u0001_)f\u001d^\u001cv\u0017\u000bR|\u007f\u001b\u0012\u0000\b@ff\u001dQ\u0017M]fgOT\u001d\u0018]m3H,q\u0013\u0003]fgOV\u0017\u0019V{~\u0006\\\u0017MGpc\n\u0012\u0014\u0002A)w\nT\u0013\u0018_}3\u001dW\u0001\u0002F{p\n\u0012U*g\u001c\bKyv\fF\u0017\t\u0013gf\u0002P\u0017\u001f\u0013fuOD\u0013\u0001Fl`OT\u001d\u001f\u0013{v\u001c]\u0007\u001fPl3H\u0002n\u000e\u0005\u001c\u0005\u001eWe$q\u0013\u0003]fgOV\u0017\u0001V}vC\u0012\u0017\u0003G`g\u0016\u0012\u001c\u0002G)c\n@\u0001\u0004@}v\u0001F";
                var4_3 = "q\u0013\u0003]fgOV\u0017\u0001V}vC\u0012\u0017\u0003G`g\u0016\u0012\u001c\u0002G)c\n@\u0001\u0004@}v\u0001F\u0002\u0015\\\u0013A\u0011\u0005VdrAS\u001e\u0001\\~W\u0000Q\u0006\u0014Cl\u0005\r\u0005\u001eWe\u0016w\u0000\u001f\\{3\u0003]\u0013\tZgtO@\u0017\u001e\\|a\fW\u0002\u0015\\\b\\\u0007\u0001_)f\u001d^\u001cv\u0017\u000bR|\u007f\u001b\u0012\u0000\b@ff\u001dQ\u0017M]fgOT\u001d\u0018]m3H,q\u0013\u0003]fgOV\u0017\u0019V{~\u0006\\\u0017MGpc\n\u0012\u0014\u0002A)w\nT\u0013\u0018_}3\u001dW\u0001\u0002F{p\n\u0012U*g\u001c\bKyv\fF\u0017\t\u0013gf\u0002P\u0017\u001f\u0013fuOD\u0013\u0001Fl`OT\u001d\u001f\u0013{v\u001c]\u0007\u001fPl3H\u0002n\u000e\u0005\u001c\u0005\u001eWe$q\u0013\u0003]fgOV\u0017\u0001V}vC\u0012\u0017\u0003G`g\u0016\u0012\u001c\u0002G)c\n@\u0001\u0004@}v\u0001F".length();
                var1_4 = 36;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ResourceAdminImpl.b(v0, ResourceAdminImpl.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "zlx9\u0004zlx1";
                    var4_3 = "zlx9\u0004zlx1".length();
                    var1_4 = 4;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 22;
                        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++] = ResourceAdminImpl.b(v0, ResourceAdminImpl.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;
            }
        }
        ResourceAdminImpl.m = var5;
        ResourceAdminImpl.e = Logger.getLogger(ResourceAdminImpl.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] ^ 0x1F);
        }
        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 -> 66;
                    case 1 -> 2;
                    case 2 -> 29;
                    case 3 -> 67;
                    case 4 -> 121;
                    case 5 -> 99;
                    default -> 31;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

