/// <summary>
        /// Verifies the semantic rule
        /// </summary>
        /// <param name="context">The Interop service context</param>
        /// <param name="info">out paramater 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 = true;

            info = null;

            // get the first segment after the service root URI
            string path;

            if (ResourcePathHelper.IsOneSegmentPath(context, out path))
            {
                if (RegexInUri.IsURI2(path, XElement.Parse(context.MetadataDocument)))
                {
                    if (context.PayloadType != RuleEngine.PayloadType.Entry)
                    {
                        passed = false;
                        info   = new ExtensionRuleViolationInfo("error payload type expected", context.Destination, context.PayloadType.ToString());
                    }
                }
            }

            return(passed);
        }
        /// <summary>
        /// Verifies the semantic rule
        /// </summary>
        /// <param name="context">The Interop service context</param>
        /// <param name="info">out paramater 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 = true;

            info = null;

            // check there is only one segement in the relative path (after the service root uri)
            // ensure the segment is a URI-2
            string path;

            if (ResourcePathHelper.IsOneSegmentPath(context, out path))
            {
                if (RegexInUri.IsURI2(path, XElement.Parse(context.MetadataDocument)))
                {
                    // fetch the .../$count resource
                    Uri target = new Uri(context.DestinationBasePath + "/$count");
                    var resp   = WebResponseHelper.GetWithHeaders(
                        target,
                        false,  // $count response cannot be application/json Content-Type
                        null,
                        RuleEngineSetting.Instance().DefaultMaximumPayloadSize,
                        context);

                    if (resp.StatusCode.HasValue)
                    {
                        var  code = resp.StatusCode.Value;
                        bool isNonExistentCode = (code == System.Net.HttpStatusCode.NotFound) || (code == System.Net.HttpStatusCode.Gone);
                        if (isNonExistentCode)
                        {
                            passed = true;
                        }
                        else
                        {
                            passed = false;
                            info   = new ExtensionRuleViolationInfo("unexpected status code", target, code.ToString());
                        }
                    }
                    else
                    {
                        passed = false;
                        info   = new ExtensionRuleViolationInfo("no status code", target, null);
                    }
                }
            }

            return(passed);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Verify the rule
        /// </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;

            // check there is only one segement in the relative path (after the service root uri)
            // ensure the segment is a URI-2
            string path;

            if (ResourcePathHelper.IsOneSegmentPath(context, out path))
            {
                if (RegexInUri.IsURI2(path, XElement.Parse(context.MetadataDocument)))
                {
                    // fetch the .../$count resource
                    Uri target = new Uri(context.DestinationBasePath + "/$count");
                    var resp   = WebResponseHelper.GetWithHeaders(
                        target,
                        false,  // $count response cannot be application/json Content-Type
                        null,
                        RuleEngineSetting.Instance().DefaultMaximumPayloadSize,
                        context);
                    string payload = resp.ResponsePayload;
                    int    count;
                    if (Int32.TryParse(payload, out count))
                    {
                        passed = count == 1;
                    }
                    else
                    {
                        passed = false;
                    }

                    if (!passed.Value)
                    {
                        info = new ExtensionRuleViolationInfo("unexpected payload content", target, payload);
                    }
                }
            }

            return(passed);
        }
        /// <summary>
        /// Verifies the semantic rule
        /// </summary>
        /// <param name="context">The Interop service context</param>
        /// <param name="info">out paramater 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 = true;

            info = null;

            // get the first segment after the service root URI
            char[] delimiters   = new char[] { '/' };
            string path         = context.DestinationBasePath.Substring(context.ServiceBaseUri.AbsoluteUri.Length).Trim('/');
            string firstSegment = path.IndexOfAny(delimiters) >= 0 ? path.Substring(0, path.IndexOfAny(delimiters)) : path;

            firstSegment = firstSegment.Trim('/');

            // check whether the first segment is URI-2
            if (RegexInUri.IsURI2(firstSegment, XElement.Parse(context.MetadataDocument)))
            {
                if (!firstSegment.Equals(path.Trim('/')))
                {
                    Uri resource = new Uri(context.ServiceBaseUri, firstSegment);
                    var resp     = WebResponseHelper.GetWithHeaders(resource,
                                                                    context.PayloadFormat == RuleEngine.PayloadFormat.Json,
                                                                    null,
                                                                    RuleEngineSetting.Instance().DefaultMaximumPayloadSize,
                                                                    context);
                    if (resp.StatusCode.HasValue)
                    {
                        int statusCode = (int)resp.StatusCode.Value;
                        if (statusCode < 100 || statusCode >= 400)
                        {
                            if (!(context.HttpStatusCode.Value == System.Net.HttpStatusCode.NotFound || context.HttpStatusCode.Value == System.Net.HttpStatusCode.Gone))
                            {
                                passed = false;
                                info   = new ExtensionRuleViolationInfo("unexpected status code " + context.HttpStatusCode.Value.ToString(), context.Destination, context.HttpStatusCode.Value.ToString());
                            }
                            else if (context.PayloadType != RuleEngine.PayloadType.Error)
                            {
                                passed = false;
                                info   = new ExtensionRuleViolationInfo("error payload expected", context.Destination, context.PayloadType.ToString());
                            }
                        }
                    }
                }
                else
                {
                    if (context.PayloadType == RuleEngine.PayloadType.Error)
                    {
                        int statusCode = (int)context.HttpStatusCode.Value;
                        passed = statusCode >= 400 && statusCode < 500;
                        if (!passed)
                        {
                            info = new ExtensionRuleViolationInfo("unexpected HTTP status code", context.Destination, context.HttpStatusCode.Value.ToString());
                        }
                    }
                }
            }

            return(passed);
        }