/// <summary> /// Uses the content type and uri to get a deserializer, and asserts that the root element is of the expected type /// </summary> /// <typeparam name="TElement">The expected root element type</typeparam> /// <param name="selector">The protocol format selector</param> /// <param name="uri">The uri to use for getting a format strategy</param> /// <param name="contentType">The content type to use for getting a format strategy</param> /// <param name="body">The body to deserialize</param> /// <returns>The root element of the body</returns> public static TElement DeserializeAndCast <TElement>(this IProtocolFormatStrategySelector selector, ODataUri uri, string contentType, byte[] body) where TElement : ODataPayloadElement { ExceptionUtilities.CheckArgumentNotNull(selector, "selector"); ExceptionUtilities.CheckStringArgumentIsNotNullOrEmpty(contentType, "contentType"); ExceptionUtilities.CheckArgumentNotNull(body, "body"); // TODO: force uri to be non-null var strategy = selector.GetStrategy(contentType, uri); ExceptionUtilities.CheckObjectNotNull(strategy, "Could not find protocol strategy for content-type '{0}'.", contentType); var deserializer = strategy.GetDeserializer(); ExceptionUtilities.CheckObjectNotNull(deserializer, "Strategy returned null deserializer"); string charset = HttpUtilities.GetContentTypeCharsetOrNull(contentType); var rootElement = deserializer.DeserializeFromBinary(body, new ODataPayloadContext { EncodingName = charset }); ExceptionUtilities.CheckObjectNotNull(rootElement, "Deserializer returned null element"); var afterCast = rootElement as TElement; ExceptionUtilities.CheckObjectNotNull(afterCast, "Root element was of unexpected type '{0}'. Expected '{1}'", rootElement.ElementType, typeof(TElement).Name); return(afterCast); }
/// <summary> /// Helper method for getting the error payload from the response. Handles a special case for media-resource operations which may contain errors despite not normally /// being deserialized as such. /// </summary> /// <param name="response">The current response</param> /// <param name="formatSelector">The format selector to use if the response needs to be deserialized</param> /// <param name="errorPayload">The error payload if one was found</param> /// <returns>Whether or not an error payload was found</returns> internal static bool TryGetErrorPayloadFromResponse(ODataResponse response, IProtocolFormatStrategySelector formatSelector, out ODataErrorPayload errorPayload) { ExceptionUtilities.CheckArgumentNotNull(response, "response"); errorPayload = null; var payload = response.RootElement; if (payload == null) { return(false); } if (payload.ElementType == ODataPayloadElementType.ODataErrorPayload) { errorPayload = (ODataErrorPayload)payload; return(true); } // From here out, try to handle special case for streams which come back with error payloads and are not interpreted if (payload.ElementType != ODataPayloadElementType.PrimitiveValue) { return(false); } var body = ((PrimitiveValue)payload).ClrValue as byte[]; if (body == null) { return(false); } var contentType = response.GetHeaderValueIfExists(HttpHeaders.ContentType); if (contentType == null) { return(false); } // deserialize it ExceptionUtilities.CheckArgumentNotNull(formatSelector, "formatSelector"); var formatForContentType = formatSelector.GetStrategy(contentType, null); var deserializer = formatForContentType.GetDeserializer(); payload = deserializer.DeserializeFromBinary(body, new ODataPayloadContext { EncodingName = HttpUtilities.GetContentTypeCharsetOrNull(contentType) }); errorPayload = payload as ODataErrorPayload; return(errorPayload != null); }
/// <summary> /// Helper method for getting the error payload from the response. Handles a special case for media-resource operations which may contain errors despite not normally /// being deserialized as such. /// </summary> /// <param name="response">The current response</param> /// <param name="formatSelector">The format selector to use if the response needs to be deserialized</param> /// <param name="errorPayload">The error payload if one was found</param> /// <returns>Whether or not an error payload was found</returns> internal static bool TryGetErrorPayloadFromResponse(ODataResponse response, IProtocolFormatStrategySelector formatSelector, out ODataErrorPayload errorPayload) { ExceptionUtilities.CheckArgumentNotNull(response, "response"); errorPayload = null; var payload = response.RootElement; if (payload == null) { return false; } if (payload.ElementType == ODataPayloadElementType.ODataErrorPayload) { errorPayload = (ODataErrorPayload)payload; return true; } // From here out, try to handle special case for streams which come back with error payloads and are not interpreted if (payload.ElementType != ODataPayloadElementType.PrimitiveValue) { return false; } var body = ((PrimitiveValue)payload).ClrValue as byte[]; if (body == null) { return false; } var contentType = response.GetHeaderValueIfExists(HttpHeaders.ContentType); if (contentType == null) { return false; } // deserialize it ExceptionUtilities.CheckArgumentNotNull(formatSelector, "formatSelector"); var formatForContentType = formatSelector.GetStrategy(contentType, null); var deserializer = formatForContentType.GetDeserializer(); payload = deserializer.DeserializeFromBinary(body, new ODataPayloadContext { EncodingName = HttpUtilities.GetContentTypeCharsetOrNull(contentType) }); errorPayload = payload as ODataErrorPayload; return errorPayload != null; }
public static TElement DeserializeAndCast <TElement>(this HttpResponseData response, IProtocolFormatStrategySelector selector) where TElement : ODataPayloadElement { ExceptionUtilities.CheckArgumentNotNull(response, "response"); // TODO: parse uri string contentType; ExceptionUtilities.Assert(response.TryGetHeaderValueIgnoreHeaderCase(HttpHeaders.ContentType, out contentType), "Cannot deserialize response. 'Content-Type' header not found"); return(DeserializeAndCast <TElement>(selector, null, contentType, response.Body)); }
public static TElement DeserializeAndCast <TElement>(this IHttpRequest request, IProtocolFormatStrategySelector selector) where TElement : ODataPayloadElement { ExceptionUtilities.CheckArgumentNotNull(request, "request"); var odataRequest = request as ODataRequest; if (odataRequest != null) { ExceptionUtilities.CheckObjectNotNull(odataRequest.Body, "OData request body unexpectedly null"); var rootElement = odataRequest.Body.RootElement; var afterCast = rootElement as TElement; ExceptionUtilities.CheckObjectNotNull(afterCast, "Root element was of unexpected type '{0}'. Expected '{1}'", rootElement.ElementType, ODataPayloadElement.GetElementType <TElement>()); return(afterCast); } else { string contentType; ExceptionUtilities.Assert(request.TryGetHeaderValueIgnoreHeaderCase(HttpHeaders.ContentType, out contentType), "Cannot deserialize request. 'Content-Type' header not found"); return(DeserializeAndCast <TElement>(selector, null, contentType, request.GetRequestBody())); } }