/// <summary> default xml deserialzier for converting byte array payload to ODataPayloadElement /// Deserializes payload data and builds ODataPayloadElement. /// </summary> /// <param name="deserializer">The default xml payload deserializer.</param> /// <param name="response">The http response from the server.</param> /// <param name="payloadUri">The payload uri</param> /// <returns>The ODataPayloadElement converted from the payload.</returns> private ODataPayloadElement DeserializePayloadData(IPayloadDeserializer deserializer, HttpResponseData response, ODataUri payloadUri) { ExceptionUtilities.CheckObjectNotNull(response.Body, "Response has no body content"); // Only expects HttpStatusCode.OK for normal query tests. Error sceanarios are not included. ExceptionUtilities.Assert(response.StatusCode == HttpStatusCode.OK, "Expected status code: OK. Actual: {0}", response.StatusCode); try { string charset; if (!response.TryGetMimeCharset(out charset)) { charset = null; } string contentType = null; if (!response.TryGetHeaderValueIgnoreHeaderCase(HttpHeaders.ContentType, out contentType)) { contentType = null; } var payloadContext = ODataPayloadContext.BuildODataPayloadContextFromODataUri(payloadUri); payloadContext.EncodingName = charset; payloadContext.ContentType = contentType; return(deserializer.DeserializeFromBinary(response.Body, payloadContext)); } catch (Exception ex) { throw new TaupoInvalidOperationException("Failed to deserialize response body", ex); } }
/// <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); }
/// <summary> /// Deserializes the given HTTP payload into a rich representation according to the rules of this format using the default encoding. /// </summary> /// <param name="deserializer">The deserializer to extend</param> /// <param name="serialized">The binary that was sent over HTTP</param> /// <returns>The payload element</returns> public static ODataPayloadElement DeserializeFromBinary(this IPayloadDeserializer deserializer, byte[] serialized) { ExceptionUtilities.CheckArgumentNotNull(deserializer, "deserializer"); return(deserializer.DeserializeFromBinary(serialized, new ODataPayloadContext())); }