/*
 * 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.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.xmlsec.WssSignElement
 *  com.l7tech.security.xml.decorator.DecorationRequirements
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.xml.InvalidXpathException
 *  com.l7tech.xml.xpath.CompilableXpath
 *  com.l7tech.xml.xpath.DeferredFailureDomCompiledXpathHolder
 *  com.l7tech.xml.xpath.XpathVariableFinder
 *  org.springframework.context.ApplicationContext
 */
package com.l7tech.server.policy.assertion.xmlsec;

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.xmlsec.WssSignElement;
import com.l7tech.security.xml.decorator.DecorationRequirements;
import com.l7tech.server.message.AuthenticationContext;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AssertionStatusException;
import com.l7tech.server.policy.assertion.xmlsec.ServerAddWssSignature;
import com.l7tech.server.util.xml.PolicyEnforcementContextXpathVariableFinder;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.xml.InvalidXpathException;
import com.l7tech.xml.xpath.CompilableXpath;
import com.l7tech.xml.xpath.DeferredFailureDomCompiledXpathHolder;
import com.l7tech.xml.xpath.XpathVariableFinder;
import java.io.IOException;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.w3c.dom.Document;

public class ServerWssSignElement
extends ServerAddWssSignature<WssSignElement> {
    private final DeferredFailureDomCompiledXpathHolder d;
    private static final String e = ServerWssSignElement.c(124, ServerWssSignElement.a("S>:aU\u0002\u0003}\u000f7`\\V\u000fd\u0000{pOP\tyT{"));

    public ServerWssSignElement(WssSignElement wssSignElement, ApplicationContext applicationContext) throws IOException {
        super(wssSignElement, applicationContext, true);
        this.d = new DeferredFailureDomCompiledXpathHolder((CompilableXpath)wssSignElement.getXpathExpression());
    }

    @Override
    protected int addDecorationRequirements(PolicyEnforcementContext policyEnforcementContext, AuthenticationContext authenticationContext, Document document, DecorationRequirements decorationRequirements, Message message) throws PolicyAssertionException {
        List list;
        block6: {
            try {
                list = this.d.getCompiledXpath().rawSelectElements(document, (XpathVariableFinder)new PolicyEnforcementContextXpathVariableFinder(policyEnforcementContext));
            }
            catch (InvalidXpathException invalidXpathException) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.XPATH_PATTERN_INVALID_MORE_INFO, new String[]{e + ExceptionUtils.getMessage((Throwable)invalidXpathException)}, ExceptionUtils.getDebugException((Throwable)invalidXpathException));
                throw new AssertionStatusException(AssertionStatus.SERVER_ERROR);
            }
            try {
                try {
                    if (list != null && list.size() >= 1) break block6;
                }
                catch (InvalidXpathException invalidXpathException) {
                    throw ServerWssSignElement.a(invalidXpathException);
                }
                return 0;
            }
            catch (InvalidXpathException invalidXpathException) {
                throw ServerWssSignElement.a(invalidXpathException);
            }
        }
        decorationRequirements.getElementsToSign().addAll(list);
        decorationRequirements.setSignTimestamp(true);
        return list.size();
    }

    private static InvalidXpathException a(InvalidXpathException invalidXpathException) {
        return invalidXpathException;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x1A);
        }
        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 -> 119;
                    case 1 -> 18;
                    case 2 -> 39;
                    case 3 -> 105;
                    case 4 -> 65;
                    case 5 -> 94;
                    default -> 26;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

