Ejemplo n.º 1
0
        public object Execute(IRequest req)
        {
            string contentType;
            var    restPath = RestHandler.FindMatchingRestPath(req.Verb, req.PathInfo, out contentType);

            req.SetRoute(restPath as RestPath);
            req.OperationName = restPath.RequestType.GetOperationName();
            var request = RestHandler.CreateRequest(req, restPath);

            req.Dto = request;

            if (appHost.ApplyRequestFilters(req, req.Response, request))
            {
                return(null);
            }

            var response = Execute(request, req);

            if (appHost.ApplyResponseFilters(req, req.Response, response))
            {
                return(null);
            }

            return(response);
        }
Ejemplo n.º 2
0
        public object Execute(object requestDto, IRequest req, bool applyFilters)
        {
            if (applyFilters)
            {
                if (appHost.ApplyRequestFilters(req, req.Response, requestDto))
                {
                    return(null);
                }
            }

            var response = Execute(requestDto, req);

            return(applyFilters
                ? ApplyResponseFilters(response, req)
                : response);
        }
Ejemplo n.º 3
0
        public override Task ProcessRequestAsync(IRequest httpReq, IResponse httpRes, string operationName)
        {
            try
            {
                appHost.AssertFeatures(format);

                if (appHost.ApplyPreRequestFilters(httpReq, httpRes))
                {
                    return(TypeConstants.EmptyTask);
                }

                httpReq.ResponseContentType = httpReq.GetQueryStringContentType() ?? this.HandlerContentType;
                var callback = httpReq.QueryString[Keywords.Callback];
                var doJsonp  = HostContext.Config.AllowJsonpRequests &&
                               !string.IsNullOrEmpty(callback);

                var request = httpReq.Dto = CreateRequest(httpReq, operationName);

                if (appHost.ApplyRequestFilters(httpReq, httpRes, request))
                {
                    return(TypeConstants.EmptyTask);
                }

                var rawResponse = GetResponse(httpReq, request);

                if (httpRes.IsClosed)
                {
                    return(TypeConstants.EmptyTask);
                }

                return(HandleResponse(rawResponse, response =>
                {
                    response = appHost.ApplyResponseConverters(httpReq, response);

                    if (appHost.ApplyResponseFilters(httpReq, httpRes, response))
                    {
                        return TypeConstants.EmptyTask;
                    }

                    if (doJsonp && !(response is CompressedResult))
                    {
                        return httpRes.WriteToResponse(httpReq, response, (callback + "(").ToUtf8Bytes(), ")".ToUtf8Bytes());
                    }

                    return httpRes.WriteToResponse(httpReq, response);
                },
                                      ex => !HostContext.Config.WriteErrorsToResponse
                    ? ex.ApplyResponseConverters(httpReq).AsTaskException()
                    : HandleException(httpReq, httpRes, operationName, ex.ApplyResponseConverters(httpReq))));
            }
            catch (Exception ex)
            {
                return(!HostContext.Config.WriteErrorsToResponse
                    ? ex.ApplyResponseConverters(httpReq).AsTaskException()
                    : HandleException(httpReq, httpRes, operationName, ex.ApplyResponseConverters(httpReq)));
            }
        }
Ejemplo n.º 4
0
        public override Task ProcessRequestAsync(IRequest httpReq, IResponse httpRes, string operationName)
        {
            try
            {
                appHost.AssertFeatures(format);

                if (appHost.ApplyPreRequestFilters(httpReq, httpRes))
                {
                    return(TypeConstants.EmptyTask);
                }

                httpReq.ResponseContentType = httpReq.GetQueryStringContentType() ?? this.HandlerContentType;
                var callback = httpReq.QueryString[Keywords.Callback];
                var doJsonp  = HostContext.Config.AllowJsonpRequests &&
                               !string.IsNullOrEmpty(callback);

                var request = httpReq.Dto = CreateRequest(httpReq, operationName);

                if (appHost.ApplyRequestFilters(httpReq, httpRes, request))
                {
                    return(TypeConstants.EmptyTask);
                }

                return(appHost.ApplyRequestFiltersAsync(httpReq, httpRes, request)
                       .Continue(t =>
                {
                    if (t.IsFaulted)
                    {
                        return t;
                    }

                    if (t.IsCanceled)
                    {
                        httpRes.StatusCode = (int)HttpStatusCode.PartialContent;
                        httpRes.EndRequest();
                    }

                    if (httpRes.IsClosed)
                    {
                        return TypeConstants.EmptyTask;
                    }

                    var rawResponse = GetResponse(httpReq, request);

                    if (httpRes.IsClosed)
                    {
                        return TypeConstants.EmptyTask;
                    }

                    return HandleResponse(rawResponse, response =>
                    {
                        UpdateResponseContentType(httpReq, response);
                        response = appHost.ApplyResponseConverters(httpReq, response);

                        if (appHost.ApplyResponseFilters(httpReq, httpRes, response))
                        {
                            return TypeConstants.EmptyTask;
                        }

                        if (doJsonp && !(response is CompressedResult))
                        {
                            return httpRes.WriteToResponse(httpReq, response, (callback + "(").ToUtf8Bytes(), ")".ToUtf8Bytes());
                        }

                        return httpRes.WriteToResponse(httpReq, response);
                    });
                })
                       .Unwrap()
                       .Continue(t =>
                {
                    if (t.IsFaulted)
                    {
                        var taskEx = t.Exception.UnwrapIfSingleException();
                        return !HostContext.Config.WriteErrorsToResponse
                                ? taskEx.ApplyResponseConverters(httpReq).AsTaskException()
                                : HandleException(httpReq, httpRes, operationName, taskEx.ApplyResponseConverters(httpReq));
                    }
                    return t;
                })
                       .Unwrap());
            }
            catch (Exception ex)
            {
                return(!HostContext.Config.WriteErrorsToResponse
                    ? ex.ApplyResponseConverters(httpReq).AsTaskException()
                    : HandleException(httpReq, httpRes, operationName, ex.ApplyResponseConverters(httpReq)));
            }
        }