Exemplo n.º 1
0
        private ActionBase GetMethodBasedAction(string name, Content content, object state)
        {
            var httpContext = (HttpContext)state;
            OperationCallingContext method;

            try
            {
                //TODO:~ Combine request body and querystring parameters into the 3th parameter.
                method = OperationCenter.GetMethodByRequest(content, name,
                                                            ODataMiddleware.Read(httpContext.Request.Body));
            }
            catch (OperationNotFoundException e)
            {
                throw new InvalidContentActionException(e, InvalidContentActionReason.UnknownAction, content.Path,
                                                        e.Message, name);
            }
            catch (AmbiguousMatchException e)
            {
                throw new InvalidContentActionException(e, InvalidContentActionReason.UnknownAction, content.Path,
                                                        e.Message, name);
            }

            method.HttpContext = httpContext;
            return(new ODataOperationMethodExecutor(method));
        }
Exemplo n.º 2
0
        private async Task <object[]> GetOperationParametersAsync(ActionBase action,
                                                                  HttpContext httpContext, ODataRequest odataRequest)
        {
            if (action.ActionParameters.Length == 0)
            {
                return(ActionParameter.EmptyValues);
            }

            var inputStream = httpContext?.Request?.Body;
            var values      = new object[action.ActionParameters.Length];

            var parameters = action.ActionParameters;

            if (parameters.Length == 1 && parameters[0].Name == null)
            {
                var parameter = parameters[0];
                if (parameter.Type == null)
                {
                    using (var reader = new StreamReader(inputStream))
                        values[0] = reader.ReadToEnd();
                    if (parameter.Required && values[0] == null)
                    {
                        // ReSharper disable once NotResolvedInText
                        throw new ArgumentNullException("[unnamed]", "Request parameter is required.");
                    }
                }
                else
                {
                    values[0] = ODataMiddleware.Read(inputStream, parameter.Type);
                    if (parameter.Required && values[0] == null)
                    {
                        // ReSharper disable once NotResolvedInText
                        throw new ArgumentNullException("[unnamed]", "Request parameter is required. Type: " + parameter.Type.FullName);
                    }
                }
            }
            else
            {
                var model = await ODataMiddleware.ReadToJsonAsync(httpContext);

                var i = 0;
                foreach (var parameter in parameters)
                {
                    var name = parameter.Name;
                    var type = parameter.Type;
                    if (type == typeof(HttpContext))
                    {
                        values[i] = httpContext;
                    }
                    else if (type == typeof(ODataRequest))
                    {
                        values[i] = odataRequest;
                    }
                    else
                    {
                        var val = model?[name];
                        if (val == null)
                        {
                            if (parameter.Required)
                            {
                                throw new ArgumentNullException(parameter.Name);
                            }
                            values[i] = Type.Missing;
                        }
                        else
                        {
                            var valStr = val.ToString();

                            if (type == typeof(string))
                            {
                                values[i] = valStr;
                            }
                            else if (type == typeof(Boolean))
                            {
                                // we handle "True", "true" and "1" as boolean true values
                                values[i] = JsonConvert.DeserializeObject(valStr.ToLower(), type);
                            }
                            else if (type.IsEnum)
                            {
                                values[i] = Enum.Parse(type, valStr, true);
                            }
                            else
                            {
                                values[i] = JsonConvert.DeserializeObject(valStr, type);
                            }
                        }
                    }
                    i++;
                }
            }
            return(values);
        }