public static IDictionary <string, object> ReadODataBody(ModelBindingContext bindingContext) { ODataActionPayloadDeserializer deserializer = bindingContext.HttpContext.Request.GetSubServiceProvider().GetService <ODataActionPayloadDeserializer>(); if (deserializer == null) { return(null); } ODataDeserializerContext context = BuildDeserializerContext(bindingContext); HttpRequest request = bindingContext.HttpContext.Request; var body = request.HttpContext.Features.Get <Http.Features.IHttpBodyControlFeature>(); if (body != null) { body.AllowSynchronousIO = true; } IODataRequestMessage oDataRequestMessage = ODataMessageWrapperHelper.Create(request.Body, request.Headers); IEdmModel model = request.GetModel(); using (var messageReader = new ODataMessageReader(oDataRequestMessage, null, model)) { var result = deserializer.Read(messageReader, typeof(ODataActionParameters), context); return(result as ODataActionParameters); } }
internal static async Task <object> ReadFromStreamAsync( Type type, object defaultValue, Uri baseAddress, ODataVersion version, HttpRequest request, IList <IDisposable> disposes) { object result; IEdmModel model = request.GetModel(); IEdmTypeReference expectedPayloadType; ODataDeserializer deserializer = GetDeserializer(request, type, out expectedPayloadType); if (deserializer == null) { throw Error.Argument("type", SRResources.FormatterReadIsNotSupportedForType, type.FullName, typeof(ODataInputFormatter).FullName); } try { ODataMessageReaderSettings oDataReaderSettings = request.GetReaderSettings(); oDataReaderSettings.BaseUri = baseAddress; oDataReaderSettings.Validations = oDataReaderSettings.Validations & ~ValidationKinds.ThrowOnUndeclaredPropertyForNonOpenType; oDataReaderSettings.Version = version; // WebAPI should read untyped values as structural values by setting ReadUntypedAsString=false. // In ODL 8.x, ReadUntypedAsString option will be deleted. oDataReaderSettings.ReadUntypedAsString = false; IODataRequestMessage oDataRequestMessage = ODataMessageWrapperHelper.Create(new StreamWrapper(request.Body), request.Headers, request.GetODataContentIdMapping(), request.GetSubServiceProvider()); ODataMessageReader oDataMessageReader = new ODataMessageReader(oDataRequestMessage, oDataReaderSettings, model); disposes.Add(oDataMessageReader); ODataPath path = request.ODataFeature().Path; ODataDeserializerContext readContext = BuildDeserializerContext(request); readContext.Path = path; readContext.Model = model; readContext.ResourceType = type; readContext.ResourceEdmType = expectedPayloadType; result = await deserializer.ReadAsync(oDataMessageReader, type, readContext).ConfigureAwait(false); } catch (Exception ex) { LoggerError(request.HttpContext, ex); result = defaultValue; } return(result); }
internal static object ReadFromStream(Type type, object defaultValue, Uri baseAddress, HttpRequest request, IList <IDisposable> disposes) { object result; IEdmModel model = request.GetModel(); IEdmTypeReference expectedPayloadType; ODataDeserializer deserializer = GetDeserializer(request, type, out expectedPayloadType); if (deserializer == null) { throw Error.Argument("type", SRResources.FormatterReadIsNotSupportedForType, type.FullName, typeof(ODataInputFormatter).FullName); } try { ODataMessageReaderSettings oDataReaderSettings = request.GetReaderSettings(); oDataReaderSettings.BaseUri = baseAddress; oDataReaderSettings.Validations = oDataReaderSettings.Validations & ~ValidationKinds.ThrowOnUndeclaredPropertyForNonOpenType; IODataRequestMessage oDataRequestMessage = ODataMessageWrapperHelper.Create(request.Body, request.Headers, request.GetODataContentIdMapping(), request.GetSubServiceProvider()); ODataMessageReader oDataMessageReader = new ODataMessageReader(oDataRequestMessage, oDataReaderSettings, model); disposes.Add(oDataMessageReader); ODataPath path = request.ODataFeature().Path; ODataDeserializerContext readContext = new ODataDeserializerContext { Request = request, }; readContext.Path = path; readContext.Model = model; readContext.ResourceType = type; readContext.ResourceEdmType = expectedPayloadType; result = deserializer.Read(oDataMessageReader, type, readContext); } catch (Exception ex) { LoggerError(request.HttpContext, ex); result = defaultValue; } return(result); }
public static async Task <IDictionary <string, object> > ReadODataBodyAsync(ModelBindingContext bindingContext) { ODataActionPayloadDeserializer deserializer = bindingContext.HttpContext.Request.GetRouteServices().GetService <ODataActionPayloadDeserializer>(); if (deserializer == null) { return(null); } ODataDeserializerContext context = BuildDeserializerContext(bindingContext); HttpRequest request = bindingContext.HttpContext.Request; IODataRequestMessage oDataRequestMessage = ODataMessageWrapperHelper.Create(request.Body, request.Headers); IEdmModel model = request.GetModel(); using (var messageReader = new ODataMessageReader(oDataRequestMessage, null, model)) { var result = await deserializer.ReadAsync(messageReader, typeof(ODataActionParameters), context).ConfigureAwait(false); return(result as ODataActionParameters); } }
internal static async Task WriteToStreamAsync( Type type, object value, IEdmModel model, ODataVersion version, Uri baseAddress, MediaTypeHeaderValue contentType, HttpRequest request, IHeaderDictionary requestHeaders, ODataSerializerProvider serializerProvider) { if (model == null) { throw Error.InvalidOperation(SRResources.RequestMustHaveModel); } ODataSerializer serializer = GetSerializer(type, value, request, serializerProvider); ODataPath path = request.ODataFeature().Path; IEdmNavigationSource targetNavigationSource = path == null ? null : path.GetNavigationSource(); HttpResponse response = request.HttpContext.Response; // serialize a response string preferHeader = RequestPreferenceHelpers.GetRequestPreferHeader(requestHeaders); string annotationFilter = null; if (!string.IsNullOrEmpty(preferHeader)) { ODataMessageWrapper messageWrapper = ODataMessageWrapperHelper.Create(response.Body, response.Headers); messageWrapper.SetHeader(RequestPreferenceHelpers.PreferHeaderName, preferHeader); annotationFilter = messageWrapper.PreferHeader().AnnotationFilter; } IODataResponseMessageAsync responseMessage = ODataMessageWrapperHelper.Create(new StreamWrapper(response.Body), response.Headers, request.GetSubServiceProvider()); if (annotationFilter != null) { responseMessage.PreferenceAppliedHeader().AnnotationFilter = annotationFilter; } ODataMessageWriterSettings writerSettings = request.GetWriterSettings(); writerSettings.BaseUri = baseAddress; writerSettings.Version = version; writerSettings.Validations = writerSettings.Validations & ~ValidationKinds.ThrowOnUndeclaredPropertyForNonOpenType; string metadataLink = request.CreateODataLink(MetadataSegment.Instance); if (metadataLink == null) { throw new SerializationException(SRResources.UnableToDetermineMetadataUrl); } // Set this variable if the SelectExpandClause is different from the processed clause on the Query options SelectExpandClause selectExpandDifferentFromQueryOptions = null; ODataQueryOptions queryOptions = request.GetQueryOptions(); SelectExpandClause processedSelectExpandClause = request.ODataFeature().SelectExpandClause; if (queryOptions != null && queryOptions.SelectExpand != null) { if (queryOptions.SelectExpand.ProcessedSelectExpandClause != processedSelectExpandClause) { selectExpandDifferentFromQueryOptions = processedSelectExpandClause; } } else if (processedSelectExpandClause != null) { selectExpandDifferentFromQueryOptions = processedSelectExpandClause; } writerSettings.ODataUri = new ODataUri { ServiceRoot = baseAddress, // TODO: 1604 Convert webapi.odata's ODataPath to ODL's ODataPath, or use ODL's ODataPath. SelectAndExpand = processedSelectExpandClause, Apply = request.ODataFeature().ApplyClause, //Path = (path == null || IsOperationPath(path)) ? null : path.Path, Path = path }; ODataMetadataLevel metadataLevel = ODataMetadataLevel.Minimal; if (contentType != null) { IEnumerable <KeyValuePair <string, string> > parameters = contentType.Parameters.Select(val => new KeyValuePair <string, string>(val.Name.ToString(), val.Value.ToString())); metadataLevel = ODataMediaTypes.GetMetadataLevel(contentType.MediaType.ToString(), parameters); } using (ODataMessageWriter messageWriter = new ODataMessageWriter(responseMessage, writerSettings, model)) { ODataSerializerContext writeContext = BuildSerializerContext(request); writeContext.NavigationSource = targetNavigationSource; writeContext.Model = model; writeContext.RootElementName = GetRootElementName(path) ?? "root"; writeContext.SkipExpensiveAvailabilityChecks = serializer.ODataPayloadKind == ODataPayloadKind.ResourceSet; writeContext.Path = path; writeContext.MetadataLevel = metadataLevel; writeContext.QueryOptions = queryOptions; //Set the SelectExpandClause on the context if it was explicitly specified. if (selectExpandDifferentFromQueryOptions != null) { writeContext.SelectExpandClause = selectExpandDifferentFromQueryOptions; } await serializer.WriteObjectAsync(value, type, messageWriter, writeContext).ConfigureAwait(false); } }