Пример #1
0
        /// <summary>
        /// Verifies the extension rule.
        /// </summary>
        /// <param name="context">The Interop service context</param>
        /// <param name="info">out parameter to return violation information when rule does not pass</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 entry;

            context.ResponsePayload.TryToJObject(out entry);

            if (entry != null && entry.Type == JTokenType.Object && context.OdataMetadataType == ODataMetadataType.MinOnly)
            {
                JsonParserHelper.ClearProperyValsList();
                List <JToken> odataMetadataVals = JsonParserHelper.GetSpecifiedPropertyValsFromEntryPayload(entry, Constants.OdataV4JsonIdentity, MatchType.Equal);

                JsonParserHelper.ClearProperyValsList();
                List <JToken> associationLinkVals = JsonParserHelper.GetSpecifiedPropertyValsFromEntryPayload(entry, Constants.OdataAssociationLinkPropertyNameSuffix, MatchType.Contained);

                string url = string.Empty;

                if (odataMetadataVals.Count == 1)
                {
                    string odataMetadataValStr = odataMetadataVals[0].ToString().Trim('"');
                    int    index = odataMetadataValStr.IndexOf(Constants.Metadata);
                    url = odataMetadataValStr.Remove(index, odataMetadataValStr.Length - index);

                    foreach (var associationLinkVal in associationLinkVals)
                    {
                        if (Uri.IsWellFormedUriString(associationLinkVal.ToString().Trim('"'), UriKind.Relative))
                        {
                            url += associationLinkVal.ToString().Trim('"') + @"/$ref";
                        }
                        else
                        {
                            url = associationLinkVal.ToString().Trim('"');
                        }

                        Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);

                        // Send a request with "application/json;odata=minimalmetadata" in Accept header.
                        string   acceptHeader = Constants.V4AcceptHeaderJsonMinimalMetadata;
                        Response response     = WebHelper.Get(uri, acceptHeader, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, context.RequestHeaders);

                        // If response's status code is not equal to 200, it will indicate the url cannot be computed.
                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            passed = true;
                        }
                        else
                        {
                            passed = false;
                            info   = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                            break;
                        }
                    }
                }
            }

            return(passed);
        }