/// <summary> /// Constructs an OData request body using the given settings /// </summary> /// <param name="contentType">The content type of the body</param> /// <param name="uri">The request uri</param> /// <param name="rootElement">The root payload element</param> /// <returns>An OData request body</returns> public ODataPayloadBody BuildBody(string contentType, ODataUri uri, ODataPayloadElement rootElement) { ExceptionUtilities.CheckArgumentNotNull(contentType, "contentType"); ExceptionUtilities.CheckArgumentNotNull(rootElement, "rootElement"); ExceptionUtilities.CheckAllRequiredDependencies(this); string charset = HttpUtilities.GetContentTypeCharsetOrNull(contentType); byte[] serializedBody = null; var batchRequestPayload = rootElement as BatchRequestPayload; if (batchRequestPayload != null) { string boundary; ExceptionUtilities.Assert(HttpUtilities.TryGetContentTypeParameter(contentType, HttpHeaders.Boundary, out boundary), "Could not find boundary in content type for batch payload. Content type was: '{0}'", contentType); serializedBody = this.BatchSerializer.SerializeBatchPayload(batchRequestPayload, boundary, charset); } else { IProtocolFormatStrategy strategy = this.FormatSelector.GetStrategy(contentType, uri); ExceptionUtilities.CheckObjectNotNull(strategy, "Strategy selector did not return a strategy for content type '{0}'", contentType); IPayloadSerializer serializer = strategy.GetSerializer(); ExceptionUtilities.CheckObjectNotNull(serializer, "Strategy returned a null serializer"); serializedBody = serializer.SerializeToBinary(rootElement, charset); } return(new ODataPayloadBody(serializedBody, rootElement)); }
/// <summary> /// Constructs an OData response to the given request using the given response data /// </summary> /// <param name="request">The odata request that was made</param> /// <param name="underlyingResponse">The response data</param> /// <returns>An odata response</returns> public ODataResponse BuildResponse(ODataRequest request, HttpResponseData underlyingResponse) { ExceptionUtilities.CheckArgumentNotNull(request, "request"); ExceptionUtilities.CheckArgumentNotNull(underlyingResponse, "underlyingResponse"); ODataResponse odataResponse = new ODataResponse(underlyingResponse); if (request.Uri.IsBatch()) { ExceptionUtilities.CheckObjectNotNull(request.Body, "Batch request had no body"); var batchRequestPayload = request.Body.RootElement as BatchRequestPayload; ExceptionUtilities.CheckObjectNotNull(batchRequestPayload, "Batch request body was not a valid batch payload"); this.ExecuteAndCatchErrors(request, odataResponse, () => odataResponse.RootElement = this.BatchDeserializer.DeserializeBatchResponse(batchRequestPayload, underlyingResponse)); return(odataResponse); } string contentType; if (underlyingResponse.TryGetHeaderValueIgnoreHeaderCase(HttpHeaders.ContentType, out contentType)) { IProtocolFormatStrategy strategy = this.FormatSelector.GetStrategy(contentType, request.Uri); ExceptionUtilities.CheckObjectNotNull(strategy, "Strategy selector did not return a strategy for content type '{0}'", contentType); IPayloadDeserializer deserializer = strategy.GetDeserializer(); ExceptionUtilities.CheckObjectNotNull(deserializer, "Strategy returned a null deserializer"); this.ExecuteAndCatchErrors( request, odataResponse, () => { var payloadContext = ODataPayloadContext.BuildPayloadContextFromRequest(request); payloadContext.ContentType = contentType; payloadContext.EncodingName = HttpUtilities.GetContentTypeCharsetOrNull(contentType); odataResponse.RootElement = deserializer.DeserializeFromBinary(odataResponse.Body, payloadContext); if (ShouldResolveMetadata(request.Uri, odataResponse.StatusCode, odataResponse.RootElement.ElementType)) { // resolve the payload's metadata this.MetadataResolver.ResolveMetadata(odataResponse.RootElement, request.Uri); // normalize the payload var normalizer = strategy.GetPayloadNormalizer(); ExceptionUtilities.CheckObjectNotNull(normalizer, "Strategy returned a null payload normalizer"); odataResponse.RootElement = normalizer.Normalize(odataResponse.RootElement); } }); } return(odataResponse); }