protected override async Task <Stream> WriteActionContentAsync(string method, string commandText, string actionName, IDictionary <string, object> parameters) { IODataRequestMessageAsync message = IsBatch ? await CreateBatchOperationMessageAsync(method, null, null, commandText, true) .ConfigureAwait(false) : new ODataRequestMessage(); using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), _model)) { var action = _model.SchemaElements.BestMatch( x => x.SchemaElementKind == EdmSchemaElementKind.Action, x => x.Name, actionName, _session.Pluralizer) as IEdmAction; var parameterWriter = await messageWriter.CreateODataParameterWriterAsync(action).ConfigureAwait(false); await parameterWriter.WriteStartAsync().ConfigureAwait(false); foreach (var parameter in parameters) { var operationParameter = action.Parameters.BestMatch(x => x.Name, parameter.Key, _session.Pluralizer); if (operationParameter == null) { throw new UnresolvableObjectException(parameter.Key, string.Format("Parameter [{0}] not found for action [{1}]", parameter.Key, actionName)); } await WriteOperationParameterAsync(parameterWriter, operationParameter, parameter.Key, parameter.Value).ConfigureAwait(false); } await parameterWriter.WriteEndAsync().ConfigureAwait(false); return(IsBatch ? null : await message.GetStreamAsync().ConfigureAwait(false)); } }
/// <summary> /// Asynchronously detects the payload kinds supported by this format for the specified message payload. /// </summary> /// <param name="requestMessage">The request 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( IODataRequestMessageAsync requestMessage, ODataPayloadKindDetectionInfo detectionInfo) { ExceptionUtils.CheckArgumentNotNull(requestMessage, "requestMessage"); ExceptionUtils.CheckArgumentNotNull(detectionInfo, "detectionInfo"); return(TaskUtils.GetTaskForSynchronousOperation(() => DetectPayloadKindImplementation(detectionInfo.ContentType))); }
/// <summary> /// Asynchronously detects the payload kinds supported by this format for the specified message payload. /// </summary> /// <param name="requestMessage">The request 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( IODataRequestMessageAsync requestMessage, ODataPayloadKindDetectionInfo detectionInfo) { ExceptionUtils.CheckArgumentNotNull(requestMessage, "requestMessage"); ExceptionUtils.CheckArgumentNotNull(detectionInfo, "detectionInfo"); // Metadata is not supported in requests! return(TaskUtils.GetCompletedTask(Enumerable.Empty <ODataPayloadKind>())); }
public override Task <Stream> GetStreamAsync() { IODataRequestMessageAsync requestMessage = this.requestMessage as IODataRequestMessageAsync; if (requestMessage == null) { throw new ODataException(Strings.ODataRequestMessage_AsyncNotAvailable); } return(base.GetStreamAsync(new Func <Task <Stream> >(requestMessage.GetStreamAsync), true)); }
/// <summary> /// Asynchronously get the stream backing this message. /// </summary> /// <returns>The stream for this message.</returns> public override Task <Stream> GetStreamAsync() { IODataRequestMessageAsync asyncRequestMessage = this.requestMessage as IODataRequestMessageAsync; if (asyncRequestMessage == null) { throw new ODataException(Strings.ODataRequestMessage_AsyncNotAvailable); } return(this.GetStreamAsync(asyncRequestMessage.GetStreamAsync, /*isRequest*/ true)); }
/// <summary> /// Asynchronously detects the payload kinds supported by this format for the specified message payload. /// </summary> /// <param name="requestMessage">The request 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( IODataRequestMessageAsync requestMessage, ODataPayloadKindDetectionInfo detectionInfo) { ExceptionUtils.CheckArgumentNotNull(requestMessage, "requestMessage"); 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)requestMessage).GetStreamAsync() .FollowOnSuccessWith(streamTask => this.DetectPayloadKindImplementation(streamTask.Result, /*readingResponse*/ false, /*synchronous*/ false, detectionInfo))); }
protected override async Task <Stream> WriteEntryContentAsync(string method, string collection, string commandText, IDictionary <string, object> entryData) { IODataRequestMessageAsync message = IsBatch ? await CreateOperationRequestMessageAsync(method, collection, entryData, commandText) : new ODataRequestMessage(); var entityType = _model.FindDeclaredType( _session.Metadata.GetEntityCollectionQualifiedTypeName(collection)) as IEdmEntityType; var model = method == RestVerbs.Patch ? new EdmDeltaModel(_model, entityType, entryData.Keys) : _model; using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), model)) { if (method == RestVerbs.Get || method == RestVerbs.Delete) { return(null); } var contentId = _deferredBatchWriter != null?_deferredBatchWriter.Value.GetContentId(entryData) : null; var entityCollection = _session.Metadata.GetEntityCollection(collection); var entryDetails = _session.Metadata.ParseEntryDetails(entityCollection.Name, entryData, contentId); var entryWriter = await messageWriter.CreateODataEntryWriterAsync(); var entry = new Microsoft.OData.Core.ODataEntry(); entry.TypeName = entityType.FullName(); var typeProperties = (_model.FindDeclaredType(entry.TypeName) as IEdmEntityType).Properties(); entry.Properties = entryDetails.Properties.Select(x => new ODataProperty() { Name = typeProperties.BestMatch(y => y.Name, x.Key, _session.Pluralizer).Name, Value = GetPropertyValue(typeProperties, x.Key, x.Value) }).ToList(); await entryWriter.WriteStartAsync(entry); if (entryDetails.Links != null) { foreach (var link in entryDetails.Links) { if (link.Value.Any(x => x.LinkData != null)) { await WriteLinkAsync(entryWriter, entry, link.Key, link.Value); } } } await entryWriter.WriteEndAsync(); return(IsBatch ? null : await message.GetStreamAsync()); } }
protected override async Task <Stream> WriteEntryContentAsync(string method, string collection, string commandText, IDictionary <string, object> entryData, bool resultRequired) { IODataRequestMessageAsync message = IsBatch ? await CreateBatchOperationMessageAsync(method, collection, entryData, commandText, resultRequired).ConfigureAwait(false) : new ODataRequestMessage(); if (method == RestVerbs.Get || method == RestVerbs.Delete) { return(null); } var entityType = _model.FindDeclaredType( _session.Metadata.GetQualifiedTypeName(collection)) as IEdmEntityType; var model = (method == RestVerbs.Patch || method == RestVerbs.Merge) ? new EdmDeltaModel(_model, entityType, entryData.Keys) : _model; using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), model)) { var contentId = _deferredBatchWriter != null?_deferredBatchWriter.Value.GetContentId(entryData, null) : null; //var entityCollection = _session.Metadata.GetEntityCollection(collection); var entityCollection = _session.Metadata.NavigateToCollection(collection); var entryDetails = _session.Metadata.ParseEntryDetails(entityCollection.Name, entryData, contentId); var entryWriter = messageWriter.CreateODataEntryWriter(); var entry = CreateODataEntry(entityType.FullName(), entryDetails.Properties); entryWriter.WriteStart(entry); if (entryDetails.Links != null) { foreach (var link in entryDetails.Links) { if (link.Value.Any(x => x.LinkData != null)) { WriteLink(entryWriter, entry, link.Key, link.Value); } } } entryWriter.WriteEnd(); if (IsBatch) { return(null); } return(await message.GetStreamAsync().ConfigureAwait(false)); } }
protected override async Task <Stream> WriteActionContentAsync(string method, string commandText, string actionName, string boundTypeName, IDictionary <string, object> parameters) { IODataRequestMessageAsync message = IsBatch ? await CreateBatchOperationMessageAsync(method, null, null, commandText, true).ConfigureAwait(false) : new ODataRequestMessage(); using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), _model)) { Func <IEdmOperationParameter, IEdmType, bool> typeMatch = (parameter, baseType) => parameter == null || parameter.Type.Definition == baseType || parameter.Type.Definition.TypeKind == EdmTypeKind.Collection && (parameter.Type.Definition as IEdmCollectionType).ElementType.Definition == baseType; var action = boundTypeName == null ? _model.SchemaElements.BestMatch( x => x.SchemaElementKind == EdmSchemaElementKind.Action, x => x.Name, actionName, _session.Pluralizer) as IEdmAction : _model.SchemaElements.BestMatch( x => x.SchemaElementKind == EdmSchemaElementKind.Action && typeMatch( ((IEdmAction)x).Parameters.FirstOrDefault(p => p.Name == "bindingParameter"), _model.FindDeclaredType(boundTypeName)), x => x.Name, actionName, _session.Pluralizer) as IEdmAction; var parameterWriter = await messageWriter.CreateODataParameterWriterAsync(action).ConfigureAwait(false); await parameterWriter.WriteStartAsync().ConfigureAwait(false); foreach (var parameter in parameters) { var operationParameter = action.Parameters.BestMatch(x => x.Name, parameter.Key, _session.Pluralizer); if (operationParameter == null) { throw new UnresolvableObjectException(parameter.Key, string.Format("Parameter [{0}] not found for action [{1}]", parameter.Key, actionName)); } await WriteOperationParameterAsync(parameterWriter, operationParameter, parameter.Key, parameter.Value).ConfigureAwait(false); } await parameterWriter.WriteEndAsync().ConfigureAwait(false); return(IsBatch ? null : await message.GetStreamAsync().ConfigureAwait(false)); } }
protected override async Task <Stream> WriteLinkContentAsync(string method, string commandText, string linkIdent) { IODataRequestMessageAsync message = IsBatch ? await CreateBatchOperationMessageAsync(method, null, null, commandText, false).ConfigureAwait(false) : new ODataRequestMessage(); using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), _model)) { var link = new ODataEntityReferenceLink { Url = Utils.CreateAbsoluteUri(_session.Settings.BaseUri.AbsoluteUri, linkIdent) }; messageWriter.WriteEntityReferenceLink(link); if (IsBatch) { return(null); } return(await message.GetStreamAsync().ConfigureAwait(false)); } }
protected override async Task <Stream> WriteActionContentAsync(string method, string commandText, string actionName, string boundTypeName, IDictionary <string, object> parameters) { IODataRequestMessageAsync message = IsBatch ? await CreateBatchOperationMessageAsync(method, null, null, commandText, true).ConfigureAwait(false) : new ODataRequestMessage(); using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(ODataFormat.Json), _model)) { var action = _model.SchemaElements .Where(x => x.SchemaElementKind == EdmSchemaElementKind.EntityContainer) .SelectMany(x => (x as IEdmEntityContainer).FunctionImports()) .BestMatch(x => x.Name, actionName, _session.Settings.NameMatchResolver); var parameterWriter = await messageWriter.CreateODataParameterWriterAsync(action).ConfigureAwait(false); await parameterWriter.WriteStartAsync().ConfigureAwait(false); foreach (var parameter in parameters) { var operationParameter = action.Parameters.BestMatch(x => x.Name, parameter.Key, _session.Settings.NameMatchResolver); if (operationParameter == null) { throw new UnresolvableObjectException(parameter.Key, $"Parameter [{parameter.Key}] not found for action [{actionName}]"); } await WriteOperationParameterAsync(parameterWriter, operationParameter, parameter.Key, parameter.Value).ConfigureAwait(false); } await parameterWriter.WriteEndAsync().ConfigureAwait(false); if (IsBatch) { return(null); } return(await message.GetStreamAsync().ConfigureAwait(false)); } }
/// <summary> /// Asynchronously get the stream backing this message. /// </summary> /// <returns>The stream for this message.</returns> internal override Task <Stream> GetStreamAsync() { DebugUtils.CheckNoExternalCallers(); IODataRequestMessageAsync asyncRequestMessage = this.requestMessage as IODataRequestMessageAsync; if (asyncRequestMessage == null) { throw new ODataException(Strings.ODataRequestMessage_AsyncNotAvailable); } Task <Stream> task = asyncRequestMessage.GetStreamAsync(); if (task == null) { throw new ODataException(Strings.ODataRequestMessage_StreamTaskIsNull); } return(task.ContinueWith((streamTask) => { Stream messageStream = streamTask.Result; ValidateMessageStream(messageStream); return messageStream; })); }
internal override Task<IEnumerable<ODataPayloadKind>> DetectPayloadKindAsync(IODataRequestMessageAsync requestMessage, ODataPayloadKindDetectionInfo detectionInfo) { ExceptionUtils.CheckArgumentNotNull<IODataRequestMessageAsync>(requestMessage, "requestMessage"); ExceptionUtils.CheckArgumentNotNull<ODataPayloadKindDetectionInfo>(detectionInfo, "detectionInfo"); return TaskUtils.GetTaskForSynchronousOperation<IEnumerable<ODataPayloadKind>>(() => DetectPayloadKindImplementation(detectionInfo.ContentType)); }
/// <summary> /// Asynchronously detects the payload kinds supported by this format for the specified message payload. /// </summary> /// <param name="requestMessage">The request 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( IODataRequestMessageAsync requestMessage, ODataPayloadKindDetectionInfo detectionInfo) { DebugUtils.CheckNoExternalCallers(); ExceptionUtils.CheckArgumentNotNull(requestMessage, "requestMessage"); 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)requestMessage).GetStreamAsync() .FollowOnSuccessWith(streamTask => this.DetectPayloadKindImplementation(streamTask.Result, /*readingResponse*/ false, /*synchronous*/ false, detectionInfo)); }
internal override Task<IEnumerable<ODataPayloadKind>> DetectPayloadKindAsync(IODataRequestMessageAsync requestMessage, ODataPayloadKindDetectionInfo detectionInfo) { ExceptionUtils.CheckArgumentNotNull<IODataRequestMessageAsync>(requestMessage, "requestMessage"); ExceptionUtils.CheckArgumentNotNull<ODataPayloadKindDetectionInfo>(detectionInfo, "detectionInfo"); return ((ODataMessage) requestMessage).GetStreamAsync().FollowOnSuccessWith<Stream, IEnumerable<ODataPayloadKind>>(streamTask => this.DetectPayloadKindImplementation(streamTask.Result, false, false, detectionInfo)); }
/// <summary> /// Asynchronously detects the payload kinds supported by this format for the specified message payload. /// </summary> /// <param name="requestMessage">The request 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(IODataRequestMessageAsync requestMessage, ODataPayloadKindDetectionInfo detectionInfo);
/// <summary> /// Asynchronously detects the payload kinds supported by this format for the specified message payload. /// </summary> /// <param name="requestMessage">The request 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(IODataRequestMessageAsync requestMessage, ODataPayloadKindDetectionInfo detectionInfo);
internal static ODataMessageReader GetODataMessageReader(IODataRequestMessageAsync oDataRequestMessage, IEdmModel edmModel) { return(new ODataMessageReader(oDataRequestMessage, new ODataMessageReaderSettings(), edmModel)); }
/// <summary> /// Asynchronously detects the payload kinds supported by this format for the specified message payload. /// </summary> /// <param name="requestMessage">The request 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( IODataRequestMessageAsync requestMessage, ODataPayloadKindDetectionInfo detectionInfo) { DebugUtils.CheckNoExternalCallers(); ExceptionUtils.CheckArgumentNotNull(requestMessage, "requestMessage"); ExceptionUtils.CheckArgumentNotNull(detectionInfo, "detectionInfo"); // Metadata is not supported in requests! return TaskUtils.GetCompletedTask(Enumerable.Empty<ODataPayloadKind>()); }
internal override Task <IEnumerable <ODataPayloadKind> > DetectPayloadKindAsync(IODataRequestMessageAsync requestMessage, ODataPayloadKindDetectionInfo detectionInfo) { ExceptionUtils.CheckArgumentNotNull <IODataRequestMessageAsync>(requestMessage, "requestMessage"); ExceptionUtils.CheckArgumentNotNull <ODataPayloadKindDetectionInfo>(detectionInfo, "detectionInfo"); return(TaskUtils.GetCompletedTask <IEnumerable <ODataPayloadKind> >(Enumerable.Empty <ODataPayloadKind>())); }
protected override async Task <Stream> WriteActionContentAsync(string method, string commandText, string actionName, IDictionary <string, object> parameters) { IODataRequestMessageAsync message = IsBatch ? await CreateBatchOperationMessageAsync(method, null, null, commandText, false) : new ODataRequestMessage(); using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), _model)) { var action = _model.SchemaElements.BestMatch( x => x.SchemaElementKind == EdmSchemaElementKind.Action, x => x.Name, actionName, _session.Pluralizer) as IEdmAction; var parameterWriter = await messageWriter.CreateODataParameterWriterAsync(action); await parameterWriter.WriteStartAsync(); foreach (var parameter in parameters) { var actionParameter = action.Parameters.BestMatch(x => x.Name, parameter.Key, _session.Pluralizer); if (actionParameter == null) { throw new UnresolvableObjectException(parameter.Key, string.Format("Parameter [{0}] not found for action [{1}]", parameter.Key, actionName)); } if (actionParameter.Type.Definition.TypeKind == EdmTypeKind.Collection) { var collectionType = actionParameter.Type.Definition as IEdmCollectionType; var elementType = collectionType.ElementType; if (elementType.Definition.TypeKind == EdmTypeKind.Entity) { var feedWriter = await parameterWriter.CreateFeedWriterAsync(parameter.Key); var feed = new ODataFeed(); await feedWriter.WriteStartAsync(feed); foreach (var item in parameter.Value as IEnumerable) { var entry = CreateODataEntry(elementType.Definition.FullTypeName(), item.ToDictionary()); await feedWriter.WriteStartAsync(entry); await feedWriter.WriteEndAsync(); } await feedWriter.WriteEndAsync(); } else { var collectionWriter = await parameterWriter.CreateCollectionWriterAsync(parameter.Key); await collectionWriter.WriteStartAsync(new ODataCollectionStart()); foreach (var item in parameter.Value as IEnumerable) { await collectionWriter.WriteItemAsync(item); } await collectionWriter.WriteEndAsync(); } } else { await parameterWriter.WriteValueAsync(parameter.Key, parameter.Value); } } await parameterWriter.WriteEndAsync(); return(IsBatch ? null : await message.GetStreamAsync()); } }
internal override Task <IEnumerable <ODataPayloadKind> > DetectPayloadKindAsync(IODataRequestMessageAsync requestMessage, ODataPayloadKindDetectionInfo detectionInfo) { ExceptionUtils.CheckArgumentNotNull <IODataRequestMessageAsync>(requestMessage, "requestMessage"); ExceptionUtils.CheckArgumentNotNull <ODataPayloadKindDetectionInfo>(detectionInfo, "detectionInfo"); return(((ODataMessage)requestMessage).GetStreamAsync().FollowOnSuccessWith <Stream, IEnumerable <ODataPayloadKind> >(streamTask => this.DetectPayloadKindImplementation(streamTask.Result, false, false, detectionInfo))); }
internal override Task<IEnumerable<ODataPayloadKind>> DetectPayloadKindAsync(IODataRequestMessageAsync requestMessage, ODataPayloadKindDetectionInfo detectionInfo) { ExceptionUtils.CheckArgumentNotNull<IODataRequestMessageAsync>(requestMessage, "requestMessage"); ExceptionUtils.CheckArgumentNotNull<ODataPayloadKindDetectionInfo>(detectionInfo, "detectionInfo"); return TaskUtils.GetCompletedTask<IEnumerable<ODataPayloadKind>>(Enumerable.Empty<ODataPayloadKind>()); }