McAfee Security v6.7.1.27版本的 MD5 值为:f931794df50c0876bab25b112d85d702

以下内容为反编译后的 CspServiceDiscoveryClient.java 源代码,内容仅作参考


package com.mcafee.csp.internal.base.servicediscovery;

import android.content.Context;
import com.mcafee.csp.internal.base.CspComponent;
import com.mcafee.csp.internal.base.enrollment.CspEnrollInfo;
import com.mcafee.csp.internal.base.enrollment.basic.CspBasicEnrollClient;
import com.mcafee.csp.internal.base.environment.CspEnvironmentStore;
import com.mcafee.csp.internal.base.errorexception.CspErrorInfo;
import com.mcafee.csp.internal.base.errorexception.CspHttpException;
import com.mcafee.csp.internal.base.errorexception.ErrorInfoSerializerType;
import com.mcafee.csp.internal.base.errorexception.ErrorInfoUtils;
import com.mcafee.csp.internal.base.logging.Tracer;
import com.mcafee.csp.internal.base.network.CspHttpClient;
import com.mcafee.csp.internal.base.network.CspHttpResponse;
import com.mcafee.csp.internal.base.network.CspHttpResponseHandler;
import com.mcafee.csp.internal.base.network.URLUtils;
import com.mcafee.csp.internal.base.utils.CoreUtils;
import com.mcafee.csp.internal.base.utils.DeviceUtils;
import com.mcafee.csp.internal.base.utils.EnvUtils;
import com.mcafee.csp.internal.base.utils.StringUtils;
import com.mcafee.csp.internal.constants.Constants;
import com.mcafee.csp.internal.constants.RESTErrors;
import com.mcafee.csp.internal.constants.ServerNames;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.locks.ReentrantLock;
import org.json.JSONException;
import org.json.JSONObject;
public class CspServiceDiscoveryClient extends CspComponent {
    public static final String OP_CODE_GET = "get";
    private static final String c = "CspServiceDiscoveryClient";
    private static boolean d = true;
    private static String e = "";
    private static ReentrantLock f = new ReentrantLock(true);
    private static HashMap<String, CspServiceDiscovery> g = new HashMap<>();
    private Context f8038a;
    private CspErrorInfo b;

    public CspServiceDiscoveryClient(Context context) {
        this.f8038a = context;
        this.name = Constants.COMPONENT_SD;
    }

    private CspServiceDiscovery a(CspAppInfo cspAppInfo) {
        if (cspAppInfo == null || !StringUtils.isValidString(cspAppInfo.getAppId())) {
            return null;
        }
        CspServiceDiscovery cspServiceDiscovery = g.get(cspAppInfo.getAppId());
        if (cspServiceDiscovery == null || cspServiceDiscovery.isExpiredData()) {
            cspServiceDiscovery = new CspServiceDiscoveryStore(this.f8038a).get(cspAppInfo.getAppId());
            g.put(cspAppInfo.getAppId(), cspServiceDiscovery);
        }
        if (cspServiceDiscovery == null) {
            return null;
        }
        if (DeviceUtils.getCurrentTime() > Long.valueOf(cspServiceDiscovery.getTtl()).longValue()) {
            cspServiceDiscovery.setExpiredData(true);
        }
        return cspServiceDiscovery;
    }

    private CspServiceDiscovery b(CspAppInfo cspAppInfo, CspServiceDiscovery cspServiceDiscovery) {
        String e2 = e(cspAppInfo);
        String c2 = c();
        CspServiceDiscovery cspServiceDiscovery2 = null;
        for (int i = 0; i < 2; i++) {
            CspHttpClient cspHttpClient = new CspHttpClient(this.f8038a, cspAppInfo.getAppId(), ServerNames.SERVER_DISCOVERY.toString());
            cspHttpClient.setParent(this);
            String str = cspServiceDiscovery != null ? cspServiceDiscovery.geteTag() : "";
            cspHttpClient.addHTTPHeader("If-None-Match", str != null ? str : "");
            try {
                CspHttpResponse doHttpPost = cspHttpClient.doHttpPost(c2, e2, "application/json", "e4924ad0-c513-11e3-be43-ef8523d0c858");
                if (doHttpPost.getResponseCode() != 304) {
                    String response = doHttpPost.getResponse();
                    if (response != null && !response.isEmpty() && response.length() <= 20480) {
                        CspServiceDiscovery cspServiceDiscovery3 = new CspServiceDiscovery();
                        try {
                            if (cspServiceDiscovery3.loadFromJson(response)) {
                                cspServiceDiscovery3.seteTag(CspHttpResponseHandler.getEtag(doHttpPost.getResponseHeaders()));
                                cspServiceDiscovery2 = cspServiceDiscovery3;
                            } else {
                                Tracer.e(c, String.format("Unable to parse sd json %s", response));
                                cspServiceDiscovery2 = null;
                            }
                        } catch (CspHttpException unused) {
                            cspServiceDiscovery2 = cspServiceDiscovery3;
                            if (cspHttpClient.getCspHttpResponse() == null) {
                                CspErrorInfo buildErrorInfoFromHttpResponse = ErrorInfoUtils.buildErrorInfoFromHttpResponse(cspHttpClient.getCspHttpResponse(), cspAppInfo.getAppId(), ServerNames.SERVER_DISCOVERY.toString());
                                if (buildErrorInfoFromHttpResponse != null) {
                                    ErrorInfoSerializerType errorInfoSerializerType = ErrorInfoSerializerType.CSP_BACKEND;
                                    if (buildErrorInfoFromHttpResponse.getErrorInfoSerializerMapForKey(errorInfoSerializerType) == null) {
                                        continue;
                                    } else if (!buildErrorInfoFromHttpResponse.getErrorInfoSerializerMapForKey(errorInfoSerializerType).getErrorCode().equals(RESTErrors.ClientIdNonceMistch.toString())) {
                                        return cspServiceDiscovery2;
                                    }
                                } else {
                                    continue;
                                }
                            } else {
                                continue;
                            }
                        }
                    }
                    Tracer.e(c, "Invalid Service discovery response" + response);
                    return cspServiceDiscovery2;
                } else if (cspServiceDiscovery != null) {
                    try {
                        cspServiceDiscovery.setTtl(String.valueOf(DeviceUtils.getCurrentTime() + CspHttpResponseHandler.getMaxAge(doHttpPost.getResponseHeaders())));
                        cspServiceDiscovery2 = cspServiceDiscovery;
                    } catch (CspHttpException unused2) {
                        cspServiceDiscovery2 = cspServiceDiscovery;
                        if (cspHttpClient.getCspHttpResponse() == null) {
                        }
                    }
                }
                if (cspServiceDiscovery2 == null) {
                    continue;
                } else if (getServiceDiscoveryStore().store(cspAppInfo.getAppId(), cspServiceDiscovery2.toJSON(), cspServiceDiscovery2.getTtl(), StringUtils.mapsToJsonString(cspAppInfo.getRoutingParams()), cspServiceDiscovery2.geteTag())) {
                    return cspServiceDiscovery2;
                } else {
                    Tracer.e(c, String.format("Unable to store sd json %s", cspServiceDiscovery2.toJSON()));
                    cspServiceDiscovery2 = null;
                }
            } catch (CspHttpException unused3) {
            }
        }
        return cspServiceDiscovery2;
    }

    private String c() {
        if (e.isEmpty()) {
            e = URLUtils.formatURLWithSetEnv(this.f8038a, "https://servicediscovery%sccs.mcafee.%s/applicationurls");
        }
        return e;
    }

    private CspServiceDiscovery d(CspAppInfo cspAppInfo) {
        String str = c;
        Tracer.i(str, "getServerInfoInternal()");
        f.lock();
        try {
            Tracer.i(str, "getServerInfoInternal() trying from cache");
            CspServiceDiscovery a2 = a(cspAppInfo);
            Tracer.i(str, "From cache result :" + a2);
            boolean z = true;
            if (a2 != null) {
                if (cspAppInfo.getCompareRoutingParams() && !StringUtils.areMapsSame(a2.getRoutingParams(), cspAppInfo.getRoutingParams())) {
                    a2.seteTag("");
                    Tracer.d(str, "The routingParams are not same for appid:" + cspAppInfo.getAppId() + ", routingParamsAreSame is set to false");
                    z = false;
                }
                if (a2.isExpiredData()) {
                    Tracer.d(str, "SD ttl is expired");
                    if (!cspAppInfo.getCompareRoutingParams()) {
                        cspAppInfo.setRoutingParams(a2.getRoutingParams());
                    }
                } else if (z) {
                    Tracer.d(str, "routingParams are same for appid:" + cspAppInfo.getAppId() + ", so returning from cache");
                    return a2;
                }
            }
            CspServiceDiscovery b = b(cspAppInfo, a2);
            Tracer.i(str, "Got from service :" + b);
            if (b != null) {
                return b;
            }
            Tracer.e(str, String.format("Invalid Service discovery response from server", new Object[0]));
            if (!z || a2 == null) {
                if ("e4924ad0-c513-11e3-be43-ef8523d0c858".compareTo(cspAppInfo.getAppId()) == 0) {
                    return new CspServiceDiscoveryDefault(this.f8038a).getDefaultCspServiceDiscoveryForCsp();
                }
                f.unlock();
                return null;
            }
            return a2;
        } finally {
            f.unlock();
        }
    }

    private String e(CspAppInfo cspAppInfo) {
        CspEnrollInfo cspEnrollInfo;
        CspSDRequest cspSDRequest = new CspSDRequest();
        if (d && (cspEnrollInfo = new CspBasicEnrollClient(this.f8038a, "e4924ad0-c513-11e3-be43-ef8523d0c858", true, false).getCspEnrollInfo()) != null) {
            cspSDRequest.setClientId(cspEnrollInfo.getCspDeviceId().getDeviceId());
            cspSDRequest.setNonce(cspEnrollInfo.getCspDeviceId().getNonce());
        }
        cspSDRequest.setOpCode(cspAppInfo.getOperation());
        cspSDRequest.setSdkVersion(CoreUtils.getVersionName());
        cspSDRequest.setLocale(DeviceUtils.getDeviceLocale());
        ArrayList<CspAppRequest> arrayList = new ArrayList<>();
        CspAppRequest cspAppRequest = new CspAppRequest();
        cspAppRequest.setApplicationId(cspAppInfo.getAppId());
        cspAppRequest.setPpVersion(cspAppInfo.getPpVersion());
        cspAppRequest.setRoutingParams(cspAppInfo.getRoutingParams());
        arrayList.add(cspAppRequest);
        cspSDRequest.setApplications(arrayList);
        cspSDRequest.setEnv(new CspEnvironmentStore(this.f8038a).getBareEnvironment());
        HashMap<String, String> hashMap = new HashMap<>();
        JSONObject envConfigJson = EnvUtils.getEnvConfigJson(this.f8038a);
        String str = c;
        Tracer.i(str, "Configuration json is :" + envConfigJson);
        if (envConfigJson != null) {
            JSONObject optJSONObject = envConfigJson.optJSONObject(cspAppInfo.getAppId() + ".sd");
            if (optJSONObject != null) {
                Iterator<String> keys = optJSONObject.keys();
                while (keys.hasNext()) {
                    String next = keys.next();
                    try {
                        hashMap.put(next, optJSONObject.getString(next));
                    } catch (JSONException e2) {
                        Tracer.e(c, e2.getMessage());
                    }
                }
            }
        }
        return cspSDRequest.toJSON(hashMap);
    }

    public static void resetSDURL() {
        e = "";
    }

    public boolean configureEndPoint(CspAppInfo cspAppInfo) {
        f.lock();
        try {
            return b(cspAppInfo, null) != null;
        } finally {
            f.unlock();
        }
    }

    public boolean expireSDTTL(String str) {
        return getServiceDiscoveryStore().expireSDTTL(str);
    }

    public CspServer getCSPServerInfo(String str, String str2, String str3) {
        if (this.b == null) {
            this.b = new CspErrorInfo();
        }
        try {
            CspAppInfo cspAppInfo = new CspAppInfo();
            cspAppInfo.setAppId(str);
            cspAppInfo.setOperation(str2);
            CspServiceDiscovery d2 = d(cspAppInfo);
            if (d2 == null || d2.getCspServers() == null) {
                Tracer.e(c, "unable to get server urls");
                this.b.setExtendedInfo(Constants.EXTENDED_INFO_KEY_SD_ERR_FAILED, Constants.EXTENDED_INFO_KEY_SD_ERR_FAILED);
            } else {
                Iterator<CspServer> it = d2.getCspServers().iterator();
                while (it.hasNext()) {
                    CspServer next = it.next();
                    if (next.getServiceName().compareTo(str3) == 0) {
                        return next;
                    }
                }
                this.b.setExtendedInfo(Constants.EXTENDED_INFO_KEY_SD_ERR_SERVICE_NOT_PRESENT, Constants.EXTENDED_INFO_KEY_SD_ERR_SERVICE_NOT_PRESENT);
            }
            return null;
        } catch (Exception e2) {
            String str4 = c;
            Tracer.e(str4, "Exception in getCSPServerInfo :" + e2.getMessage());
            return null;
        }
    }

    public CspErrorInfo getCspErrorInfo() {
        return this.b;
    }

    public CspServiceDiscovery getServerDiscoveryInfo(CspAppInfo cspAppInfo) {
        return d(cspAppInfo);
    }

    public ArrayList<CspServer> getServerDiscoveryInfo(String str, String str2) {
        CspAppInfo cspAppInfo = new CspAppInfo();
        cspAppInfo.setAppId(str);
        cspAppInfo.setOperation(str2);
        CspServiceDiscovery serverDiscoveryInfo = getServerDiscoveryInfo(cspAppInfo);
        if (serverDiscoveryInfo == null || serverDiscoveryInfo.getCspServers() == null) {
            String str3 = c;
            Tracer.e(str3, "service discovery failed for appId :" + str);
            return null;
        }
        return serverDiscoveryInfo.getCspServers();
    }

    public CspServiceDiscovery getServerDiscoveryInfoWithID(CspAppInfo cspAppInfo) {
        String str = c;
        Tracer.i(str, "Getting service discovery with appinfo :" + cspAppInfo);
        CspServiceDiscovery serverDiscoveryInfo = getServerDiscoveryInfo(cspAppInfo);
        Tracer.i(str, "Got service discovery info :" + serverDiscoveryInfo);
        if (serverDiscoveryInfo != null) {
            serverDiscoveryInfo.setHardwareId(DeviceUtils.getHardwareId(this.f8038a));
            serverDiscoveryInfo.setSoftwareId(DeviceUtils.getSoftwareId(this.f8038a));
            serverDiscoveryInfo.setDeviceId(new CspBasicEnrollClient(this.f8038a, "e4924ad0-c513-11e3-be43-ef8523d0c858", true, false).getDeviceId());
        }
        return serverDiscoveryInfo;
    }

    public ArrayList<String> getServersForService(String str, String str2, String str3) {
        ArrayList<String> arrayList = new ArrayList<>();
        if (this.b == null) {
            this.b = new CspErrorInfo();
        }
        if (str.isEmpty()) {
            Tracer.e(c, "appid is empty");
            this.b.setExtendedInfo(Constants.EXTENDED_INFO_KEY_SD_ERR_MANDATORY_PARAMS_MISSING, Constants.EXTENDED_INFO_KEY_SD_ERR_MANDATORY_PARAMS_MISSING);
            return null;
        } else if (str3.isEmpty()) {
            Tracer.e(c, "serviceName is empty");
            this.b.setExtendedInfo(Constants.EXTENDED_INFO_KEY_SD_ERR_MANDATORY_PARAMS_MISSING, Constants.EXTENDED_INFO_KEY_SD_ERR_MANDATORY_PARAMS_MISSING);
            return null;
        } else {
            String str4 = c;
            Tracer.i(str4, String.format("doing service discovery for appid %s serviceName %s", str, str3));
            CspServer cSPServerInfo = getCSPServerInfo(str, str2, str3);
            if (cSPServerInfo != null) {
                String primaryURL = cSPServerInfo.getPrimaryURL();
                if (primaryURL == null || primaryURL.isEmpty()) {
                    Tracer.i(str4, "primaryUrl is empty");
                } else {
                    arrayList.add(primaryURL);
                }
                String secondaryURL = cSPServerInfo.getSecondaryURL();
                if (secondaryURL == null || secondaryURL.isEmpty()) {
                    Tracer.i(str4, "secondaryURL is empty");
                } else {
                    arrayList.add(secondaryURL);
                }
                if (arrayList.size() == 0) {
                    this.b.setExtendedInfo(Constants.EXTENDED_INFO_KEY_SD_ERR_SERVICE_NOT_PRESENT, Constants.EXTENDED_INFO_KEY_SD_ERR_SERVICE_NOT_PRESENT);
                }
            }
            if (arrayList.size() > 0) {
                return arrayList;
            }
            return null;
        }
    }

    protected CspServiceDiscoveryStore getServiceDiscoveryStore() {
        return new CspServiceDiscoveryStore(this.f8038a);
    }
}