/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.XmlUtil
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.gateway.common.audit.AuditRecord
 *  com.l7tech.gateway.common.audit.AuditRecordDomMarshaller
 *  com.l7tech.message.Message
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.AuditRecordToXmlAssertion
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.variable.NoSuchVariableException
 *  com.l7tech.util.ExceptionUtils
 *  jakarta.xml.bind.MarshalException
 */
package com.l7tech.server.policy.assertion;

import com.l7tech.common.io.XmlUtil;
import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.gateway.common.audit.AuditRecord;
import com.l7tech.gateway.common.audit.AuditRecordDomMarshaller;
import com.l7tech.message.Message;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.AuditRecordToXmlAssertion;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.variable.NoSuchVariableException;
import com.l7tech.server.message.AuthenticationContext;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractMessageTargetableServerAssertion;
import com.l7tech.server.policy.assertion.AssertionStatusException;
import com.l7tech.util.ExceptionUtils;
import jakarta.xml.bind.MarshalException;
import java.io.IOException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ServerAuditRecordToXmlAssertion
extends AbstractMessageTargetableServerAssertion<AuditRecordToXmlAssertion> {
    private static final String d;
    private static final AuditRecordDomMarshaller e;
    private static final String[] f;

    public ServerAuditRecordToXmlAssertion(AuditRecordToXmlAssertion auditRecordToXmlAssertion) {
        super(auditRecordToXmlAssertion);
    }

    @Override
    protected AssertionStatus doCheckRequest(PolicyEnforcementContext policyEnforcementContext, Message message, String string, AuthenticationContext authenticationContext) throws IOException, PolicyAssertionException {
        AuditRecord auditRecord = this.a(policyEnforcementContext);
        Document document = this.a(message);
        try {
            document.appendChild(e.marshal(document, auditRecord));
        }
        catch (MarshalException marshalException) {
            String string2 = f[4] + ExceptionUtils.getMessage((Throwable)marshalException);
            this.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_WARNING_WITH_MORE_INFO, new String[]{string2}, marshalException);
            throw new AssertionStatusException(AssertionStatus.SERVER_ERROR, string2);
        }
        return AssertionStatus.NONE;
    }

    private Document a(Message message) {
        block9: {
            try {
                if (!message.isXml()) break block9;
                Document document = message.getXmlKnob().getDocumentWritable();
                NodeList nodeList = document.getChildNodes();
                try {
                    for (int i2 = 0; i2 < nodeList.getLength(); ++i2) {
                        document.removeChild(nodeList.item(i2));
                    }
                }
                catch (IOException iOException) {
                    throw ServerAuditRecordToXmlAssertion.a(iOException);
                }
                return document;
            }
            catch (IOException iOException) {
            }
            catch (SAXException sAXException) {
                // empty catch block
            }
        }
        message.initialize(XmlUtil.createEmptyDocument());
        try {
            return message.getXmlKnob().getDocumentWritable();
        }
        catch (SAXException sAXException) {
            throw new RuntimeException(sAXException);
        }
        catch (IOException iOException) {
            throw new RuntimeException(iOException);
        }
    }

    private AuditRecord a(PolicyEnforcementContext policyEnforcementContext) {
        try {
            String[] stringArray = f;
            Object object = policyEnforcementContext.getVariable(stringArray[2]);
            try {
                if (!(object instanceof AuditRecord)) {
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_WARNING_WITH_MORE_INFO, stringArray[1]);
                    throw new AssertionStatusException(AssertionStatus.SERVER_ERROR, stringArray[0]);
                }
            }
            catch (NoSuchVariableException noSuchVariableException) {
                throw ServerAuditRecordToXmlAssertion.a((Exception)((Object)noSuchVariableException));
            }
            return (AuditRecord)object;
        }
        catch (NoSuchVariableException noSuchVariableException) {
            String[] stringArray = new String[1];
            String[] stringArray2 = f;
            stringArray[0] = stringArray2[5];
            this.logAndAudit((AuditDetailMessage)AssertionMessages.NO_SUCH_VARIABLE, stringArray);
            throw new AssertionStatusException(AssertionStatus.SERVER_ERROR, stringArray2[3]);
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[6];
                var3_1 = 0;
                var2_2 = "D\u0001Qzz~\u0012c\fJ}3p\u000f0\u0006Ve#?\u0016\u007f\u001bSzzv\u000fc\u0000\\lz~\u000f0\bMm3kAc\u0000Vbzo\u000e|\u0000[ptCD\u0001])9p\u000fd\f@}zi\u0000b\u0000Yk6zA7\bMm3kF0\f@`)k\u00120\u000bM}zv\u00120\u0007W}zp\u00070\u001dAy?? e\rQ}\bz\u0002\u007f\u001b\\'\u0005q\u001c\\`.6D\u0001Qzz~\u0012c\fJ}3p\u000f0\u0006Ve#?\u0016\u007f\u001bSzzv\u000fc\u0000\\lz~\u000f0\bMm3kAc\u0000Vbzo\u000e|\u0000[pt";
                var4_3 = "D\u0001Qzz~\u0012c\fJ}3p\u000f0\u0006Ve#?\u0016\u007f\u001bSzzv\u000fc\u0000\\lz~\u000f0\bMm3kAc\u0000Vbzo\u000e|\u0000[ptCD\u0001])9p\u000fd\f@}zi\u0000b\u0000Yk6zA7\bMm3kF0\f@`)k\u00120\u000bM}zv\u00120\u0007W}zp\u00070\u001dAy?? e\rQ}\bz\u0002\u007f\u001b\\'\u0005q\u001c\\`.6D\u0001Qzz~\u0012c\fJ}3p\u000f0\u0006Ve#?\u0016\u007f\u001bSzzv\u000fc\u0000\\lz~\u000f0\bMm3kAc\u0000Vbzo\u000e|\u0000[pt".length();
                var1_4 = 54;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerAuditRecordToXmlAssertion.c(v0, ServerAuditRecordToXmlAssertion.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u0010R\f>c/\u00141SM?`$B N\u0019|n?P,HM.j)[7XM(`jl\bpW|\u0005$I\t5{";
                    var4_3 = "\u0010R\f>c/\u00141SM?`$B N\u0019|n?P,HM.j)[7XM(`jl\bpW|\u0005$I\t5{".length();
                    var1_4 = 39;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 42;
                        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++] = ServerAuditRecordToXmlAssertion.c(v0, ServerAuditRecordToXmlAssertion.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;
            }
        }
        ServerAuditRecordToXmlAssertion.f = var5;
        ServerAuditRecordToXmlAssertion.d = ServerAuditRecordToXmlAssertion.f[3];
        ServerAuditRecordToXmlAssertion.e = new AuditRecordDomMarshaller();
    }

    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] ^ 0x1E);
        }
        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 -> 111;
                    case 1 -> 22;
                    case 2 -> 71;
                    case 3 -> 118;
                    case 4 -> 37;
                    case 5 -> 96;
                    default -> 30;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

