/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.mime.ContentTypeHeader
 *  com.l7tech.gateway.common.custom.JsonDataToCustomConverter
 *  com.l7tech.json.InvalidJsonException
 *  com.l7tech.json.JSONFactory
 *  com.l7tech.message.Message
 *  com.l7tech.policy.assertion.ext.message.CustomJsonData
 *  com.l7tech.policy.assertion.ext.message.CustomMessage
 *  com.l7tech.policy.assertion.ext.message.CustomMessageAccessException
 *  com.l7tech.util.BufferPool
 *  org.jetbrains.annotations.NotNull
 */
package com.l7tech.server.custom.format;

import com.l7tech.common.mime.ContentTypeHeader;
import com.l7tech.gateway.common.custom.JsonDataToCustomConverter;
import com.l7tech.json.InvalidJsonException;
import com.l7tech.json.JSONFactory;
import com.l7tech.message.Message;
import com.l7tech.policy.assertion.ext.message.CustomJsonData;
import com.l7tech.policy.assertion.ext.message.CustomMessage;
import com.l7tech.policy.assertion.ext.message.CustomMessageAccessException;
import com.l7tech.server.StashManagerFactory;
import com.l7tech.server.custom.CustomMessageImpl;
import com.l7tech.server.custom.format.CustomMessageFormatBase;
import com.l7tech.util.BufferPool;
import java.io.IOException;
import java.io.InputStream;
import org.jetbrains.annotations.NotNull;

/*
 * Exception performing whole class analysis ignored.
 */
public class CustomMessageJsonFormat
extends CustomMessageFormatBase<CustomJsonData> {
    private static final String[] b;

    public CustomMessageJsonFormat(@NotNull StashManagerFactory stashManagerFactory, @NotNull String string, @NotNull String string2) {
        if (stashManagerFactory == null) {
            CustomMessageJsonFormat.b(0);
        }
        if (string == null) {
            CustomMessageJsonFormat.b(1);
        }
        if (string2 == null) {
            CustomMessageJsonFormat.b(2);
        }
        super(stashManagerFactory, string, string2);
    }

    public Class<CustomJsonData> getRepresentationClass() {
        return CustomJsonData.class;
    }

    public CustomJsonData extract(CustomMessage customMessage) throws CustomMessageAccessException {
        block10: {
            try {
                if (customMessage == null) {
                    throw new CustomMessageAccessException(b[9]);
                }
            }
            catch (IOException iOException) {
                throw CustomMessageJsonFormat.a(iOException);
            }
            try {
                if (!(customMessage instanceof CustomMessageImpl)) {
                    throw new CustomMessageAccessException(b[0]);
                }
            }
            catch (IOException iOException) {
                throw CustomMessageJsonFormat.a(iOException);
            }
            Message message = ((CustomMessageImpl)customMessage).getMessage();
            try {
                try {
                    if (!message.isInitialized() || !message.isJson()) break block10;
                }
                catch (IOException iOException) {
                    throw CustomMessageJsonFormat.a(iOException);
                }
                return new JsonDataToCustomConverter(message.getJsonKnob().getJsonData());
            }
            catch (InvalidJsonException | IOException throwable) {
                throw new CustomMessageAccessException(b[6], throwable);
            }
        }
        return null;
    }

    /*
     * Loose catch block
     */
    public void overwrite(CustomMessage customMessage, CustomJsonData customJsonData) throws CustomMessageAccessException {
        try {
            if (customMessage == null) {
                throw new CustomMessageAccessException(b[2]);
            }
        }
        catch (IOException iOException) {
            throw CustomMessageJsonFormat.a(iOException);
        }
        try {
            if (!(customMessage instanceof CustomMessageImpl)) {
                throw new CustomMessageAccessException(b[5]);
            }
        }
        catch (IOException iOException) {
            throw CustomMessageJsonFormat.a(iOException);
        }
        Message message = ((CustomMessageImpl)customMessage).getMessage();
        try {
            byte[] byArray;
            block14: {
                block12: {
                    block13: {
                        if (customJsonData == null) break block12;
                        try {
                            block15: {
                                if (customJsonData.getJsonData() == null) break block13;
                                break block15;
                                catch (IOException iOException) {
                                    throw CustomMessageJsonFormat.a(iOException);
                                }
                            }
                            byArray = customJsonData.getJsonData().getBytes();
                            break block14;
                        }
                        catch (IOException iOException) {
                            throw CustomMessageJsonFormat.a(iOException);
                        }
                    }
                    byArray = new byte[]{};
                    break block14;
                }
                byArray = new byte[]{};
            }
            byte[] byArray2 = byArray;
            message.initialize(ContentTypeHeader.APPLICATION_JSON, byArray2);
        }
        catch (IOException iOException) {
            throw new CustomMessageAccessException(b[14], (Throwable)iOException);
        }
    }

    /*
     * WARNING - Removed back jump from a try to a catch block - possible behaviour change.
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public <K> CustomJsonData createBody(K k2) throws CustomMessageAccessException {
        try {
            if (k2 == null) {
                throw new CustomMessageAccessException(b[7]);
            }
        }
        catch (IOException iOException) {
            throw CustomMessageJsonFormat.a(iOException);
        }
        try {
            if (k2 instanceof String) {
                return new JsonDataToCustomConverter(JSONFactory.INSTANCE.newJsonData((String)k2));
            }
        }
        catch (IOException iOException) {
            throw CustomMessageJsonFormat.a(iOException);
        }
        catch (InvalidJsonException invalidJsonException) {
            String[] stringArray = b;
            throw new CustomMessageAccessException(stringArray[8], (Throwable)invalidJsonException);
        }
        {
            if (!(k2 instanceof InputStream)) throw new CustomMessageAccessException(b[1] + k2.getClass().getSimpleName());
            byte[] byArray = BufferPool.getBuffer((int)16384);
            try {
                InputStream inputStream = (InputStream)k2;
                StringBuilder stringBuilder = new StringBuilder();
                while (true) {
                    int n2 = inputStream.read(byArray);
                    try {
                        if (n2 <= 0) break;
                        stringBuilder.append(new String(byArray, 0, n2));
                    }
                    catch (IOException iOException) {
                        throw CustomMessageJsonFormat.a(iOException);
                    }
                }
                JsonDataToCustomConverter jsonDataToCustomConverter = new JsonDataToCustomConverter(JSONFactory.INSTANCE.newJsonData(stringBuilder.toString()));
                return jsonDataToCustomConverter;
            }
            catch (IOException iOException) {
                String[] stringArray = b;
                throw new CustomMessageAccessException(stringArray[10], (Throwable)iOException);
            }
            finally {
                BufferPool.returnBuffer((byte[])byArray);
            }
        }
    }

    /*
     * Exception decompiling
     */
    private static /* synthetic */ void b(int var0) {
        /*
         * 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: Tried to end blocks [0[TRYBLOCK]], but top level block is 1[SWITCH]
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.processEndingBlocks(Op04StructuredStatement.java:435)
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.buildNestedBlocks(Op04StructuredStatement.java:484)
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op03SimpleStatement.createInitialStructuredBlock(Op03SimpleStatement.java:736)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisInner(CodeAnalyser.java:850)
         *     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:1050)
         *     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");
    }

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

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[16];
                var3_1 = 0;
                var2_2 = "+deUQI\"fhe\u0006_Hg3oeS@^(4usB\u0010Z>6d\u001a\u0013oeS@^(4usB\u0010M((usHD\u000e3?qs\u001c\u0010\u0016+deUQI\"fbwH^A3fcs\u0006^[+*7%n{\t\\\u00193#b~\tCK50dd\tS[42n{\tVA5+`b\ts[42n{kU]4'fslCA)\u0000ndKQZ>\u0007sqS]K)2!pIB\u000e\u0007\bnbhEB+fqwTQC\"2dd\u0006\u0017\u000b4a!y@\u0010\u000b4h$e\u0006][42!xID\u000e%#!xS\\B\u001e+deUQI\"fhe\u0006_Hg3oeS@^(4usB\u0010Z>6d)\u0003sdIB\u000e0.hzC\u0010K?2swEDG)!!\\u\u007f`g+deUQI\"fwwJEK\u0016%nxRU@3fbwH^A3fcs\u0006^[+*\f\u000fo`G\\G#fKEi~\u0016+deUQI\"fbwH^A3fcs\u0006^[+*4\u0003sdIB\u000e0.hzC\u0010M5#`bO^Ig\u0005teR_C\r5nxbQZ&fgdI]\u000e\u000e(qcRcZ5#`{\u0004(`{C\u0006zhxOD\u0010\u00135uwUXc&(`qCBh&%uyTI";
                var4_3 = "+deUQI\"fhe\u0006_Hg3oeS@^(4usB\u0010Z>6d\u001a\u0013oeS@^(4usB\u0010M((usHD\u000e3?qs\u001c\u0010\u0016+deUQI\"fbwH^A3fcs\u0006^[+*7%n{\t\\\u00193#b~\tCK50dd\tS[42n{\tVA5+`b\ts[42n{kU]4'fslCA)\u0000ndKQZ>\u0007sqS]K)2!pIB\u000e\u0007\bnbhEB+fqwTQC\"2dd\u0006\u0017\u000b4a!y@\u0010\u000b4h$e\u0006][42!xID\u000e%#!xS\\B\u001e+deUQI\"fhe\u0006_Hg3oeS@^(4usB\u0010Z>6d)\u0003sdIB\u000e0.hzC\u0010K?2swEDG)!!\\u\u007f`g+deUQI\"fwwJEK\u0016%nxRU@3fbwH^A3fcs\u0006^[+*\f\u000fo`G\\G#fKEi~\u0016+deUQI\"fbwH^A3fcs\u0006^[+*4\u0003sdIB\u000e0.hzC\u0010M5#`bO^Ig\u0005teR_C\r5nxbQZ&fgdI]\u000e\u000e(qcRcZ5#`{\u0004(`{C\u0006zhxOD\u0010\u00135uwUXc&(`qCBh&%uyTI".length();
                var1_4 = 30;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = CustomMessageJsonFormat.b(v0, CustomMessageJsonFormat.b(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "y\t\u001e38tJT\u0012\u00009j;KY\t\u001b.# TR\u001cL\u0016\u0019\u001bs\u001c\u0016\t/95ZY[\u001a=&!X\u000bX\u001e\u001f?8=MH\u0012\u00032";
                    var4_3 = "y\t\u001e38tJT\u0012\u00009j;KY\t\u001b.# TR\u001cL\u0016\u0019\u001bs\u001c\u0016\t/95ZY[\u001a=&!X\u000bX\u001e\u001f?8=MH\u0012\u00032".length();
                    var1_4 = 42;
                    var0_5 = -1;
lbl22:
                    // 2 sources

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

    private static char[] b(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 7);
        }
        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 -> 6;
                    case 1 -> 65;
                    case 2 -> 86;
                    case 3 -> 102;
                    case 4 -> 112;
                    case 5 -> 110;
                    default -> 7;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

