Exemple #1
0
        /// <inheritdoc/>
        public override bool CanReadType(Type type)
        {
            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }

            if (Request != null)
            {
                ODataDeserializerProvider deserializerProvider = Request.GetRequestContainer()
                                                                 .GetRequiredService <ODataDeserializerProvider>();

                return(ODataInputFormatterHelper.CanReadType(
                           type,
                           Request.GetModel(),
                           Request.ODataProperties().Path,
                           _payloadKinds,
                           (objectType) => deserializerProvider.GetEdmTypeDeserializer(objectType),
                           (objectType) => deserializerProvider.GetODataDeserializer(objectType, Request)));
            }

            return(false);
        }
Exemple #2
0
        /// <inheritdoc/>
        public override bool CanRead(InputFormatterContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            HttpRequest request = context.HttpContext.Request;

            if (request == null)
            {
                throw Error.InvalidOperation(SRResources.ReadFromStreamAsyncMustHaveRequest);
            }

            // Ignore non-OData requests.
            if (request.ODataFeature().Path == null)
            {
                return(false);
            }

            Type type = context.ModelType;

            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }

            ODataDeserializerProvider deserializerProvider = request.GetRequestContainer().GetRequiredService <ODataDeserializerProvider>();

            return(ODataInputFormatterHelper.CanReadType(
                       type,
                       request.GetModel(),
                       request.ODataFeature().Path,
                       _payloadKinds,
                       (objectType) => deserializerProvider.GetEdmTypeDeserializer(objectType),
                       (objectType) => deserializerProvider.GetODataDeserializer(objectType, request)));
        }
Exemple #3
0
        public override Task <object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
        {
            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }

            if (readStream == null)
            {
                throw Error.ArgumentNull("readStream");
            }

            if (Request == null)
            {
                throw Error.InvalidOperation(SRResources.ReadFromStreamAsyncMustHaveRequest);
            }

            object defaultValue = GetDefaultValueForType(type);

            // If content length is 0 then return default value for this type
            HttpContentHeaders contentHeaders = (content == null) ? null : content.Headers;

            if (contentHeaders == null || contentHeaders.ContentLength == 0)
            {
                return(Task.FromResult(defaultValue));
            }

            try
            {
                Func <ODataDeserializerContext> getODataDeserializerContext = () =>
                {
                    return(new ODataDeserializerContext
                    {
                        Request = Request,
                    });
                };

                Action <Exception> logErrorAction = (ex) =>
                {
                    if (formatterLogger == null)
                    {
                        throw ex;
                    }

                    formatterLogger.LogError(String.Empty, ex);
                };

                ODataDeserializerProvider deserializerProvider = Request.GetRequestContainer()
                                                                 .GetRequiredService <ODataDeserializerProvider>();

                return(Task.FromResult(ODataInputFormatterHelper.ReadFromStream(
                                           type,
                                           defaultValue,
                                           Request.GetModel(),
                                           GetBaseAddressInternal(Request),
                                           new WebApiRequestMessage(Request),
                                           () => ODataMessageWrapperHelper.Create(readStream, contentHeaders, Request.GetODataContentIdMapping(), Request.GetRequestContainer()),
                                           (objectType) => deserializerProvider.GetEdmTypeDeserializer(objectType),
                                           (objectType) => deserializerProvider.GetODataDeserializer(objectType, Request),
                                           getODataDeserializerContext,
                                           (disposable) => Request.RegisterForDispose(disposable),
                                           logErrorAction)));
            }
            catch (Exception ex)
            {
                return(TaskHelpers.FromError <object>(ex));
            }
        }
Exemple #4
0
        public override async Task <InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            Type type = context.ModelType;

            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }

            HttpRequest request = context.HttpContext.Request;

            if (request == null)
            {
                throw Error.InvalidOperation(SRResources.ReadFromStreamAsyncMustHaveRequest);
            }

            // If content length is 0 then return default value for this type
            RequestHeaders contentHeaders = request.GetTypedHeaders();
            object         defaultValue   = GetDefaultValueForType(type);

            if (contentHeaders == null || contentHeaders.ContentLength == 0)
            {
                return(InputFormatterResult.Success(defaultValue));
            }

            try
            {
                Func <ODataDeserializerContext> getODataDeserializerContext = () =>
                {
                    return(new ODataDeserializerContext
                    {
                        Request = request,
                    });
                };

                Action <Exception> logErrorAction = (ex) =>
                {
                    ILogger logger = context.HttpContext.RequestServices.GetService <ILogger>();
                    if (logger == null)
                    {
                        throw ex;
                    }

                    logger.LogError(ex, String.Empty);
                };

                List <IDisposable> toDispose = new List <IDisposable>();

                ODataDeserializerProvider deserializerProvider = request.GetRequestContainer().GetRequiredService <ODataDeserializerProvider>();

                object result = await ODataInputFormatterHelper.ReadFromStreamAsync(
                    type,
                    defaultValue,
                    request.GetModel(),
                    GetBaseAddressInternal(request),
                    new WebApiRequestMessage(request),
                    () => ODataMessageWrapperHelper.Create(new StreamWrapper(request.Body), request.Headers, request.GetODataContentIdMapping(), request.GetRequestContainer()),
                    (objectType) => deserializerProvider.GetEdmTypeDeserializer(objectType),
                    (objectType) => deserializerProvider.GetODataDeserializer(objectType, request),
                    getODataDeserializerContext,
                    (disposable) => toDispose.Add(disposable),
                    logErrorAction);

                foreach (IDisposable obj in toDispose)
                {
                    obj.Dispose();
                }

                return(InputFormatterResult.Success(result));
            }
            catch (Exception ex)
            {
                context.ModelState.AddModelError(context.ModelName, ex, context.Metadata);
                return(InputFormatterResult.Failure());
            }
        }