/// <summary> /// Initializes a new instance of the <see cref="ODataMediaTypeFormatter"/> class. /// </summary> /// <param name="deserializerProvider">The <see cref="ODataDeserializerProvider"/> to use.</param> /// <param name="serializerProvider">The <see cref="ODataSerializerProvider"/> to use.</param> /// <param name="payloadKinds">The kind of payloads this formatter supports.</param> public ODataMediaTypeFormatter(ODataDeserializerProvider deserializerProvider, ODataSerializerProvider serializerProvider, IEnumerable <ODataPayloadKind> payloadKinds) { if (deserializerProvider == null) { throw Error.ArgumentNull("deserializerProvider"); } if (serializerProvider == null) { throw Error.ArgumentNull("serializerProvider"); } if (payloadKinds == null) { throw Error.ArgumentNull("payloadKinds"); } _deserializerProvider = deserializerProvider; _serializerProvider = serializerProvider; _payloadKinds = payloadKinds; // Maxing out the received message size as we depend on the hosting layer to enforce this limit. MessageReaderQuotas = new ODataMessageQuotas { MaxReceivedMessageSize = Int64.MaxValue }; MessageWriterQuotas = new ODataMessageQuotas { MaxReceivedMessageSize = Int64.MaxValue }; _version = DefaultODataVersion; }
/// <summary>Initializes a new instance of the <see cref="T:Microsoft.OData.Core.ODataMessageQuotas" /> class.</summary> /// <param name="other">The instance to copy.</param> public ODataMessageQuotas(ODataMessageQuotas other) { ExceptionUtils.CheckArgumentNotNull(other, "other"); this.maxPartsPerBatch = other.maxPartsPerBatch; this.maxOperationsPerChangeset = other.maxOperationsPerChangeset; this.maxNestingDepth = other.maxNestingDepth; this.maxReceivedMessageSize = other.maxReceivedMessageSize; }
public void CreateODataBatchResponseAsync_ReturnsHttpStatusCodeOK() { // Arrange HttpRequestMessage request = new HttpRequestMessage(); var responses = new ODataBatchResponseItem[] { }; var quotas = new ODataMessageQuotas(); // Act var response = request.CreateODataBatchResponseAsync(responses, quotas).Result; // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
/// <summary> /// Set the message quota limits for WCF Data services server. /// </summary> /// <param name="messageQuotas">Instance of ODataMessageQuotas.</param> internal static void SetDefaultMessageQuotas(ODataMessageQuotas messageQuotas) { // NOTE: the size of the input message is only limited by the WCF message size in Astoria // In WCF DS client, we never had a limit on any of these. Hence for client, it makes sense // to set these values to some high limit. In WCF DS server, there are bunch of APIs to // cover some of these limits and if we pass the value to ODL, for batch requests, there is // a breaking change, since WCF DS server cannot figure out why the exception was thrown and // and hence fail way early. For now, the best way is to tell ODL to not impose any limits // and WCF DS server imposes the limits in its own way. messageQuotas.MaxReceivedMessageSize = long.MaxValue; messageQuotas.MaxPartsPerBatch = int.MaxValue; messageQuotas.MaxOperationsPerChangeset = int.MaxValue; messageQuotas.MaxNestingDepth = int.MaxValue; }
public async Task CreateODataBatchResponseAsync_ReturnsHttpStatusCodeOK() { // Arrange HttpRequestMessage request = new HttpRequestMessage(); request.EnableHttpDependencyInjectionSupport(); var responses = new ODataBatchResponseItem[] { }; var quotas = new ODataMessageQuotas(); // Act var response = await request.CreateODataBatchResponseAsync(responses, quotas); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
public void CopyConstructorShouldCopyMessageQuotas() { var originalQuotas = new ODataMessageQuotas() { MaxNestingDepth = 2, MaxOperationsPerChangeset = 3, MaxPartsPerBatch = 4, MaxReceivedMessageSize = 5 }; this.settings.MessageQuotas = originalQuotas; var copiedQuotas = (new ODataMessageWriterSettings(this.settings)).MessageQuotas; copiedQuotas.MaxNestingDepth.Should().Be(originalQuotas.MaxNestingDepth); copiedQuotas.MaxOperationsPerChangeset.Should().Be(originalQuotas.MaxOperationsPerChangeset); copiedQuotas.MaxPartsPerBatch.Should().Be(originalQuotas.MaxPartsPerBatch); copiedQuotas.MaxReceivedMessageSize.Should().Be(originalQuotas.MaxReceivedMessageSize); }
public void CopyConstructorShouldCopyMessageQuotas() { var originalQuotas = new ODataMessageQuotas() { MaxNestingDepth = 2, MaxOperationsPerChangeset = 3, MaxPartsPerBatch = 4, MaxReceivedMessageSize = 5 }; this.settings.MessageQuotas = originalQuotas; var copiedQuotas = this.settings.Clone().MessageQuotas; Assert.Equal(copiedQuotas.MaxNestingDepth, originalQuotas.MaxNestingDepth); Assert.Equal(copiedQuotas.MaxOperationsPerChangeset, originalQuotas.MaxOperationsPerChangeset); Assert.Equal(copiedQuotas.MaxPartsPerBatch, originalQuotas.MaxPartsPerBatch); Assert.Equal(copiedQuotas.MaxReceivedMessageSize, originalQuotas.MaxReceivedMessageSize); }
public async Task CreateODataBatchResponseAsync_ReturnsHttpStatusCodeOK() { // Arrange IEdmModel model = new EdmModel(); HttpContext context = new DefaultHttpContext(); HttpRequest request = context.Request; context.ODataFeature().RoutePrefix = "odata"; context.RequestServices = BuildServiceProvider(opt => opt.AddRouteComponents("odata", model)); ODataBatchResponseItem[] responses = new ODataBatchResponseItem[] { }; ODataMessageQuotas quotas = new ODataMessageQuotas(); // Act await request.CreateODataBatchResponseAsync(responses, quotas); // Assert Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode); }
internal ODataMediaTypeFormatter(ODataDeserializerProvider deserializerProvider, ODataSerializerProvider serializerProvider, IEnumerable <ODataPayloadKind> payloadKinds, ODataVersion version, HttpRequestMessage request) { Contract.Assert(deserializerProvider != null); Contract.Assert(serializerProvider != null); Contract.Assert(payloadKinds != null); _deserializerProvider = deserializerProvider; _serializerProvider = serializerProvider; _payloadKinds = payloadKinds; _version = version; _request = request; // Maxing out the received message size as we depend on the hosting layer to enforce this limit. MessageReaderQuotas = new ODataMessageQuotas { MaxReceivedMessageSize = Int64.MaxValue }; MessageWriterQuotas = new ODataMessageQuotas { MaxReceivedMessageSize = Int64.MaxValue }; }
internal static Task <HttpResponseMessage> CreateODataBatchResponseAsync(this HttpRequestMessage request, IEnumerable <ODataBatchResponseItem> responses, ODataMessageQuotas messageQuotas) { Contract.Assert(request != null); ODataVersion odataVersion = ResultHelpers.GetODataResponseVersion(request); IServiceProvider requestContainer = request.GetRequestContainer(); ODataMessageWriterSettings writerSettings = requestContainer.GetRequiredService <ODataMessageWriterSettings>(); writerSettings.Version = odataVersion; writerSettings.MessageQuotas = messageQuotas; HttpHeaderValueCollection <MediaTypeWithQualityHeaderValue> acceptHeaderValues = request.Headers.Accept; MediaTypeHeaderValue responseContentType = null; if (!acceptHeaderValues.Any()) { // In absence of accept, if request was JSON then default response to be JSON. // Note that, if responseContentType is not set, then it will default to multipart/mixed // when constructing the BatchContent, so we don't need to handle that case here if (request.Content != null && request.Content.Headers.Any(h => String.Equals(h.Key, ODataBatchHttpRequestMessageExtensions.ContentType, StringComparison.OrdinalIgnoreCase) && h.Value.Any(v => v.IndexOf(ODataBatchHttpRequestMessageExtensions.BatchMediaTypeJson, StringComparison.OrdinalIgnoreCase) > -1))) { responseContentType = MediaTypeHeaderValue.Parse(ODataBatchHttpRequestMessageExtensions.BatchMediaTypeJson); } } else if (acceptHeaderValues.Any( t => t.MediaType.Equals(ODataBatchHttpRequestMessageExtensions.BatchMediaTypeMime, StringComparison.OrdinalIgnoreCase))) { responseContentType = MediaTypeHeaderValue.Parse( String.Format(CultureInfo.InvariantCulture, "multipart/mixed;boundary=batchresponse_{0}", Guid.NewGuid())); } else if (acceptHeaderValues.Any( t => t.MediaType.Equals(ODataBatchHttpRequestMessageExtensions.BatchMediaTypeJson, StringComparison.OrdinalIgnoreCase))) { responseContentType = MediaTypeHeaderValue.Parse(ODataBatchHttpRequestMessageExtensions.BatchMediaTypeJson); } HttpResponseMessage response = request.CreateResponse(HttpStatusCode.OK); response.Content = new ODataBatchContent(responses, requestContainer, responseContentType); return(Task.FromResult(response)); }
internal static Task <HttpResponseMessage> CreateODataBatchResponseAsync(this HttpRequestMessage request, IEnumerable <ODataBatchResponseItem> responses, ODataMessageQuotas messageQuotas) { Contract.Assert(request != null); ODataVersion odataVersion = ODataMediaTypeFormatter.GetODataResponseVersion(request); ODataMessageWriterSettings writerSettings = new ODataMessageWriterSettings() { Version = odataVersion, Indent = true, DisableMessageStreamDisposal = true, MessageQuotas = messageQuotas }; HttpResponseMessage response = request.CreateResponse(HttpStatusCode.OK); response.Content = new ODataBatchContent(responses, writerSettings); return(Task.FromResult(response)); }
internal static Task CreateODataBatchResponseAsync(this HttpRequest request, IEnumerable <ODataBatchResponseItem> responses, ODataMessageQuotas messageQuotas) { Contract.Assert(request != null); ODataVersion odataVersion = GetODataResponseVersion(request); IServiceProvider requestContainer = request.GetSubServiceProvider(); ODataMessageWriterSettings writerSettings = requestContainer.GetRequiredService <ODataMessageWriterSettings>(); writerSettings.Version = odataVersion; writerSettings.MessageQuotas = messageQuotas; HttpResponse response = request.HttpContext.Response; StringValues acceptHeader = request.Headers["Accept"]; string responseContentType = null; if (StringValues.IsNullOrEmpty(acceptHeader)) { // In absence of accept, if request was JSON then default response to be JSON. // Note that, if responseContentType is not set, then it will default to multipart/mixed // when constructing the BatchContent, so we don't need to handle that case here if (!String.IsNullOrEmpty(request.ContentType) && request.ContentType.IndexOf(BatchMediaTypeJson, StringComparison.OrdinalIgnoreCase) > -1) { responseContentType = BatchMediaTypeJson; } } else if (acceptHeader.Any(h => h.Equals(BatchMediaTypeMime, StringComparison.OrdinalIgnoreCase))) { responseContentType = String.Format(CultureInfo.InvariantCulture, "multipart/mixed;boundary=batchresponse_{0}", Guid.NewGuid()); } else if (acceptHeader.Any(h => h.IndexOf(BatchMediaTypeJson, StringComparison.OrdinalIgnoreCase) > -1)) { responseContentType = BatchMediaTypeJson; } response.StatusCode = StatusCodes.Status200OK; ODataBatchContent batchContent = new ODataBatchContent(responses, requestContainer, responseContentType); foreach (var header in batchContent.Headers) { // Copy headers from batch content, overwriting any existing headers. response.Headers[header.Key] = header.Value; } return(batchContent.SerializeToStreamAsync(response.Body)); }
internal static Task CreateODataBatchResponseAsync(this HttpRequest request, IEnumerable <ODataBatchResponseItem> responses, ODataMessageQuotas messageQuotas) { Contract.Assert(request != null); ODataVersion odataVersion = ODataInputFormatter.GetODataResponseVersion(request); IServiceProvider requestContainer = request.GetRequestContainer(); ODataMessageWriterSettings writerSettings = requestContainer.GetRequiredService <ODataMessageWriterSettings>(); writerSettings.Version = odataVersion; writerSettings.MessageQuotas = messageQuotas; HttpResponse response = request.HttpContext.Response; StringValues acceptHeader = request.Headers["Accept"]; string contentType = null; if (StringValues.IsNullOrEmpty(acceptHeader) || acceptHeader.Any(h => h.Equals(ODataBatchHttpRequestExtensions.BatchMediaTypeMime, StringComparison.OrdinalIgnoreCase))) { contentType = String.Format(CultureInfo.InvariantCulture, "multipart/mixed;boundary=batchresponse_{0}", Guid.NewGuid()); } else if (acceptHeader.Any(h => h.Equals(ODataBatchHttpRequestExtensions.BatchMediaTypeJson, StringComparison.OrdinalIgnoreCase))) { contentType = ODataBatchHttpRequestExtensions.BatchMediaTypeJson; } response.StatusCode = (int)HttpStatusCode.OK; ODataBatchContent batchContent = new ODataBatchContent(responses, requestContainer, contentType); foreach (var header in batchContent.Headers) { // Copy headers from batch content, overwriting any existing headers. response.Headers[header.Key] = header.Value; } return(batchContent.SerializeToStreamAsync(response.Body)); }
internal static Task CreateODataBatchResponseAsync(this HttpRequest request, IEnumerable <ODataBatchResponseItem> responses, ODataMessageQuotas messageQuotas) { Contract.Assert(request != null); ODataVersion odataVersion = ODataInputFormatter.GetODataResponseVersion(request); IServiceProvider requestContainer = request.GetRequestContainer(); ODataMessageWriterSettings writerSettings = requestContainer.GetRequiredService <ODataMessageWriterSettings>(); writerSettings.Version = odataVersion; writerSettings.MessageQuotas = messageQuotas; HttpResponse response = request.HttpContext.Response; response.StatusCode = (int)HttpStatusCode.OK; // Need to get the stream from ODataBatchContent. // maybe use the shared class to define bahaviour and put // HttpContent and public stream getter in platform-specific? ODataBatchContent batchContent = new ODataBatchContent(responses, requestContainer); foreach (var header in batchContent.Headers) { // Copy headers from batch content, overwriting any existing headers. response.Headers[header.Key] = header.Value; } return(batchContent.SerializeToStreamAsync(response.Body)); }
internal static Task <HttpResponseMessage> CreateODataBatchResponseAsync(this HttpRequestMessage request, IEnumerable <ODataBatchResponseItem> responses, ODataMessageQuotas messageQuotas) { Contract.Assert(request != null); ODataVersion odataVersion = ResultHelpers.GetODataResponseVersion(request); IServiceProvider requestContainer = request.GetRequestContainer(); ODataMessageWriterSettings writerSettings = requestContainer.GetRequiredService <ODataMessageWriterSettings>(); writerSettings.Version = odataVersion; writerSettings.MessageQuotas = messageQuotas; MediaTypeHeaderValue responseContentType = null; if (request.Headers.Accept.Any( t => t.MediaType.Equals(ODataBatchHttpRequestMessageExtensions.BatchMediaTypeMime, StringComparison.OrdinalIgnoreCase))) { responseContentType = MediaTypeHeaderValue.Parse( String.Format(CultureInfo.InvariantCulture, "multipart/mixed;boundary=batchresponse_{0}", Guid.NewGuid())); } else if (request.Headers.Accept.Any( t => t.MediaType.Equals(ODataBatchHttpRequestMessageExtensions.BatchMediaTypeJson, StringComparison.OrdinalIgnoreCase))) { responseContentType = MediaTypeHeaderValue.Parse(ODataBatchHttpRequestMessageExtensions.BatchMediaTypeJson); } HttpResponseMessage response = request.CreateResponse(HttpStatusCode.OK); response.Content = new ODataBatchContent(responses, requestContainer, responseContentType); return(Task.FromResult(response)); }
internal static Task <HttpResponseMessage> CreateODataBatchResponseAsync(this HttpRequestMessage request, IEnumerable <ODataBatchResponseItem> responses, ODataMessageQuotas messageQuotas) { Contract.Assert(request != null); ODataVersion odataVersion = ODataMediaTypeFormatter.GetODataResponseVersion(request); IServiceProvider requestContainer = request.GetRequestContainer(); ODataMessageWriterSettings writerSettings = requestContainer.GetRequiredService <ODataMessageWriterSettings>(); writerSettings.Version = odataVersion; writerSettings.MessageQuotas = messageQuotas; HttpResponseMessage response = request.CreateResponse(HttpStatusCode.OK); response.Content = new ODataBatchContent(responses, requestContainer); return(Task.FromResult(response)); }