/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.http.HttpConstants
 *  com.l7tech.common.mime.ContentTypeHeader
 *  com.l7tech.common.mime.NoSuchPartException
 *  com.l7tech.common.mime.PartInfo
 *  com.l7tech.common.mime.StashManager
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.gateway.common.audit.Messages
 *  com.l7tech.message.HttpResponseKnob
 *  com.l7tech.message.HttpServletResponseKnob
 *  com.l7tech.message.Message
 *  com.l7tech.message.MessageRole
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.HardcodedResponseAssertion
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.RoutingStatus
 *  com.l7tech.policy.variable.Syntax
 *  com.l7tech.util.ConversionUtils
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.IOUtils
 *  com.l7tech.util.Option
 *  jakarta.servlet.ServletOutputStream
 *  jakarta.servlet.http.HttpServletResponse
 *  org.jetbrains.annotations.Nullable
 *  org.springframework.context.ApplicationContext
 */
package com.l7tech.server.policy.assertion;

import com.l7tech.common.http.HttpConstants;
import com.l7tech.common.mime.ContentTypeHeader;
import com.l7tech.common.mime.NoSuchPartException;
import com.l7tech.common.mime.PartInfo;
import com.l7tech.common.mime.StashManager;
import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.gateway.common.audit.Messages;
import com.l7tech.message.HttpResponseKnob;
import com.l7tech.message.HttpServletResponseKnob;
import com.l7tech.message.Message;
import com.l7tech.message.MessageRole;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.HardcodedResponseAssertion;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.RoutingStatus;
import com.l7tech.policy.variable.Syntax;
import com.l7tech.server.StashManagerFactory;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractServerAssertion;
import com.l7tech.server.policy.assertion.AssertionStatusException;
import com.l7tech.server.policy.assertion.a;
import com.l7tech.server.policy.variable.ExpandVariables;
import com.l7tech.util.ConversionUtils;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.IOUtils;
import com.l7tech.util.Option;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.logging.Level;
import org.jetbrains.annotations.Nullable;
import org.springframework.context.ApplicationContext;

public class ServerHardcodedResponseAssertion
extends AbstractServerAssertion<HardcodedResponseAssertion> {
    private final StashManagerFactory d;
    private final String e;
    private final byte[] f;
    private final ContentTypeHeader g;
    private final boolean h;
    private final String[] i;
    private final String j;
    private static final String[] k;

    public ServerHardcodedResponseAssertion(HardcodedResponseAssertion hardcodedResponseAssertion, ApplicationContext applicationContext) throws PolicyAssertionException {
        block13: {
            Object object;
            block12: {
                String string;
                super(hardcodedResponseAssertion);
                String[] stringArray = k;
                this.d = (StashManagerFactory)applicationContext.getBean(stringArray[4], StashManagerFactory.class);
                if (hardcodedResponseAssertion.getResponseContentType() == null) {
                    this.logger.log(Level.WARNING, stringArray[3]);
                    this.g = ContentTypeHeader.TEXT_DEFAULT;
                } else if (Syntax.getReferencedNames((String)hardcodedResponseAssertion.getResponseContentType()).length == 0) {
                    try {
                        object = ContentTypeHeader.parseValue((String)hardcodedResponseAssertion.getResponseContentType());
                    }
                    catch (IOException iOException) {
                        this.logger.log(Level.WARNING, k[1] + ExceptionUtils.getMessage((Throwable)iOException), ExceptionUtils.getDebugException((Throwable)iOException));
                        object = ContentTypeHeader.TEXT_DEFAULT;
                    }
                    this.g = object;
                } else {
                    this.g = null;
                }
                try {
                    string = hardcodedResponseAssertion.responseBodyString() == null ? "" : hardcodedResponseAssertion.responseBodyString();
                }
                catch (IOException iOException) {
                    throw ServerHardcodedResponseAssertion.a(iOException);
                }
                object = string;
                try {
                    try {
                        if (this.g == null || Syntax.getReferencedNames((String)object).length != 0) break block12;
                    }
                    catch (IOException iOException) {
                        throw ServerHardcodedResponseAssertion.a(iOException);
                    }
                    this.e = null;
                    this.f = ((String)object).getBytes(this.g.getEncoding());
                    this.j = null;
                    break block13;
                }
                catch (IOException iOException) {
                    throw ServerHardcodedResponseAssertion.a(iOException);
                }
            }
            this.e = object;
            this.f = null;
            this.j = Syntax.getSingleVariableReferenced((String)object);
        }
        this.h = hardcodedResponseAssertion.isEarlyResponse();
        this.i = hardcodedResponseAssertion.getVariablesUsed();
    }

    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        block15: {
            block16: {
                StashManager stashManager = this.d.createStashManager();
                Message message = policyEnforcementContext.getResponse();
                HttpResponseKnob httpResponseKnob = this.a(message);
                Map<String, Object> map = policyEnforcementContext.getVariableMap(this.i, this.getAudit());
                ContentTypeHeader contentTypeHeader = this.a(map);
                byte[] byArray = this.a(map, contentTypeHeader);
                String string = ExpandVariables.process(((HardcodedResponseAssertion)this.assertion).getResponseStatus(), map, this.getAudit());
                Option option = (Option)ConversionUtils.getTextToIntegerConverter().call((Object)string);
                try {
                    if (option.isSome() && (Integer)option.some() > 0) {
                    }
                }
                catch (Throwable throwable) {
                    throw ServerHardcodedResponseAssertion.a(throwable);
                }
                this.logAndAudit((AuditDetailMessage)AssertionMessages.TEMPLATE_RESPONSE_INVALID_STATUS, string);
                return AssertionStatus.FAILED;
                Integer n2 = (Integer)option.some();
                httpResponseKnob.setStatus(n2.intValue());
                message.initialize(stashManager, contentTypeHeader, (InputStream)new ByteArrayInputStream(byArray));
                Message message2 = policyEnforcementContext.getRequest();
                try {
                    message2.notifyMessage(message, MessageRole.RESPONSE);
                    message.notifyMessage(message2, MessageRole.REQUEST);
                    policyEnforcementContext.setRoutingStatus(RoutingStatus.ROUTED);
                    if (!this.h) break block15;
                    if (!(httpResponseKnob instanceof HttpServletResponseKnob)) break block16;
                }
                catch (Throwable throwable) {
                    throw ServerHardcodedResponseAssertion.a(throwable);
                }
                this.logAndAudit((AuditDetailMessage)AssertionMessages.TEMPLATE_RESPONSE_EARLY);
                HttpServletResponseKnob httpServletResponseKnob = (HttpServletResponseKnob)httpResponseKnob;
                HttpServletResponse httpServletResponse = httpServletResponseKnob.getHttpServletResponse();
                try {
                    message2.getMimeKnob().getEntireMessageBodyAsInputStream().close();
                    httpServletResponse.setStatus(n2.intValue());
                    if (n2 != 204) {
                        httpServletResponse.setContentType(contentTypeHeader.getFullValue());
                        String[] stringArray = k;
                        httpServletResponse.addHeader(HttpConstants.HEADER_CONNECTION, stringArray[5]);
                        ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
                        try (InputStream inputStream = message.getMimeKnob().getEntireMessageBodyAsInputStream();){
                            IOUtils.copyStream((InputStream)inputStream, (OutputStream)servletOutputStream);
                        }
                        servletOutputStream.close();
                    }
                    httpServletResponse.flushBuffer();
                }
                catch (NoSuchPartException noSuchPartException) {
                    this.logAndAudit((AuditDetailMessage)Messages.EXCEPTION_WARNING_WITH_MORE_INFO, new String[]{k[0]}, noSuchPartException);
                    return AssertionStatus.FAILED;
                }
            }
            this.logAndAudit((AuditDetailMessage)AssertionMessages.TEMPLATE_RESPONSE_NOT_HTTP);
            return AssertionStatus.FALSIFIED;
        }
        return AssertionStatus.NONE;
    }

    private HttpResponseKnob a(Message message) {
        Object object = (HttpResponseKnob)message.getKnob(HttpResponseKnob.class);
        if (object == null) {
            object = new a(this);
            message.attachHttpResponseKnob(object);
        }
        return object;
    }

    private ContentTypeHeader a(Map<String, Object> map) {
        ContentTypeHeader contentTypeHeader;
        block5: {
            contentTypeHeader = this.g;
            try {
                if (contentTypeHeader != null || ((HardcodedResponseAssertion)this.assertion).getResponseContentType() == null) break block5;
            }
            catch (IOException iOException) {
                throw ServerHardcodedResponseAssertion.a(iOException);
            }
            String string = ExpandVariables.process(((HardcodedResponseAssertion)this.assertion).getResponseContentType(), map, this.getAudit());
            try {
                contentTypeHeader = ContentTypeHeader.parseValue((String)string);
            }
            catch (IOException iOException) {
                this.logAndAudit((AuditDetailMessage)Messages.EXCEPTION_WARNING_WITH_MORE_INFO, new String[]{k[2] + ExceptionUtils.getMessage((Throwable)iOException)}, ExceptionUtils.getDebugException((Throwable)iOException));
            }
        }
        if (contentTypeHeader == null) {
            contentTypeHeader = ContentTypeHeader.TEXT_DEFAULT;
        }
        return contentTypeHeader;
    }

    private byte[] a(Map<String, Object> map, ContentTypeHeader contentTypeHeader) throws IOException {
        byte[] byArray = null;
        if (this.e != null) {
            String string = this.e;
            if (null != this.j) {
                Object object = ExpandVariables.processSingleVariableAsObject(string, map, this.getAudit());
                try {
                    byArray = ServerHardcodedResponseAssertion.a(object);
                }
                catch (NoSuchPartException noSuchPartException) {
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.NO_SUCH_PART, this.j, noSuchPartException.getWhatWasMissing());
                    throw new AssertionStatusException(AssertionStatus.SERVER_ERROR, (Throwable)noSuchPartException);
                }
            }
            if (null == byArray) {
                string = ExpandVariables.process(string, map, this.getAudit());
                byArray = string.getBytes(contentTypeHeader.getEncoding());
            }
        } else {
            byArray = this.f;
        }
        return byArray;
    }

    @Nullable
    private static byte[] a(@Nullable Object object) throws IOException, NoSuchPartException {
        if (object instanceof Message) {
            byte[] byArray;
            Message message = (Message)object;
            InputStream inputStream = message.getMimeKnob().getEntireMessageBodyAsInputStream(false);
            try {
                byArray = IOUtils.slurpStream((InputStream)inputStream);
            }
            catch (Throwable throwable) {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    }
                    catch (Throwable throwable2) {
                        throwable.addSuppressed(throwable2);
                    }
                }
                throw throwable;
            }
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            }
            catch (Throwable throwable) {
                throw ServerHardcodedResponseAssertion.a(throwable);
            }
            return byArray;
        }
        if (object instanceof PartInfo) {
            byte[] byArray;
            PartInfo partInfo = (PartInfo)object;
            InputStream inputStream = partInfo.getInputStream(false);
            try {
                byArray = IOUtils.slurpStream((InputStream)inputStream);
            }
            catch (Throwable throwable) {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    }
                    catch (Throwable throwable3) {
                        throwable.addSuppressed(throwable3);
                    }
                }
                throw throwable;
            }
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            }
            catch (Throwable throwable) {
                throw ServerHardcodedResponseAssertion.a(throwable);
            }
            return byArray;
        }
        return null;
    }

    private static Throwable a(Throwable throwable) {
        return throwable;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[6];
                var3_1 = 0;
                var2_2 = "TmN&\u0001C\\ul\u000f7\bH\u0018!kN6\tE\u0013efKd\u001fC\u000fqlA7\b8Dq]+\u001f\u0006\f`q\\-\u0003A\\blA0\bH\b!wV4\b\b\\GbC(\u0004H\u001b!aN'\u0006\u0006\u0013o#[!\u0015RSqoN-\u0003\u001c\\)HmY%\u0001O\u0018!`@*\u0019C\u0012u#[=\u001dCP!v\\-\u0003A\\ufW0BV\u0010`jAdW\u00060Lj\\7\u0004H\u001b!`@*\u0019C\u0012u#[=\u001dCR!EN(\u0001O\u0012f#M%\u000eM\\nm\u000f0\b^\b.sC%\u0004H";
                var4_3 = "TmN&\u0001C\\ul\u000f7\bH\u0018!kN6\tE\u0013efKd\u001fC\u000fqlA7\b8Dq]+\u001f\u0006\f`q\\-\u0003A\\blA0\bH\b!wV4\b\b\\GbC(\u0004H\u001b!aN'\u0006\u0006\u0013o#[!\u0015RSqoN-\u0003\u001c\\)HmY%\u0001O\u0018!`@*\u0019C\u0012u#[=\u001dCP!v\\-\u0003A\\ufW0BV\u0010`jAdW\u00060Lj\\7\u0004H\u001b!`@*\u0019C\u0012u#[=\u001dCR!EN(\u0001O\u0012f#M%\u000eM\\nm\u000f0\b^\b.sC%\u0004H".length();
                var1_4 = 33;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerHardcodedResponseAssertion.b(v0, ServerHardcodedResponseAssertion.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "X]d\u001d/A7EHb\u000b5J7H]j\u001c>\u0005HEj\u001d\"";
                    var4_3 = "X]d\u001d/A7EHb\u000b5J7H]j\u001c>\u0005HEj\u001d\"".length();
                    var1_4 = 19;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 104;
                        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++] = ServerHardcodedResponseAssertion.b(v0, ServerHardcodedResponseAssertion.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;
            }
        }
        ServerHardcodedResponseAssertion.k = 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 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 -> 67;
                    case 1 -> 65;
                    case 2 -> 109;
                    case 3 -> 6;
                    case 4 -> 47;
                    case 5 -> 100;
                    default -> 62;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

