/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.ByteLimitInputStream$DataSizeLimitExceededException
 *  com.l7tech.common.mime.NoSuchPartException
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.message.Message
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.RequestSizeLimit
 *  com.l7tech.policy.variable.Syntax
 */
package com.l7tech.server.policy.assertion;

import com.l7tech.common.io.ByteLimitInputStream;
import com.l7tech.common.mime.NoSuchPartException;
import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.Message;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.RequestSizeLimit;
import com.l7tech.policy.variable.Syntax;
import com.l7tech.server.message.AuthenticationContext;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractMessageTargetableServerAssertion;
import com.l7tech.server.policy.variable.ExpandVariables;
import java.io.IOException;

public class ServerRequestSizeLimit
extends AbstractMessageTargetableServerAssertion<RequestSizeLimit> {
    private final boolean d;
    private final String e;
    private static final String[] f;

    public ServerRequestSizeLimit(RequestSizeLimit requestSizeLimit) {
        super(requestSizeLimit);
        this.d = requestSizeLimit.isEntireMessage();
        this.e = requestSizeLimit.getLimit();
    }

    @Override
    protected AssertionStatus doCheckRequest(PolicyEnforcementContext policyEnforcementContext, Message message, String string, AuthenticationContext authenticationContext) throws IOException, PolicyAssertionException {
        long l2;
        try {
            l2 = this.a(policyEnforcementContext);
        }
        catch (NumberFormatException numberFormatException) {
            String[] stringArray = new String[2];
            stringArray[0] = this.e;
            String[] stringArray2 = f;
            stringArray[1] = stringArray2[1];
            this.logAndAudit((AuditDetailMessage)AssertionMessages.VARIABLE_INVALID_VALUE, stringArray);
            return AssertionStatus.FAILED;
        }
        if (this.d) {
            long l3;
            try {
                message.getMimeKnob().setContentLengthLimit(l2);
                l3 = message.getMimeKnob().getContentLength();
            }
            catch (ByteLimitInputStream.DataSizeLimitExceededException dataSizeLimitExceededException) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.MESSAGE_BODY_TOO_LARGE, ((RequestSizeLimit)this.assertion).getTargetName());
                return AssertionStatus.FALSIFIED;
            }
            catch (IOException iOException) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_SEVERE_WITH_MORE_INFO, new String[]{iOException.getMessage()}, iOException);
                return AssertionStatus.SERVER_ERROR;
            }
            try {
                if (l3 > l2) {
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.MESSAGE_BODY_TOO_LARGE, ((RequestSizeLimit)this.assertion).getTargetName());
                    return AssertionStatus.FALSIFIED;
                }
            }
            catch (NumberFormatException numberFormatException) {
                throw ServerRequestSizeLimit.a(numberFormatException);
            }
            return AssertionStatus.NONE;
        }
        try {
            long l4 = message.getMimeKnob().getFirstPart().getActualContentLength();
            if (l4 > l2) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.MESSAGE_FIRST_PART_TOO_LARGE, ((RequestSizeLimit)this.assertion).getTargetName());
                return AssertionStatus.FALSIFIED;
            }
            return AssertionStatus.NONE;
        }
        catch (NoSuchPartException noSuchPartException) {
            String[] stringArray = new String[1];
            String[] stringArray3 = f;
            stringArray[0] = stringArray3[2] + noSuchPartException.getWhatWasMissing() + stringArray3[0];
            this.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_INFO_WITH_MORE_INFO, stringArray, noSuchPartException);
            return AssertionStatus.FALSIFIED;
        }
    }

    private long a(PolicyEnforcementContext policyEnforcementContext) throws NumberFormatException {
        long l2;
        String[] stringArray = Syntax.getReferencedNames((String)this.e);
        if (stringArray.length > 0) {
            String string = ExpandVariables.process(this.e, policyEnforcementContext.getVariableMap(stringArray, this.getAudit()), this.getAudit());
            l2 = Long.parseLong(string) * 1024L;
        } else {
            l2 = Long.parseLong(this.e) * 1024L;
        }
        return l2;
    }

    private static NumberFormatException a(NumberFormatException numberFormatException) {
        return numberFormatException;
    }

    /*
     * Enabled aggressive block sorting
     */
    static {
        String[] stringArray = new String[3];
        int n2 = 0;
        String string = "\u000fzFC,$qX}Z\u0016,/%\u0011u\u0015\u0017*.%\n~D\u0016'8q\u00044t[\u0004\u0018,sPC0.t\rrG\u0006&kd\foT\u0000*&`\u0016o\u0015";
        int n3 = "\u000fzFC,$qX}Z\u0016,/%\u0011u\u0015\u0017*.%\n~D\u0016'8q\u00044t[\u0004\u0018,sPC0.t\rrG\u0006&kd\foT\u0000*&`\u0016o\u0015".length();
        int n4 = 28;
        int n5 = -1;
        while (true) {
            int n6 = ++n5;
            stringArray[n2++] = ServerRequestSizeLimit.c(35, ServerRequestSizeLimit.a(string.substring(n6, n6 + n4)));
            if ((n5 += n4) >= n3) {
                f = stringArray;
                return;
            }
            n4 = string.charAt(n5);
        }
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x26);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String c(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 -> 91;
                    case 1 -> 56;
                    case 2 -> 22;
                    case 3 -> 64;
                    case 4 -> 97;
                    case 5 -> 104;
                    default -> 38;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

