/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.XmlUtil
 *  com.l7tech.gateway.common.service.PublishedService
 *  com.l7tech.message.Message
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.util.InvalidDocumentFormatException
 *  com.l7tech.xml.soap.SoapUtil
 *  jakarta.annotation.PostConstruct
 *  jakarta.inject.Inject
 *  org.springframework.context.ApplicationEvent
 */
package com.l7tech.server.wsdm;

import com.l7tech.common.io.XmlUtil;
import com.l7tech.gateway.common.service.PublishedService;
import com.l7tech.message.Message;
import com.l7tech.objectmodel.Goid;
import com.l7tech.server.event.EntityInvalidationEvent;
import com.l7tech.server.event.ServiceEnablementEvent;
import com.l7tech.server.service.ServiceCache;
import com.l7tech.server.util.PostStartupApplicationListener;
import com.l7tech.server.wsdm.QoSMetricsService;
import com.l7tech.server.wsdm.faults.FaultMappableException;
import com.l7tech.server.wsdm.faults.ResourceUnknownFault;
import com.l7tech.server.wsdm.method.ESMMethod;
import com.l7tech.server.wsdm.method.GetMultipleResourceProperties;
import com.l7tech.server.wsdm.method.Renew;
import com.l7tech.server.wsdm.method.Subscribe;
import com.l7tech.server.wsdm.method.Unsubscribe;
import com.l7tech.server.wsdm.subscription.ServiceStateMonitor;
import com.l7tech.server.wsdm.subscription.Subscription;
import com.l7tech.server.wsdm.subscription.SubscriptionNotifier;
import com.l7tech.server.wsdm.util.EsmUtils;
import com.l7tech.util.InvalidDocumentFormatException;
import com.l7tech.xml.soap.SoapUtil;
import jakarta.annotation.PostConstruct;
import jakarta.inject.Inject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.context.ApplicationEvent;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class ServiceManagementAdministrationService
implements PostStartupApplicationListener {
    private static final Logger a;
    @Inject
    private QoSMetricsService qosService;
    @Inject
    private SubscriptionNotifier subscriptionNotifier;
    @Inject
    private ServiceCache serviceCache;
    private final ArrayList<ServiceStateMonitor> b = new ArrayList();
    private final List<ServiceStateMonitor> c;
    private static final String[] d;

    public ServiceManagementAdministrationService(List<ServiceStateMonitor> list) {
        this.c = list;
    }

    @PostConstruct
    public void start() {
        this.b.add(this.subscriptionNotifier);
        this.b.addAll(this.c);
    }

    public Document handleESMRequest(Goid goid, Message message) throws FaultMappableException, IOException, SAXException {
        ESMMethod eSMMethod = ESMMethod.resolve(message, goid);
        try {
            if (eSMMethod instanceof GetMultipleResourceProperties) {
                return this.qosService.handleMultipleResourcePropertiesRequest((GetMultipleResourceProperties)eSMMethod);
            }
        }
        catch (FaultMappableException faultMappableException) {
            throw ServiceManagementAdministrationService.a(faultMappableException);
        }
        throw new FaultMappableException(this.a(eSMMethod));
    }

    public Document handleSubscriptionRequest(Goid goid, Message message, String string) throws FaultMappableException, IOException, SAXException {
        ESMMethod eSMMethod = ESMMethod.resolve(message, goid);
        try {
            if (eSMMethod instanceof Renew) {
                return this.a((Renew)eSMMethod, string);
            }
        }
        catch (FaultMappableException faultMappableException) {
            throw ServiceManagementAdministrationService.a(faultMappableException);
        }
        try {
            if (eSMMethod instanceof Subscribe) {
                return this.a((Subscribe)eSMMethod, string);
            }
        }
        catch (FaultMappableException faultMappableException) {
            throw ServiceManagementAdministrationService.a(faultMappableException);
        }
        try {
            if (eSMMethod instanceof Unsubscribe) {
                return this.a((Unsubscribe)eSMMethod);
            }
        }
        catch (FaultMappableException faultMappableException) {
            throw ServiceManagementAdministrationService.a(faultMappableException);
        }
        try {
            throw new FaultMappableException(this.a(eSMMethod));
        }
        catch (SAXException sAXException) {
            a.log(Level.WARNING, d[8] + String.valueOf(eSMMethod), sAXException);
            throw new FaultMappableException(sAXException.getMessage());
        }
    }

    private String a(ESMMethod eSMMethod) {
        Object object;
        if (eSMMethod == null) {
            String[] stringArray = d;
            object = stringArray[0];
        } else {
            object = d[9] + eSMMethod.toString();
        }
        return object;
    }

    private Document a(Unsubscribe unsubscribe) throws SAXException, ResourceUnknownFault {
        this.subscriptionNotifier.unsubscribe(unsubscribe.getSubscriptionIdValue());
        String string = unsubscribe.respond(unsubscribe.getIncomingUrl().toString());
        return XmlUtil.stringToDocument((String)string);
    }

    private Document a(Subscribe subscribe, String string) throws SAXException, FaultMappableException {
        block3: {
            block2: {
                String string2 = this.a(subscribe.getIncomingUrl().toString(), subscribe.getReqestDoc());
                try {
                    if (string2 == null) break block2;
                    subscribe.setServiceId(string2);
                    break block3;
                }
                catch (SAXException sAXException) {
                    throw ServiceManagementAdministrationService.a(sAXException);
                }
            }
            String[] stringArray = d;
            a.warning(stringArray[11]);
            throw new ResourceUnknownFault(stringArray[7]);
        }
        Subscription subscription = this.subscriptionNotifier.subscribe(subscribe, string);
        String string3 = subscribe.respond(subscribe.getIncomingUrl().toString(), subscription.getUuid());
        return XmlUtil.stringToDocument((String)string3);
    }

    private String a(String string, Document document) throws ResourceUnknownFault {
        String[] stringArray;
        String string2 = EsmUtils.determineServiceFromUrl(string, this.serviceCache);
        try {
            if (string2 != null) {
                return string2;
            }
        }
        catch (InvalidDocumentFormatException invalidDocumentFormatException) {
            throw ServiceManagementAdministrationService.a(invalidDocumentFormatException);
        }
        Element element = null;
        try {
            element = SoapUtil.getHeaderElement((Document)document);
        }
        catch (InvalidDocumentFormatException invalidDocumentFormatException) {
            stringArray = d;
            a.log(Level.WARNING, stringArray[5], invalidDocumentFormatException);
        }
        if (element != null) {
            String[] stringArray2 = new String[2];
            stringArray = d;
            stringArray2[0] = stringArray[2];
            stringArray2[1] = stringArray[10];
            Element element2 = XmlUtil.findFirstChildElementByName((Node)element, (String[])stringArray2, (String)stringArray[12]);
            if (element2 != null) {
                String string3 = XmlUtil.getTextValue((Element)element2);
                string2 = EsmUtils.determineServiceFromUrl(string3, this.serviceCache);
                try {
                    if (string2 != null) {
                        return string2;
                    }
                }
                catch (InvalidDocumentFormatException invalidDocumentFormatException) {
                    throw ServiceManagementAdministrationService.a(invalidDocumentFormatException);
                }
                stringArray = d;
                a.warning(stringArray[4] + string3);
            } else {
                stringArray = d;
                a.info(stringArray[6]);
            }
        }
        a.warning(d[3]);
        return null;
    }

    private Document a(Renew renew, String string) throws SAXException, FaultMappableException {
        this.subscriptionNotifier.renewSubscription(renew.getSubscriptionIdValue(), renew.getTermination(), string);
        String string2 = renew.respond(renew.getIncomingUrl().toString());
        return XmlUtil.stringToDocument((String)string2);
    }

    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        try {
            if (!(applicationEvent instanceof EntityInvalidationEvent)) {
                return;
            }
        }
        catch (Throwable throwable) {
            throw ServiceManagementAdministrationService.a(throwable);
        }
        try {
            EntityInvalidationEvent entityInvalidationEvent = (EntityInvalidationEvent)applicationEvent;
            if (!PublishedService.class.isAssignableFrom(entityInvalidationEvent.getEntityClass())) {
                return;
            }
            block11: for (int i2 = 0; i2 < entityInvalidationEvent.getEntityIds().length; ++i2) {
                Goid goid = entityInvalidationEvent.getEntityIds()[i2];
                char c10 = entityInvalidationEvent.getEntityOperations()[i2];
                switch (c10) {
                    case 'C': {
                        for (ServiceStateMonitor serviceStateMonitor : this.b) {
                            serviceStateMonitor.onServiceCreated(goid);
                        }
                        continue block11;
                    }
                    case 'U': {
                        if (!(entityInvalidationEvent instanceof ServiceEnablementEvent)) continue block11;
                        ServiceEnablementEvent serviceEnablementEvent = (ServiceEnablementEvent)entityInvalidationEvent;
                        for (ServiceStateMonitor serviceStateMonitor : this.b) {
                            try {
                                if (serviceEnablementEvent.isEnabled()) {
                                    serviceStateMonitor.onServiceEnabled(goid);
                                    continue;
                                }
                            }
                            catch (Throwable throwable) {
                                throw ServiceManagementAdministrationService.a(throwable);
                            }
                            serviceStateMonitor.onServiceDisabled(goid);
                        }
                        continue block11;
                    }
                    case 'D': {
                        for (ServiceStateMonitor serviceStateMonitor : this.b) {
                            serviceStateMonitor.onServiceDeleted(goid);
                        }
                        continue block11;
                    }
                }
            }
        }
        catch (Throwable throwable) {
            a.log(Level.SEVERE, d[1], throwable);
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[13];
                var3_1 = 0;
                var2_2 = "}\u0010$|-\"eZ\n2m}=zM\f6}4=d\u0006+}\u00102q-7i\\\u001b3)8 xG\fwa<<nD\u00179n}3zX\u0012>j<&cG\u0010wl+7d\\+@\n#yg}%L\u00114zs=k[\u0017$$2\"oFP8{:}}[\u001a:&0'}[Oz;s*yL9K\u001f9g2&*L\u001b3|>7*[\u001b%\u007f41o\b\t?f.7*Z\u001b$f( iM^>m}&bA\rw`.rgM\u001f9}}4eZ0K\u0011\"e9rdG\nwl%&xI\u001d#)<ryM\f!`>7*A\u001awo/=g\b\n?l}\"k\\\n2{3r\u0014M\f%f/rmM\n#`35*@\u001b6m8 \u001ff\u0011w[8!e]\f4l\u00146*M\u00122d8<~\b\u00179)57kL\u001b%\u0014z\u001b$f( iM^\u001eM} oY\u000b>{86!x\f8k17g\b\u001d8g.&x]\u001d#`35*Z\u001b$y2<yM^#f}\u0016e\u001b#a26*F\u0011#).'zX\u0011%}860\b+@\n#yg}%L\u00114zs=k[\u0017$$2\"oFP8{:}}[\u001a:&0'}[Lz;s*yL";
                var4_3 = "}\u0010$|-\"eZ\n2m}=zM\f6}4=d\u0006+}\u00102q-7i\\\u001b3)8 xG\fwa<<nD\u00179n}3zX\u0012>j<&cG\u0010wl+7d\\+@\n#yg}%L\u00114zs=k[\u0017$$2\"oFP8{:}}[\u001a:&0'}[Oz;s*yL9K\u001f9g2&*L\u001b3|>7*[\u001b%\u007f41o\b\t?f.7*Z\u001b$f( iM^>m}&bA\rw`.rgM\u001f9}}4eZ0K\u0011\"e9rdG\nwl%&xI\u001d#)<ryM\f!`>7*A\u001awo/=g\b\n?l}\"k\\\n2{3r\u0014M\f%f/rmM\n#`35*@\u001b6m8 \u001ff\u0011w[8!e]\f4l\u00146*M\u00122d8<~\b\u00179)57kL\u001b%\u0014z\u001b$f( iM^\u001eM} oY\u000b>{86!x\f8k17g\b\u001d8g.&x]\u001d#`35*Z\u001b$y2<yM^#f}\u0016e\u001b#a26*F\u0011#).'zX\u0011%}860\b+@\n#yg}%L\u00114zs=k[\u0017$$2\"oFP8{:}}[\u001a:&0'}[Lz;s*yL".length();
                var1_4 = 22;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServiceManagementAdministrationService.a(v0, ServiceManagementAdministrationService.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u0005uK\u0004@^\u001d&t@\u0018M\f\u00009 HWPI\u0006 iJ\u0012\u0003[\u001d\"hF\u0002W\f\u00158y\t\u0005F_\u001b#rJ\u0012\u0003E\u0010vsY\u0012@E\u0012?eM\n\u0004eZ\u0018V^\u00173IM";
                    var4_3 = "\u0005uK\u0004@^\u001d&t@\u0018M\f\u00009 HWPI\u0006 iJ\u0012\u0003[\u001d\"hF\u0002W\f\u00158y\t\u0005F_\u001b#rJ\u0012\u0003E\u0010vsY\u0012@E\u0012?eM\n\u0004eZ\u0018V^\u00173IM".length();
                    var1_4 = 59;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 100;
                        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++] = ServiceManagementAdministrationService.a(v0, ServiceManagementAdministrationService.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;
            }
        }
        ServiceManagementAdministrationService.d = var5;
        ServiceManagementAdministrationService.a = Logger.getLogger(ServiceManagementAdministrationService.class.getName());
    }

    private static Throwable a(Throwable throwable) {
        return throwable;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x10);
        }
        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 -> 50;
                    case 1 -> 100;
                    case 2 -> 77;
                    case 3 -> 19;
                    case 4 -> 71;
                    case 5 -> 72;
                    default -> 16;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

