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);
        }
Beispiel #2
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)));
            }
        }
Beispiel #3
0
        public object ApplyResponseFilters(object response, IRequest req)
        {
            var taskResponse = response as Task;

            if (taskResponse != null)
            {
                taskResponse.Wait();
                response = taskResponse.GetResult();
            }

            response = appHost.ApplyResponseConverters(req, response);

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

            return(response);
        }
Beispiel #4
0
        private object ApplyResponseFiltersInternal(object response, IRequest req)
        {
            response = appHost.ApplyResponseConverters(req, response);

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

            return(response);
        }
Beispiel #5
0
        protected Message ExecuteMessage(Message message, RequestAttributes requestAttributes, IRequest httpReq, IResponse httpRes)
        {
            var soapFeature = requestAttributes.ToSoapFeature();

            appHost.AssertFeatures(soapFeature);

            if (httpReq == null)
            {
                httpReq = HostContext.GetCurrentRequest();
            }

            if (httpRes == null && httpReq != null)
            {
                httpRes = httpReq.Response;
            }

            if (httpReq == null)
            {
                throw new ArgumentNullException("httpReq");
            }

            if (httpRes == null)
            {
                throw new ArgumentNullException("httpRes");
            }

            httpReq.UseBufferedStream = true;
            var requestMsg = message ?? GetRequestMessageFromStream(httpReq.InputStream);

            var soapAction = httpReq.GetHeader(HttpHeaders.SOAPAction)
                             ?? GetAction(requestMsg);

            if (soapAction != null)
            {
                httpReq.OperationName = soapAction.Trim('"');
            }

            if (HostContext.ApplyCustomHandlerRequestFilters(httpReq, httpRes))
            {
                return(PrepareEmptyResponse(message, httpReq));
            }

            string requestXml  = GetRequestXml(requestMsg);
            var    requestType = GetRequestType(requestMsg, requestXml);

            httpReq.OperationName = requestType.GetOperationName();
            if (!HostContext.Metadata.CanAccess(requestAttributes, soapFeature.ToFormat(), requestType.GetOperationName()))
            {
                throw HostContext.UnauthorizedAccess(requestAttributes);
            }

            try
            {
                var useXmlSerializerRequest = requestType.HasAttribute <XmlSerializerFormatAttribute>();

                var request = appHost.ApplyRequestConverters(httpReq,
                                                             useXmlSerializerRequest
                        ? XmlSerializableSerializer.Instance.DeserializeFromString(requestXml, requestType)
                        : Serialization.DataContractSerializer.Instance.DeserializeFromString(requestXml, requestType)
                                                             );

                httpReq.Dto = request;

                var requiresSoapMessage = request as IRequiresSoapMessage;
                if (requiresSoapMessage != null)
                {
                    requiresSoapMessage.Message = requestMsg;
                }

                httpReq.SetItem(Keywords.SoapMessage, requestMsg);

                httpRes.ContentType = GetSoapContentType(httpReq.ContentType);

                var hasRequestFilters = HostContext.GlobalRequestFilters.Count > 0 ||
                                        FilterAttributeCache.GetRequestFilterAttributes(request.GetType()).Any();

                if (hasRequestFilters && HostContext.ApplyRequestFilters(httpReq, httpRes, request))
                {
                    return(EmptyResponse(requestMsg, requestType));
                }

                httpReq.RequestAttributes |= requestAttributes;
                var response = ExecuteService(request, httpReq);

                var taskResponse = response as Task;
                if (taskResponse != null)
                {
                    taskResponse.Wait();
                    response = Text.FastMember.TypeAccessor.Create(taskResponse.GetType())[taskResponse, "Result"];
                }

                response = appHost.ApplyResponseConverters(httpReq, response);

                if (appHost.ApplyResponseFilters(httpReq, httpRes, response))
                {
                    return(EmptyResponse(requestMsg, requestType));
                }

                var httpResult = response as IHttpResult;
                if (httpResult != null)
                {
                    response = httpResult.Response;
                }

                var noMsgAction = requestMsg.Headers.Action == null;
                var responseMsg = CreateResponseMessage(response, requestMsg.Version, requestType, noMsgAction);

                if (httpResult != null)
                {
                    SetErrorStatusIfAny(httpReq.Response, responseMsg, httpResult.Status);
                }

                return(responseMsg);
            }
            catch (Exception ex)
            {
                if (httpReq.Dto != null)
                {
                    HostContext.RaiseServiceException(httpReq, httpReq.Dto, ex);
                }
                else
                {
                    HostContext.RaiseUncaughtException(httpReq, httpRes, httpReq.OperationName, ex);
                }

                throw new SerializationException("3) Error trying to deserialize requestType: "
                                                 + requestType
                                                 + ", xml body: " + requestXml, ex);
            }
        }
        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)));
            }
        }