Beispiel #1
0
        /// <summary>
        /// Verify Common.Core.4624
        /// </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;

            XmlDocument xmlDoc = new XmlDocument();

            // Single primitive individual property is not allowed to have instance annotation.
            if (VerificationHelper.IsIndividualPropertySinglePrimitiveType(context.ResponsePayload, context.PayloadType))
            {
                return(null);
            }

            // Test Data
            // Url:http://services.odata.org/V4/OData/OData.svc/Products(0)/Description?$format=xml
            // xmlDoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><m:value m:context=\"http://services.odata.org/V4/OData/OData.svc/$metadata#Products(0)/Description\" xmlns:atom=\"http://www.w3.org/2005/Atom\"  xmlns:d=\"http://docs.oasis-open.org/odata/ns/data\" xmlns:georss=\"http://www.georss.org/georss\" xmlns:gml=\"http://www.opengis.net/gml\" xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\">Whole grain bread <m:annotation atom:term=\"ODataDemo.Product.Display\" m:target=\"Description\" m:type=\"Boolean\">true</m:annotation></m:value>");
            xmlDoc.LoadXml(context.ResponsePayload);

            XmlNodeList annotationElements = xmlDoc.SelectNodes(@"//*[local-name()='annotation']", ODataNamespaceManager.Instance);

            foreach (XmlNode annotatEle in annotationElements)
            {
                if (annotatEle.Attributes["type", Constants.NSMetadata] != null)
                {
                    string typeName = annotatEle.Attributes["type", Constants.NSMetadata].Value;

                    if (!typeName.Contains("."))
                    {
                        typeName = "Edm." + typeName;
                    }

                    if (!EdmTypeManager.IsEdmSimpleType(typeName))
                    {
                        continue;
                    }

                    if (typeName != "Edm.String")
                    {
                        passed = true;
                    }
                    else
                    {
                        passed = false;
                        info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                        break;
                    }
                }
            }

            return(passed);
        }
        /// <summary>
        /// Verify Common.Core.4626
        /// </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;

            // Single primitive individual property is not allowed to have instance annotation.
            if (VerificationHelper.IsIndividualPropertySinglePrimitiveType(context.ResponsePayload, context.PayloadType))
            {
                return(null);
            }

            XmlDocument xmlDoc = new XmlDocument();

            // Test Data
            // Url:http://services.odata.org/V4/OData/OData.svc/Products(0)/Description?$format=xml or http://services.odata.org/V4/OData/OData.svc/Persons(0)?$format=atom
            // xmlDoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><m:value m:context=\"http://services.odata.org/V4/OData/OData.svc/$metadata#Products(0)/Description\" xmlns:atom=\"http://www.w3.org/2005/Atom\"  xmlns:d=\"http://docs.oasis-open.org/odata/ns/data\" xmlns:georss=\"http://www.georss.org/georss\" xmlns:gml=\"http://www.opengis.net/gml\" xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\">Whole grain bread <m:annotation term=\"com.contoso.PersonalInfo.PhoneNumbers\" m:type=\"Collection(String)\"><m:element>(203)555-1718</m:element><m:element>(203)555-1719</m:element></m:annotation></m:value>");
            xmlDoc.LoadXml(context.ResponsePayload);

            XmlNodeList annotationElements = xmlDoc.SelectNodes(@"//*[local-name()='annotation']", ODataNamespaceManager.Instance);

            foreach (XmlNode annotatEle in annotationElements)
            {
                bool isCollectionValued = false;
                foreach (XmlNode ele in annotatEle.ChildNodes)
                {
                    if (ele.Name.Equals("element"))
                    {
                        isCollectionValued = true;
                    }
                    else
                    {
                        isCollectionValued = false;
                        break;
                    }
                }

                if (isCollectionValued)
                {
                    if (annotatEle.Attributes["type", Constants.NSMetadata] != null)
                    {
                        string typeName = annotatEle.Attributes["type", Constants.NSMetadata].Value;
                        if (typeName.Contains("Collection(") && typeName.Remove(0, typeName.Length - 1).Equals(")"))
                        {
                            passed = true;
                        }
                        else
                        {
                            passed = false;
                            info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                            break;
                        }
                    }
                    else
                    {
                        passed = false;
                        info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                        break;
                    }
                }
            }

            return(passed);
        }
Beispiel #3
0
        /// <summary>
        /// Verify Common.Core.4633
        /// </summary>
        /// <param name="context">Service context</param>
        /// <param name="info">out paramater 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;

            // Single primitive individual property is not allowed to have instance annotation.
            if (VerificationHelper.IsIndividualPropertySinglePrimitiveType(context.ResponsePayload, context.PayloadType))
            {
                info = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                return(null);
            }

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(context.ResponsePayload);
            XmlElement root = xmlDoc.DocumentElement;
            string     type = string.Empty;

            if (root.LocalName.Equals("value") && root.NamespaceURI.Equals(Constants.NSMetadata))
            {
                if (root.Attributes["type", Constants.NSMetadata] != null)
                {
                    type = root.Attributes["type", Constants.NSMetadata].Value;

                    if (type.Contains("Collection("))
                    {
                        type = type.Substring(type.IndexOf('(') + 1, type.Length - 12);
                    }

                    type = type.Substring(type.LastIndexOf('.') + 1);

                    List <string> complexTypeNames = MetadataHelper.GetAllComplexNameFromMetadata(context.MetadataDocument);

                    if (complexTypeNames.Contains(type))
                    {
                        string      annotation = @"//*[local-name()='value']/*[local-name()='annotation']";
                        XmlNodeList annoations = xmlDoc.SelectNodes(annotation, ODataNamespaceManager.Instance);
                        if (annoations != null && annoations.Count > 0)
                        {
                            foreach (XmlNode anno in annoations)
                            {
                                if (anno.Attributes["target"] == null || anno.Attributes["target"].Value.Contains(type))
                                {
                                    passed = true;
                                }
                                else
                                {
                                    passed = false;
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            info = new ExtensionRuleViolationInfo(passed == true ? null : this.ErrorMessage, context.Destination, context.ResponsePayload);

            return(passed);
        }
        /// <summary>
        /// Verify Common.Core.4627
        /// </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;

            // Single primitive individual property is not allowed to have instance annotation.
            if (VerificationHelper.IsIndividualPropertySinglePrimitiveType(context.ResponsePayload, context.PayloadType))
            {
                return(null);
            }

            XmlDocument xmlDoc = new XmlDocument();

            // Test Data
            // Url:http://services.odata.org/V4/OData/OData.svc/Products(0)/Description?$format=xml or http://services.odata.org/V4/OData/OData.svc/Persons(0)?$format=atom
            // xmlDoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><m:value m:context=\"http://services.odata.org/V4/OData/OData.svc/$metadata#Products(0)/Description\" xmlns:atom=\"http://www.w3.org/2005/Atom\"  xmlns:d=\"http://docs.oasis-open.org/odata/ns/data\" xmlns:georss=\"http://www.georss.org/georss\" xmlns:gml=\"http://www.opengis.net/gml\" xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\">Whole grain bread <m:annotation atom:term=\"com.contoso.address\" m:type=\"#ODataDemo.Address\"><d:Street>2817 Milton Dr.</d:Street><d:City>Albuquerque</d:City><d:State>NM</d:State><d:ZipCode>87110</d:ZipCode><d:Country>USA</d:Country></m:annotation></m:value>");
            xmlDoc.LoadXml(context.ResponsePayload);

            XmlNodeList annotationElements = xmlDoc.SelectNodes(@"//*[local-name()='annotation']", ODataNamespaceManager.Instance);

            foreach (XmlNode annotatEle in annotationElements)
            {
                bool isStructuredValue = false;

                if (annotatEle.ChildNodes != null && annotatEle.ChildNodes.Count >= 1)
                {
                    isStructuredValue = true;

                    foreach (XmlNode ele in annotatEle.ChildNodes)
                    {
                        if (!ele.NamespaceURI.Equals(Constants.V4NSData))
                        {
                            isStructuredValue = false;
                            break;
                        }
                    }
                }

                if (!isStructuredValue)
                {
                    continue;
                }

                if (annotatEle.Attributes["type", Constants.NSMetadata] != null)
                {
                    string typeName = annotatEle.Attributes["type", Constants.NSMetadata].Value.GetLastSegment();

                    List <string> complexTypeNames = MetadataHelper.GetAllComplexNameFromMetadata(context.MetadataDocument);
                    List <string> entityTypeNames  = MetadataHelper.GetEntityTypeNamesOfAllEntityset(context.MetadataDocument);

                    if (complexTypeNames.Contains(typeName))
                    {
                        if (this.IsChildProperty("ComplexType", annotatEle.ChildNodes, typeName, context.MetadataDocument))
                        {
                            passed = true;
                        }
                        else
                        {
                            info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                            passed = false;
                            break;
                        }
                    }
                    else if (entityTypeNames.Contains(typeName))
                    {
                        if (this.IsChildProperty("EntityType", annotatEle.ChildNodes, typeName, context.MetadataDocument))
                        {
                            passed = true;
                        }
                        else
                        {
                            info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                            passed = false;
                            break;
                        }
                    }
                }
            }

            return(passed);
        }