/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.security.keystore.SsgKeyEntry
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.util.Background
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.OpaqueId
 *  org.springframework.beans.BeansException
 *  org.springframework.context.ApplicationContext
 *  org.springframework.context.ApplicationContextAware
 */
package com.l7tech.server.audit;

import com.l7tech.gateway.common.security.keystore.SsgKeyEntry;
import com.l7tech.objectmodel.Goid;
import com.l7tech.server.DefaultKey;
import com.l7tech.server.audit.AuditExporter;
import com.l7tech.server.audit.aw;
import com.l7tech.server.audit.q;
import com.l7tech.util.Background;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.OpaqueId;
import java.io.IOException;
import java.io.InputStream;
import java.security.PrivateKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TimerTask;
import java.util.logging.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class AuditDownloadManager
implements ApplicationContextAware {
    private static final Logger a;
    private static final long b = 90000L;
    private static final int c = 8192;
    private static Map<OpaqueId, q> d;
    private static final long e = 10000L;
    private static final long f = 250L;
    private ApplicationContext g;
    private final DefaultKey h;
    private static boolean i;
    private static final TimerTask j;
    private static final String[] k;

    public AuditDownloadManager(DefaultKey defaultKey) {
        this.h = defaultKey;
    }

    public OpaqueId createDownloadContext(long l2, long l3, Goid[] goidArray) throws IOException {
        PrivateKey privateKey;
        X509Certificate x509Certificate;
        SsgKeyEntry ssgKeyEntry;
        try {
            SsgKeyEntry ssgKeyEntry2 = ssgKeyEntry = this.h == null ? null : this.h.getServerSslInfo();
        }
        catch (UnrecoverableKeyException unrecoverableKeyException) {
            throw AuditDownloadManager.a(unrecoverableKeyException);
        }
        if (ssgKeyEntry != null) {
            x509Certificate = ssgKeyEntry.getCertificateChain()[0];
            try {
                privateKey = ssgKeyEntry.getPrivate();
            }
            catch (UnrecoverableKeyException unrecoverableKeyException) {
                String[] stringArray = k;
                throw new RuntimeException(stringArray[4] + ExceptionUtils.getMessage((Throwable)unrecoverableKeyException), unrecoverableKeyException);
            }
        } else {
            String[] stringArray = k;
            throw new RuntimeException(stringArray[0]);
        }
        AuditExporter auditExporter = (AuditExporter)this.g.getBean(k[2], AuditExporter.class);
        q q2 = new q(l2, l3, goidArray, 0, auditExporter, x509Certificate, privateKey);
        q2.checkForException();
        d.put(q2.a(), q2);
        return q2.a();
    }

    public void closeDownloadContext(OpaqueId opaqueId) {
        q q2 = d.get(opaqueId);
        try {
            if (q2 != null) {
                q2.close();
            }
        }
        catch (RuntimeException runtimeException) {
            throw AuditDownloadManager.a(runtimeException);
        }
    }

    public InputStream getDownloadInputStream(OpaqueId opaqueId) {
        q q2 = d.get(opaqueId);
        try {
            if (q2 == null) {
                throw new RuntimeException(k[3]);
            }
        }
        catch (RuntimeException runtimeException) {
            throw AuditDownloadManager.a(runtimeException);
        }
        q2.checkForException();
        return q2.c();
    }

    public long getEstimatedTotalAudits(OpaqueId opaqueId) {
        q q2 = d.get(opaqueId);
        try {
            if (q2 == null) {
                throw new RuntimeException(k[3]);
            }
        }
        catch (RuntimeException runtimeException) {
            throw AuditDownloadManager.a(runtimeException);
        }
        return q2.f();
    }

    public long getDownloadedAuditCount(OpaqueId opaqueId) {
        q q2 = d.get(opaqueId);
        try {
            if (q2 == null) {
                throw new RuntimeException(k[1]);
            }
        }
        catch (RuntimeException runtimeException) {
            throw AuditDownloadManager.a(runtimeException);
        }
        return q2.g();
    }

    public byte[] nextDownloadChunk(OpaqueId opaqueId) {
        q q2 = d.get(opaqueId);
        try {
            if (q2 == null) {
                throw new RuntimeException(k[3]);
            }
        }
        catch (RuntimeException runtimeException) {
            throw AuditDownloadManager.a(runtimeException);
        }
        q2.checkForException();
        byte[] byArray = q2.e();
        try {
            if (byArray == null) {
                q2.close();
            }
        }
        catch (RuntimeException runtimeException) {
            throw AuditDownloadManager.a(runtimeException);
        }
        q2.checkForException();
        return byArray;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void a() {
        TimerTask timerTask = j;
        synchronized (timerTask) {
            if (!i) {
                i = true;
                Background.scheduleRepeated((TimerTask)j, (long)1011L, (long)1011L);
            }
        }
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.g = applicationContext;
        this.a();
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[5];
                var3_1 = 0;
                var2_2 = "\u0016\u000bs?I\u001e\u00187\n2.L\u001cVc\u0000j-J\tL&\u00012<P\u001fQ7\u0016(}K\u0014\u0018'\u0000t<P\u0017Lc6A\u0011\u0005\u0010]:E{.\u0005\u0018M1\u0017w3Q\u0017Ac\u0001w.L\u001cV\"\u0011w9#\r\n2.P\u0018Pc\u0001}*K\u0017W\"\u00012>J\u0015L&\u001df}L\b\u00183\u0000|9L\u0015_\r\"\u0010v4Q>@3\n`)@\t";
                var4_3 = "\u0016\u000bs?I\u001e\u00187\n2.L\u001cVc\u0000j-J\tL&\u00012<P\u001fQ7\u0016(}K\u0014\u0018'\u0000t<P\u0017Lc6A\u0011\u0005\u0010]:E{.\u0005\u0018M1\u0017w3Q\u0017Ac\u0001w.L\u001cV\"\u0011w9#\r\n2.P\u0018Pc\u0001}*K\u0017W\"\u00012>J\u0015L&\u001df}L\b\u00183\u0000|9L\u0015_\r\"\u0010v4Q>@3\n`)@\t".length();
                var1_4 = 74;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = AuditDownloadManager.a(v0, AuditDownloadManager.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\t\u000e6*T\u001cTg\u0005y.O\u0013S&\u00056:N\u0011H\"\u0019byH\f\u001c7\u0004x=H\u0011[=\u0012\u000fw;M\u001a\u001c3\u000e68B\u001cY4\u00126=D\u0019]2\rbyr,pg\ns \u0001\u0019S5Ae0F\u0011U)\u000668T\u001bU3Ar6V\u0011P(\u0000rc\u0001";
                    var4_3 = "\t\u000e6*T\u001cTg\u0005y.O\u0013S&\u00056:N\u0011H\"\u0019byH\f\u001c7\u0004x=H\u0011[=\u0012\u000fw;M\u001a\u001c3\u000e68B\u001cY4\u00126=D\u0019]2\rbyr,pg\ns \u0001\u0019S5Ae0F\u0011U)\u000668T\u001bU3Ar6V\u0011P(\u0000rc\u0001".length();
                    var1_4 = 35;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 31;
                        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++] = AuditDownloadManager.a(v0, AuditDownloadManager.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;
            }
        }
        AuditDownloadManager.k = var5;
        AuditDownloadManager.a = Logger.getLogger(AuditDownloadManager.class.getName());
        AuditDownloadManager.d = Collections.synchronizedMap(new HashMap<K, V>());
        AuditDownloadManager.i = false;
        AuditDownloadManager.j = new aw();
    }

    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] ^ 0x23);
        }
        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 -> 88;
                    case 1 -> 126;
                    case 2 -> 9;
                    case 3 -> 70;
                    case 4 -> 62;
                    case 5 -> 96;
                    default -> 35;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

