/*
 * 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.gateway.common.audit.CommonMessages
 *  com.l7tech.message.Message
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.MessageTargetable
 *  com.l7tech.policy.assertion.MessageTargetableSupport
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.SetVariableAssertion
 *  com.l7tech.policy.variable.DataType
 *  com.l7tech.policy.variable.NoSuchVariableException
 *  com.l7tech.util.Config
 *  com.l7tech.util.DateTimeConfigUtils
 *  com.l7tech.util.DateTimeConfigUtils$InvalidDateFormatException
 *  com.l7tech.util.DateTimeConfigUtils$UnknownDateFormatException
 *  com.l7tech.util.DateUtils
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.TimeSource
 *  jakarta.inject.Inject
 *  jakarta.inject.Named
 */
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.gateway.common.audit.CommonMessages;
import com.l7tech.message.Message;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.MessageTargetable;
import com.l7tech.policy.assertion.MessageTargetableSupport;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.SetVariableAssertion;
import com.l7tech.policy.variable.DataType;
import com.l7tech.policy.variable.NoSuchVariableException;
import com.l7tech.server.StashManagerFactory;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractServerAssertion;
import com.l7tech.server.policy.variable.ExpandVariables;
import com.l7tech.server.policy.variable.ExpandVariablesTemplate;
import com.l7tech.util.Config;
import com.l7tech.util.DateTimeConfigUtils;
import com.l7tech.util.DateUtils;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.TimeSource;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import java.io.IOException;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

public class ServerSetVariableAssertion
extends AbstractServerAssertion<SetVariableAssertion> {
    private final String[] d;
    @Inject
    private DateTimeConfigUtils dateParser;
    @Inject
    private TimeSource timeSource;
    @Inject
    private Config config;
    @Inject
    @Named(value="stashManagerFactory")
    private StashManagerFactory stashManagerFactory;
    private final ExpandVariablesTemplate e;
    private final String f;
    private static final String[] g;

    /*
     * Exception decompiling
     */
    public ServerSetVariableAssertion(SetVariableAssertion var1_1) throws PolicyAssertionException {
        /*
         * This method has failed to decompile.  When submitting a bug report, please provide this stack trace, and (if you hold appropriate legal rights) the relevant class file.
         * 
         * org.benf.cfr.reader.util.ConfusedCFRException: Back jump on a try block [egrp 2[TRYBLOCK] [1, 0, 3 : 62->72)] java.lang.IllegalArgumentException
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op02WithProcessedDataAndRefs.insertExceptionBlocks(Op02WithProcessedDataAndRefs.java:2283)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisInner(CodeAnalyser.java:415)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisOrWrapFail(CodeAnalyser.java:278)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysis(CodeAnalyser.java:201)
         *     at org.benf.cfr.reader.entities.attributes.AttributeCode.analyse(AttributeCode.java:94)
         *     at org.benf.cfr.reader.entities.Method.analyse(Method.java:531)
         *     at org.benf.cfr.reader.entities.ClassFile.analyseMid(ClassFile.java:1055)
         *     at org.benf.cfr.reader.entities.ClassFile.analyseTop(ClassFile.java:942)
         *     at org.benf.cfr.reader.Driver.doJarVersionTypes(Driver.java:257)
         *     at org.benf.cfr.reader.Driver.doJar(Driver.java:139)
         *     at org.benf.cfr.reader.CfrDriverImpl.analyse(CfrDriverImpl.java:76)
         *     at org.benf.cfr.reader.Main.main(Main.java:54)
         */
        throw new IllegalStateException("Decompilation failed");
    }

    /*
     * Loose catch block
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        DataType dataType;
        Map<String, Object> map;
        block32: {
            map = policyEnforcementContext.getVariableMap(this.d, this.getAudit());
            dataType = ((SetVariableAssertion)this.assertion).getDataType();
            if (dataType == DataType.STRING) {
                String string = this.e.process(map, this.getAudit());
                policyEnforcementContext.setVariable(((SetVariableAssertion)this.assertion).getVariableToSet(), string);
                return AssertionStatus.NONE;
            }
            if (dataType == DataType.MESSAGE) {
                ContentTypeHeader contentTypeHeader = ContentTypeHeader.parseValue((String)((SetVariableAssertion)this.assertion).getContentType());
                try {
                    Message message = policyEnforcementContext.getOrCreateTargetMessage((MessageTargetable)new MessageTargetableSupport(((SetVariableAssertion)this.assertion).getVariableToSet()), false);
                    boolean bl2 = false;
                    if (this.f != null) {
                        bl2 = this.a(message, contentTypeHeader, ((SetVariableAssertion)this.assertion).expression(), map);
                    }
                    if (bl2) return AssertionStatus.NONE;
                    String string = this.e.process(map, this.getAudit());
                    message.initialize(contentTypeHeader, string.getBytes(contentTypeHeader.getEncoding()));
                    return AssertionStatus.NONE;
                }
                catch (NoSuchVariableException noSuchVariableException) {
                    this.logAndAudit((AuditDetailMessage)CommonMessages.TEMPLATE_UNSUPPORTED_VARIABLE, ((SetVariableAssertion)this.assertion).getVariableToSet());
                    return AssertionStatus.FALSIFIED;
                }
                catch (NoSuchPartException noSuchPartException) {
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.NO_SUCH_PART, this.f, noSuchPartException.getWhatWasMissing());
                    return AssertionStatus.FALSIFIED;
                }
            }
            if (dataType != DataType.DATE_TIME) break block32;
            String string = this.e.process(map, this.getAudit());
            try {
                Date date;
                block31: {
                    Serializable serializable;
                    String string2;
                    String string3;
                    block34: {
                        block29: {
                            block30: {
                                block33: {
                                    boolean bl3 = ((SetVariableAssertion)this.assertion).expression().isEmpty();
                                    if (!bl3) break block33;
                                    date = new Date(this.timeSource.currentTimeMillis());
                                    break block34;
                                }
                                string3 = ((SetVariableAssertion)this.assertion).getDateFormat();
                                if (string3 == null) break block29;
                                try {
                                    block35: {
                                        if (string3.trim().isEmpty()) break block29;
                                        break block35;
                                        catch (NoSuchVariableException noSuchVariableException) {
                                            throw ServerSetVariableAssertion.a((Exception)((Object)noSuchVariableException));
                                        }
                                    }
                                    if (!string3.equalsIgnoreCase(DateTimeConfigUtils.TIMESTAMP)) break block30;
                                }
                                catch (NoSuchVariableException noSuchVariableException) {
                                    throw ServerSetVariableAssertion.a((Exception)((Object)noSuchVariableException));
                                }
                                date = DateTimeConfigUtils.parseTimestamp((String)string);
                                break block34;
                            }
                            if (string3.equalsIgnoreCase(DateTimeConfigUtils.MILLISECOND_TIMESTAMP)) {
                                date = DateTimeConfigUtils.parseMilliTimestamp((String)string);
                            } else if (string3.equalsIgnoreCase(DateTimeConfigUtils.SECOND_TIMESTAMP)) {
                                date = DateTimeConfigUtils.parseSecondTimestamp((String)string);
                            } else {
                                string2 = ExpandVariables.process(string3, map, this.getAudit());
                                if (string2.trim().isEmpty()) {
                                    String[] stringArray = new String[1];
                                    String[] stringArray2 = g;
                                    stringArray[0] = stringArray2[0];
                                    this.logAndAudit((AuditDetailMessage)AssertionMessages.SET_VARIABLE_UNRECOGNISED_DATE_FORMAT, stringArray);
                                    return AssertionStatus.FALSIFIED;
                                }
                                try {
                                    serializable = new SimpleDateFormat(string2);
                                }
                                catch (IllegalArgumentException illegalArgumentException) {
                                    throw new DateTimeConfigUtils.InvalidDateFormatException(ExceptionUtils.getMessage((Throwable)illegalArgumentException));
                                }
                                String[] stringArray = g;
                                ((DateFormat)serializable).setLenient(this.config.getBooleanProperty(stringArray[4], false));
                                ((DateFormat)serializable).setTimeZone(DateUtils.getZuluTimeZone());
                                date = ((DateFormat)serializable).parse(string);
                            }
                            break block34;
                        }
                        date = this.dateParser.parseDateFromString(string);
                    }
                    string3 = ((SetVariableAssertion)this.assertion).getDateOffsetExpression();
                    try {
                        if (string3 == null || string3.trim().isEmpty()) break block31;
                    }
                    catch (NoSuchVariableException noSuchVariableException) {
                        throw ServerSetVariableAssertion.a((Exception)((Object)noSuchVariableException));
                    }
                    string2 = ExpandVariables.process(string3, map, this.getAudit());
                    try {
                        serializable = Integer.valueOf(string2);
                    }
                    catch (NumberFormatException numberFormatException) {
                        this.logAndAudit((AuditDetailMessage)AssertionMessages.SET_VARIABLE_INVALID_DATE_OFFSET, string2);
                        return AssertionStatus.FALSIFIED;
                    }
                    int n2 = ((SetVariableAssertion)this.assertion).getDateOffsetField();
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(date);
                    calendar.add(n2, (Integer)serializable);
                    policyEnforcementContext.setVariable(((SetVariableAssertion)this.assertion).getVariableToSet(), calendar.getTime());
                    return AssertionStatus.NONE;
                }
                policyEnforcementContext.setVariable(((SetVariableAssertion)this.assertion).getVariableToSet(), date);
                return AssertionStatus.NONE;
            }
            catch (ParseException parseException) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.SET_VARIABLE_UNABLE_TO_PARSE_DATE, string);
                return AssertionStatus.FALSIFIED;
            }
            catch (DateTimeConfigUtils.UnknownDateFormatException unknownDateFormatException) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.SET_VARIABLE_UNRECOGNISED_DATE_FORMAT, unknownDateFormatException.getMessage());
                return AssertionStatus.FALSIFIED;
            }
            catch (DateTimeConfigUtils.InvalidDateFormatException invalidDateFormatException) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.SET_VARIABLE_INVALID_DATE_PATTERN, invalidDateFormatException.getMessage());
                return AssertionStatus.FALSIFIED;
            }
        }
        if (dataType == DataType.INTEGER) {
            String string = this.e.process(map, this.getAudit());
            try {
                policyEnforcementContext.setVariable(((SetVariableAssertion)this.assertion).getVariableToSet(), Integer.parseInt(string));
                return AssertionStatus.NONE;
            }
            catch (NumberFormatException numberFormatException) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.SET_VARIABLE_UNABLE_TO_PARSE_INTEGER, string);
                return AssertionStatus.FALSIFIED;
            }
        } else {
            String[] stringArray = g;
            throw new RuntimeException(stringArray[3] + dataType.getName() + stringArray[5] + ((SetVariableAssertion)this.assertion).getVariableToSet() + stringArray[1]);
        }
    }

    private boolean a(Message message, ContentTypeHeader contentTypeHeader, String string, Map<String, Object> map) throws IOException, NoSuchPartException {
        boolean bl2 = false;
        Object object = ExpandVariables.processSingleVariableAsObject(string, map, this.getAudit());
        if (object instanceof Message) {
            Message message2 = (Message)object;
            message.initialize(this.stashManagerFactory.createStashManager(), contentTypeHeader, message2.getMimeKnob().getEntireMessageBodyAsInputStream(false));
            bl2 = true;
        } else if (object instanceof PartInfo) {
            PartInfo partInfo = (PartInfo)object;
            message.initialize(this.stashManagerFactory.createStashManager(), contentTypeHeader, partInfo.getInputStream(false));
            bl2 = true;
        }
        try {
            if (bl2) {
                message.getMimeKnob().getEntireMessageBodyAsInputStream().close();
            }
        }
        catch (IOException iOException) {
            throw ServerSetVariableAssertion.a(iOException);
        }
        return bl2;
    }

    private static Exception a(Exception exception) {
        return exception;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[6];
                var3_1 = 0;
                var2_2 = "4/\u00031!\r\u0015ZfU\u0017\u000bsc3lB\u0002\u0002:a|zN\u0017\u0013 '\"]gWV\u000eww\u007fmN\u0013\tnbw(Z\u0013\u0013:a|z\u0003\u0012\u0006nf3|Z\u0006\u0002:";
                var4_3 = "4/\u00031!\r\u0015ZfU\u0017\u000bsc3lB\u0002\u0002:a|zN\u0017\u0013 '\"]gWV\u000eww\u007fmN\u0013\tnbw(Z\u0013\u0013:a|z\u0003\u0012\u0006nf3|Z\u0006\u0002:".length();
                var1_4 = 2;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerSetVariableAssertion.b(v0, ServerSetVariableAssertion.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u000f\u00181'tR\f\t\u00144'm\u0011\u0011\u0000Y0lv\f\u001d\u0002\u0003\u0018hl\u0000>\u0003\u00051hl\u0011L_*hj\f\u0019\u000e\u001b9)v\u0004\u0015\tJ~";
                    var4_3 = "\u000f\u00181'tR\f\t\u00144'm\u0011\u0011\u0000Y0lv\f\u001d\u0002\u0003\u0018hl\u0000>\u0003\u00051hl\u0011L_*hj\f\u0019\u000e\u001b9)v\u0004\u0015\tJ~".length();
                    var1_4 = 33;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 115;
                        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++] = ServerSetVariableAssertion.b(v0, ServerSetVariableAssertion.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;
            }
        }
        ServerSetVariableAssertion.g = var5;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0xB);
        }
        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 -> 31;
                    case 1 -> 4;
                    case 2 -> 47;
                    case 3 -> 122;
                    case 4 -> 107;
                    case 5 -> 22;
                    default -> 11;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

