/// <summary> /// Interprets the client preference for having a response body. /// </summary> /// <param name="requestDescription">The request description.</param> /// <param name="verb">The request verb.</param> /// <param name="requestMessage">The request message.</param> /// <returns>An enum representation of the client's preference.</returns> private static ResponseBodyPreference InterpretClientPreference(RequestDescription requestDescription, HttpVerbs verb, IODataRequestMessage requestMessage) { Debug.Assert(requestDescription != null, "requestDescription != null"); // If no responseBodyPreference given, we have default behavior of producing content for POST and not producing it for PUT/PATCH. // If responseBodyPreference is given we honor the responseBodyPreference only if the request was for an entity and following conditions are true: // This is not a service operation invoke // DSV was set to 3.0 and above // Server is configured to be >= 3.0 if (requestDescription.LinkUri || requestDescription.SegmentInfos[0].TargetSource == RequestTargetSource.ServiceOperation || requestDescription.RequestVersion < VersionUtil.Version4Dot0) { return(ResponseBodyPreference.None); } if ((verb.IsInsert()) || ((verb.IsUpdate()) && (requestDescription.TargetKind == RequestTargetKind.Resource || requestDescription.IsRequestForNonEntityProperty))) { if (requestMessage.PreferHeader().ReturnContent.HasValue) { return(requestMessage.PreferHeader().ReturnContent.Value ? ResponseBodyPreference.Content : ResponseBodyPreference.NoContent); } } // TODO: move logic for when/when-not-to write a response body here and remove all checks for 'none' elsewhere return(ResponseBodyPreference.None); }
/// <summary> /// Initializes a new instance of the <see cref="ClientPreference"/> class. /// </summary> /// <param name="requestDescription">The request description.</param> /// <param name="verb">The request verb.</param> /// <param name="requestMessage">The request message.</param> /// <param name="effectiveMaxResponseVersion">The effective max response version for the request, which is the min of MDSV and MPV.</param> public ClientPreference(RequestDescription requestDescription, HttpVerbs verb, IODataRequestMessage requestMessage, Version effectiveMaxResponseVersion) : this(InterpretClientPreference(requestDescription, verb, requestMessage)) { if (effectiveMaxResponseVersion >= VersionUtil.Version4Dot0) { this.annotationFilter = requestMessage.PreferHeader().AnnotationFilter; } }
/// <summary> /// Initializes a new instance of the <see cref="ClientPreference"/> class. /// </summary> /// <param name="requestDescription">The request description.</param> /// <param name="verb">The request verb.</param> /// <param name="requestMessage">The request message.</param> /// <param name="effectiveMaxResponseVersion">The effective max response version for the request, which is the min of MDSV and MPV.</param> public ClientPreference(RequestDescription requestDescription, HttpVerbs verb, IODataRequestMessage requestMessage, Version effectiveMaxResponseVersion) : this(InterpretClientPreference(requestDescription, verb, requestMessage)) { if (effectiveMaxResponseVersion >= VersionUtil.Version4Dot0) { this.annotationFilter = requestMessage.PreferHeader().AnnotationFilter; } }
internal static object ReadFromStream( #endif Type type, object defaultValue, IEdmModel model, ODataVersion version, Uri baseAddress, IWebApiRequestMessage internalRequest, Func <IODataRequestMessage> getODataRequestMessage, Func <IEdmTypeReference, ODataDeserializer> getEdmTypeDeserializer, Func <Type, ODataDeserializer> getODataPayloadDeserializer, Func <ODataDeserializerContext> getODataDeserializerContext, Action <IDisposable> registerForDisposeAction, Action <Exception> logErrorAction) { object result; IEdmTypeReference expectedPayloadType; ODataDeserializer deserializer = GetDeserializer(type, internalRequest.Context.Path, model, getEdmTypeDeserializer, getODataPayloadDeserializer, out expectedPayloadType); if (deserializer == null) { throw Error.Argument("type", SRResources.FormatterReadIsNotSupportedForType, type.FullName, typeof(ODataInputFormatterHelper).FullName); } try { ODataMessageReaderSettings oDataReaderSettings = internalRequest.ReaderSettings; oDataReaderSettings.BaseUri = baseAddress; oDataReaderSettings.Validations = oDataReaderSettings.Validations & ~ValidationKinds.ThrowOnUndeclaredPropertyForNonOpenType; oDataReaderSettings.Version = version; IODataRequestMessage oDataRequestMessage = getODataRequestMessage(); string preferHeader = RequestPreferenceHelpers.GetRequestPreferHeader(internalRequest.Headers); string annotationFilter = null; if (!String.IsNullOrEmpty(preferHeader)) { oDataRequestMessage.SetHeader(RequestPreferenceHelpers.PreferHeaderName, preferHeader); annotationFilter = oDataRequestMessage.PreferHeader().AnnotationFilter; } if (annotationFilter != null) { oDataReaderSettings.ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter(annotationFilter); } ODataMessageReader oDataMessageReader = new ODataMessageReader(oDataRequestMessage, oDataReaderSettings, model); registerForDisposeAction(oDataMessageReader); ODataPath path = internalRequest.Context.Path; ODataDeserializerContext readContext = getODataDeserializerContext(); readContext.Path = path; readContext.Model = model; readContext.ResourceType = type; readContext.ResourceEdmType = expectedPayloadType; #if NETCORE result = await deserializer.ReadAsync(oDataMessageReader, type, readContext); #else result = deserializer.Read(oDataMessageReader, type, readContext); #endif } catch (Exception e) { logErrorAction(e); result = defaultValue; } return(result); }
/// <summary> /// Interprets the client preference for having a response body. /// </summary> /// <param name="requestDescription">The request description.</param> /// <param name="verb">The request verb.</param> /// <param name="requestMessage">The request message.</param> /// <returns>An enum representation of the client's preference.</returns> private static ResponseBodyPreference InterpretClientPreference(RequestDescription requestDescription, HttpVerbs verb, IODataRequestMessage requestMessage) { Debug.Assert(requestDescription != null, "requestDescription != null"); // If no responseBodyPreference given, we have default behavior of producing content for POST and not producing it for PUT/PATCH. // If responseBodyPreference is given we honor the responseBodyPreference only if the request was for an entity and following conditions are true: // This is not a service operation invoke // DSV was set to 3.0 and above // Server is configured to be >= 3.0 if (requestDescription.LinkUri || requestDescription.SegmentInfos[0].TargetSource == RequestTargetSource.ServiceOperation || requestDescription.RequestVersion < VersionUtil.Version4Dot0) { return ResponseBodyPreference.None; } if ((verb.IsInsert()) || ((verb.IsUpdate()) && (requestDescription.TargetKind == RequestTargetKind.Resource || requestDescription.IsRequestForNonEntityProperty))) { if (requestMessage.PreferHeader().ReturnContent.HasValue) { return requestMessage.PreferHeader().ReturnContent.Value ? ResponseBodyPreference.Content : ResponseBodyPreference.NoContent; } } // TODO: move logic for when/when-not-to write a response body here and remove all checks for 'none' elsewhere return ResponseBodyPreference.None; }