private static HttpServerResponse GetMethodNotAllowedResponse(MethodNotAllowedResponse methodNotAllowedResponse, RestServerRequest restReq)
        {
            var serverResponse = GetDefaultResponse(methodNotAllowedResponse);
            serverResponse.Allow = methodNotAllowedResponse.Allows;

            return serverResponse;
        }
        public async Task<IRestResponse> ExecuteMethodAsync(RestControllerMethodInfo info, RestServerRequest request)
        {
            var methodInvokeResult = ExecuteAnonymousMethod(info, request);

            if (!info.IsAsync)
                return await Task.Run(() => (IRestResponse)methodInvokeResult);

            return await (dynamic)methodInvokeResult;
        }
        private HttpServerResponse GetPostResponse(PostResponse response, RestServerRequest restReq)
        {
            var serverResponse = GetDefaultContentResponse(response, restReq);

            if (response.Status == PostResponse.ResponseStatus.Created)
                serverResponse.Location = new Uri(response.LocationRedirect, UriKind.RelativeOrAbsolute);

            return serverResponse;
        }
        private HttpServerResponse GetDefaultContentResponse(IContentRestResponse response, RestServerRequest restReq)
        {
            var defaultResponse = GetDefaultResponse(response);

            if (response.ContentData != null)
            {
                defaultResponse.ContentType = GetMediaTypeAsString(restReq.AcceptMediaType);
                defaultResponse.ContentCharset = restReq.AcceptCharset;
                defaultResponse.Content = _contentSerializer.ToAcceptContent(response.ContentData, restReq);
            }

            return defaultResponse;
        }
        internal HttpServerResponse ConvertToHttpResponse(IRestResponse restResponse, RestServerRequest restServerRequest)
        {
            var methodNotAllowedResponse = restResponse as MethodNotAllowedResponse;
            if (methodNotAllowedResponse != null)
                return GetMethodNotAllowedResponse(methodNotAllowedResponse, restServerRequest);

            var postResponse = restResponse as PostResponse;
            if (postResponse != null)
                return GetPostResponse(postResponse, restServerRequest);

            var response = restResponse as IContentRestResponse;
            if (response != null)
                return GetDefaultContentResponse(response, restServerRequest);
            
            return GetDefaultResponse(restResponse);
        }        
Esempio n. 6
0
        internal byte[] ToAcceptContent(object contentObject, RestServerRequest req)
        {
            if (contentObject == null)
            {
                return new byte[0];
            }

            if (req.AcceptMediaType == MediaType.JSON)
            {
                return req.AcceptEncoding.GetBytes(JsonConvert.SerializeObject(contentObject));
            }
            else if (req.AcceptMediaType == MediaType.XML)
            {
                return req.AcceptEncoding.GetBytes(XmlSerializeObject(contentObject));
            }

            return new byte[0];
        }
        private 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);
        }
        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);
        }