Ejemplo n.º 1
0
 public Create(AppManifest manifest, AppInstance instance)
 {
     this.manifest = manifest;
     this.instance = instance;
     this.okMsg = new AppManifestOk();
     this.okMsg.InstanceId = manifest.InstanceId != null ? manifest.InstanceId : instance.InstanceId;
     this.Log = Logger.GetInstance();
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns a new AppManifest from the given json.
        /// If a property is not given in the json then the instance
        /// variable to which it corresponds is set to null.
        /// Capabilities will always be non-null, but may be empty
        /// Dependencies will always be non-null, but also may be empty
        /// If API was not supplied it is set to -1, since ints cannot be nulled
        /// </summary>
        /// <param name="json">the manifest json to parse</param>
        /// <returns>a new AppManifest</returns>
        public static new DataPacket Deserialize(string json)
        {
            try
            {
                var ret = new AppManifest();
                var obj = Json.Decode(json);
                ret.Version = TryGetValueFromDynamic(obj, "version");
                dynamic API = TryGetValueFromDynamic(obj, "API");
                if (API != null)
                    ret.API = TryGetValueFromDynamic(obj, "API");
                else
                    ret.API = -1;
                ret.Description = TryGetValueFromDynamic(obj, "description");
                ret.Name = TryGetValueFromDynamic(obj, "name");
                ret.DisplayName = TryGetValueFromDynamic(obj, "displayName");
                ret.InstanceId = TryGetValueFromDynamic(obj, "instanceId");

                dynamic capabilities = TryGetValueFromDynamic(obj, "capabilities");
                // process the capabilities into a dictionary
                ret.Capabilities = new Dictionary<string, string>();
                if (capabilities != null)
                {
                    var jobj = capabilities as DynamicJsonObject;
                    foreach (string capability in jobj.GetDynamicMemberNames())
                    {
                        ret.Capabilities.Add(capability, capabilities[capability]);
                    }
                }

                dynamic dependencies = TryGetValueFromDynamic(obj, "dependencies");
                // process dependencies into a dictionary
                ret.Dependencies = new Dictionary<string, string>();
                if (dependencies != null)
                {
                    var jobj = dependencies as DynamicJsonObject;
                    foreach (string dependency in jobj.GetDynamicMemberNames())
                    {
                        ret.Dependencies.Add(dependency, dependencies[dependency]);
                    }
                }

                Validate(ret);

                return ret;
            }
            catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)
            {
                throw new ParseException(json, "Unknown parsing exception");
            }
            catch (ParseException ex)
            {
                throw new ParseException(json, ex.Message);
            }
            catch (System.ArgumentException)
            {
                throw new ParseException(json, "Invalid JSON");
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Validate a given manifest. If the manifest is invalid a ParseException is thrown.
        /// The Received property of that exception is set to an empty string, and should be set
        /// by the caller that knows about the json that was parsed.
        /// </summary>
        /// <param name="manifest">manifest to check</param>
        private static void Validate(AppManifest manifest)
        {
            var problems = new List<string>();

            var versionRegex = new Regex(@"(^\d+\.\d+\.\d+|[*])$");
            if (manifest.Version == null || !versionRegex.IsMatch(manifest.Version))
            {
                problems.Add("Supplied version number is invalid");
            }

            if (manifest.API < 0)
            {
                problems.Add("API number is invalid");
            }

            if (manifest.DisplayName == null || manifest.DisplayName == "")
            {
                problems.Add("No displayName was found");
            }

            if (manifest.Description == null || manifest.Description == "")
            {
                problems.Add("No description was found");
            }

            if (manifest.Name == null || manifest.Name == "")
            {
                problems.Add("No name was found");
            }

            foreach (var item in manifest.Capabilities)
            {
                if (!versionRegex.IsMatch(item.Value))
                {
                    problems.Add("Capability version number for " + item.Key + " is not semantic");
                }
            }

            foreach (var item in manifest.Dependencies)
            {
                if (!versionRegex.IsMatch(item.Value))
                {
                    problems.Add("Dependency version number for " + item.Key + "is not semantic");
                }
            }

            // if we have problems report them
            if (problems.Count != 0)
            {
                var msg = string.Join("\n", problems);
                throw new ParseException("", msg);
            }
        }