/// <summary>
        /// Verify Common.Core.4403
        /// </summary>
        /// <param name="context">Service context</param>
        /// <param name="info">out parameter to return violation information when rule fail</param>
        /// <returns>true if rule passes; false otherwise</returns>
        public override bool?Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            bool?passed = null;

            info = null;
            JObject allobject;

            context.ResponsePayload.TryToJObject(out allobject);

            // If PayloadType is Feed, verify as below, else is Entry.
            if (context.PayloadType.Equals(RuleEngine.PayloadType.Feed))
            {
                var entries = JsonParserHelper.GetEntries(allobject);
                foreach (JObject entry in entries)
                {
                    var jProps = entry.Children();
                    foreach (JProperty jProp in jProps)
                    {
                        // Whether the property name contains odata.type.
                        if (jProp.Name.EndsWith(Constants.V4OdataType) && !jProp.Name.Equals(Constants.V4OdataType))
                        {
                            // Whether the property is built-in primitive types.
                            bool isBuiltInPriType = JsonSchemaHelper.IsBuiltInPrimitiveTypes(jProp, context);

                            if (!isBuiltInPriType)
                            {
                                string typeValue = jProp.Value.ToString().StripOffDoubleQuotes().TrimStart('#');

                                // Whether the value of  odata.type is absolute or relative URl.
                                if (Uri.IsWellFormedUriString(typeValue, UriKind.RelativeOrAbsolute))
                                {
                                    passed = true;
                                    continue;
                                }
                                else
                                {
                                    passed = false;
                                    info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                var jProps = allobject.Children();
                foreach (JProperty jProp in jProps)
                {
                    // Whether the property name contains odata.type.
                    if (jProp.Name.EndsWith(Constants.V4OdataType) && !jProp.Name.Equals(Constants.V4OdataType))
                    {
                        // Whether the property is built-in primitive types.
                        bool isBuiltInPriType = JsonSchemaHelper.IsBuiltInPrimitiveTypes(jProp, context);

                        if (!isBuiltInPriType)
                        {
                            string typeValue = jProp.Value.ToString().StripOffDoubleQuotes().TrimStart('#');

                            // Whether the value of  odata.type is absolute or relative URl.
                            if (Uri.IsWellFormedUriString(typeValue, UriKind.RelativeOrAbsolute))
                            {
                                passed = true;
                                continue;
                            }
                            else
                            {
                                passed = false;
                                info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                                break;
                            }
                        }
                    }
                }
            }

            return(passed);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Verify Entry.Common.4402
        /// </summary>
        /// <param name="context">Service context</param>
        /// <param name="info">out parameter to return violation information when rule fail</param>
        /// <returns>true if rule passes; false otherwise</returns>
        public override bool?Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            bool?passed = null;

            info = null;
            JObject allobject;

            context.ResponsePayload.TryToJObject(out allobject);

            // Whether odata.type value is namespace- or alias-qualified the instance's type
            bool isNamespaceValue = false;
            bool isAliasValue     = false;

            // Use the XPath query language to access the metadata document and get all Namespace value.
            string        xpath = @"//*[local-name()='DataServices']/*[local-name()='Schema']";
            List <string> appropriateNamespace = MetadataHelper.GetPropertyValues(context, xpath, "Namespace");

            // Get Alias value.
            List <string> appropriateAlias = MetadataHelper.GetPropertyValues(context, xpath, "Alias");

            // If PayloadType is Feed, verify as below, else is Entry.
            if (context.PayloadType.Equals(RuleEngine.PayloadType.Feed))
            {
                var entries = JsonParserHelper.GetEntries(allobject);
                foreach (JObject entry in entries)
                {
                    var jProps = entry.Children();
                    foreach (JProperty jProp in jProps)
                    {
                        // Whether the property name contains odata.type.
                        if (jProp.Name.Contains(Constants.OdataType))
                        {
                            // Whether the property is built-in primitive types.
                            bool isBuiltInPrimitiveType = JsonSchemaHelper.IsBuiltInPrimitiveTypes(jProp, context);

                            if (isBuiltInPrimitiveType)
                            {
                                if (appropriateNamespace.Count != 0)
                                {
                                    // Verify the annoatation contains the namespace-qualified.
                                    foreach (string currentvalue in appropriateNamespace)
                                    {
                                        if (jProp.Name.Contains(currentvalue))
                                        {
                                            isNamespaceValue = true;
                                        }
                                        else
                                        {
                                            isNamespaceValue = false;
                                        }
                                    }
                                }

                                if (appropriateAlias.Count != 0)
                                {
                                    // Verify the annoatation contains the alias-qualified.
                                    foreach (string currentvalue in appropriateAlias)
                                    {
                                        if (jProp.Name.Contains(currentvalue))
                                        {
                                            isAliasValue = true;
                                        }
                                        else
                                        {
                                            isAliasValue = false;
                                        }
                                    }
                                }

                                // odata.type annotation is the unqualified name of the primitive type means it does not contain the namespace-qualified or alias-qualified type.
                                if (!isAliasValue && !isNamespaceValue)
                                {
                                    passed = true;
                                    continue;
                                }
                                else
                                {
                                    passed = false;
                                    info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                var jProps = allobject.Children();

                foreach (JProperty jProp in jProps)
                {
                    // Whether the property name contains odata.type.
                    if (jProp.Name.Contains(Constants.OdataType))
                    {
                        // Whether the property is built-in primitive types.
                        bool isBuiltInPrimitiveType = JsonSchemaHelper.IsBuiltInPrimitiveTypes(jProp, context);

                        if (isBuiltInPrimitiveType)
                        {
                            if (appropriateNamespace.Count != 0)
                            {
                                // Verify the annoatation contains the namespace-qualified.
                                foreach (string currentvalue in appropriateNamespace)
                                {
                                    if (jProp.Name.Contains(currentvalue))
                                    {
                                        isNamespaceValue = true;
                                    }
                                    else
                                    {
                                        isNamespaceValue = false;
                                    }
                                }
                            }

                            if (appropriateAlias.Count != 0)
                            {
                                // Verify the annoatation contains the alias-qualified.
                                foreach (string currentvalue in appropriateAlias)
                                {
                                    if (jProp.Name.Contains(currentvalue))
                                    {
                                        isAliasValue = true;
                                    }
                                    else
                                    {
                                        isAliasValue = false;
                                    }
                                }
                            }

                            // odata.type annotation is the unqualified name of the primitive type means it does not contain the namespace-qualified or alias-qualified type.
                            if (!isAliasValue && !isNamespaceValue)
                            {
                                passed = true;
                                continue;
                            }
                            else
                            {
                                passed = false;
                                info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                                break;
                            }
                        }
                    }
                }
            }

            return(passed);
        }