/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.cluster.ServiceUsage
 *  com.l7tech.gateway.common.transport.SsgConnector
 *  com.l7tech.gateway.common.transport.SsgConnector$Endpoint
 *  com.l7tech.objectmodel.EntityHeader
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.util.Config
 *  com.l7tech.util.ConfigFactory
 *  jakarta.servlet.ServletConfig
 *  jakarta.servlet.ServletContext
 *  jakarta.servlet.ServletException
 *  jakarta.servlet.ServletRequest
 *  jakarta.servlet.http.HttpServlet
 *  jakarta.servlet.http.HttpServletRequest
 *  jakarta.servlet.http.HttpServletResponse
 *  org.springframework.web.context.WebApplicationContext
 *  org.springframework.web.context.support.WebApplicationContextUtils
 */
package com.l7tech.server;

import com.l7tech.gateway.common.cluster.ServiceUsage;
import com.l7tech.gateway.common.transport.SsgConnector;
import com.l7tech.objectmodel.EntityHeader;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.server.cluster.ServiceUsageManager;
import com.l7tech.server.service.ServiceManager;
import com.l7tech.server.transport.ListenerException;
import com.l7tech.server.transport.http.HttpTransportModule;
import com.l7tech.util.Config;
import com.l7tech.util.ConfigFactory;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SnmpQueryServlet
extends HttpServlet {
    private static final Logger logger;
    private static final String BASE;
    private static final String BASE_PUBLISHED_SERVICES;
    private static final String CONTENT_TYPE_SNMP;
    private WebApplicationContext applicationContext;
    private Config serverConfig;
    private ServiceUsage[] serviceTable = null;
    private long serviceTableTime = 0L;
    private final Pattern urlParser;
    private final Pattern match2;
    static final long serialVersionUID = -7670528938527358340L;
    private static final String[] a;

    public SnmpQueryServlet() {
        String[] stringArray = a;
        this.urlParser = Pattern.compile(stringArray[17]);
        this.match2 = Pattern.compile(stringArray[6]);
    }

    public void init(ServletConfig servletConfig) throws ServletException {
        try {
            super.init(servletConfig);
            this.applicationContext = WebApplicationContextUtils.getWebApplicationContext((ServletContext)this.getServletContext());
            if (this.applicationContext == null) {
                throw new ServletException(a[26]);
            }
        }
        catch (ServletException servletException) {
            throw SnmpQueryServlet.a((Exception)((Object)servletException));
        }
        this.serverConfig = (Config)this.applicationContext.getBean(a[20], Config.class);
    }

    public void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        boolean bl2;
        SsgConnector ssgConnector = HttpTransportModule.getConnector((ServletRequest)httpServletRequest);
        HttpTransportModule.addHstsResponseHeaderIfRequired(httpServletRequest, httpServletResponse, ssgConnector, this.serverConfig);
        String[] stringArray = a;
        boolean bl3 = ConfigFactory.getBooleanProperty((String)stringArray[8], (boolean)false);
        try {
            if (!bl3) {
                logger.info(stringArray[21]);
                httpServletResponse.sendError(503, stringArray[18]);
                return;
            }
        }
        catch (ListenerException listenerException) {
            throw SnmpQueryServlet.a(listenerException);
        }
        try {
            HttpTransportModule.requireEndpoint(ssgConnector, SsgConnector.Endpoint.SNMPQUERY);
        }
        catch (ListenerException listenerException) {
            stringArray = a;
            logger.info(stringArray[10]);
            httpServletResponse.sendError(503, stringArray[23]);
            return;
        }
        try {
            try {
                if (!InetAddress.getByName(httpServletRequest.getRemoteAddr()).isLoopbackAddress()) {
                    throw new UnknownHostException();
                }
            }
            catch (ListenerException listenerException) {
                throw SnmpQueryServlet.a(listenerException);
            }
        }
        catch (UnknownHostException unknownHostException) {
            httpServletResponse.sendError(403);
            return;
        }
        String string = httpServletRequest.getRequestURI();
        Matcher matcher = this.urlParser.matcher(string);
        try {
            if (!matcher.matches()) {
                this.badRequest(httpServletResponse);
                return;
            }
        }
        catch (ListenerException listenerException) {
            throw SnmpQueryServlet.a(listenerException);
        }
        String string2 = matcher.group(1);
        String string3 = matcher.group(2);
        stringArray = a;
        if (stringArray[24].equalsIgnoreCase(string2)) {
            bl2 = false;
        } else {
            stringArray = a;
            if (stringArray[14].equalsIgnoreCase(string2)) {
                bl2 = true;
            } else {
                this.badRequest(httpServletResponse);
                return;
            }
        }
        try {
            stringArray = a;
            if (string3.startsWith(stringArray[7])) {
                this.respondToPublishedServiceRequest(httpServletResponse, string3.substring(stringArray[19].length()), bl2);
                return;
            }
            this.badRequest(httpServletResponse);
            return;
        }
        catch (IOException iOException) {
            throw iOException;
        }
        catch (Exception exception) {
            httpServletResponse.setStatus(500);
            logger.log(Level.INFO, a[11], exception);
            httpServletResponse.getWriter().close();
            return;
        }
    }

    /*
     * Exception decompiling
     */
    private void respondToPublishedServiceRequest(HttpServletResponse var1_1, String var2_2, boolean var3_3) throws IOException, FindException {
        /*
         * 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 [7[TRYBLOCK]], but top level block is 8[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: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");
    }

    private synchronized ServiceUsage[] getCurrentServiceUsage() throws FindException {
        long l2;
        block11: {
            l2 = System.currentTimeMillis();
            try {
                try {
                    if (this.serviceTable == null || l2 - this.serviceTableTime <= 3500L) break block11;
                }
                catch (FindException findException) {
                    throw SnmpQueryServlet.a((Exception)((Object)findException));
                }
                this.serviceTable = null;
            }
            catch (FindException findException) {
                throw SnmpQueryServlet.a((Exception)((Object)findException));
            }
        }
        try {
            if (this.serviceTable != null) {
                this.serviceTableTime = l2;
                return this.serviceTable;
            }
        }
        catch (FindException findException) {
            throw SnmpQueryServlet.a((Exception)((Object)findException));
        }
        String[] stringArray = a;
        ServiceUsageManager serviceUsageManager = (ServiceUsageManager)this.applicationContext.getBean(stringArray[22]);
        HashMap<Goid, ServiceUsage> hashMap = new HashMap<Goid, ServiceUsage>();
        Collection<ServiceUsage> collection = serviceUsageManager.getAll();
        for (ServiceUsage entityHeaderArray2 : collection) {
            hashMap.put(entityHeaderArray2.getServiceid(), entityHeaderArray2);
        }
        ServiceManager serviceManager = (ServiceManager)this.applicationContext.getBean(a[2]);
        EntityHeader[] entityHeaderArray = serviceManager.findAllHeaders().toArray(new EntityHeader[0]);
        ServiceUsage[] serviceUsageArray = new ServiceUsage[entityHeaderArray.length];
        for (int i2 = 0; i2 < entityHeaderArray.length; ++i2) {
            EntityHeader entityHeader;
            block13: {
                block12: {
                    entityHeader = entityHeaderArray[i2];
                    ServiceUsage serviceUsage = (ServiceUsage)hashMap.get(entityHeader.getGoid());
                    try {
                        if (serviceUsage == null) break block12;
                        serviceUsageArray[i2] = serviceUsage;
                        break block13;
                    }
                    catch (FindException findException) {
                        throw SnmpQueryServlet.a((Exception)((Object)findException));
                    }
                }
                serviceUsageArray[i2] = new ServiceUsage();
                serviceUsageArray[i2].setServiceid(entityHeader.getGoid());
            }
            serviceUsageArray[i2].setName(entityHeader.getName());
        }
        this.serviceTable = serviceUsageArray;
        this.serviceTableTime = System.currentTimeMillis();
        return this.serviceTable;
    }

    private void badRequest(HttpServletResponse httpServletResponse) throws IOException {
        httpServletResponse.setStatus(400);
        httpServletResponse.getWriter().close();
    }

    private void send(HttpServletResponse httpServletResponse, String string, String string2, String string3) throws IOException {
        String string4;
        PrintWriter printWriter;
        PrintWriter printWriter2;
        block7: {
            block6: {
                String string5;
                PrintWriter printWriter3;
                httpServletResponse.setStatus(200);
                String[] stringArray = a;
                httpServletResponse.setContentType(stringArray[0]);
                printWriter2 = httpServletResponse.getWriter();
                try {
                    printWriter3 = printWriter2;
                    string5 = string != null ? string : "";
                }
                catch (IOException iOException) {
                    throw SnmpQueryServlet.a(iOException);
                }
                try {
                    try {
                        ((Writer)printWriter3).write(a[4] + string5);
                        ((Writer)printWriter2).write(10);
                        printWriter = printWriter2;
                        if (string2 == null || string2.length() <= 0) break block6;
                    }
                    catch (IOException iOException) {
                        throw SnmpQueryServlet.a(iOException);
                    }
                    string4 = string2;
                    break block7;
                }
                catch (IOException iOException) {
                    throw SnmpQueryServlet.a(iOException);
                }
            }
            string4 = a[15];
        }
        ((Writer)printWriter).write(string4);
        ((Writer)printWriter2).write(10);
        ((Writer)printWriter2).write(string3);
        ((Writer)printWriter2).write(10);
        ((Writer)printWriter2).flush();
        ((Writer)printWriter2).close();
    }

    private void send(HttpServletResponse httpServletResponse, String string, String string2, long l2) throws IOException {
        this.send(httpServletResponse, string, string2, String.valueOf(l2));
    }

    private void send(HttpServletResponse httpServletResponse, String string, String string2, Goid goid) throws IOException {
        this.send(httpServletResponse, string, string2, String.valueOf(goid));
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[27];
                var3_1 = 0;
                var2_2 = "\u000bkV!i?)\u001erI#/$e\u0019uK=\u0012D*\b~.jf[5\u0012c1ry](\u0016y\u000e\u0019~T;i?-'zH,g9:\r9~T;i?-JNU,g9\u0012D*\b~.jf[5\u0012c1ry](\u0016y\u0012\u000bkV!i?)\u001erI#/$e\u0019uK=\u00104G\be\\8cCG\be\\8cC?\u0004D,\b| \bnO!t5&9~T;i?-DhH p\r=\u000fi_ce2)\bwC)\u0007\u0003uR(g9:09Uk\u001d -=\u000fi_ms9:\u001crE( 2'\u001e;C#a>$\u000f\u007f\u0006\"n|<\u0002rUmc3&\u0004~E9o.\u0015#uR(r2)\u0006;u(r*-\u0018;c?r3:\u0007\ttS#t9:\u0007\ttS#t9:\u0007\r~R#e$<\u0006\u0019oT$n;\u0006\u0019oT$n;;44U>gs%\u000buG*e1-\u0004o\te[\u0002g71\u000fb.mfY5\u0010c1r|D*\b|7ox^3}}-ef71\u000fe/rbC$\u0002 9Uk\u001d -=\u000fi_ms9:\u001crE( 2'\u001e;G;a5$\u000byJ(\u0004D,\b|\f\u0019~T;e.\u000b\u0005u@$gS9Uk\u001d -=\u000fi_ms9:\u001crE( 8!\u0019zD!e8h\bb\u0006.l);\u001e~Tmp.'\u001a~T9yphMyS$l(!\u0004HC?v5+\u000f5U#m,\u0019\u001f~T4.9&\u000byJ(d{\u0013\u0019~T;i?-?hG*e\u0011)\u0004zA(r-9Uk\u001d -=\u000fi_ms9:\u001crE( 2'\u001e;G;a5$\u000byJ( 3&JoN$s|8\u0005iR\u0003\r~R";
                var4_3 = "\u000bkV!i?)\u001erI#/$e\u0019uK=\u0012D*\b~.jf[5\u0012c1ry](\u0016y\u000e\u0019~T;i?-'zH,g9:\r9~T;i?-JNU,g9\u0012D*\b~.jf[5\u0012c1ry](\u0016y\u0012\u000bkV!i?)\u001erI#/$e\u0019uK=\u00104G\be\\8cCG\be\\8cC?\u0004D,\b| \bnO!t5&9~T;i?-DhH p\r=\u000fi_ce2)\bwC)\u0007\u0003uR(g9:09Uk\u001d -=\u000fi_ms9:\u001crE( 2'\u001e;C#a>$\u000f\u007f\u0006\"n|<\u0002rUmc3&\u0004~E9o.\u0015#uR(r2)\u0006;u(r*-\u0018;c?r3:\u0007\ttS#t9:\u0007\ttS#t9:\u0007\r~R#e$<\u0006\u0019oT$n;\u0006\u0019oT$n;;44U>gs%\u000buG*e1-\u0004o\te[\u0002g71\u000fb.mfY5\u0010c1r|D*\b|7ox^3}}-ef71\u000fe/rbC$\u0002 9Uk\u001d -=\u000fi_ms9:\u001crE( 2'\u001e;G;a5$\u000byJ(\u0004D,\b|\f\u0019~T;e.\u000b\u0005u@$gS9Uk\u001d -=\u000fi_ms9:\u001crE( 8!\u0019zD!e8h\bb\u0006.l);\u001e~Tmp.'\u001a~T9yphMyS$l(!\u0004HC?v5+\u000f5U#m,\u0019\u001f~T4.9&\u000byJ(d{\u0013\u0019~T;i?-?hG*e\u0011)\u0004zA(r-9Uk\u001d -=\u000fi_ms9:\u001crE( 2'\u001e;G;a5$\u000byJ( 3&JoN$s|8\u0005iR\u0003\r~R".length();
                var1_4 = 18;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = SnmpQueryServlet.a(v0, SnmpQueryServlet.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "L$\u0000t&6!|@#a35\u0010rZ,g:`\u0007a\\*zo`\u0001|[)lt.\rg\u000e\"m `\u0003c^)a7!\u0016zA+(7/\fgK=|";
                    var4_3 = "L$\u0000t&6!|@#a35\u0010rZ,g:`\u0007a\\*zo`\u0001|[)lt.\rg\u000e\"m `\u0003c^)a7!\u0016zA+(7/\fgK=|".length();
                    var1_4 = 5;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 48;
                        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++] = SnmpQueryServlet.a(v0, SnmpQueryServlet.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;
            }
        }
        SnmpQueryServlet.a = var5;
        SnmpQueryServlet.CONTENT_TYPE_SNMP = SnmpQueryServlet.a[5];
        SnmpQueryServlet.BASE_PUBLISHED_SERVICES = SnmpQueryServlet.a[19];
        SnmpQueryServlet.BASE = SnmpQueryServlet.a[1];
        SnmpQueryServlet.logger = Logger.getLogger(SnmpQueryServlet.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] ^ 0x70);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String a(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 -> 82;
                    case 1 -> 35;
                    case 2 -> 30;
                    case 3 -> 117;
                    case 4 -> 56;
                    case 5 -> 100;
                    default -> 112;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

