/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.mime.ContentTypeHeader
 *  com.l7tech.common.mime.NoSuchPartException
 *  com.l7tech.common.mime.PartInfo
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.message.Message
 *  com.l7tech.message.MimeKnob
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.HttpFormPost
 *  com.l7tech.policy.assertion.InverseHttpFormPost
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.util.Charsets
 *  com.l7tech.util.IOUtils
 *  com.l7tech.util.PoolByteArrayOutputStream
 *  org.springframework.context.ApplicationContext
 */
package com.l7tech.server.policy.assertion;

import com.l7tech.common.mime.ContentTypeHeader;
import com.l7tech.common.mime.NoSuchPartException;
import com.l7tech.common.mime.PartInfo;
import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.Message;
import com.l7tech.message.MimeKnob;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.HttpFormPost;
import com.l7tech.policy.assertion.InverseHttpFormPost;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.server.StashManagerFactory;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractServerAssertion;
import com.l7tech.util.Charsets;
import com.l7tech.util.IOUtils;
import com.l7tech.util.PoolByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import org.springframework.context.ApplicationContext;

public class ServerInverseHttpFormPost
extends AbstractServerAssertion<InverseHttpFormPost> {
    private final StashManagerFactory d;
    private static final Charset e;
    private final ContentTypeHeader f;
    private static final String[] g;

    public ServerInverseHttpFormPost(InverseHttpFormPost inverseHttpFormPost, ApplicationContext applicationContext) {
        super(inverseHttpFormPost);
        String[] stringArray = g;
        this.d = (StashManagerFactory)applicationContext.getBean(stringArray[0], StashManagerFactory.class);
        try {
            this.f = ContentTypeHeader.parseValue((String)(stringArray[1] + HttpFormPost.X_WWW_FORM_URLENCODED));
        }
        catch (IOException iOException) {
            throw new RuntimeException(iOException);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        AssertionStatus assertionStatus;
        Message message = policyEnforcementContext.getRequest();
        MimeKnob mimeKnob = message.getMimeKnob();
        PoolByteArrayOutputStream poolByteArrayOutputStream = null;
        try {
            poolByteArrayOutputStream = new PoolByteArrayOutputStream(512);
            for (int i2 = 0; i2 < ((InverseHttpFormPost)this.assertion).getFieldNames().length; ++i2) {
                ContentTypeHeader contentTypeHeader;
                String string = ((InverseHttpFormPost)this.assertion).getFieldNames()[i2];
                try {
                    PartInfo partInfo = mimeKnob.getPart(i2);
                    contentTypeHeader = partInfo.getContentType();
                    InputStream inputStream = partInfo.getInputStream(false);
                    byte[] byArray = IOUtils.slurpStream((InputStream)inputStream);
                    try {
                        poolByteArrayOutputStream.write(string.getBytes(e));
                        poolByteArrayOutputStream.write("=".getBytes());
                        poolByteArrayOutputStream.write(URLEncoder.encode(new String(byArray, contentTypeHeader.getEncoding()), e.name()).getBytes(e));
                        if (i2 >= ((InverseHttpFormPost)this.assertion).getFieldNames().length - 1) continue;
                        poolByteArrayOutputStream.write("&".getBytes());
                        continue;
                    }
                    catch (NoSuchPartException noSuchPartException) {
                        throw ServerInverseHttpFormPost.a(noSuchPartException);
                    }
                }
                catch (NoSuchPartException noSuchPartException) {
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.INVERSE_HTTPFORM_NO_SUCH_PART, Integer.toString(i2));
                    contentTypeHeader = AssertionStatus.FAILED;
                    try {
                        if (poolByteArrayOutputStream == null) return contentTypeHeader;
                        poolByteArrayOutputStream.close();
                        return contentTypeHeader;
                    }
                    catch (NoSuchPartException noSuchPartException2) {
                        throw ServerInverseHttpFormPost.a(noSuchPartException2);
                    }
                }
            }
            message.initialize(this.d.createStashManager(), this.f, (InputStream)new ByteArrayInputStream(poolByteArrayOutputStream.toByteArray()));
            assertionStatus = AssertionStatus.NONE;
        }
        catch (Throwable throwable) {
            try {
                if (poolByteArrayOutputStream == null) throw throwable;
                poolByteArrayOutputStream.close();
                throw throwable;
            }
            catch (NoSuchPartException noSuchPartException) {
                throw ServerInverseHttpFormPost.a(noSuchPartException);
            }
        }
        try {
            if (poolByteArrayOutputStream == null) return assertionStatus;
            poolByteArrayOutputStream.close();
            return assertionStatus;
        }
        catch (NoSuchPartException noSuchPartException) {
            throw ServerInverseHttpFormPost.a(noSuchPartException);
        }
    }

    /*
     * Enabled aggressive block sorting
     */
    static {
        String[] stringArray = new String[2];
        int n2 = 0;
        String string = "y :G,5\u001ed5<Q6>\u001ei 4F=\fk$+X-\u001b\u001e~=4Zk";
        int n3 = "y :G,5\u001ed5<Q6>\u001ei 4F=\fk$+X-\u001b\u001e~=4Zk".length();
        int n4 = 19;
        int n5 = -1;
        while (true) {
            int n6 = ++n5;
            stringArray[n2++] = ServerInverseHttpFormPost.b(19, ServerInverseHttpFormPost.a(string.substring(n6, n6 + n4)));
            if ((n5 += n4) >= n3) {
                g = stringArray;
                e = Charsets.UTF8;
                return;
            }
            n4 = string.charAt(n5);
        }
    }

    private static NoSuchPartException a(NoSuchPartException noSuchPartException) {
        return noSuchPartException;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x6C);
        }
        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 -> 25;
                    case 1 -> 71;
                    case 2 -> 72;
                    case 3 -> 39;
                    case 4 -> 87;
                    case 5 -> 107;
                    default -> 108;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

