示例#1
0
        /// <summary>
        /// Get whether the bound function properties have title and target objects.
        /// </summary>
        /// <param name="payloadType">Feed or Entry as the payload type.</param>
        /// <param name="boundfunctionNames">The bound function names with the current entity.</param>
        /// <param name="jo">The json object of response.</param>
        /// <param name="metadata">The Schema XML element of the metadata.</param>
        /// <returns>true: rule pass; false: rule fail; null: not applicable.</returns>
        public bool?HasBoundFunctionTitleTarget(RuleEngine.PayloadType payloadType, List <string> boundfunctionNames, JObject jo, XElement metadataSchema)
        {
            if (payloadType == RuleEngine.PayloadType.Feed)
            {
                bool?result = HasBoundFunctionTitleTarget(RuleEngine.PayloadType.Entry, boundfunctionNames, jo, metadataSchema);

                if (result.HasValue && result.Value == false)
                {
                    return(result);
                }

                foreach (JObject ob in (JArray)jo[Constants.Value])
                {
                    result = HasBoundFunctionTitleTarget(RuleEngine.PayloadType.Entry, boundfunctionNames, ob, metadataSchema);

                    if (result.HasValue && result.Value == false)
                    {
                        break;
                    }
                }
                return(result);
            }
            else if (payloadType == RuleEngine.PayloadType.Entry)
            {
                bool?result = null;

                var jProps = jo.Children();

                foreach (JProperty jProp in jProps)
                {
                    bool isFunctionProperty = JsonParserHelper.isFunctionOrActionProperty(boundfunctionNames, jProp, metadataSchema);

                    // Whether the property is bound function property.
                    if (isFunctionProperty)
                    {
                        if (jProp.Value["title"] != null && jProp.Value["target"] != null)
                        {
                            result = true;
                        }
                        else
                        {
                            result = false;
                            break;
                        }
                    }
                }
                return(result);
            }
            return(null);
        }
        /// <summary>
        /// Verify Common.Core.4719
        /// </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");
            }

            info = null;
            bool?  passed           = null;
            bool   isActionProperty = false;
            string actionName       = string.Empty;

            XElement metadata = XElement.Parse(context.MetadataDocument);

            JObject allobject;

            context.ResponsePayload.TryToJObject(out allobject);
            var jProps = allobject.Children();

            // Use the XPath query language to access the metadata document to get all bound Action properties.
            // The bound action has IsBound as true and the first parameter with Type attribute equal to the entity type full name.
            string        xpath            = string.Format(@"//*[local-name()='Action' and @IsBound='true']/*[local-name()='Parameter' and position()=1 and @Type='{0}']/parent::*", context.EntityTypeFullName);
            List <string> boundActionNames = MetadataHelper.GetPropertyValues(context, xpath, "Name");

            xpath = @"//*[local-name() = 'Schema'][1]";
            XElement metadataSchema = metadata.XPathSelectElement(xpath, ODataNamespaceManager.Instance);

            if (boundActionNames.Count > 0)
            {
                foreach (JProperty jProp in jProps)
                {
                    isActionProperty = JsonParserHelper.isFunctionOrActionProperty(boundActionNames, jProp, metadataSchema);

                    // Whether the property is bound action property exist.
                    if (isActionProperty)
                    {
                        if (jProp.Value["title"] != null && jProp.Value["target"] != null)
                        {
                            passed = true;
                        }
                        else
                        {
                            passed = false;
                            info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                            break;
                        }
                    }
                }

                if (passed.HasValue && passed.Value == false)
                {
                    return(false);
                }

                if (context.PayloadType == RuleEngine.PayloadType.Feed && allobject[Constants.Value] != null)
                {
                    foreach (JObject ob in (JArray)allobject[Constants.Value])
                    {
                        jProps = ob.Children();

                        foreach (JProperty jProp in jProps)
                        {
                            isActionProperty = JsonParserHelper.isFunctionOrActionProperty(boundActionNames, jProp, metadataSchema);

                            // Whether the property is bound action property exist.
                            if (isActionProperty)
                            {
                                if (jProp.Value["title"] != null && jProp.Value["target"] != null)
                                {
                                    passed = true;
                                }
                                else
                                {
                                    passed = false;
                                    info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                                    break;
                                }
                            }
                        }

                        if (passed.HasValue && passed.Value == false)
                        {
                            break;
                        }
                    }
                }
            }
            return(passed);
        }