Example #1
0
        /// <summary>
        /// Handle where an input object could be a string or a list of strings, and output must be a list of strings
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string AssignObjectToString(object input)
        {
            if (input == null)
            {
                return(null);
            }
            if (input.GetType() == typeof(Newtonsoft.Json.Linq.JArray))
            {
                var list = input as Newtonsoft.Json.Linq.JArray;
                if (list != null && list.Count() > 0)
                {
                    return(list[0].ToString());
                }
            }
            else if (input.GetType() == typeof(System.String))
            {
                return(input.ToString());
            }
            else if (input.GetType() == typeof(Newtonsoft.Json.Linq.JObject) && input.ToString().IndexOf("en-US") > -1)
            {
                Dictionary <string, object> dictionary = RegistryServices.JsonToDictionary(input.ToString());
                var output = dictionary["en-US"].ToString();
                return(output);
            }
            else
            {
                //unexpected/unhandled
            }
            //-		input.GetType()	{Name = "JObject" FullName = "Newtonsoft.Json.Linq.JObject"}	System.Type {System.RuntimeType}

            return(null);
        }
Example #2
0
        public RegistryObject(string payload)
        {
            if (!string.IsNullOrWhiteSpace(payload) && (payload.IndexOf("errors") == -1 || payload.IndexOf("errors") > 50))
            {
                dictionary = RegistryServices.JsonToDictionary(payload);
                if (payload.IndexOf("@graph") > 0 && payload.IndexOf("@graph\": null") == -1)
                {
                    IsGraphObject = true;
                    //get the graph object
                    object graph = dictionary["@graph"];
                    //serialize the graph object
                    var glist = JsonConvert.SerializeObject(graph);
                    //parse graph in to list of objects
                    JArray graphList = JArray.Parse(glist);

                    var main = graphList[0].ToString();
                    BaseObject = JsonConvert.DeserializeObject <RegistryBaseObject>(main);
                    CtdlType   = BaseObject.CdtlType;
                    Ctid       = BaseObject.Ctid;

                    var n = RegistryServices.AssignObjectToString(BaseObject.Name);
                    //not important to fully resolve yet
                    if (BaseObject.Name != null)
                    {
                        Name = BaseObject.Name.ToString();
                    }
                    else if (CtdlType == "ceasn:CompetencyFramework")
                    {
                        //var n = JsonConvert.DeserializeObject<LanguageMap>( BaseObject.CompetencyFrameworkName.ToString() );
                        Name = (BaseObject.CompetencyFrameworkName ?? "").ToString();
                    }
                    else
                    {
                        Name = "?????";
                    }
                }
                else
                {
                    //check if old resource or standalone resource
                    BaseObject = JsonConvert.DeserializeObject <RegistryBaseObject>(payload);
                    CtdlType   = BaseObject.CdtlType;
                    Ctid       = BaseObject.Ctid;
                    if (BaseObject.Name != null)
                    {
                        Name = BaseObject.Name.ToString();
                    }
                    else
                    {
                        Name = "no name property for this document";
                    }
                }
                if ((Name ?? "").IndexOf("{") == 0)
                {
                    var n = JsonConvert.DeserializeObject <LanguageMap>(Name);
                    Name = n.ToString();
                    //Name = n.;
                }
                CtdlType = CtdlType.Replace("ceterms:", "");
                CtdlType = CtdlType.Replace("ceasn:", "");
            }
        }
Example #3
0
        /// <summary>
        /// Retrieve a resource from the registry by resourceId
        /// </summary>
        /// <param name="resourceId">Url to a resource in the registry</param>
        /// <param name="statusMessage"></param>
        /// <returns></returns>
        public static string GetResourceByUrl(string resourceUrl, ref string ctdlType, ref string statusMessage)
        {
            string payload = "";

            statusMessage = "";
            ctdlType      = "";
            string ceApiKey = UtilityManager.GetAppKeyValue("ceApiKey");

            try
            {
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.
                    Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    if (!string.IsNullOrWhiteSpace(ceApiKey))
                    {
                        client.DefaultRequestHeaders.Add("Authorization", "Token " + ceApiKey);
                    }
                    var task = client.GetAsync(resourceUrl);
                    task.Wait();
                    var response1 = task.Result;
                    payload = task.Result.Content.ReadAsStringAsync().Result;

                    //just in case, likely the caller knows the context
                    if (!string.IsNullOrWhiteSpace(payload) &&
                        payload.Length > 100
                        //&& payload.IndexOf("\"errors\":") == -1
                        )
                    {
                        ctdlType = RegistryServices.GetResourceType(payload);
                    }
                    else
                    {
                        //nothing found, or error/not found
                        LoggingHelper.DoTrace(1, "RegistryServices.GetResourceByUrl. Did not find: " + resourceUrl);
                        statusMessage = "The referenced resource was not found in the credential registry: " + resourceUrl;
                        payload       = "";
                    }
                    //
                }
            }
            catch (Exception exc)
            {
                if (exc.Message.IndexOf("(404) Not Found") > 0)
                {
                    //need to surface these better
                    statusMessage = "The referenced resource was not found in the credential registry: " + resourceUrl;
                }
                else
                {
                    var msg = LoggingHelper.FormatExceptions(exc);
                    if (msg.IndexOf("remote name could not be resolved: 'sandbox.credentialengineregistry.org'") > 0)
                    {
                        //retry?
                        statusMessage = "retry";
                    }
                    LoggingHelper.LogError(exc, "RegistryServices.GetResource");
                    statusMessage = exc.Message;
                }
            }
            return(payload);
        }