internal ODataMessageReaderDeserializer(bool update, IDataService dataService, UpdateTracker tracker, RequestDescription requestDescription, bool enableWcfDataServicesServerBehavior) : base(update, dataService, tracker, requestDescription)
 {
     System.Data.Services.ODataRequestMessage requestMessage = new System.Data.Services.ODataRequestMessage(dataService.OperationContext.Host);
     if (WebUtil.CompareMimeType(requestMessage.ContentType, "*/*"))
     {
         requestMessage.ContentType = "application/atom+xml";
     }
     this.messageReader = new ODataMessageReader(requestMessage, WebUtil.CreateMessageReaderSettings(dataService, enableWcfDataServicesServerBehavior), dataService.Provider.GetMetadataModel(base.Service.OperationContext));
     this.contentFormat = System.Data.Services.ContentFormat.Unknown;
 }
示例#2
0
        /// <summary>
        /// Gets the XmlReader for the m:properties element under the atom:content element
        /// </summary>
        /// <param name="item">item to read from</param>
        /// <returns>XmlReader for the m:properties element if found, null otherwise.</returns>
        private static XmlReader GetPropertiesReaderFromContent(SyndicationItem item)
        {
            XmlSyndicationContent itemContent = item.Content as XmlSyndicationContent;
            XmlReader             reader      = null;

            if (itemContent != null)
            {
                string contentType = itemContent.Type;
                if (!WebUtil.CompareMimeType(contentType, XmlConstants.MimeApplicationXml))
                {
                    throw DataServiceException.CreateBadRequestError(
                              Strings.Syndication_EntryContentTypeUnsupported(contentType));
                }

                bool shouldDispose = false;
                try
                {
                    reader = itemContent.GetReaderAtContent();
                    WebUtil.XmlReaderEnsureElement(reader);
                    Debug.Assert(
                        reader.NodeType == XmlNodeType.Element,
                        reader.NodeType.ToString() + " == XmlNodeType.Element -- otherwise XmlSyndicationContent didn't see a 'content' tag");

                    reader.ReadStartElement(XmlConstants.AtomContentElementName, XmlConstants.AtomNamespace);
                    if (!reader.IsStartElement(XmlConstants.AtomPropertiesElementName, XmlConstants.DataWebMetadataNamespace))
                    {
                        shouldDispose = true;
                    }
                }
                catch
                {
                    shouldDispose = true;
                    throw;
                }
                finally
                {
                    if (shouldDispose)
                    {
                        WebUtil.Dispose(reader);
                        reader = null;
                    }
                }
            }

            return(reader);
        }
示例#3
0
            /// <summary>Gets a number of non-* matching types, or -1 if not matching at all.</summary>
            /// <param name="candidate">Candidate MIME type to match.</param>
            /// <returns>The number of non-* matching types, or -1 if not matching at all.</returns>
            internal int GetMatchingParts(string candidate)
            {
                Debug.Assert(candidate != null, "candidate must not be null.");

                int result = -1;

                if (candidate.Length > 0)
                {
                    if (this.type == "*")
                    {
                        result = 0;
                    }
                    else
                    {
                        int separatorIdx = candidate.IndexOf('/');
                        if (separatorIdx >= 0)
                        {
                            string candidateType = candidate.Substring(0, separatorIdx);
                            if (WebUtil.CompareMimeType(this.type, candidateType))
                            {
                                if (this.subType == "*")
                                {
                                    result = 1;
                                }
                                else
                                {
                                    string candidateSubType = candidate.Substring(candidateType.Length + 1);
                                    if (WebUtil.CompareMimeType(this.subType, candidateSubType))
                                    {
                                        result = 2;
                                    }
                                }
                            }
                        }
                    }
                }

                return(result);
            }
示例#4
0
        /// <summary>Gets the appropriate MIME type for the request, throwing if there is none.</summary>
        /// <param name='acceptTypesText'>Text as it appears in an HTTP Accepts header.</param>
        /// <param name='exactContentType'>Preferred content type to match if an exact media type is given - this is in descending order of preference.</param>
        /// <param name='inexactContentType'>Preferred fallback content type for inexact matches.</param>
        /// <returns>One of exactContentType or inexactContentType.</returns>
        internal static string SelectRequiredMimeType(
            string acceptTypesText,
            string[] exactContentType,
            string inexactContentType)
        {
            Debug.Assert(exactContentType != null && exactContentType.Length != 0, "exactContentType != null && exactContentType.Length != 0");
            Debug.Assert(inexactContentType != null, "inexactContentType != null");

            string selectedContentType   = null;
            int    selectedMatchingParts = -1;
            int    selectedQualityValue  = 0;
            bool   acceptable            = false;
            bool   acceptTypesEmpty      = true;
            bool   foundExactMatch       = false;

            if (!String.IsNullOrEmpty(acceptTypesText))
            {
                IEnumerable <MediaType> acceptTypes = MimeTypesFromAcceptHeader(acceptTypesText);
                foreach (MediaType acceptType in acceptTypes)
                {
                    acceptTypesEmpty = false;
                    for (int i = 0; i < exactContentType.Length; i++)
                    {
                        if (WebUtil.CompareMimeType(acceptType.MimeType, exactContentType[i]))
                        {
                            selectedContentType  = exactContentType[i];
                            selectedQualityValue = acceptType.SelectQualityValue();
                            acceptable           = selectedQualityValue != 0;
                            foundExactMatch      = true;
                            break;
                        }
                    }

                    if (foundExactMatch)
                    {
                        break;
                    }

                    int matchingParts = acceptType.GetMatchingParts(inexactContentType);
                    if (matchingParts < 0)
                    {
                        continue;
                    }

                    if (matchingParts > selectedMatchingParts)
                    {
                        // A more specific type wins.
                        selectedContentType   = inexactContentType;
                        selectedMatchingParts = matchingParts;
                        selectedQualityValue  = acceptType.SelectQualityValue();
                        acceptable            = selectedQualityValue != 0;
                    }
                    else if (matchingParts == selectedMatchingParts)
                    {
                        // A type with a higher q-value wins.
                        int candidateQualityValue = acceptType.SelectQualityValue();
                        if (candidateQualityValue > selectedQualityValue)
                        {
                            selectedContentType  = inexactContentType;
                            selectedQualityValue = candidateQualityValue;
                            acceptable           = selectedQualityValue != 0;
                        }
                    }
                }
            }

            if (!acceptable && !acceptTypesEmpty)
            {
                throw Error.HttpHeaderFailure(415, Strings.DataServiceException_UnsupportedMediaType);
            }

            if (acceptTypesEmpty)
            {
                Debug.Assert(selectedContentType == null, "selectedContentType == null - otherwise accept types were not empty");
                selectedContentType = inexactContentType;
            }

            Debug.Assert(selectedContentType != null, "selectedContentType != null - otherwise no selection was made");
            return(selectedContentType);
        }