/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.message.Message
 *  com.l7tech.policy.assertion.Assertion
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.OversizedTextAssertion
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.util.DomUtils
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.Pair
 *  com.l7tech.xml.ElementCursor
 *  com.l7tech.xml.InvalidXpathException
 *  com.l7tech.xml.SoapValidator
 *  com.l7tech.xml.xpath.CompiledXpath
 *  com.l7tech.xml.xpath.XpathExpression
 *  org.springframework.context.ApplicationContext
 */
package com.l7tech.server.policy.assertion;

import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.Message;
import com.l7tech.policy.assertion.Assertion;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.OversizedTextAssertion;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.server.audit.Auditor;
import com.l7tech.server.message.AuthenticationContext;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.ServerPolicyException;
import com.l7tech.server.policy.assertion.AbstractMessageTargetableServerAssertion;
import com.l7tech.server.policy.assertion.al;
import com.l7tech.util.DomUtils;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.Pair;
import com.l7tech.xml.ElementCursor;
import com.l7tech.xml.InvalidXpathException;
import com.l7tech.xml.SoapValidator;
import com.l7tech.xml.xpath.CompiledXpath;
import com.l7tech.xml.xpath.XpathExpression;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.xpath.XPathExpressionException;
import org.springframework.context.ApplicationContext;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ServerOversizedTextAssertion
extends AbstractMessageTargetableServerAssertion<OversizedTextAssertion> {
    private static final Logger d;
    private final Auditor e;
    private final CompiledXpath f;
    private final long g;
    private final int h;
    private final CompiledXpath i;
    private final CompiledXpath j;
    private final boolean k;
    private final int l;
    private final int m;
    private static final String[] n;

    public ServerOversizedTextAssertion(OversizedTextAssertion oversizedTextAssertion, ApplicationContext applicationContext) throws ServerPolicyException {
        super(oversizedTextAssertion);
        int n2;
        int n3;
        int n4;
        long l2;
        this.e = new Auditor(this, applicationContext, d);
        String string = oversizedTextAssertion.makeTextXpath();
        try {
            l2 = oversizedTextAssertion.isLimitAttrChars() ? oversizedTextAssertion.getMaxAttrChars() : -1L;
        }
        catch (InvalidXpathException invalidXpathException) {
            throw ServerOversizedTextAssertion.a((Exception)((Object)invalidXpathException));
        }
        long l3 = l2;
        try {
            n4 = oversizedTextAssertion.isLimitAttrNameChars() ? oversizedTextAssertion.getMaxAttrNameChars() : -1;
        }
        catch (InvalidXpathException invalidXpathException) {
            throw ServerOversizedTextAssertion.a((Exception)((Object)invalidXpathException));
        }
        int n5 = n4;
        String string2 = oversizedTextAssertion.makeNestingXpath();
        String string3 = oversizedTextAssertion.makePayloadLimitXpath();
        try {
            CompiledXpath compiledXpath;
            CompiledXpath compiledXpath2;
            CompiledXpath compiledXpath3;
            try {
                ServerOversizedTextAssertion serverOversizedTextAssertion = this;
                compiledXpath3 = string == null ? null : new XpathExpression(string).compile();
            }
            catch (InvalidXpathException invalidXpathException) {
                throw ServerOversizedTextAssertion.a((Exception)((Object)invalidXpathException));
            }
            try {
                serverOversizedTextAssertion.f = compiledXpath3;
                this.g = l3;
                this.h = n5;
                ServerOversizedTextAssertion serverOversizedTextAssertion = this;
                compiledXpath2 = string2 == null ? null : new XpathExpression(string2).compile();
            }
            catch (InvalidXpathException invalidXpathException) {
                throw ServerOversizedTextAssertion.a((Exception)((Object)invalidXpathException));
            }
            try {
                serverOversizedTextAssertion.i = compiledXpath2;
                ServerOversizedTextAssertion serverOversizedTextAssertion = this;
                compiledXpath = string3 == null ? null : new XpathExpression(string3).compile();
            }
            catch (InvalidXpathException invalidXpathException) {
                throw ServerOversizedTextAssertion.a((Exception)((Object)invalidXpathException));
            }
            serverOversizedTextAssertion.j = compiledXpath;
            this.k = oversizedTextAssertion.isRequireValidSoapEnvelope();
        }
        catch (InvalidXpathException invalidXpathException) {
            throw new ServerPolicyException((Assertion)oversizedTextAssertion, n[0] + ExceptionUtils.getMessage((Throwable)invalidXpathException), invalidXpathException);
        }
        try {
            ServerOversizedTextAssertion serverOversizedTextAssertion = this;
            n3 = oversizedTextAssertion.isLimitNamespaceCount() ? oversizedTextAssertion.getMaxNamespaceCount() : -1;
        }
        catch (InvalidXpathException invalidXpathException) {
            throw ServerOversizedTextAssertion.a((Exception)((Object)invalidXpathException));
        }
        try {
            serverOversizedTextAssertion.l = n3;
            ServerOversizedTextAssertion serverOversizedTextAssertion = this;
            n2 = oversizedTextAssertion.isLimitNamespacePrefixCount() ? oversizedTextAssertion.getMaxNamespacePrefixCount() : -1;
        }
        catch (InvalidXpathException invalidXpathException) {
            throw ServerOversizedTextAssertion.a((Exception)((Object)invalidXpathException));
        }
        serverOversizedTextAssertion.m = n2;
    }

    /*
     * Unable to fully structure code
     */
    @Override
    protected AssertionStatus doCheckRequest(PolicyEnforcementContext var1_1, Message var2_2, String var3_3, AuthenticationContext var4_4) throws PolicyAssertionException, IOException {
        block28: {
            block27: {
                if (!this.isRequest()) break block27;
                try {
                    block35: {
                        if (!var1_1.isPostRouting()) break block27;
                        break block35;
                        catch (SAXException v0) {
                            throw ServerOversizedTextAssertion.a(v0);
                        }
                    }
                    this.e.logAndAudit((AuditDetailMessage)AssertionMessages.OVERSIZEDTEXT_ALREADY_ROUTED);
                    return AssertionStatus.FAILED;
                }
                catch (SAXException v1) {
                    throw ServerOversizedTextAssertion.a(v1);
                }
            }
            if (!this.isResponse()) break block28;
            try {
                block36: {
                    if (var1_1.isPostRouting()) break block28;
                    break block36;
                    catch (SAXException v2) {
                        throw ServerOversizedTextAssertion.a(v2);
                    }
                }
                this.e.logAndAudit((AuditDetailMessage)AssertionMessages.OVERSIZEDTEXT_SKIP_RESPONSE_NOT_ROUTED);
                return AssertionStatus.NONE;
            }
            catch (SAXException v3) {
                throw ServerOversizedTextAssertion.a(v3);
            }
        }
        try {
            if (!var2_2.isXml()) {
                this.e.logAndAudit((AuditDetailMessage)AssertionMessages.OVERSIZEDTEXT_NOT_XML, new String[]{var3_3});
                return AssertionStatus.NOT_APPLICABLE;
            }
        }
        catch (SAXException v4) {
            throw ServerOversizedTextAssertion.a(v4);
        }
        try {
            block31: {
                block30: {
                    block29: {
                        var5_5 = var2_2.getXmlKnob().getElementCursor();
                        try {
                            var5_5.moveToRoot();
                            if (this.f == null || !var5_5.matches(this.f)) break block29;
                        }
                        catch (SAXException v5) {
                            throw ServerOversizedTextAssertion.a(v5);
                        }
                        this.e.logAndAudit((AuditDetailMessage)AssertionMessages.OVERSIZEDTEXT_NODE_OR_ATTRIBUTE, new String[]{var3_3});
                        return this.getBadMessageStatus();
                    }
                    if (this.g >= 0L) ** GOTO lbl52
                    try {
                        block37: {
                            if (this.h < 0) break block30;
                            break block37;
                            catch (SAXException v6) {
                                throw ServerOversizedTextAssertion.a(v6);
                            }
                        }
                        if (!this.a(var5_5, this.g, (long)this.h)) break block30;
                    }
                    catch (SAXException v7) {
                        throw ServerOversizedTextAssertion.a(v7);
                    }
                    this.e.logAndAudit((AuditDetailMessage)AssertionMessages.OVERSIZEDTEXT_NODE_OR_ATTRIBUTE, new String[]{var3_3});
                    return this.getBadMessageStatus();
                }
                try {
                    if (this.i == null || !var5_5.matches(this.i)) break block31;
                }
                catch (SAXException v8) {
                    throw ServerOversizedTextAssertion.a(v8);
                }
                this.e.logAndAudit((AuditDetailMessage)AssertionMessages.OVERSIZEDTEXT_XML_NESTING_DEPTH_EXCEEDED, new String[]{var3_3});
                return this.getBadMessageStatus();
            }
            return this.a(var2_2, var3_3, var5_5, this.e);
        }
        catch (SAXException var5_6) {
            block33: {
                block34: {
                    block32: {
                        try {
                            if (!this.isRequest()) break block32;
                            this.e.logAndAudit((AuditDetailMessage)AssertionMessages.XPATH_REQUEST_NOT_XML);
                            break block33;
                        }
                        catch (SAXException v9) {
                            throw ServerOversizedTextAssertion.a(v9);
                        }
                    }
                    try {
                        if (!this.isResponse()) break block34;
                        this.e.logAndAudit((AuditDetailMessage)AssertionMessages.XPATH_RESPONSE_NOT_XML);
                        break block33;
                    }
                    catch (SAXException v10) {
                        throw ServerOversizedTextAssertion.a(v10);
                    }
                }
                this.e.logAndAudit((AuditDetailMessage)AssertionMessages.XPATH_MESSAGE_NOT_XML, new String[]{var3_3});
            }
            return this.getBadMessageStatus();
        }
        catch (XPathExpressionException var5_7) {
            this.e.logAndAudit((AuditDetailMessage)AssertionMessages.XPATH_PATTERN_INVALID);
            return AssertionStatus.FAILED;
        }
        catch (TransformerConfigurationException var5_8) {
            this.e.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_SEVERE_WITH_MORE_INFO, new String[]{ExceptionUtils.getMessage((Throwable)var5_8)}, var5_8);
            return this.getBadMessageStatus();
        }
    }

    private boolean a(ElementCursor elementCursor, long l2, long l3) throws TransformerConfigurationException {
        DOMSource dOMSource = new DOMSource(elementCursor.asDomElement().getOwnerDocument());
        SAXResult sAXResult = new SAXResult(new al(this, l2, l3));
        try {
            TransformerFactory.newInstance().newTransformer().transform(dOMSource, sAXResult);
            return false;
        }
        catch (TransformerException transformerException) {
            return true;
        }
    }

    AssertionStatus a(Message message, String string, ElementCursor elementCursor, Auditor auditor) throws XPathExpressionException, IOException, SAXException {
        block15: {
            block12: {
                block14: {
                    block13: {
                        try {
                            try {
                                if (!this.k) break block12;
                                if (message.isSoap()) break block13;
                            }
                            catch (XPathExpressionException xPathExpressionException) {
                                throw ServerOversizedTextAssertion.a(xPathExpressionException);
                            }
                            auditor.logAndAudit((AuditDetailMessage)AssertionMessages.OVERSIZEDTEXT_NOT_SOAP, string);
                            return this.getBadMessageStatus();
                        }
                        catch (XPathExpressionException xPathExpressionException) {
                            throw ServerOversizedTextAssertion.a(xPathExpressionException);
                        }
                    }
                    String string2 = SoapValidator.validateSoapMessage((ElementCursor)elementCursor);
                    try {
                        try {
                            if (string2 == null) break block12;
                            if (!d.isLoggable(Level.INFO)) break block14;
                        }
                        catch (XPathExpressionException xPathExpressionException) {
                            throw ServerOversizedTextAssertion.a(xPathExpressionException);
                        }
                        d.info(n[1] + string2);
                    }
                    catch (XPathExpressionException xPathExpressionException) {
                        throw ServerOversizedTextAssertion.a(xPathExpressionException);
                    }
                }
                auditor.logAndAudit((AuditDetailMessage)AssertionMessages.OVERSIZEDTEXT_NOT_SOAP, string);
                return this.getBadMessageStatus();
            }
            try {
                try {
                    elementCursor.moveToRoot();
                    if (this.j == null || !elementCursor.matches(this.j)) break block15;
                }
                catch (XPathExpressionException xPathExpressionException) {
                    throw ServerOversizedTextAssertion.a(xPathExpressionException);
                }
                auditor.logAndAudit((AuditDetailMessage)AssertionMessages.OVERSIZEDTEXT_EXTRA_PAYLOAD_ELEMENTS, string);
                return this.getBadMessageStatus();
            }
            catch (XPathExpressionException xPathExpressionException) {
                throw ServerOversizedTextAssertion.a(xPathExpressionException);
            }
        }
        return this.b(message, string, elementCursor, auditor);
    }

    AssertionStatus b(Message message, String string, ElementCursor elementCursor, Auditor auditor) throws IOException, SAXException {
        block16: {
            Pair<Set<String>, Set<String>> pair;
            boolean bl2;
            block17: {
                boolean bl3;
                boolean bl4;
                try {
                    bl4 = this.l > 0;
                }
                catch (IOException iOException) {
                    throw ServerOversizedTextAssertion.a(iOException);
                }
                boolean bl5 = bl4;
                try {
                    bl3 = this.m > 0;
                }
                catch (IOException iOException) {
                    throw ServerOversizedTextAssertion.a(iOException);
                }
                bl2 = bl3;
                try {
                    try {
                        if (!bl5 && !bl2) break block16;
                    }
                    catch (IOException iOException) {
                        throw ServerOversizedTextAssertion.a(iOException);
                    }
                    if (!message.isXml()) break block16;
                }
                catch (IOException iOException) {
                    throw ServerOversizedTextAssertion.a(iOException);
                }
                pair = this.a(message.getXmlKnob().getDocumentReadOnly().getDocumentElement(), this.l, this.m);
                try {
                    try {
                        d.log(Level.INFO, n[4], new Object[]{((Set)pair.left).size(), ((Set)pair.right).size()});
                        if (!bl5 || ((Set)pair.left).size() <= this.l) break block17;
                    }
                    catch (IOException iOException) {
                        throw ServerOversizedTextAssertion.a(iOException);
                    }
                    auditor.logAndAudit((AuditDetailMessage)AssertionMessages.OVERSIZEDTEXT_NS_DECLARATION_EXCEEDED, string);
                    return this.getBadMessageStatus();
                }
                catch (IOException iOException) {
                    throw ServerOversizedTextAssertion.a(iOException);
                }
            }
            try {
                try {
                    if (!bl2 || ((Set)pair.right).size() <= this.m) break block16;
                }
                catch (IOException iOException) {
                    throw ServerOversizedTextAssertion.a(iOException);
                }
                auditor.logAndAudit((AuditDetailMessage)AssertionMessages.OVERSIZEDTEXT_NS_PREFIX_DECLARATION_EXCEEDED, string);
                return this.getBadMessageStatus();
            }
            catch (IOException iOException) {
                throw ServerOversizedTextAssertion.a(iOException);
            }
        }
        return AssertionStatus.NONE;
    }

    private Pair<Set<String>, Set<String>> a(Element element, int n2, int n3) {
        String[] stringArray;
        Object object;
        Object object2;
        Object object3;
        NamedNodeMap namedNodeMap = element.getAttributes();
        TreeSet<Object> treeSet = new TreeSet<Object>();
        TreeSet<String> treeSet2 = new TreeSet<String>();
        for (int i2 = 0; i2 < namedNodeMap.getLength(); ++i2) {
            Attr attr = (Attr)namedNodeMap.item(i2);
            object3 = attr.getPrefix();
            object2 = attr.getNamespaceURI();
            String string = attr.getLocalName();
            object = attr.getValue();
            if (object == null || ((String)object).trim().length() <= 0 || (!(stringArray = n)[3].equals(object3) || !DomUtils.XMLNS_NS.equals(object2)) && !(stringArray = n)[2].equals(string)) continue;
            treeSet.add(object);
            stringArray = n;
            if (stringArray[2].equals(string)) continue;
            treeSet2.add(string);
        }
        NodeList nodeList = element.getElementsByTagName("*");
        for (int i3 = 0; i3 < nodeList.getLength(); ++i3) {
            object3 = nodeList.item(i3);
            if (object3.getNodeType() == 1) {
                object2 = object3.getAttributes();
                for (int i4 = 0; i4 < object2.getLength(); ++i4) {
                    object = (Attr)object2.item(i4);
                    String string = object.getPrefix();
                    String string2 = object.getNamespaceURI();
                    String string3 = object.getLocalName();
                    String string4 = object.getValue();
                    if (string4 == null || string4.trim().length() <= 0 || (!(stringArray = n)[2].equals(string) || !DomUtils.XMLNS_NS.equals(string2)) && !(stringArray = n)[2].equals(string3)) continue;
                    treeSet.add(string4);
                    if (n[2].equals(string3)) continue;
                    treeSet2.add(string3);
                }
            }
            if (n2 > 0 && treeSet.size() > n2 || n3 > 0 && treeSet2.size() > n3) break;
        }
        return new Pair(treeSet, treeSet2);
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[5];
                var3_1 = 0;
                var2_2 = "p;`\u0004\u0011\u0019h\u0019%d\n\t\u0015oM<y\u000b]\b|X!~_]\u0018k0g\u0010\u0018\u0003x\u0019;y\u0011]\u0006mU<rE.?Mio6\u0005A8z\u000b\u000e";
                var4_3 = "p;`\u0004\u0011\u0019h\u0019%d\n\t\u0015oM<y\u000b]\b|X!~_]\u0018k0g\u0010\u0018\u0003x\u0019;y\u0011]\u0006mU<rE.?Mio6\u0005A8z\u000b\u000e".length();
                var1_4 = 26;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerOversizedTextAssertion.c(v0, ServerOversizedTextAssertion.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "6O\r|y\u001b\rM\u0014|~t[ Q\\i:z@nR\u0013wln\u0003+Q\\i;z";
                    var4_3 = "6O\r|y\u001b\rM\u0014|~t[ Q\\i:z@nR\u0013wln\u0003+Q\\i;z".length();
                    var1_4 = 5;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 67;
                        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++] = ServerOversizedTextAssertion.c(v0, ServerOversizedTextAssertion.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;
            }
        }
        ServerOversizedTextAssertion.n = var5;
        ServerOversizedTextAssertion.d = Logger.getLogger(ServerOversizedTextAssertion.class.getName());
    }

    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] ^ 0x38);
        }
        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 -> 13;
                    case 1 -> 97;
                    case 2 -> 34;
                    case 3 -> 81;
                    case 4 -> 73;
                    case 5 -> 68;
                    default -> 56;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

