/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.protocol.SecureSpanConstants
 *  com.l7tech.common.protocol.SecureSpanConstants$HttpHeaders
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.gateway.common.audit.AuditFactory
 *  com.l7tech.gateway.common.audit.MessageProcessingMessages
 *  com.l7tech.gateway.common.service.PublishedService
 *  com.l7tech.gateway.common.transport.ResolutionConfiguration
 *  com.l7tech.message.HasServiceId
 *  com.l7tech.message.HttpRequestKnob
 *  com.l7tech.message.Message
 *  com.l7tech.objectmodel.EntityType
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.util.GoidUpgradeMapper
 */
package com.l7tech.server.service.resolution;

import com.l7tech.common.protocol.SecureSpanConstants;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.gateway.common.audit.AuditFactory;
import com.l7tech.gateway.common.audit.MessageProcessingMessages;
import com.l7tech.gateway.common.service.PublishedService;
import com.l7tech.gateway.common.transport.ResolutionConfiguration;
import com.l7tech.message.HasServiceId;
import com.l7tech.message.HttpRequestKnob;
import com.l7tech.message.Message;
import com.l7tech.objectmodel.EntityType;
import com.l7tech.objectmodel.Goid;
import com.l7tech.server.service.resolution.NameValueServiceResolver;
import com.l7tech.server.service.resolution.ServiceResolutionException;
import com.l7tech.util.GoidUpgradeMapper;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ServiceIdResolver
extends NameValueServiceResolver<String> {
    private final Pattern[] b;
    private final AtomicBoolean c = new AtomicBoolean(true);
    private final AtomicBoolean d = new AtomicBoolean(true);
    private static final String[] e;

    public ServiceIdResolver(AuditFactory auditFactory) {
        super(auditFactory);
        Pattern[] patternArray;
        ArrayList<Pattern> arrayList = new ArrayList<Pattern>();
        try {
            for (String string : SecureSpanConstants.getResolutionByIdRegexes()) {
                arrayList.add(Pattern.compile(string));
            }
        }
        catch (Exception exception) {
            this.logger.log(Level.WARNING, e[0], exception);
            arrayList.clear();
        }
        try {
            ServiceIdResolver serviceIdResolver = this;
            patternArray = arrayList.isEmpty() ? null : arrayList.toArray(new Pattern[arrayList.size()]);
        }
        catch (Exception exception) {
            throw ServiceIdResolver.a(exception);
        }
        serviceIdResolver.b = patternArray;
    }

    @Override
    public void configure(ResolutionConfiguration resolutionConfiguration) {
        super.configure(resolutionConfiguration);
        this.c.set(resolutionConfiguration.isUseServiceOid());
        this.d.set(resolutionConfiguration.isUseL7OriginalUrl());
    }

    @Override
    protected List<String> buildTargetValues(PublishedService publishedService) {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add(Goid.toString((Goid)publishedService.getGoid()));
        if (GoidUpgradeMapper.prefixMatches((EntityType)EntityType.SERVICE, (long)publishedService.getGoid().getHi())) {
            arrayList.add(Long.toString(publishedService.getGoid().getLow()));
        }
        return arrayList;
    }

    /*
     * Loose catch block
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    @Override
    protected String getRequestValue(Message message) throws ServiceResolutionException {
        String string;
        String string2;
        HttpRequestKnob httpRequestKnob;
        block25: {
            block23: {
                block22: {
                    HasServiceId hasServiceId = (HasServiceId)message.getKnob(HasServiceId.class);
                    if (hasServiceId == null) break block22;
                    try {
                        if (Goid.isDefault((Goid)hasServiceId.getServiceGoid())) break block22;
                        return Goid.toString((Goid)hasServiceId.getServiceGoid());
                        catch (MalformedURLException malformedURLException) {
                            throw ServiceIdResolver.a(malformedURLException);
                        }
                    }
                    catch (MalformedURLException malformedURLException) {
                        throw ServiceIdResolver.a(malformedURLException);
                    }
                }
                if (!this.c.get()) return null;
                try {
                    if (this.b != null) break block23;
                    return null;
                    catch (MalformedURLException malformedURLException) {
                        throw ServiceIdResolver.a(malformedURLException);
                    }
                }
                catch (MalformedURLException malformedURLException) {
                    throw ServiceIdResolver.a(malformedURLException);
                }
            }
            httpRequestKnob = (HttpRequestKnob)message.getKnob(HttpRequestKnob.class);
            try {
                if (httpRequestKnob == null) {
                    return null;
                }
            }
            catch (MalformedURLException malformedURLException) {
                throw ServiceIdResolver.a(malformedURLException);
            }
            if (this.d.get()) {
                block24: {
                    string2 = httpRequestKnob.getHeaderFirstValue(SecureSpanConstants.HttpHeaders.ORIGINAL_URL);
                    try {
                        if (string2 != null) break block24;
                        this.auditor.logAndAudit((AuditDetailMessage)MessageProcessingMessages.SR_ORIGURL_NOHEADER, new String[]{SecureSpanConstants.HttpHeaders.ORIGINAL_URL});
                        break block25;
                    }
                    catch (MalformedURLException malformedURLException) {
                        throw ServiceIdResolver.a(malformedURLException);
                    }
                }
                try {
                    Object object;
                    if (SecureSpanConstants.USE_RESOLUTION_BY_ID_PRE_8_1) {
                        string = string2;
                    } else {
                        object = new URL(string2);
                        string = ((URL)object).getFile();
                    }
                    object = this.a(string);
                    if (object != null) {
                        this.auditor.logAndAudit((AuditDetailMessage)MessageProcessingMessages.SR_ORIGURL_HEADER_MATCH, new String[]{SecureSpanConstants.HttpHeaders.ORIGINAL_URL, string2});
                        return ((String)object).toLowerCase();
                    }
                }
                catch (MalformedURLException malformedURLException) {
                    String string3 = MessageFormat.format(e[1], string2);
                    this.logger.log(Level.WARNING, string3, malformedURLException);
                    throw new ServiceResolutionException(string3);
                }
                this.auditor.logAndAudit((AuditDetailMessage)MessageProcessingMessages.SR_ORIGURL_HEADER_NOMATCH, new String[]{SecureSpanConstants.HttpHeaders.ORIGINAL_URL, string2});
            }
        }
        string2 = httpRequestKnob.getRequestUri();
        string = this.a(string2);
        try {
            if (string != null) {
                this.auditor.logAndAudit((AuditDetailMessage)MessageProcessingMessages.SR_ORIGURL_URI_MATCH, new String[]{string2});
                return string.toLowerCase();
            }
        }
        catch (MalformedURLException malformedURLException) {
            throw ServiceIdResolver.a(malformedURLException);
        }
        this.auditor.logAndAudit((AuditDetailMessage)MessageProcessingMessages.SR_ORIGURL_URI_NOMATCH, new String[]{string2});
        return null;
    }

    private String a(String string) {
        for (Pattern pattern : this.b) {
            Matcher matcher = pattern.matcher(string);
            if (!matcher.find() || matcher.groupCount() != 1) continue;
            return matcher.group(1);
        }
        return null;
    }

    @Override
    public boolean usesMessageContent() {
        return false;
    }

    @Override
    public boolean isApplicableToMessage(Message message) throws ServiceResolutionException {
        boolean bl2;
        try {
            bl2 = this.getRequestValue(message) != null;
        }
        catch (ServiceResolutionException serviceResolutionException) {
            throw ServiceIdResolver.a(serviceResolutionException);
        }
        return bl2;
    }

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

    /*
     * Enabled aggressive block sorting
     */
    static {
        String[] stringArray = new String[2];
        int n2 = 0;
        String string = "\u000b0\u001e|\u0011\u000e\u0001+bl\\\u000e\u000b\u001f/c?p\u0019\u0015M,q%u\u0013\u001fM>\u007flz\u0019\u0016\u001d#|)7V/\u0005#clk\u0013\b\u0002&f)kV\u0012\u001ejt%j\u0017\u0019\u0001/tb&\u0003~:x\u001a\u0012\tj\\{49\t\u0004-y\"x\u001aV8\u0018\\lo\u0017\u0017\u0018/*l>Q\u0000]77k";
        int n3 = "\u000b0\u001e|\u0011\u000e\u0001+bl\\\u000e\u000b\u001f/c?p\u0019\u0015M,q%u\u0013\u001fM>\u007flz\u0019\u0016\u001d#|)7V/\u0005#clk\u0013\b\u0002&f)kV\u0012\u001ejt%j\u0017\u0019\u0001/tb&\u0003~:x\u001a\u0012\tj\\{49\t\u0004-y\"x\u001aV8\u0018\\lo\u0017\u0017\u0018/*l>Q\u0000]77k".length();
        int n4 = 66;
        int n5 = -1;
        while (true) {
            int n6 = ++n5;
            stringArray[n2++] = ServiceIdResolver.b(102, ServiceIdResolver.b(string.substring(n6, n6 + n4)));
            if ((n5 += n4) >= n3) {
                e = stringArray;
                return;
            }
            n4 = string.charAt(n5);
        }
    }

    private static char[] b(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 -> 44;
                    case 1 -> 118;
                    case 2 -> 42;
                    case 3 -> 127;
                    case 4 -> 16;
                    case 5 -> 29;
                    default -> 11;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

