/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.folder.Folder
 *  com.l7tech.policy.Policy
 *  com.l7tech.policy.assertion.Assertion
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.util.Functions$Nullary
 *  com.l7tech.util.ResourceUtils
 *  com.l7tech.util.TimeSource
 */
package com.l7tech.server.policy;

import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.folder.Folder;
import com.l7tech.policy.Policy;
import com.l7tech.policy.assertion.Assertion;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.message.metrics.GatewayMetricsUtils;
import com.l7tech.server.message.metrics.LatencyMetrics;
import com.l7tech.server.policy.PolicyMetadata;
import com.l7tech.server.policy.PolicyUniqueIdentifier;
import com.l7tech.server.policy.ServerPolicyHandle;
import com.l7tech.server.policy.ServerPolicyMetadata;
import com.l7tech.server.policy.am;
import com.l7tech.server.policy.assertion.AssertionStatusException;
import com.l7tech.server.policy.assertion.ServerAssertion;
import com.l7tech.server.util.AbstractReferenceCounted;
import com.l7tech.util.Functions;
import com.l7tech.util.ResourceUtils;
import com.l7tech.util.TimeSource;
import java.io.Closeable;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ServerPolicy
extends AbstractReferenceCounted<ServerPolicyHandle> {
    private final PolicyMetadata c;
    private final ServerPolicyMetadata d;
    private final ServerAssertion e;
    private final TimeSource f = new TimeSource();
    private static final String[] g;

    public ServerPolicy(Policy policy, PolicyMetadata policyMetadata, Set<Goid> set, Map<Goid, Integer> map, ServerAssertion serverAssertion, Functions.Nullary<Collection<Folder>> nullary) {
        if (policy == null) {
            String[] stringArray = g;
            throw new IllegalArgumentException(stringArray[4]);
        }
        try {
            if (policyMetadata == null) {
                throw new IllegalArgumentException(g[0]);
            }
        }
        catch (IllegalArgumentException illegalArgumentException) {
            throw ServerPolicy.a(illegalArgumentException);
        }
        try {
            if (set == null) {
                throw new IllegalArgumentException(g[5]);
            }
        }
        catch (IllegalArgumentException illegalArgumentException) {
            throw ServerPolicy.a(illegalArgumentException);
        }
        try {
            if (map == null) {
                throw new IllegalArgumentException(g[6]);
            }
        }
        catch (IllegalArgumentException illegalArgumentException) {
            throw ServerPolicy.a(illegalArgumentException);
        }
        try {
            if (serverAssertion == null) {
                throw new IllegalArgumentException(g[3]);
            }
        }
        catch (IllegalArgumentException illegalArgumentException) {
            throw ServerPolicy.a(illegalArgumentException);
        }
        try {
            if (nullary == null) {
                throw new IllegalArgumentException(g[1]);
            }
        }
        catch (IllegalArgumentException illegalArgumentException) {
            throw ServerPolicy.a(illegalArgumentException);
        }
        this.d = new am(policy, this.a(policy, map, set), set, nullary);
        this.c = policyMetadata;
        this.e = serverAssertion;
    }

    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws PolicyAssertionException, IOException {
        long l2;
        AssertionStatus assertionStatus;
        try {
            if (policyEnforcementContext != null) {
                policyEnforcementContext.assertionStarting(this.e);
            }
        }
        catch (AssertionStatusException assertionStatusException) {
            throw ServerPolicy.a(assertionStatusException);
        }
        long l3 = this.f.currentTimeMillis();
        try {
            assertionStatus = this.e.checkRequest(policyEnforcementContext);
        }
        catch (AssertionStatusException assertionStatusException) {
            assertionStatus = assertionStatusException.getAssertionStatus();
        }
        catch (Throwable throwable) {
            try {
                if (policyEnforcementContext != null) {
                    GatewayMetricsUtils.publishAssertionFinish(policyEnforcementContext, this.e, new LatencyMetrics(l3, this.f.currentTimeMillis()));
                }
            }
            catch (AssertionStatusException assertionStatusException) {
                throw ServerPolicy.a(assertionStatusException);
            }
            throw throwable;
        }
        finally {
            l2 = this.f.currentTimeMillis();
        }
        try {
            if (policyEnforcementContext != null) {
                policyEnforcementContext.assertionFinished(this.e, assertionStatus, new LatencyMetrics(l3, l2));
            }
        }
        catch (AssertionStatusException assertionStatusException) {
            throw ServerPolicy.a(assertionStatusException);
        }
        return assertionStatus;
    }

    public Assertion getPolicyAssertion() throws IOException {
        return this.d.getPolicy().getAssertion();
    }

    @Override
    protected ServerPolicyHandle createHandle() {
        return new ServerPolicyHandle(this, this.c, this.d);
    }

    @Override
    protected void doClose() {
        ResourceUtils.closeQuietly((Closeable)this.e);
    }

    private PolicyUniqueIdentifier a(Policy policy, Map<Goid, Integer> map, Set<Goid> set) {
        HashMap<Goid, Integer> hashMap = new HashMap<Goid, Integer>();
        for (Goid goid : set) {
            Integer n2 = map.get(goid);
            try {
                if (n2 == null) {
                    throw new IllegalArgumentException(g[2] + String.valueOf(goid));
                }
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw ServerPolicy.a(illegalArgumentException);
            }
            hashMap.put(goid, n2);
        }
        return new PolicyUniqueIdentifier(policy.getGoid(), policy.getVersion(), hashMap);
    }

    private static RuntimeException a(RuntimeException runtimeException) {
        return runtimeException;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[7];
                var3_1 = 0;
                var2_2 = "=;\u001b4_\\]( \u00169]Qqm9\u0002.H\u0005~\" W?Y\u0005~88\u001b#+;\u001b9YW@, \u001f\u001e]I|/5\u00146\u001cHe> W3SQ0/1W3II|%\u0000=\u0004.UKwm\"\u0012/OL\u007f#t\u00112N\u0005`\"8\u001e>E\u0005g$ \u001f}[Jy)t\u001e?;\u0018)}Vc(&\u00034SK0 !\u0004)\u001cK\u007f9t\u00158\u001cKe!8\u0017=;\u001b4_\\0 !\u0004)\u001cK\u007f9t\u00158\u001cKe!8";
                var4_3 = "=;\u001b4_\\]( \u00169]Qqm9\u0002.H\u0005~\" W?Y\u0005~88\u001b#+;\u001b9YW@, \u001f\u001e]I|/5\u00146\u001cHe> W3SQ0/1W3II|%\u0000=\u0004.UKwm\"\u0012/OL\u007f#t\u00112N\u0005`\"8\u001e>E\u0005g$ \u001f}[Jy)t\u001e?;\u0018)}Vc(&\u00034SK0 !\u0004)\u001cK\u007f9t\u00158\u001cKe!8\u0017=;\u001b4_\\0 !\u0004)\u001cK\u007f9t\u00158\u001cKe!8".length();
                var1_4 = 31;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerPolicy.a(v0, ServerPolicy.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "poZq$\u00024l\u007fF\\\u0010\u001exhiLaT\u00037q<]pT\u0003-ip\"ayOp\u001a\t=khip\u0006\u001e1jrL5\u0019\u0018+q<Qz\u0000M:`<Q`\u0018\u0001";
                    var4_3 = "poZq$\u00024l\u007fF\\\u0010\u001exhiLaT\u00037q<]pT\u0003-ip\"ayOp\u001a\t=khip\u0006\u001e1jrL5\u0019\u0018+q<Qz\u0000M:`<Q`\u0018\u0001".length();
                    var1_4 = 30;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 102;
                        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++] = ServerPolicy.a(v0, ServerPolicy.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;
            }
        }
        ServerPolicy.g = var5;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x3E);
        }
        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 -> 99;
                    case 1 -> 122;
                    case 2 -> 89;
                    case 3 -> 115;
                    case 4 -> 18;
                    case 5 -> 11;
                    default -> 62;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

