Пример #1
0
        /// <summary>
        /// Verify the code rule
        /// </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;

            info = null;

            var segments = ResourcePathHelper.GetPathSegments(context).ToArray();

            if (segments.Length > 0)
            {
                string lastSeg = segments[segments.Length - 1];
                if (lastSeg.Equals("$value", StringComparison.Ordinal))
                {
                    var     edmxHelper = new EdmxHelper(XElement.Parse(context.MetadataDocument));
                    UriType uriType;
                    var     target = edmxHelper.GetTargetType(segments, out uriType);
                    if (uriType == UriType.URI4 || uriType == UriType.URI5)
                    {
                        string targetType = ((EdmProperty)target).TypeUsage.EdmType.FullName;
                        if (!string.IsNullOrEmpty(targetType))
                        {
                            // do the validation here
                            IEdmType type = EdmTypeManager.GetEdmType(targetType);
                            if (type != null)
                            {
                                passed = type.IsGoodWith(context.ResponsePayload);
                                if (passed.HasValue && !passed.Value)
                                {
                                    info = new ExtensionRuleViolationInfo("pattern not matched", context.Destination, context.ResponsePayload, 1);
                                }
                            }
                            else
                            {
                                // type unknown
                                info   = new ExtensionRuleViolationInfo("unrecognized Edm type", context.Destination, targetType, 1);
                                passed = false;
                            }
                        }
                    }
                }
            }

            return(passed);
        }
Пример #2
0
        /// <summary>
        /// Checks whether the value is well-formatted based on the type
        /// </summary>
        /// <param name="value">The value</param>
        /// <param name="typeName">Name of the type</param>
        /// <returns>bool indicating value being well-formatted</returns>
        protected bool VerifyValueByPrimitiveType(string value, string typeName)
        {
            bool result = true;

            if (!string.IsNullOrEmpty(value))
            {
                IEdmType type = EdmTypeManager.GetEdmType(typeName);
                if (type == null)
                {
                    //unrecognized primitive type
                    result = false;
                }
                else
                {
                    result = type.IsGoodWith(value);
                }
            }

            return(result);
        }
        /// <summary>
        /// Verify Common.Core.4623
        /// </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
            // 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["null", Constants.NSMetadata] != null &&
                    annotatEle.Attributes["null", Constants.NSMetadata].Value.Equals("true"))
                {
                    return(null);
                }

                string content = annotatEle.InnerText;

                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))
                    {
                        IEdmType edmType = EdmTypeManager.GetEdmType(typeName);
                        if (edmType.IsGoodWith(content))
                        {
                            passed = true;
                        }
                        else
                        {
                            passed = false;
                            info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                            break;
                        }
                    }
                }
            }

            return(passed);
        }
Пример #4
0
        /// <summary>
        /// Verify Metadata.Core.4228
        /// </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;

            // Load MetadataDocument into XMLDOM
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(context.MetadataDocument);

            string      xpath            = "//*[local-name()='EnumType']";
            XmlNodeList enumTypeNodeList = xmlDoc.SelectNodes(xpath);

            foreach (XmlNode enumType in enumTypeNodeList)
            {
                string underlyingType = string.Empty;
                if (enumType.Attributes["UnderlyingType"] != null)
                {
                    underlyingType = enumType.Attributes["UnderlyingType"].Value;
                }
                else
                {
                    underlyingType = "Edm.Int32";
                }

                foreach (XmlNode member in enumType.ChildNodes)
                {
                    if (!member.Name.Equals("Member"))
                    {
                        continue;
                    }
                    else
                    {
                        if (member.Attributes["Value"] != null)
                        {
                            IEdmType edmType = EdmTypeManager.GetEdmType(underlyingType);

                            if (edmType.IsGoodWith(member.Attributes["Value"].Value))
                            {
                                passed = true;
                            }
                            else
                            {
                                passed = false;
                                info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                                break;
                            }
                        }
                    }
                }

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

            return(passed);
        }