/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.gateway.common.audit.SystemMessages
 *  com.l7tech.objectmodel.EntityHeader
 *  com.l7tech.objectmodel.EntityManager$UniqueType
 *  com.l7tech.objectmodel.EntityType
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.security.cert.TrustedCert
 *  com.l7tech.security.cert.TrustedCert$TrustedFor
 *  com.l7tech.security.cert.TrustedCertManager
 *  com.l7tech.util.Config
 *  com.l7tech.util.Functions
 *  com.l7tech.util.Functions$Unary
 *  com.l7tech.util.TimeUnit
 *  org.springframework.beans.BeansException
 *  org.springframework.beans.factory.InitializingBean
 *  org.springframework.context.ApplicationContext
 *  org.springframework.dao.DataAccessException
 *  org.springframework.orm.hibernate5.HibernateCallback
 *  org.springframework.transaction.annotation.Propagation
 *  org.springframework.transaction.annotation.Transactional
 */
package com.l7tech.server.identity.cert;

import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.gateway.common.audit.SystemMessages;
import com.l7tech.objectmodel.EntityHeader;
import com.l7tech.objectmodel.EntityManager;
import com.l7tech.objectmodel.EntityType;
import com.l7tech.objectmodel.FindException;
import com.l7tech.security.cert.TrustedCert;
import com.l7tech.security.cert.TrustedCertManager;
import com.l7tech.server.HibernateEntityManager;
import com.l7tech.server.audit.Auditor;
import com.l7tech.server.cluster.ClusterInfoManager;
import com.l7tech.server.identity.cert.g;
import com.l7tech.server.identity.cert.h;
import com.l7tech.server.identity.cert.i;
import com.l7tech.server.identity.cert.j;
import com.l7tech.server.util.ManagedTimer;
import com.l7tech.util.Config;
import com.l7tech.util.Functions;
import com.l7tech.util.TimeUnit;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.auth.x500.X500Principal;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor={Throwable.class})
public class TrustedCertManagerImp
extends HibernateEntityManager<TrustedCert, EntityHeader>
implements TrustedCertManager,
InitializingBean,
PropertyChangeListener {
    private static final Logger m;
    private Auditor n;
    private static final String o;
    private static final long p;
    private static final long q;
    private final ManagedTimer r;
    private final Config s;
    private final ClusterInfoManager t;
    private j u;
    private static final String[] v;

    public TrustedCertManagerImp(Config config, ManagedTimer managedTimer, ClusterInfoManager clusterInfoManager) {
        this.s = config;
        this.r = managedTimer;
        this.t = clusterInfoManager;
    }

    @Transactional(readOnly=true)
    public Collection<TrustedCert> findBySubjectDn(String string) throws FindException {
        String[] stringArray = v;
        StringBuffer stringBuffer = new StringBuffer(stringArray[14]);
        stringBuffer.append(this.getTableName()).append(stringArray[27]).append(this.getImpClass().getName());
        stringBuffer.append(stringArray[12]).append(this.getTableName()).append(stringArray[2]);
        try {
            return (Collection)this.getHibernateTemplate().execute((HibernateCallback)new g(this, stringBuffer, string));
        }
        catch (DataAccessException dataAccessException) {
            m.log(Level.SEVERE, dataAccessException.getMessage(), dataAccessException);
            throw new FindException(v[5], (Throwable)dataAccessException);
        }
    }

    @Transactional(readOnly=true)
    public Collection<TrustedCert> findByName(String string) throws FindException {
        String[] stringArray = v;
        StringBuffer stringBuffer = new StringBuffer(stringArray[15]);
        stringBuffer.append(this.getTableName()).append(stringArray[6]).append(this.getImpClass().getName());
        stringBuffer.append(stringArray[9]).append(this.getTableName()).append(stringArray[10]);
        try {
            return (Collection)this.getHibernateTemplate().execute((HibernateCallback)new h(this, stringBuffer, string));
        }
        catch (DataAccessException dataAccessException) {
            m.log(Level.SEVERE, dataAccessException.getMessage(), dataAccessException);
            throw new FindException(v[19], (Throwable)dataAccessException);
        }
    }

    @Transactional(readOnly=true)
    public Collection<TrustedCert> findByTrustFlag(TrustedCert.TrustedFor trustedFor) throws FindException {
        return Functions.grep(this.findAll(), (Functions.Unary)TrustedCert.TrustedFor.predicate((TrustedCert.TrustedFor)trustedFor));
    }

    /*
     * WARNING - Removed back jump from a try to a catch block - possible behaviour change.
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    @Transactional(readOnly=true)
    public List<TrustedCert> findByThumbprint(String string) throws FindException {
        String[] stringArray = v;
        StringBuffer stringBuffer = new StringBuffer(stringArray[15]);
        try {
            stringBuffer.append(this.getTableName()).append(stringArray[6]).append(this.getImpClass().getName());
            stringBuffer.append(stringArray[18]).append(this.getTableName()).append(stringArray[3]);
            try {
                if (string == null) {
                    stringBuffer.append(stringArray[8]);
                    return this.getHibernateTemplate().find(stringBuffer.toString(), new Object[0]);
                }
            }
            catch (DataAccessException dataAccessException) {
                m.log(Level.SEVERE, dataAccessException.getMessage(), dataAccessException);
                throw new FindException(v[23], (Throwable)dataAccessException);
            }
        }
        catch (FindException findException) {
            throw TrustedCertManagerImp.a((Exception)((Object)findException));
        }
        {
            stringArray = v;
            stringBuffer.append(stringArray[26]);
            return this.getHibernateTemplate().findByNamedParam(stringBuffer.toString(), stringArray[11], (Object)string.trim());
        }
    }

    /*
     * WARNING - Removed back jump from a try to a catch block - possible behaviour change.
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    @Transactional(readOnly=true)
    public List<TrustedCert> findBySki(String string) throws FindException {
        String[] stringArray = v;
        StringBuffer stringBuffer = new StringBuffer(stringArray[15]);
        try {
            stringBuffer.append(this.getTableName()).append(stringArray[6]).append(this.getImpClass().getName());
            stringBuffer.append(stringArray[18]).append(this.getTableName()).append(stringArray[16]);
            try {
                if (string == null) {
                    stringBuffer.append(stringArray[17]);
                    return this.getHibernateTemplate().find(stringBuffer.toString(), new Object[0]);
                }
            }
            catch (DataAccessException dataAccessException) {
                m.log(Level.SEVERE, dataAccessException.getMessage(), dataAccessException);
                throw new FindException(v[20], (Throwable)dataAccessException);
            }
        }
        catch (FindException findException) {
            throw TrustedCertManagerImp.a((Exception)((Object)findException));
        }
        {
            stringArray = v;
            stringBuffer.append(stringArray[24]);
            return this.getHibernateTemplate().findByNamedParam(stringBuffer.toString(), stringArray[13], (Object)string.trim());
        }
    }

    public List<TrustedCert> findByIssuerAndSerial(X500Principal x500Principal, BigInteger bigInteger) throws FindException {
        return (List)this.getHibernateTemplate().execute((HibernateCallback)new i(this, x500Principal, bigInteger));
    }

    public Class<TrustedCert> getImpClass() {
        return TrustedCert.class;
    }

    @Override
    public Class<TrustedCert> getInterfaceClass() {
        return TrustedCert.class;
    }

    @Override
    public String getTableName() {
        return v[25];
    }

    @Override
    public EntityType getEntityType() {
        return EntityType.TRUSTED_CERT;
    }

    protected void initDao() throws Exception {
        long l2;
        String[] stringArray = v;
        String string = this.s.getProperty(stringArray[1]);
        try {
            l2 = TimeUnit.parse((String)string);
        }
        catch (Exception exception) {
            String[] stringArray2 = new String[2];
            stringArray2[0] = string;
            stringArray = v;
            stringArray2[1] = stringArray[7];
            this.n.logAndAudit((AuditDetailMessage)SystemMessages.CERT_EXPIRY_BAD_PERIOD, stringArray2);
            l2 = TimeUnit.parse((String)stringArray[21]);
        }
        this.a(l2, false);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void a(long l2, boolean bl2) {
        long l3;
        ManagedTimer managedTimer = this.r;
        synchronized (managedTimer) {
            if (this.u != null) {
                this.u.cancel();
            }
            this.u = new j(this);
        }
        try {
            l3 = bl2 ? 0L : TimeUnit.MINUTES.toMillis(10L);
        }
        catch (DataAccessException dataAccessException) {
            throw TrustedCertManagerImp.a((Exception)((Object)dataAccessException));
        }
        long l4 = l3;
        this.r.schedule((TimerTask)this.u, l4, l2);
    }

    @Override
    protected Collection<Map<String, Object>> getUniqueConstraints(final TrustedCert trustedCert) {
        HashMap<String, Object> hashMap = new HashMap<String, Object>(){
            static final long serialVersionUID = -5094139241417303334L;
            private static final String a = 4.a(111, 4.a("\u0016\u0017l,\u000bh+\u000b\u0011m\u0012\u0001yh"));
            {
                this.put(a, trustedCert.getThumbprintSha1());
            }

            private static char[] a(String string) {
                char[] cArray = string.toCharArray();
                if (cArray.length < 2) {
                    cArray = cArray;
                    cArray[0] = (char)(cArray[0] ^ 0x36);
                }
                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 -> 13;
                            case 1 -> 16;
                            case 2 -> 118;
                            case 3 -> 46;
                            case 4 -> 6;
                            case 5 -> 119;
                            default -> 54;
                        })));
                        ++n3;
                        n4 = n4;
                    } while (n4 > n3);
                }
                return new String(cArray2).intern();
            }
        };
        return Collections.singletonList(hashMap);
    }

    @Override
    protected EntityManager.UniqueType getUniqueType() {
        return EntityManager.UniqueType.OTHER;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.n = new Auditor(this, applicationContext, m);
        this.applicationContext = applicationContext;
    }

    /*
     * Loose catch block
     */
    @Override
    public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
        block6: {
            String string;
            block8: {
                block7: {
                    if (!v[4].equals(propertyChangeEvent.getPropertyName())) break block6;
                    try {
                        block9: {
                            if (propertyChangeEvent.getOldValue() != null) break block7;
                            break block9;
                            catch (Exception exception) {
                                throw TrustedCertManagerImp.a(exception);
                            }
                        }
                        string = null;
                        break block8;
                    }
                    catch (Exception exception) {
                        throw TrustedCertManagerImp.a(exception);
                    }
                }
                string = propertyChangeEvent.getOldValue().toString();
            }
            String string2 = string;
            String string3 = propertyChangeEvent.getNewValue().toString();
            try {
                this.a(TimeUnit.parse((String)string3), true);
            }
            catch (Exception exception) {
                this.n.logAndAudit((AuditDetailMessage)SystemMessages.CERT_EXPIRY_BAD_PERIOD, string3, string2);
            }
        }
    }

    static /* synthetic */ ApplicationContext access$000(TrustedCertManagerImp trustedCertManagerImp) {
        return trustedCertManagerImp.applicationContext;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[28];
                var3_1 = 0;
                var2_2 = "\tN\u001dNXN4Z]XyOI3\u0000]DJCI>mPYYAk\"\\QS^\u0017\u0014YN%D]_NnUg\u0013\u0018\u0006I_Y-K[H~D\u0010\u0014^S2CZLHCU3}P]\u000b\n\u001dNXN4Z]XyOI3\u0000]DJCI>mPYYAk\"\\QS^\u0016yEN+JV\u001bN\nI\"ZJU_\\^gM]NN\n\u001acugmt}iy\u001b\u0003\u000b\u0018S\u0007SY\u001b)[TP\r\u001a}s\u0002|}\u001cozk\u0002|\u0010\u000e\u0014DZ*K\u0011\u001c\u0007\n\u0001)OUY\u000eNBN*LHNSDO\u0014FY\r\u0007\u001a}s\u0002|}\u001c\u0003IAR\u0005|xt\n\u000e\u0005|xt\n\u000e\u0005\u0014YP.\u000e\u0007SY\u001b)[TP\u0007\u001a}s\u0002|}\u001c\u0016yEN+JV\u001bN\nI\"ZJU_\\^gM]NN\u0015yEN+JV\u001bN\n].@\\\u001cYOI3\u0006K\u0015\u0003\u000b\u0018S\u0002\tB\u0015yEN+JV\u001bN\n].@\\\u001cYOI3\u0006K\u0015\u0007\u001a\u0017\u001b}]SU\fNXN4Z]XeI^5Z";
                var4_3 = "\tN\u001dNXN4Z]XyOI3\u0000]DJCI>mPYYAk\"\\QS^\u0017\u0014YN%D]_NnUg\u0013\u0018\u0006I_Y-K[H~D\u0010\u0014^S2CZLHCU3}P]\u000b\n\u001dNXN4Z]XyOI3\u0000]DJCI>mPYYAk\"\\QS^\u0016yEN+JV\u001bN\nI\"ZJU_\\^gM]NN\n\u001acugmt}iy\u001b\u0003\u000b\u0018S\u0007SY\u001b)[TP\r\u001a}s\u0002|}\u001cozk\u0002|\u0010\u000e\u0014DZ*K\u0011\u001c\u0007\n\u0001)OUY\u000eNBN*LHNSDO\u0014FY\r\u0007\u001a}s\u0002|}\u001c\u0003IAR\u0005|xt\n\u000e\u0005|xt\n\u000e\u0005\u0014YP.\u000e\u0007SY\u001b)[TP\u0007\u001a}s\u0002|}\u001c\u0016yEN+JV\u001bN\nI\"ZJU_\\^gM]NN\u0015yEN+JV\u001bN\n].@\\\u001cYOI3\u0006K\u0015\u0003\u000b\u0018S\u0002\tB\u0015yEN+JV\u001bN\n].@\\\u001cYOI3\u0006K\u0015\u0007\u001a\u0017\u001b}]SU\fNXN4Z]XeI^5Z".length();
                var1_4 = 2;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = TrustedCertManagerImp.b(v0, TrustedCertManagerImp.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "al`\u0006!+2,30N<-3\u00129!\r\na\u0018\u000e\u001c\u0016\u000f\u0006\u0012\u0002`";
                    var4_3 = "al`\u0006!+2,30N<-3\u00129!\r\na\u0018\u000e\u001c\u0016\u000f\u0006\u0012\u0002`".length();
                    var1_4 = 18;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 70;
                        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++] = TrustedCertManagerImp.b(v0, TrustedCertManagerImp.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;
            }
        }
        TrustedCertManagerImp.v = var5;
        TrustedCertManagerImp.o = TrustedCertManagerImp.v[21];
        TrustedCertManagerImp.m = Logger.getLogger(TrustedCertManagerImp.class.getName());
        var0_6 = TrustedCertManagerImp.v;
        TrustedCertManagerImp.p = TimeUnit.parse((String)var0_6[0]);
        TrustedCertManagerImp.q = TimeUnit.parse((String)var0_6[22]);
    }

    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] ^ '\u0001');
        }
        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 -> 7;
                    case 1 -> 23;
                    case 2 -> 6;
                    case 3 -> 122;
                    case 4 -> 19;
                    case 5 -> 5;
                    default -> 1;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

