/// <summary>
        /// Verify Entry.Core.4717
        /// </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;
            List <string> xmlNodeTypes = new List <string>();

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(context.ResponsePayload);
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);

            nsmgr.AddNamespace("m", Constants.NSMetadata);
            XmlNodeList xmlNodeList = xmlDoc.SelectNodes(@"//m:properties/*", nsmgr);

            foreach (XmlNode xmlNode in xmlNodeList)
            {
                if (xmlNode.NamespaceURI.Equals(Constants.V4NSData) && xmlNode.Attributes["type", Constants.NSMetadata] != null)
                {
                    string xmlNodeName = xmlNode.LocalName;

                    if (!AtomSchemaHelper.IsBuiltInPrimitiveTypes(xmlNodeName, context, out xmlNodeTypes))
                    {
                        string metadataTypeValue = xmlNode.Attributes["type", Constants.NSMetadata].Value;

                        if (metadataTypeValue.StartsWith("#"))
                        {
                            passed = true;
                        }
                        else
                        {
                            passed = false;
                            info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                            break;
                        }
                    }
                }
            }

            return(passed);
        }
        /// <summary>
        /// Verify Entry.Core.4620
        /// </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;
            List <string> xmlNodeTypes = new List <string>();

            // 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 and Alias value.
            string        xpath = @"//*[local-name()='DataServices']/*[local-name()='Schema']";
            List <string> appropriateNamespace = MetadataHelper.GetPropertyValues(context, xpath, "Namespace");
            List <string> appropriateAlias     = MetadataHelper.GetPropertyValues(context, xpath, "Alias");

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(context.ResponsePayload);
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);

            nsmgr.AddNamespace("m", Constants.V3NSMetadata);
            string MeataNS = Constants.V3NSMetadata;
            string DataNs  = Constants.V3NSData;

            if (context.Version == ODataVersion.V4)
            {
                nsmgr.AddNamespace("m", Constants.NSMetadata);
                MeataNS = Constants.NSMetadata;
                DataNs  = Constants.V4NSData;
            }
            XmlNodeList xmlNodeList = xmlDoc.SelectNodes(@"//m:properties/*", nsmgr);

            foreach (XmlNode xmlNode in xmlNodeList)
            {
                if (xmlNode.NamespaceURI.Equals(DataNs) && xmlNode.Attributes["type", MeataNS] != null)
                {
                    string xmlNodeName = xmlNode.LocalName;

                    if (!AtomSchemaHelper.IsBuiltInPrimitiveTypes(xmlNodeName, context, out xmlNodeTypes))
                    {
                        string metadataTypeValue = xmlNode.Attributes["type", MeataNS].Value;

                        if (appropriateNamespace.Count != 0)
                        {
                            // Verify the annoatation start with namespace.
                            foreach (string currentvalue in appropriateNamespace)
                            {
                                if (metadataTypeValue.Contains(currentvalue))
                                {
                                    isNamespaceValue = true;
                                    break;
                                }
                                else
                                {
                                    isNamespaceValue = false;
                                }
                            }
                        }

                        if (appropriateAlias.Count != 0)
                        {
                            // Verify the annoatation start with alias.
                            foreach (string currentvalue in appropriateAlias)
                            {
                                if (metadataTypeValue.Contains(currentvalue))
                                {
                                    isAliasValue = true;
                                    break;
                                }
                                else
                                {
                                    isAliasValue = false;
                                }
                            }
                        }

                        if (isNamespaceValue || isAliasValue)
                        {
                            passed = true;
                            continue;
                        }
                        else
                        {
                            passed = false;
                            info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                            break;
                        }
                    }
                }
            }

            return(passed);
        }
Exemple #3
0
        /// <summary>
        /// Verify Entry.Core.4617
        /// </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;

            string        qualifiedName = "Edm.";
            List <string> xmlNodeTypes  = new List <string>();

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(context.ResponsePayload);
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);

            nsmgr.AddNamespace("m", Constants.V3NSMetadata);
            string MeataNS = Constants.V3NSMetadata;
            string DataNs  = Constants.V3NSData;

            if (context.Version == ODataVersion.V4)
            {
                nsmgr.AddNamespace("m", Constants.NSMetadata);
                MeataNS = Constants.NSMetadata;
                DataNs  = Constants.V4NSData;
            }

            XmlNodeList xmlNodeList = xmlDoc.SelectNodes(@"//m:properties/*", nsmgr);

            foreach (XmlNode xmlNode in xmlNodeList)
            {
                if (xmlNode.NamespaceURI.Equals(DataNs) && xmlNode.Attributes["type", MeataNS] != null)
                {
                    string xmlNodeName = xmlNode.LocalName;

                    if (AtomSchemaHelper.IsBuiltInPrimitiveTypes(xmlNodeName, context, out xmlNodeTypes))
                    {
                        foreach (string xmlNodeType in xmlNodeTypes)
                        {
                            string unqualifiedName   = xmlNodeType.Remove(0, qualifiedName.Length);
                            string metadataTypeValue = xmlNode.Attributes["type", MeataNS].Value;

                            if (metadataTypeValue.Equals(unqualifiedName))
                            {
                                passed = true;
                            }
                            else
                            {
                                passed = false;
                                info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                                break;
                            }
                        }

                        if (passed == false)
                        {
                            break;
                        }
                    }
                }
            }

            return(passed);
        }