Example #1
0
        private List <NetHttpChannelDispatcher> GetTemplates(List <NetHttpChannelDispatcher> templates)
        {
            InnerChannels.ForEach(delegate(NetHttpChannelManager manager)
            {
                manager.GetTemplates(templates);
            });

            WADLMethod[] methods   = GetWADLMethods();
            XDocument    wadl      = XDocument.Parse(Wadl());
            var          resources = wadl.Descendants(XName.Get("resource", CONSTANTS.Namespace)).ToArray <XElement>();
            int          index     = 0;

            Array.ForEach <WADLMethod>(methods, delegate(WADLMethod method)
            {
                WADLUtility.Url url = WADLUtility.Url.CreateUrl(resources[index].Parent.Attribute("base").Value, resources[index].Attribute("path").Value);
                NetHttpChannelDispatcher dispatcher = new NetHttpChannelDispatcher(this, method, new NetHttpUriTemplate(url, true));
                templates.Add(dispatcher);
                index++;
            });

            return(templates);
        }
Example #2
0
        private Stream ProcessRequest()
        {
            if (State.CurrentStatus == ServiceStatus.FAULT)
            {
                WebOperationContext.Current.OutgoingResponse.Headers.Add("Server-Status", State.CurrentStatus.ToString());
                WebOperationContext.Current.OutgoingResponse.Headers.Add("Server-Status-Message", System.Web.HttpUtility.UrlEncode(State.Message));
                return(null);
            }
            else
            {
                WebOperationContext.Current.OutgoingResponse.Headers.Add("Server-Status", State.CurrentStatus.ToString());
            }

            Stream output;

            using (NetHttpContext context = new NetHttpContext(WebOperationContext.Current, out output))
            {
                NetHttpChannelDispatcher dispatcher = null;
                try
                {
                    var matches = (from keys in Templates
                                   select keys).Where <NetHttpChannelDispatcher>(delegate(NetHttpChannelDispatcher item)
                    {
                        //return item.IsMatch(new Uri(Configuration.BaseUri), context.Request.Url);
                        return(item.IsMatch(this.BaseUrl, context.Request.Url));
                    }).ToArray <NetHttpChannelDispatcher>();

                    switch (matches.Length)
                    {
                    case 0:
                        throw new HttpException((int)HttpStatusCode.NotFound, "not found");

                    case 1:
                        dispatcher = matches[0];
                        break;

                    default:
                        try
                        {
                            if (context.Request.Params.Count == 0)
                            {
                                var nonQuery = (from match in matches
                                                select match).Where(delegate(NetHttpChannelDispatcher item)
                                {
                                    return(item.MatchType == MatchType.Static);
                                });

                                if (nonQuery.Count() == 0)
                                {
                                    nonQuery = (from match in matches
                                                select match).Where(delegate(NetHttpChannelDispatcher item)
                                    {
                                        return(item.MatchType == MatchType.Template);
                                    });
                                }

                                dispatcher = nonQuery.Single();
                            }
                            else
                            {
                                var query = (from match in matches
                                             select match).Where(delegate(NetHttpChannelDispatcher item)
                                {
                                    return((item.MatchType & MatchType.Static) == MatchType.Static && (item.MatchType & MatchType.Query) == MatchType.Query);
                                });

                                if (query.Count() == 0)
                                {
                                    query = (from match in matches
                                             select match).Where(delegate(NetHttpChannelDispatcher item)
                                    {
                                        return((item.MatchType & MatchType.Query) == MatchType.Query);             //&& item.Method.Parameters.Count() == context.Request.Params.Count;
                                    });
                                }

                                if (query.Count() > 1)
                                {
                                    query = (from match in query
                                             select match).Where(delegate(NetHttpChannelDispatcher item)
                                    {
                                        return((item.MatchType & MatchType.Query) == MatchType.Query && item.Method.Parameters.Count() == context.Request.Params.Count);
                                    });
                                }

                                dispatcher = query.Single();
                            }
                        }
                        catch (InvalidOperationException)
                        {
                            int max = matches.Max <NetHttpChannelDispatcher>(m => ((NetHttpUriTemplate)m.Template).Url.GetMatchingSegments(context.Request.Url).Length);
                            dispatcher = (from match in matches
                                          select match).Where(delegate(NetHttpChannelDispatcher candidate)
                            {
                                bool use = ((NetHttpUriTemplate)candidate.Template).Url.GetMatchingSegments(context.Request.Url).Length == max;
                                if (context.Request.Params.Count == 0)
                                {
                                    use = use && (((candidate.MatchType & MatchType.Static) == MatchType.Static) || ((candidate.MatchType & MatchType.Template) == MatchType.Template));
                                }
                                else
                                {
                                    use = use && ((candidate.MatchType & MatchType.Query) == MatchType.Query);
                                }
                                return(use);
                            }).Single();
                        }

                        break;
                    }

                    dispatcher.Invoke(context);
                }
                //TODO: Find best approach
                catch (HttpException httpException)
                {
                    //context.Response.ContentType = "text/plain; charset=UTF-8";
                    //context.Response.StatusCode = (HttpStatusCode)httpException.GetHttpCode();
                    //context.Response.StatusDescription = httpException.Message;
                    //context.Response.SetStatusAsNotFound(httpException.Message);
                    switch (httpException.ErrorCode)
                    {
                    case 404:
                        context.Response.SetStatusAsNotFound(httpException.Message);
                        context.Response.SuppressEntityBody = true;
                        break;

                    default:
                        throw httpException;
                    }
                }
                catch (System.Exception exception)
                {
                    var httpException = exception.InnerException as HttpException;
                    if (httpException == null)
                    {
                        context.Response.ContentType       = "text/plain; charset=UTF-8";
                        context.Response.StatusCode        = HttpStatusCode.BadRequest;
                        context.Response.StatusDescription = exception.Message;
                    }
                    else
                    {
                        switch (httpException.ErrorCode)
                        {
                        case 404:
                            context.Response.SetStatusAsNotFound(httpException.Message);
                            context.Response.SuppressEntityBody = true;
                            break;

                        default:
                            throw exception.InnerException;
                        }
                    }
                }

                return(output);
            }
        }
        private List<NetHttpChannelDispatcher> GetTemplates(List<NetHttpChannelDispatcher> templates)
        {
            InnerChannels.ForEach(delegate(NetHttpChannelManager manager)
            {
                manager.GetTemplates(templates);
            });

            WADLMethod[] methods = GetWADLMethods();
            XDocument wadl = XDocument.Parse(Wadl());
            var resources = wadl.Descendants(XName.Get("resource",CONSTANTS.Namespace)).ToArray<XElement>();
            int index = 0;
            Array.ForEach<WADLMethod>(methods, delegate(WADLMethod method)
            {
               WADLUtility.Url url = WADLUtility.Url.CreateUrl(resources[index].Parent.Attribute("base").Value, resources[index].Attribute("path").Value);
               NetHttpChannelDispatcher dispatcher = new NetHttpChannelDispatcher(this, method, new NetHttpUriTemplate(url, true));
               templates.Add(dispatcher);
               index++;       
            });
       
           return templates;
        }