/// <summary>
        /// Initializes a new instance of the <see cref="ResponseMediaTypeMatch"/> class.
        /// </summary>
        /// <param name="mediaTypeMatch">The <see cref="MediaTypeMatch"/> containing the media type and its quality factor.</param>
        /// <param name="result">The kind of match.</param>
        public ResponseMediaTypeMatch(MediaTypeMatch mediaTypeMatch, ResponseFormatterSelectionResult result)
        {
            Contract.Assert(mediaTypeMatch != null, "mediaTypeMatch cannot be null.");
            Contract.Assert(Enum.IsDefined(typeof(ResponseFormatterSelectionResult), result), "result must be valid ResponseFormatterSelectionResult.");

            ResponseFormatterSelectionResult = result;
            MediaTypeMatch = mediaTypeMatch;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ResponseMediaTypeMatch"/> class.
        /// </summary>
        /// <param name="mediaTypeMatch">The <see cref="MediaTypeMatch"/> containing the media type and its quality factor.</param>
        /// <param name="result">The kind of match.</param>
        public ResponseMediaTypeMatch(MediaTypeMatch mediaTypeMatch, ResponseFormatterSelectionResult result)
        {
            Contract.Assert(mediaTypeMatch != null, "mediaTypeMatch cannot be null.");
            Contract.Assert(Enum.IsDefined(typeof(ResponseFormatterSelectionResult), result), "result must be valid ResponseFormatterSelectionResult.");

            ResponseFormatterSelectionResult = result;
            MediaTypeMatch = mediaTypeMatch;
        }
        internal bool TryMatchSupportedMediaType(HttpRequestMessage request, out MediaTypeMatch mediaTypeMatch)
        {
            Contract.Assert(request != null);

            IEnumerable <MediaTypeWithQualityHeaderValue> acceptMediaTypeValues = SortByQFactor(request.Headers.Accept);

            foreach (MediaTypeHeaderValue acceptMediaTypeValue in acceptMediaTypeValues)
            {
                if (TryMatchSupportedMediaType(acceptMediaTypeValue, out mediaTypeMatch))
                {
                    return(true);
                }
            }

            mediaTypeMatch = null;
            return(false);
        }
        internal bool TryMatchMediaTypeMapping(HttpRequestMessage request, out MediaTypeMatch mediaTypeMatch)
        {
            Contract.Assert(request != null, "request cannot be null.");

            foreach (MediaTypeMapping mapping in MediaTypeMappings)
            {
                // Collection<T> is not protected against null, so avoid them
                double quality;
                if (mapping != null && ((quality = mapping.TryMatchMediaType(request)) > 0.0))
                {
                    mediaTypeMatch = new MediaTypeMatch(mapping.MediaType, quality);
                    return(true);
                }
            }

            mediaTypeMatch = null;
            return(false);
        }
        internal bool TryMatchSupportedMediaType(MediaTypeHeaderValue mediaType, out MediaTypeMatch mediaTypeMatch)
        {
            Contract.Assert(mediaType != null);

            foreach (MediaTypeHeaderValue supportedMediaType in SupportedMediaTypes)
            {
                if (supportedMediaType.IsSubsetOf(mediaType))
                {
                    // If the incoming media type had an associated quality factor, propagate it to the match
                    MediaTypeWithQualityHeaderValue mediaTypeWithQualityHeaderValue = mediaType as MediaTypeWithQualityHeaderValue;
                    double quality = mediaTypeWithQualityHeaderValue != null && mediaTypeWithQualityHeaderValue.Quality.HasValue
                                         ? mediaTypeWithQualityHeaderValue.Quality.Value
                                         : MediaTypeMatch.Match;

                    mediaTypeMatch = new MediaTypeMatch(supportedMediaType, quality);
                    return(true);
                }
            }

            mediaTypeMatch = null;
            return(false);
        }
        internal ResponseMediaTypeMatch SelectResponseMediaType(Type type, HttpRequestMessage request)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (!CanWriteType(type))
            {
                return(null);
            }

            // Determine the best character encoding if we have any registered encoders.
            // Note that it is ok for a formatter not to register any encoders in case it doesn't
            // do any structured reading or writing.
            Encoding characterEncodingMatch = SupportedEncodings.Any() ? SelectResponseCharacterEncoding(request) : null;

            // Determine the best media type
            MediaTypeMatch mediaTypeMatch = null;

            // Match against media type mapping first
            if (TryMatchMediaTypeMapping(request, out mediaTypeMatch))
            {
                mediaTypeMatch.SetEncoding(characterEncodingMatch);
                return(new ResponseMediaTypeMatch(
                           mediaTypeMatch,
                           ResponseFormatterSelectionResult.MatchOnRequestWithMediaTypeMapping));
            }

            // Match against the accept header.
            if (TryMatchSupportedMediaType(request, out mediaTypeMatch))
            {
                mediaTypeMatch.SetEncoding(characterEncodingMatch);
                return(new ResponseMediaTypeMatch(
                           mediaTypeMatch,
                           ResponseFormatterSelectionResult.MatchOnRequestAcceptHeader));
            }

            // Match against request's content type
            HttpContent requestContent = request.Content;

            if (requestContent != null)
            {
                MediaTypeHeaderValue requestContentType = requestContent.Headers.ContentType;
                if (requestContentType != null && TryMatchSupportedMediaType(requestContentType, out mediaTypeMatch))
                {
                    mediaTypeMatch.SetEncoding(characterEncodingMatch);
                    return(new ResponseMediaTypeMatch(
                               mediaTypeMatch,
                               ResponseFormatterSelectionResult.MatchOnRequestContentType));
                }
            }

            // No match at all.
            // Pick the first supported media type and indicate we've matched only on type
            MediaTypeHeaderValue mediaType = SupportedMediaTypes.FirstOrDefault();

            mediaTypeMatch = new MediaTypeMatch(mediaType);
            mediaTypeMatch.SetEncoding(characterEncodingMatch);
            return(new ResponseMediaTypeMatch(
                       mediaTypeMatch,
                       ResponseFormatterSelectionResult.MatchOnCanWriteType));
        }
Exemplo n.º 7
0
        internal bool TryMatchSupportedMediaType(IEnumerable <MediaTypeHeaderValue> mediaTypes, out MediaTypeMatch mediaTypeMatch)
        {
            Contract.Assert(mediaTypes != null, "mediaTypes cannot be null.");
            foreach (MediaTypeHeaderValue mediaType in mediaTypes)
            {
                if (TryMatchSupportedMediaType(mediaType, out mediaTypeMatch))
                {
                    return(true);
                }
            }

            mediaTypeMatch = null;
            return(false);
        }