コード例 #1
0
ファイル: ODataMaterializer.cs プロジェクト: pbvs/odata.net
        /// <summary>
        /// Creates an <see cref="ODataMessageReader"/> for a given message and context using
        /// WCF DS client settings.
        /// </summary>
        /// <param name="responseMessage">The response message</param>
        /// <param name="responseInfo">The response context</param>
        /// <param name="payloadKind">Type of the message.</param>
        /// <returns>The message reader.</returns>
        protected static ODataMessageReader CreateODataMessageReader(IODataResponseMessage responseMessage, ResponseInfo responseInfo, ref ODataPayloadKind payloadKind)
        {
            ODataMessageReaderSettings settings = responseInfo.ReadHelper.CreateSettings();

            ODataMessageReader odataMessageReader = responseInfo.ReadHelper.CreateReader(responseMessage, settings);

            if (payloadKind == ODataPayloadKind.Unsupported)
            {
                var payloadKinds = odataMessageReader.DetectPayloadKind().ToList();

                if (payloadKinds.Count == 0)
                {
                    throw DSClient.Error.InvalidOperation(DSClient.Strings.AtomMaterializer_InvalidResponsePayload(XmlConstants.DataWebNamespace));
                }

                // Pick the first payload kind detected by ODataLib and use that to parse the exception.
                // The only exception being payload with entity reference link(s). If one of the payload kinds
                // is reference links, then we need to give preference to reference link payloads.
                ODataPayloadKindDetectionResult detectionResult = payloadKinds.FirstOrDefault(k => k.PayloadKind == ODataPayloadKind.EntityReferenceLink || k.PayloadKind == ODataPayloadKind.EntityReferenceLinks);

                if (detectionResult == null)
                {
                    detectionResult = payloadKinds.First();
                }

                if (detectionResult.Format != ODataFormat.Json && detectionResult.Format != ODataFormat.RawValue)
                {
                    throw DSClient.Error.InvalidOperation(DSClient.Strings.AtomMaterializer_InvalidContentTypeEncountered(responseMessage.GetHeader(XmlConstants.HttpContentType)));
                }

                payloadKind = detectionResult.PayloadKind;
            }

            return(odataMessageReader);
        }
コード例 #2
0
ファイル: ODataMaterializer.cs プロジェクト: tapika/swupd
        protected static ODataMessageReader CreateODataMessageReader(IODataResponseMessage responseMessage, ResponseInfo responseInfo, ref ODataPayloadKind payloadKind)
        {
            ODataMessageReaderSettings settings = responseInfo.ReadHelper.CreateSettings(ReadingEntityInfo.BufferAndCacheEntryPayload);

            ODataMessageReader odataMessageReader = responseInfo.ReadHelper.CreateReader(responseMessage, settings);

            if (payloadKind == ODataPayloadKind.Unsupported)
            {
                var payloadKinds = odataMessageReader.DetectPayloadKind().ToList();

                if (payloadKinds.Count == 0)
                {
                    throw DSClient.Error.InvalidOperation(DSClient.Strings.AtomMaterializer_InvalidResponsePayload(responseInfo.DataNamespace));
                }

                // Pick the first payload kind detected by ODataLib and use that to parse the exception.
                // The only exception being payload with entity reference link(s). If one of the payload kinds
                // is reference links, then we need to give preference to reference link payloads.
                ODataPayloadKindDetectionResult detectionResult = payloadKinds.FirstOrDefault(k => k.PayloadKind == ODataPayloadKind.EntityReferenceLink || k.PayloadKind == ODataPayloadKind.EntityReferenceLinks);

                if (detectionResult == null)
                {
                    ODataVersion dataServiceVersion = responseMessage.GetDataServiceVersion(CommonUtil.ConvertToODataVersion(responseInfo.MaxProtocolVersion));

                    // if its a collection or a Property we should choose collection if its less than V3, this enables older service operations on Execute
                    if (dataServiceVersion < ODataVersion.V3 && payloadKinds.Any(pk => pk.PayloadKind == ODataPayloadKind.Property) && payloadKinds.Any(pk => pk.PayloadKind == ODataPayloadKind.Collection))
                    {
                        detectionResult = payloadKinds.Single(pk => pk.PayloadKind == ODataPayloadKind.Collection);
                    }
                    else
                    {
                        detectionResult = payloadKinds.First();
                    }
                }

                // Astoria client only supports atom, jsonverbose, jsonlight and raw value payloads.
                if (detectionResult.Format != ODataFormat.Atom && detectionResult.Format != ODataFormat.VerboseJson && detectionResult.Format != ODataFormat.Json && detectionResult.Format != ODataFormat.RawValue)
                {
                    throw DSClient.Error.InvalidOperation(DSClient.Strings.AtomMaterializer_InvalidContentTypeEncountered(responseMessage.GetHeader(XmlConstants.HttpContentType)));
                }

                payloadKind = detectionResult.PayloadKind;
            }

            return(odataMessageReader);
        }
コード例 #3
0
        protected static ODataMessageReader CreateODataMessageReader(IODataResponseMessage responseMessage, System.Data.Services.Client.ResponseInfo responseInfo, bool projectionQuery, ref ODataPayloadKind payloadKind)
        {
            Func <ODataEntry, XmlReader, Uri, XmlReader> entryXmlCustomizer = null;

            if (responseInfo.HasReadingEntityHandlers)
            {
                entryXmlCustomizer = new Func <ODataEntry, XmlReader, Uri, XmlReader>(ODataMaterializer.EntryXmlCustomizer);
            }
            ODataMessageReaderSettings settings = WebUtil.CreateODataMessageReaderSettings(responseInfo, entryXmlCustomizer, projectionQuery);
            ODataMessageReader         reader   = new ODataMessageReader(responseMessage, settings, ClientEdmModel.GetModel(responseInfo.MaxProtocolVersion));

            if (payloadKind == ODataPayloadKind.Unsupported)
            {
                List <ODataPayloadKindDetectionResult> source = reader.DetectPayloadKind().ToList <ODataPayloadKindDetectionResult>();
                if (source.Count == 0)
                {
                    throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.AtomMaterializer_InvalidResponsePayload(responseInfo.DataNamespace));
                }
                ODataPayloadKindDetectionResult result = source.FirstOrDefault <ODataPayloadKindDetectionResult>(delegate(ODataPayloadKindDetectionResult k) {
                    if (k.PayloadKind != ODataPayloadKind.EntityReferenceLink)
                    {
                        return(k.PayloadKind == ODataPayloadKind.EntityReferenceLinks);
                    }
                    return(true);
                });
                if (result == null)
                {
                    result = source.First <ODataPayloadKindDetectionResult>();
                }
                if ((result.Format != ODataFormat.Atom) && (result.Format != ODataFormat.RawValue))
                {
                    throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.AtomMaterializer_InvalidContentTypeEncountered(responseMessage.GetHeader("Content-Type")));
                }
                payloadKind = result.PayloadKind;
            }
            return(reader);
        }