/// <summary>Disallows requests that would like the response in json format.</summary>
        /// <param name="requestKind">Type of request.</param>
        /// <param name="acceptHeader">Accept header value.</param>
        /// <returns>True if request is accepting json response.</returns>
        internal static bool IsJsonRequest(RequestKind requestKind, string acceptHeader)
        {
            string mimeType = null;

            switch (requestKind)
            {
            case RequestKind.ServiceDocument:
                mimeType = HttpProcessUtils.SelectRequiredMimeType(
                    acceptHeader,
                    HttpProcessUtils.ServiceDocumentMimeTypes,
                    HttpProcessUtils.MimeApplicationXml);
                break;

            case RequestKind.MetadataDocument:
                mimeType = HttpProcessUtils.SelectRequiredMimeType(
                    acceptHeader,
                    HttpProcessUtils.MetadataDocumentMimeTypes,
                    HttpProcessUtils.MimeApplicationXml);
                break;

            case RequestKind.ResourceSet:
                mimeType = HttpProcessUtils.SelectRequiredMimeType(
                    acceptHeader,
                    HttpProcessUtils.ResourceSetMimeTypes,
                    HttpProcessUtils.MimeApplicationAtom);
                break;

            case RequestKind.ServiceOperation:
                mimeType = HttpProcessUtils.SelectRequiredMimeType(
                    acceptHeader,
                    HttpProcessUtils.ServiceOperationMimeTypes,
                    HttpProcessUtils.MimeApplicationXml);
                break;

            default:
                Debug.Assert(false, "Must never receive any other kind of request.");
                break;
            }

            return(HttpProcessUtils.CompareMimeType(mimeType, HttpProcessUtils.MimeApplicationJson));
        }
            /// <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 (HttpProcessUtils.CompareMimeType(this.type, candidateType))
                            {
                                if (this.subType == "*")
                                {
                                    result = 1;
                                }
                                else
                                {
                                    string candidateSubType = candidate.Substring(candidateType.Length + 1);
                                    if (HttpProcessUtils.CompareMimeType(this.subType, candidateSubType))
                                    {
                                        result = 2;
                                    }
                                }
                            }
                        }
                    }
                }

                return(result);
            }
        /// <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>
        private 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 (HttpProcessUtils.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 new DomainDataServiceException((int)HttpStatusCode.UnsupportedMediaType, Resource.HttpProcessUtility_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);
        }