public async Task <ODataResponse> GetResponseAsync(IODataResponseMessageAsync responseMessage) { var readerSettings = new ODataMessageReaderSettings(); // TODO ODataLib7 if (_session.Settings.IgnoreUnmappedProperties) { readerSettings.Validations &= ~ValidationKinds.ThrowOnUndeclaredPropertyForNonOpenType; } readerSettings.MessageQuotas.MaxReceivedMessageSize = Int32.MaxValue; readerSettings.ShouldIncludeAnnotation = x => _session.Settings.IncludeAnnotationsInResults; using (var messageReader = new ODataMessageReader(responseMessage, readerSettings, _model)) { var payloadKind = messageReader.DetectPayloadKind(); if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Error)) { return(ODataResponse.FromStatusCode(responseMessage.StatusCode)); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Value)) { if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { throw new NotImplementedException(); } else { return(ODataResponse.FromValueStream(await responseMessage.GetStreamAsync().ConfigureAwait(false), responseMessage is ODataBatchOperationResponseMessage)); } } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Batch)) { return(await ReadResponse(messageReader.CreateODataBatchReader()).ConfigureAwait(false)); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.ResourceSet)) { return(ReadResponse(messageReader.CreateODataResourceSetReader())); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { return(ReadResponse(messageReader.CreateODataCollectionReader())); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Property)) { if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Resource)) { return(ODataResponse.FromValueStream(await responseMessage.GetStreamAsync().ConfigureAwait(false), responseMessage is ODataBatchOperationResponseMessage)); } else { var property = messageReader.ReadProperty(); return(ODataResponse.FromProperty(property.Name, GetPropertyValue(property.Value))); } } else { return(ReadResponse(messageReader.CreateODataResourceReader())); } } }
public async Task <ODataResponse> GetResponseAsync(IODataResponseMessageAsync responseMessage, bool includeResourceTypeInEntryProperties = false) #endif { var readerSettings = new ODataMessageReaderSettings(); readerSettings.MessageQuotas.MaxReceivedMessageSize = Int32.MaxValue; using (var messageReader = new ODataMessageReader(responseMessage, readerSettings, _model)) { var payloadKind = messageReader.DetectPayloadKind(); if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Error)) { return(ODataResponse.FromStatusCode(responseMessage.StatusCode)); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Value)) { if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { throw new NotImplementedException(); } else { #if SILVERLIGHT var text = Utils.StreamToString(responseMessage.GetStream()); #else var text = Client.Utils.StreamToString(await responseMessage.GetStreamAsync()); #endif return(ODataResponse.FromFeed(new[] { new Dictionary <string, object>() { { FluentCommand.ResultLiteral, text } } })); } } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Batch)) { return(await ReadResponse(messageReader.CreateODataBatchReader(), includeResourceTypeInEntryProperties)); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Feed)) { return(ReadResponse(messageReader.CreateODataFeedReader(), includeResourceTypeInEntryProperties)); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { return(ReadResponse(messageReader.CreateODataCollectionReader(), includeResourceTypeInEntryProperties)); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Property)) { var property = messageReader.ReadProperty(); return(ODataResponse.FromFeed(new[] { new Dictionary <string, object>() { { property.Name ?? FluentCommand.ResultLiteral, GetPropertyValue(property.Value) } } })); } else { return(ReadResponse(messageReader.CreateODataEntryReader(), includeResourceTypeInEntryProperties)); } } }
/// <summary> /// Asynchronously detects the payload kinds supported by this format for the specified message payload. /// </summary> /// <param name="responseMessage">The response message with the payload stream.</param> /// <param name="detectionInfo">Additional information available for the payload kind detection.</param> /// <returns>A task that when completed returns the set of <see cref="ODataPayloadKind"/>s /// that are supported with the specified payload.</returns> internal override Task <IEnumerable <ODataPayloadKind> > DetectPayloadKindAsync( IODataResponseMessageAsync responseMessage, ODataPayloadKindDetectionInfo detectionInfo) { ExceptionUtils.CheckArgumentNotNull(responseMessage, "responseMessage"); ExceptionUtils.CheckArgumentNotNull(detectionInfo, "detectionInfo"); return(TaskUtils.GetTaskForSynchronousOperation(() => DetectPayloadKindImplementation(detectionInfo.ContentType))); }
public async Task <ODataResponse> GetResponseAsync(IODataResponseMessageAsync responseMessage) { if (responseMessage.StatusCode == (int)HttpStatusCode.NoContent) { return(ODataResponse.FromStatusCode(TypeCache, responseMessage.StatusCode, responseMessage.Headers)); } var readerSettings = _session.ToReaderSettings(); using (var messageReader = new ODataMessageReader(responseMessage, readerSettings, _model)) { var payloadKind = messageReader.DetectPayloadKind().ToList(); if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Error)) { return(ODataResponse.FromStatusCode(TypeCache, responseMessage.StatusCode, responseMessage.Headers)); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Value)) { if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { throw new NotImplementedException(); } else { return(ODataResponse.FromValueStream(TypeCache, await responseMessage.GetStreamAsync().ConfigureAwait(false), responseMessage is ODataBatchOperationResponseMessage)); } } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Batch)) { return(await ReadResponse(messageReader.CreateODataBatchReader()).ConfigureAwait(false)); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.ResourceSet)) { return(ReadResponse(messageReader.CreateODataResourceSetReader(), responseMessage)); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { return(ReadResponse(messageReader.CreateODataCollectionReader())); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Property)) { if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Resource)) { return(ReadResponse(messageReader.CreateODataResourceReader(), responseMessage)); } else { var property = messageReader.ReadProperty(); return(ODataResponse.FromProperty(TypeCache, property.Name, GetPropertyValue(property.Value))); } } else { return(ReadResponse(messageReader.CreateODataResourceReader(), responseMessage)); } } }
/// <summary> /// Asynchronously detects the payload kinds supported by this format for the specified message payload. /// </summary> /// <param name="responseMessage">The response message with the payload stream.</param> /// <param name="detectionInfo">Additional information available for the payload kind detection.</param> /// <returns>A task that when completed returns the set of <see cref="ODataPayloadKind"/>s /// that are supported with the specified payload.</returns> internal override Task<IEnumerable<ODataPayloadKind>> DetectPayloadKindAsync( IODataResponseMessageAsync responseMessage, ODataPayloadKindDetectionInfo detectionInfo) { DebugUtils.CheckNoExternalCallers(); ExceptionUtils.CheckArgumentNotNull(responseMessage, "responseMessage"); ExceptionUtils.CheckArgumentNotNull(detectionInfo, "detectionInfo"); return TaskUtils.GetTaskForSynchronousOperation(() => DetectPayloadKindImplementation(detectionInfo.ContentType)); }
public override Task <Stream> GetStreamAsync() { IODataResponseMessageAsync responseMessage = this.responseMessage as IODataResponseMessageAsync; if (responseMessage == null) { throw new ODataException(Strings.ODataResponseMessage_AsyncNotAvailable); } return(base.GetStreamAsync(new Func <Task <Stream> >(responseMessage.GetStreamAsync), false)); }
/// <summary> /// Asynchronously get the stream backing this message. /// </summary> /// <returns>The stream for this message.</returns> public override Task <Stream> GetStreamAsync() { IODataResponseMessageAsync asyncResponseMessage = this.responseMessage as IODataResponseMessageAsync; if (asyncResponseMessage == null) { throw new ODataException(Strings.ODataResponseMessage_AsyncNotAvailable); } return(this.GetStreamAsync(asyncResponseMessage.GetStreamAsync, /*isRequest*/ false)); }
/// <summary> /// Asynchronously detects the payload kinds supported by this format for the specified message payload. /// </summary> /// <param name="responseMessage">The response message with the payload stream.</param> /// <param name="detectionInfo">Additional information available for the payload kind detection.</param> /// <returns>A task that when completed returns the set of <see cref="ODataPayloadKind"/>s /// that are supported with the specified payload.</returns> internal override Task <IEnumerable <ODataPayloadKind> > DetectPayloadKindAsync( IODataResponseMessageAsync responseMessage, ODataPayloadKindDetectionInfo detectionInfo) { ExceptionUtils.CheckArgumentNotNull(responseMessage, "responseMessage"); ExceptionUtils.CheckArgumentNotNull(detectionInfo, "detectionInfo"); // NOTE: After getting the message stream we already (asynchronously) buffered the whole stream in memory (in the AsyncBufferedStream). // Until we get Task-based async stream APIs and retire the AsyncBufferedStream, we call the synchronous method on the buffered stream. return(((ODataMessage)responseMessage).GetStreamAsync() .FollowOnSuccessWith(streamTask => this.DetectPayloadKindImplementation(streamTask.Result, /*readingResponse*/ true, /*synchronous*/ false, detectionInfo))); }
public async Task<ODataResponse> GetResponseAsync(IODataResponseMessageAsync responseMessage, bool includeResourceTypeInEntryProperties = false) { var readerSettings = new ODataMessageReaderSettings(); readerSettings.MessageQuotas.MaxReceivedMessageSize = Int32.MaxValue; using (var messageReader = new ODataMessageReader(responseMessage, readerSettings, _model)) { var payloadKind = messageReader.DetectPayloadKind(); if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Error)) { return ODataResponse.FromStatusCode(responseMessage.StatusCode); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Value)) { if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { throw new NotImplementedException(); } else { var text = Utils.StreamToString(await responseMessage.GetStreamAsync()); return ODataResponse.FromFeed(new[] { new Dictionary<string, object>() { { FluentCommand.ResultLiteral, text } } }); } } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Batch)) { return await ReadResponse(messageReader.CreateODataBatchReader(), includeResourceTypeInEntryProperties); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Feed)) { return ReadResponse(messageReader.CreateODataFeedReader(), includeResourceTypeInEntryProperties); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { return ReadResponse(messageReader.CreateODataCollectionReader(), includeResourceTypeInEntryProperties); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Property)) { var property = messageReader.ReadProperty(); return ODataResponse.FromFeed(new[] { new Dictionary<string, object>() { { property.Name ?? FluentCommand.ResultLiteral, GetPropertyValue(property.Value) } } }); } else { return ReadResponse(messageReader.CreateODataEntryReader(), includeResourceTypeInEntryProperties); } } }
public async Task <ODataResponse> GetResponseAsync(IODataResponseMessageAsync responseMessage) { var readerSettings = new ODataMessageReaderSettings(); readerSettings.MessageQuotas.MaxReceivedMessageSize = Int32.MaxValue; readerSettings.ShouldIncludeAnnotation = x => _session.Settings.IncludeAnnotationsInResults; using (var messageReader = new ODataMessageReader(responseMessage, readerSettings, _model)) { var payloadKind = messageReader.DetectPayloadKind(); if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Error)) { return(ODataResponse.FromStatusCode(responseMessage.StatusCode)); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Value)) { if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { throw new NotImplementedException(); } else { return(ODataResponse.FromValueStream(await responseMessage.GetStreamAsync(), responseMessage is ODataBatchOperationResponseMessage)); } } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Batch)) { return(await ReadResponse(messageReader.CreateODataBatchReader())); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Feed)) { return(ReadResponse(messageReader.CreateODataFeedReader())); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { return(ReadResponse(messageReader.CreateODataCollectionReader())); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Property)) { var property = messageReader.ReadProperty(); return(ODataResponse.FromProperty(property.Name, GetPropertyValue(property.Value))); } else { return(ReadResponse(messageReader.CreateODataEntryReader())); } } }
public async Task<ODataResponse> GetResponseAsync(IODataResponseMessageAsync responseMessage) { var readerSettings = new ODataMessageReaderSettings(); readerSettings.MessageQuotas.MaxReceivedMessageSize = Int32.MaxValue; using (var messageReader = new ODataMessageReader(responseMessage, readerSettings, _model)) { var payloadKind = messageReader.DetectPayloadKind(); if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Error)) { return ODataResponse.FromStatusCode(responseMessage.StatusCode); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Value)) { if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { throw new NotImplementedException(); } else { return ODataResponse.FromValueStream(await responseMessage.GetStreamAsync()); } } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Batch)) { return await ReadResponse(messageReader.CreateODataBatchReader()); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Feed)) { return ReadResponse(messageReader.CreateODataFeedReader()); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { return ReadResponse(messageReader.CreateODataCollectionReader()); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Property)) { var property = messageReader.ReadProperty(); return ODataResponse.FromProperty(property.Name, GetPropertyValue(property.Value)); } else { return ReadResponse(messageReader.CreateODataEntryReader()); } } }
private ODataResponse ReadResponse(ODataReader odataReader, IODataResponseMessageAsync responseMessage) { ResponseNode rootNode = null; var nodeStack = new Stack <ResponseNode>(); while (odataReader.Read()) { if (odataReader.State == ODataReaderState.Completed) { break; } switch (odataReader.State) { case ODataReaderState.ResourceSetStart: case ODataReaderState.DeltaResourceSetStart: StartFeed(nodeStack, CreateAnnotations(odataReader.Item as ODataResourceSetBase)); break; case ODataReaderState.ResourceSetEnd: case ODataReaderState.DeltaResourceSetEnd: EndFeed(nodeStack, CreateAnnotations(odataReader.Item as ODataResourceSetBase), ref rootNode); break; case ODataReaderState.ResourceStart: StartEntry(nodeStack); break; case ODataReaderState.ResourceEnd: EndEntry(nodeStack, ref rootNode, odataReader.Item); break; case ODataReaderState.NestedResourceInfoStart: StartNavigationLink(nodeStack, (odataReader.Item as ODataNestedResourceInfo).Name); break; case ODataReaderState.NestedResourceInfoEnd: EndNavigationLink(nodeStack); break; } } return(ODataResponse.FromNode(TypeCache, rootNode, responseMessage.Headers)); }
public async Task VerifyCanReadResponseAsyncTest() { IODataResponseMessageAsync responseMessage = await new ClientHttpRequestMessage(new Uri(TestDemoService.ServiceBaseUri, "Products(1)")).GetResponseAsync(); using (ODataMessageReader messageReader = new ODataMessageReader( responseMessage, new ODataMessageReaderSettings(), TestUtils.GetServiceModel(TestDemoService.ServiceBaseUri))) { ODataReader reader = await messageReader.CreateODataEntryReaderAsync(); while (await reader.ReadAsync()) { if (reader.State == ODataReaderState.EntryEnd) { ODataEntry entry = (ODataEntry)reader.Item; Assert.AreEqual("DataServiceProviderDemo.Product", entry.TypeName); } } } }
/// <summary> /// Asynchronously get the stream backing this message. /// </summary> /// <returns>The stream for this message.</returns> internal override Task <Stream> GetStreamAsync() { DebugUtils.CheckNoExternalCallers(); IODataResponseMessageAsync asyncResponseMessage = this.responseMessage as IODataResponseMessageAsync; if (asyncResponseMessage == null) { throw new ODataException(Strings.ODataResponseMessage_AsyncNotAvailable); } Task <Stream> task = asyncResponseMessage.GetStreamAsync(); if (task == null) { throw new ODataException(Strings.ODataResponseMessage_StreamTaskIsNull); } return(task.ContinueWith((streamTask) => { Stream messageStream = streamTask.Result; ValidateMessageStream(messageStream); return messageStream; })); }
internal override Task<IEnumerable<ODataPayloadKind>> DetectPayloadKindAsync(IODataResponseMessageAsync responseMessage, ODataPayloadKindDetectionInfo detectionInfo) { ExceptionUtils.CheckArgumentNotNull<IODataResponseMessageAsync>(responseMessage, "responseMessage"); ExceptionUtils.CheckArgumentNotNull<ODataPayloadKindDetectionInfo>(detectionInfo, "detectionInfo"); return ((ODataMessage) responseMessage).GetStreamAsync().FollowOnSuccessWith<Stream, IEnumerable<ODataPayloadKind>>(streamTask => this.DetectPayloadKindImplementation(streamTask.Result, true, false, detectionInfo)); }
/// <summary> /// Asynchronously detects the payload kinds supported by this format for the specified message payload. /// </summary> /// <param name="responseMessage">The response message with the payload stream.</param> /// <param name="detectionInfo">Additional information available for the payload kind detection.</param> /// <returns>A task that when completed returns the set of <see cref="ODataPayloadKind"/>s /// that are supported with the specified payload.</returns> internal abstract Task <IEnumerable <ODataPayloadKind> > DetectPayloadKindAsync(IODataResponseMessageAsync responseMessage, ODataPayloadKindDetectionInfo detectionInfo);
public async Task <ODataResponse> GetResponseAsync(IODataResponseMessageAsync responseMessage) { var readerSettings = new ODataMessageReaderSettings(); if (_session.Settings.IgnoreUnmappedProperties) { readerSettings.UndeclaredPropertyBehaviorKinds = ODataUndeclaredPropertyBehaviorKinds.IgnoreUndeclaredValueProperty; } readerSettings.MessageQuotas.MaxReceivedMessageSize = Int32.MaxValue; readerSettings.ShouldIncludeAnnotation = x => _session.Settings.IncludeAnnotationsInResults; using (var messageReader = new ODataMessageReader(responseMessage, readerSettings, _model)) { var payloadKind = messageReader.DetectPayloadKind(); if (payloadKind.Any(x => x.PayloadKind != ODataPayloadKind.Property)) { _hasResponse = true; } if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Error)) { return(ODataResponse.FromStatusCode(responseMessage.StatusCode)); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Value)) { if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { throw new NotImplementedException(); } else { var stream = await responseMessage.GetStreamAsync().ConfigureAwait(false); return(ODataResponse.FromValueStream(stream, responseMessage is ODataBatchOperationResponseMessage)); } } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Batch)) { return(await ReadResponse(messageReader.CreateODataBatchReader()).ConfigureAwait(false)); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Feed)) { return(ReadResponse(messageReader.CreateODataFeedReader())); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { return(ReadResponse(messageReader.CreateODataCollectionReader())); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Property)) { var property = messageReader.ReadProperty(); if (property.Value != null && (property.Value.GetType() != typeof(string) || !string.IsNullOrEmpty(property.Value.ToString()))) { _hasResponse = true; } if (_hasResponse) { return(ODataResponse.FromProperty(property.Name, GetPropertyValue(property.Value))); } else { return(ODataResponse.EmptyFeed); } } else { return(ReadResponse(messageReader.CreateODataEntryReader())); } } }
/// <summary> /// Asynchronously detects the payload kinds supported by this format for the specified message payload. /// </summary> /// <param name="responseMessage">The response message with the payload stream.</param> /// <param name="detectionInfo">Additional information available for the payload kind detection.</param> /// <returns>A task that when completed returns the set of <see cref="ODataPayloadKind"/>s /// that are supported with the specified payload.</returns> internal abstract Task<IEnumerable<ODataPayloadKind>> DetectPayloadKindAsync(IODataResponseMessageAsync responseMessage, ODataPayloadKindDetectionInfo detectionInfo);
public async Task <ODataResponse> GetResponseAsync(IODataResponseMessageAsync responseMessage) { if (responseMessage.StatusCode == (int)HttpStatusCode.NoContent) { return(ODataResponse.FromStatusCode(TypeCache, responseMessage.StatusCode, responseMessage.Headers)); } var readerSettings = _session.ToReaderSettings(); using var messageReader = new ODataMessageReader(responseMessage, readerSettings, _model); var payloadKind = messageReader.DetectPayloadKind(); if (payloadKind.Any(x => x.PayloadKind != ODataPayloadKind.Property)) { _hasResponse = true; } if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Error)) { return(ODataResponse.FromStatusCode(TypeCache, responseMessage.StatusCode, responseMessage.Headers)); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Value)) { if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { throw new NotImplementedException(); } else { var stream = await responseMessage.GetStreamAsync().ConfigureAwait(false); return(ODataResponse.FromValueStream(TypeCache, stream, responseMessage is ODataBatchOperationResponseMessage)); } } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Batch)) { return(await ReadResponse(messageReader.CreateODataBatchReader()).ConfigureAwait(false)); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Feed)) { return(ReadResponse(messageReader.CreateODataFeedReader(), responseMessage)); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { return(ReadResponse(messageReader.CreateODataCollectionReader())); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Property)) { var property = messageReader.ReadProperty(); if (property.Value != null && (property.Value.GetType() != typeof(string) || !string.IsNullOrEmpty(property.Value.ToString()))) { _hasResponse = true; } if (_hasResponse) { return(ODataResponse.FromProperty(TypeCache, property.Name, GetPropertyValue(property.Value))); } else { return(ODataResponse.EmptyFeeds(TypeCache)); } } else { return(ReadResponse(messageReader.CreateODataEntryReader(), responseMessage)); } }
internal override Task <IEnumerable <ODataPayloadKind> > DetectPayloadKindAsync(IODataResponseMessageAsync responseMessage, ODataPayloadKindDetectionInfo detectionInfo) { ExceptionUtils.CheckArgumentNotNull <IODataResponseMessageAsync>(responseMessage, "responseMessage"); ExceptionUtils.CheckArgumentNotNull <ODataPayloadKindDetectionInfo>(detectionInfo, "detectionInfo"); return(((ODataMessage)responseMessage).GetStreamAsync().FollowOnSuccessWith <Stream, IEnumerable <ODataPayloadKind> >(streamTask => this.DetectPayloadKindImplementation(streamTask.Result, true, false, detectionInfo))); }
/// <summary> /// Asynchronously detects the payload kinds supported by this format for the specified message payload. /// </summary> /// <param name="responseMessage">The response message with the payload stream.</param> /// <param name="detectionInfo">Additional information available for the payload kind detection.</param> /// <returns>A task that when completed returns the set of <see cref="ODataPayloadKind"/>s /// that are supported with the specified payload.</returns> internal override Task<IEnumerable<ODataPayloadKind>> DetectPayloadKindAsync( IODataResponseMessageAsync responseMessage, ODataPayloadKindDetectionInfo detectionInfo) { DebugUtils.CheckNoExternalCallers(); ExceptionUtils.CheckArgumentNotNull(responseMessage, "responseMessage"); ExceptionUtils.CheckArgumentNotNull(detectionInfo, "detectionInfo"); // NOTE: After getting the message stream we already (asynchronously) buffered the whole stream in memory (in the AsyncBufferedStream). // Until we get Task-based async stream APIs and retire the AsyncBufferedStream, we call the synchronous method on the buffered stream. return ((ODataMessage)responseMessage).GetStreamAsync() .FollowOnSuccessWith(streamTask => this.DetectPayloadKindImplementation(streamTask.Result, /*readingResponse*/ true, /*synchronous*/ false, detectionInfo)); }
public async Task<ODataResponse> GetResponseAsync(IODataResponseMessageAsync responseMessage) #endif { var readerSettings = new ODataMessageReaderSettings(); readerSettings.MessageQuotas.MaxReceivedMessageSize = Int32.MaxValue; using (var messageReader = new ODataMessageReader(responseMessage, readerSettings, _model)) { var payloadKind = messageReader.DetectPayloadKind(); if (payloadKind.Any(x => x.PayloadKind != ODataPayloadKind.Property)) _hasResponse = true; if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Error)) { return ODataResponse.FromStatusCode(responseMessage.StatusCode); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Value)) { if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { throw new NotImplementedException(); } else { #if SILVERLIGHT var stream = responseMessage.GetStream(); #else var stream = await responseMessage.GetStreamAsync(); #endif return ODataResponse.FromValueStream(stream); } } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Batch)) { return await ReadResponse(messageReader.CreateODataBatchReader()); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Feed)) { return ReadResponse(messageReader.CreateODataFeedReader()); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { return ReadResponse(messageReader.CreateODataCollectionReader()); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Property)) { var property = messageReader.ReadProperty(); if (property.Value != null && (property.Value.GetType() != typeof(string) || !string.IsNullOrEmpty(property.Value.ToString()))) _hasResponse = true; if (_hasResponse) { return ODataResponse.FromProperty(property.Name, GetPropertyValue(property.Value)); } else { return ODataResponse.EmptyFeed; } } else { return ReadResponse(messageReader.CreateODataEntryReader()); } } }
public async Task <ODataResponse> GetResponseAsync(IODataResponseMessageAsync responseMessage) #endif { var readerSettings = new ODataMessageReaderSettings(); readerSettings.MessageQuotas.MaxReceivedMessageSize = Int32.MaxValue; using (var messageReader = new ODataMessageReader(responseMessage, readerSettings, _model)) { var payloadKind = messageReader.DetectPayloadKind(); if (payloadKind.Any(x => x.PayloadKind != ODataPayloadKind.Property)) { _hasResponse = true; } if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Error)) { return(ODataResponse.FromStatusCode(responseMessage.StatusCode)); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Value)) { if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { throw new NotImplementedException(); } else { #if SILVERLIGHT var stream = responseMessage.GetStream(); #else var stream = await responseMessage.GetStreamAsync(); #endif return(ODataResponse.FromValueStream(stream, responseMessage is ODataBatchOperationResponseMessage)); } } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Batch)) { return(await ReadResponse(messageReader.CreateODataBatchReader())); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Feed)) { return(ReadResponse(messageReader.CreateODataFeedReader())); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Collection)) { return(ReadResponse(messageReader.CreateODataCollectionReader())); } else if (payloadKind.Any(x => x.PayloadKind == ODataPayloadKind.Property)) { var property = messageReader.ReadProperty(); if (property.Value != null && (property.Value.GetType() != typeof(string) || !string.IsNullOrEmpty(property.Value.ToString()))) { _hasResponse = true; } if (_hasResponse) { return(ODataResponse.FromProperty(property.Name, GetPropertyValue(property.Value))); } else { return(ODataResponse.EmptyFeed); } } else { return(ReadResponse(messageReader.CreateODataEntryReader())); } } }
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); } }