private bool ReadBatch(string requestPayload, ODataVersion version) { IODataRequestMessage requestMessage = new InMemoryMessage() { Stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(requestPayload)) }; requestMessage.SetHeader("Content-Type", batchContentTypeMultipartMime); ODataMessageReaderSettings settings = new ODataMessageReaderSettings { Version = version }; using (ODataMessageReader messageReader = new ODataMessageReader(requestMessage, settings, this.userModel)) { ODataBatchReader batchReader = messageReader.CreateODataBatchReader(); while (batchReader.Read()) { switch (batchReader.State) { case ODataBatchReaderState.Operation: // Encountered an operation (either top-level or in a change set) ODataBatchOperationRequestMessage operationMessage = batchReader.CreateOperationRequestMessage(); break; } } } return(true); }
public static OeOperationMessage Create(IEdmModel edmModel, Uri baseUri, ODataBatchReader reader, IServiceProvider serviceProvider) { ODataBatchOperationRequestMessage batchRequest = reader.CreateOperationRequestMessage(); ODataResource entry = new ResourceFactory(edmModel, baseUri, serviceProvider).CreateEntry(batchRequest, out IEdmEntitySet entitSet); return(new OeOperationMessage(batchRequest, entitSet, entry)); }
public static OeOperationMessage Create(OeMessageContext context, ODataBatchReader reader) { ODataBatchOperationRequestMessage batchRequest = reader.CreateOperationRequestMessage(); var operation = new OeOperationMessage(batchRequest); if (batchRequest.Method == ODataConstants.MethodDelete) { operation.EntityItem = operation.ReadEntityFromUrl(context); } else { using (Stream stream = batchRequest.GetStream()) operation.EntityItem = operation.ReadEntityFromStream(context, stream); } return(operation); }
private static async Task <HttpRequestMessage> ReadOperationInternalAsync( ODataBatchReader reader, Guid batchId, Guid?changeSetId, CancellationToken cancellationToken, bool bufferContentStream = true) { ODataBatchOperationRequestMessage batchRequest = reader.CreateOperationRequestMessage(); HttpRequestMessage request = new HttpRequestMessage(); request.Method = new HttpMethod(batchRequest.Method); request.RequestUri = batchRequest.Url; if (bufferContentStream) { using (Stream stream = batchRequest.GetStream()) { MemoryStream bufferedStream = new MemoryStream(); // Passing in the default buffer size of 81920 so that we can also pass in a cancellation token await stream.CopyToAsync(bufferedStream, bufferSize : 81920, cancellationToken : cancellationToken); bufferedStream.Position = 0; request.Content = new StreamContent(bufferedStream); } } else { request.Content = new LazyStreamContent(() => batchRequest.GetStream()); } foreach (var header in batchRequest.Headers) { string headerName = header.Key; string headerValue = header.Value; if (!request.Headers.TryAddWithoutValidation(headerName, headerValue)) { request.Content.Headers.TryAddWithoutValidation(headerName, headerValue); } } request.SetODataBatchId(batchId); request.SetODataContentId(batchRequest.ContentId); if (changeSetId != null && changeSetId.HasValue) { request.SetODataChangeSetId(changeSetId.Value); } return(request); }
private static async Task <HttpContext> ReadOperationInternalAsync( ODataBatchReader reader, HttpContext originalContext, Guid batchId, Guid?changeSetId, CancellationToken cancellationToken, bool bufferContentStream = true) { ODataBatchOperationRequestMessage batchRequest = reader.CreateOperationRequestMessage(); HttpContext context = CreateHttpContext(originalContext); HttpRequest request = context.Request; request.Method = batchRequest.Method; request.CopyAbsoluteUrl(batchRequest.Url); // Not using bufferContentStream. Unlike AspNet, AspNetCore cannot guarantee the disposal // of the stream in the context of execution so there is no choice but to copy the stream // from the batch reader. using (Stream stream = batchRequest.GetStream()) { MemoryStream bufferedStream = new MemoryStream(); // Passing in the default buffer size of 81920 so that we can also pass in a cancellation token await stream.CopyToAsync(bufferedStream, bufferSize : 81920, cancellationToken : cancellationToken); bufferedStream.Position = 0; request.Body = bufferedStream; } foreach (var header in batchRequest.Headers) { // Copy headers from batch, overwriting any existing headers. string headerName = header.Key; string headerValue = header.Value; request.Headers[headerName] = headerValue; } request.SetODataBatchId(batchId); request.SetODataContentId(batchRequest.ContentId); if (changeSetId != null && changeSetId.HasValue) { request.SetODataChangeSetId(changeSetId.Value); } return(context); }
private void ServiceProcessBatchRequest(ODataJsonBatchPayloadTestCase testCase, ODataVersion version) { string requestPayload = testCase.RequestPayload; ODataJsonBatchPayloadTestCase.ValidateContentType requestMessageContentTypeVerifier = testCase.ContentTypeVerifier; IODataRequestMessage requestMessage = new InMemoryMessage() { Stream = new MemoryStream(Encoding.ASCII.GetBytes(requestPayload)) }; requestMessage.SetHeader("Content-Type", batchContentTypeApplicationJson); using (ODataMessageReader messageReader = new ODataMessageReader(requestMessage, new ODataMessageReaderSettings() { Version = version, BaseUri = new Uri("http://odata.org") }, this.edmModel)) { ODataBatchReader batchReader = messageReader.CreateODataBatchReader(); int operationIdx = 0; while (batchReader.Read()) { switch (batchReader.State) { case ODataBatchReaderState.Operation: ODataBatchOperationRequestMessage operationMessage = batchReader.CreateOperationRequestMessage(); // Verify operation message content type processing. requestMessageContentTypeVerifier.Invoke(operationMessage, operationIdx); operationIdx++; break; } } } }
private byte[] ServiceReadRequestAndWriterResponseForMultipartBatchVerifyDependsOnIds(string requestPayload, ODataVersion maxVersion) { byte[] responseBytes = null; IODataRequestMessage requestMessage = new InMemoryMessage() { Stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(requestPayload)) }; requestMessage.SetHeader("Content-Type", batchContentTypeMultipartMime); ODataMessageReaderSettings settings = new ODataMessageReaderSettings { Version = maxVersion }; using (ODataMessageReader messageReader = new ODataMessageReader(requestMessage, settings, this.userModel)) { MemoryStream responseStream = new MemoryStream(); IODataResponseMessage responseMessage = new InMemoryMessage { Stream = responseStream }; // Client is expected to receive the response message in the same format as that is used in the request sent. responseMessage.SetHeader("Content-Type", batchContentTypeMultipartMime); using (ODataMessageWriter messageWriter = new ODataMessageWriter(responseMessage)) { ODataBatchWriter batchWriter = messageWriter.CreateODataBatchWriter(); batchWriter.WriteStartBatch(); ODataBatchReader batchReader = messageReader.CreateODataBatchReader(); while (batchReader.Read()) { switch (batchReader.State) { case ODataBatchReaderState.Operation: // Encountered an operation (either top-level or in a change set) ODataBatchOperationRequestMessage operationMessage = batchReader.CreateOperationRequestMessage(); // Verify DependsOnIds are set correctly IEnumerable <string> dependsOnIds = operationMessage.DependsOnIds; switch (operationMessage.ContentId) { case "1": case "2A": Assert.True(dependsOnIds.Count() == 0); break; case "2B": Assert.True(dependsOnIds.SequenceEqual(new List <string> { "2A" })); break; case "2C": Assert.True(dependsOnIds.SequenceEqual(new List <string> { "2A", "2B" })); break; case "3": Assert.True(dependsOnIds.SequenceEqual(new List <string> { "1" })); break; default: break; } ODataBatchOperationResponseMessage response = batchWriter.CreateOperationResponseMessage(operationMessage.ContentId); if (operationMessage.Method == "PATCH") { response.StatusCode = 204; response.SetHeader("Content-Type", "application/json;odata.metadata=none"); } else if (operationMessage.Method == "PUT") { response.StatusCode = 201; response.SetHeader("Content-Type", "application/json;"); } break; case ODataBatchReaderState.ChangesetStart: batchWriter.WriteStartChangeset(); break; case ODataBatchReaderState.ChangesetEnd: batchWriter.WriteEndChangeset(); break; } } batchWriter.WriteEndBatch(); responseStream.Position = 0; responseBytes = responseStream.ToArray(); } return(responseBytes); } }
private byte[] ServiceReadReferenceUriBatchRequestAndWriteResponse(byte[] requestPayload) { IODataRequestMessage requestMessage = new InMemoryMessage() { Stream = new MemoryStream(requestPayload) }; requestMessage.SetHeader("Content-Type", batchContentTypeApplicationJson); ODataMessageReaderSettings settings = new ODataMessageReaderSettings(); settings.BaseUri = new Uri(serviceDocumentUri); byte[] responseBytes = null; using (ODataMessageReader messageReader = new ODataMessageReader(requestMessage, settings, this.userModel)) { MemoryStream responseStream = new MemoryStream(); IODataResponseMessage responseMessage = new InMemoryMessage { Stream = responseStream }; // Client is expected to receive the response message in the same format as that is used in the request sent. responseMessage.SetHeader("Content-Type", batchContentTypeApplicationJson); using (ODataMessageWriter messageWriter = new ODataMessageWriter(responseMessage)) { ODataBatchWriter batchWriter = messageWriter.CreateODataBatchWriter(); batchWriter.WriteStartBatch(); ODataBatchReader batchReader = messageReader.CreateODataBatchReader(); while (batchReader.Read()) { switch (batchReader.State) { case ODataBatchReaderState.Operation: // Encountered an operation (either top-level or in a change set) ODataBatchOperationRequestMessage operationMessage = batchReader.CreateOperationRequestMessage(); ODataBatchOperationResponseMessage response = batchWriter.CreateOperationResponseMessage(operationMessage.ContentId); if (operationMessage.Method == "PUT") { using (ODataMessageReader operationMessageReader = new ODataMessageReader( operationMessage, new ODataMessageReaderSettings(), this.userModel)) { ODataReader reader = operationMessageReader.CreateODataResourceReader(); Assert.NotNull(reader); } response.StatusCode = 201; response.SetHeader("Content-Type", "application/json;odata.metadata=none"); } else if (operationMessage.Method == "PATCH") { using (ODataMessageReader operationMessageReader = new ODataMessageReader( operationMessage, new ODataMessageReaderSettings(), this.userModel)) { ODataReader reader = operationMessageReader.CreateODataResourceReader(); Assert.NotNull(reader); } response.StatusCode = 204; } else if (operationMessage.Method == "GET") { response.StatusCode = 200; response.SetHeader("Content-Type", "application/json;"); ODataMessageWriterSettings writerSettings = new ODataMessageWriterSettings(); writerSettings.ODataUri.ServiceRoot = new Uri(serviceDocumentUri); using ( ODataMessageWriter operationMessageWriter = new ODataMessageWriter(response, writerSettings, this.userModel)) { ODataWriter entryWriter = operationMessageWriter.CreateODataResourceWriter(this.singleton, this.webType); ODataResource entry = new ODataResource() { TypeName = "NS.Web", Properties = new[] { new ODataProperty() { Name = "WebId", Value = -1 }, new ODataProperty() { Name = "Name", Value = aVeryLongString } } }; entryWriter.WriteStart(entry); entryWriter.WriteEnd(); } } break; case ODataBatchReaderState.ChangesetStart: // Set the group Id on the writer side to correlate with request. string atomicGroupId = batchReader.CurrentGroupId; batchWriter.WriteStartChangeset(atomicGroupId); break; case ODataBatchReaderState.ChangesetEnd: batchWriter.WriteEndChangeset(); break; } } batchWriter.WriteEndBatch(); responseStream.Position = 0; responseBytes = responseStream.ToArray(); } return(responseBytes); } }
private byte[] ServiceReadBatchRequestAndWriterBatchResponse(byte[] requestPayload, BodyContentType bodyContentType) { IODataRequestMessage requestMessage = new InMemoryMessage() { Stream = new MemoryStream(requestPayload) }; requestMessage.SetHeader(ODataConstants.ContentTypeHeader, batchContentTypeApplicationJson); using (ODataMessageReader messageReader = new ODataMessageReader(requestMessage, new ODataMessageReaderSettings(), null)) { MemoryStream responseStream = new MemoryStream(); IODataResponseMessage responseMessage = new InMemoryMessage { Stream = responseStream }; // Client is expected to receive the response message in the same format as that is used in the request sent. responseMessage.SetHeader(ODataConstants.ContentTypeHeader, batchContentTypeApplicationJson); ODataMessageWriter messageWriter = new ODataMessageWriter(responseMessage); ODataBatchWriter batchWriter = messageWriter.CreateODataBatchWriter(); batchWriter.WriteStartBatch(); ODataBatchReader batchReader = messageReader.CreateODataBatchReader(); while (batchReader.Read()) { switch (batchReader.State) { case ODataBatchReaderState.Operation: ODataBatchOperationRequestMessage operationMessage = batchReader.CreateOperationRequestMessage(); if (operationMessage.Method == "PUT") { using (Stream operationMessageBodyStream = operationMessage.GetStream()) { // Verify the bytes in the request operation stream. byte[] sampleBytes = bodyContentType == BodyContentType.Textual ? Encoding.UTF8.GetBytes("\"" + this.textualSampleStringA + "\"") : binarySampleBytesA; Assert.Equal(operationMessageBodyStream.Length, sampleBytes.Length); foreach (byte samplebyte in sampleBytes) { Assert.Equal(samplebyte, operationMessageBodyStream.ReadByte()); } } // Create the response. ODataBatchOperationResponseMessage operationResponse = batchWriter.CreateOperationResponseMessage(operationMessage.ContentId); operationResponse.StatusCode = 201; operationResponse.SetHeader("CoNtEnT-TyPe", "application/json;odata.metadata=none"); } else if (operationMessage.Method == "GET") { ODataBatchOperationResponseMessage operationResponse = batchWriter.CreateOperationResponseMessage(operationMessage.ContentId); operationResponse.StatusCode = 200; operationResponse.SetHeader("CoNtEnT-TyPe", GetContentType(bodyContentType)); ODataMessageWriterSettings settings = new ODataMessageWriterSettings(); settings.SetServiceDocumentUri(new Uri(serviceDocumentUri)); using (ODataMessageWriter operationMessageWriter = new ODataMessageWriter(operationResponse, settings, null)) { operationMessageWriter.WriteValue(GetEncodedContentObject(bodyContentType, false)); } } break; case ODataBatchReaderState.ChangesetStart: batchWriter.WriteStartChangeset(); break; case ODataBatchReaderState.ChangesetEnd: batchWriter.WriteEndChangeset(); break; } } batchWriter.WriteEndBatch(); responseStream.Position = 0; return(responseStream.ToArray()); } }
private byte[] ServiceReadSingletonBatchRequestAndWriterBatchResponse(ODataJsonBatchPayloadTestCase testCase, ODataVersion version) { string requestPayload = testCase.RequestPayload; Action <ODataBatchOperationRequestMessage, IList <string> > requestOpMessageVerifier = testCase.RequestMessageDependsOnIdVerifier; IODataRequestMessage requestMessage = new InMemoryMessage() { Stream = new MemoryStream(Encoding.ASCII.GetBytes(requestPayload)) }; requestMessage.SetHeader("Content-Type", batchContentTypeApplicationJson); MemoryStream responseStream = new MemoryStream(); using (ODataMessageReader messageReader = new ODataMessageReader(requestMessage, new ODataMessageReaderSettings() { Version = version }, this.edmModel)) { IODataResponseMessage responseMessage = new InMemoryMessage { Stream = responseStream }; // Client is expected to receive the response message in the same format as that is used in the request sent. responseMessage.SetHeader("Content-Type", batchContentTypeApplicationJson); ODataMessageWriterSettings settings = new ODataMessageWriterSettings() { Version = version }; settings.SetServiceDocumentUri(new Uri(serviceDocumentUri)); ODataMessageWriter messageWriter = new ODataMessageWriter(responseMessage, settings, null); int operationIdx = 0; ODataBatchWriter batchWriter = messageWriter.CreateODataBatchWriter(); batchWriter.WriteStartBatch(); ODataBatchReader batchReader = messageReader.CreateODataBatchReader(); while (batchReader.Read()) { switch (batchReader.State) { case ODataBatchReaderState.Operation: // Encountered an operation (either top-level or in a change set) ODataBatchOperationRequestMessage operationMessage = batchReader.CreateOperationRequestMessage(); // Verify operation message if applicable. requestOpMessageVerifier?.Invoke(operationMessage, testCase.ListOfDependsOnIds.ElementAt(operationIdx)); if (operationMessage.Method == "POST") { ODataBatchOperationResponseMessage response = batchWriter.CreateOperationResponseMessage(operationMessage.ContentId); response.StatusCode = 201; response.SetHeader("Content-Type", batchContentTypeApplicationJson); } else if (operationMessage.Method == "GET") { ODataBatchOperationResponseMessage response = batchWriter.CreateOperationResponseMessage(operationMessage.ContentId); response.StatusCode = 200; response.SetHeader("Content-Type", batchContentTypeApplicationJson); using (ODataMessageWriter operationMessageWriter = new ODataMessageWriter(response, settings, this.edmModel)) { ODataWriter entryWriter = operationMessageWriter.CreateODataResourceWriter(this.singleton, this.userType); ODataResource entry = new ODataResource() { TypeName = "NS.User", Properties = new[] { new ODataProperty() { Name = "UserPrincipalName", Value = "*****@*****.**" }, new ODataProperty() { Name = "GivenName", Value = "Jon" } } }; entryWriter.WriteStart(entry); entryWriter.WriteEnd(); } } operationIdx++; break; case ODataBatchReaderState.ChangesetStart: batchWriter.WriteStartChangeset(); break; case ODataBatchReaderState.ChangesetEnd: batchWriter.WriteEndChangeset(); break; } } batchWriter.WriteEndBatch(); } responseStream.Position = 0; return(responseStream.ToArray()); }
private static async Task<HttpRequestMessage> ReadOperationInternalAsync( ODataBatchReader reader, Guid batchId, Guid? changeSetId, CancellationToken cancellationToken, bool bufferContentStream = true) { ODataBatchOperationRequestMessage batchRequest = reader.CreateOperationRequestMessage(); HttpRequestMessage request = new HttpRequestMessage(); request.Method = new HttpMethod(batchRequest.Method); request.RequestUri = batchRequest.Url; if (bufferContentStream) { using (Stream stream = batchRequest.GetStream()) { MemoryStream bufferedStream = new MemoryStream(); // Passing in the default buffer size of 81920 so that we can also pass in a cancellation token await stream.CopyToAsync(bufferedStream, bufferSize: 81920, cancellationToken: cancellationToken); bufferedStream.Position = 0; request.Content = new StreamContent(bufferedStream); } } else { request.Content = new LazyStreamContent(() => batchRequest.GetStream()); } foreach (var header in batchRequest.Headers) { string headerName = header.Key; string headerValue = header.Value; if (!request.Headers.TryAddWithoutValidation(headerName, headerValue)) { request.Content.Headers.TryAddWithoutValidation(headerName, headerValue); } } request.SetODataBatchId(batchId); if (changeSetId != null && changeSetId.HasValue) { request.SetODataChangeSetId(changeSetId.Value); } return request; }