internal async Task <IRestResponse> HandleRequest(RestServerRequest req)
        {
            if (!req.HttpServerRequest.IsComplete ||
                req.HttpServerRequest.Method == HttpMethod.Unsupported)
            {
                return(_responseFactory.CreateBadRequest());
            }

            var restMethods = _restMethodCollection.Where(r => r.Match(req.HttpServerRequest.Uri));

            if (!restMethods.Any())
            {
                return(_responseFactory.CreateBadRequest());
            }

            var restMethod = restMethods.SingleOrDefault(r => r.Verb == req.HttpServerRequest.Method);

            if (restMethod == null)
            {
                return(new MethodNotAllowedResponse(restMethods.Select(r => r.Verb)));
            }

            var restMethodExecutor = _methodExecuteFactory.Create(restMethod);

            try
            {
                return(await restMethodExecutor.ExecuteMethodAsync(restMethod, req));
            }
            catch
            {
                return(_responseFactory.CreateBadRequest());
            }
        }
Exemple #2
0
        private object ExecuteAnonymousMethod(RestControllerMethodInfo info, HttpRequest request)
        {
            var instantiator = InstanceCreatorCache.Default.GetCreator(info.MethodInfo.DeclaringType);

            object bodyObj = null;

            try
            {
                bodyObj = _bodySerializer.FromBody(request.Content, request.RequestContentType, info.BodyParameterType);
            }
            catch (JsonReaderException)
            {
                return(_responseFactory.CreateBadRequest());
            }
            catch (InvalidOperationException)
            {
                return(_responseFactory.CreateBadRequest());
            }

            object[] parameters = null;
            try
            {
                parameters = info.GetParametersFromUri(request.Uri).Concat(new[] { bodyObj }).ToArray();
            }
            catch (FormatException)
            {
                return(_responseFactory.CreateBadRequest());
            }

            return(info.MethodInfo.Invoke(
                       instantiator.Create(info.MethodInfo.DeclaringType, info.ControllerConstructorArgs()),
                       parameters));
        }
        private object ExecuteAnonymousMethod(RestControllerMethodInfo info, RestServerRequest request)
        {
            var instantiator = InstanceCreatorCache.Default.GetCreator(info.MethodInfo.DeclaringType);

            object contentObj = null;

            try
            {
                if (request.HttpServerRequest.Content != null)
                {
                    contentObj = _contentSerializer.FromContent(
                        request.ContentEncoding.GetString(request.HttpServerRequest.Content),
                        request.ContentMediaType,
                        info.ContentParameterType);
                }
            }
            catch (JsonReaderException)
            {
                return(_responseFactory.CreateBadRequest());
            }
            catch (InvalidOperationException)
            {
                return(_responseFactory.CreateBadRequest());
            }

            object[] parameters = null;
            try
            {
                parameters = info.GetParametersFromUri(request.HttpServerRequest.Uri).Concat(new[] { contentObj }).ToArray();
            }
            catch (FormatException)
            {
                return(_responseFactory.CreateBadRequest());
            }

            return(info.MethodInfo.Invoke(
                       instantiator.Create(info.MethodInfo.DeclaringType, info.ControllerConstructorArgs()),
                       parameters));
        }
Exemple #4
0
        protected override object ExecuteAnonymousMethod(RestControllerMethodInfo info, RestServerRequest request)
        {
            var instantiator = InstanceCreatorCache.Default.GetCreator(info.MethodInfo.DeclaringType);

            object[] parameters = null;
            try
            {
                parameters = info.GetParametersFromUri(request.HttpServerRequest.Uri).ToArray();
            }
            catch (FormatException)
            {
                return(_responseFactory.CreateBadRequest());
            }

            return(info.MethodInfo.Invoke(
                       instantiator.Create(info.MethodInfo.DeclaringType, info.ControllerConstructorArgs()),
                       parameters));
        }