public Func <Stream, Stream> GetEncoder(
            HttpHeaderValueCollection <StringWithQualityHeaderValue> list)
        {
            if (list != null && list.Count > 0)
            {
                var headerValue = list.OrderByDescending(e => e.Quality ?? 1.0D)
                                  .Where(e => !e.Quality.HasValue ||
                                         e.Quality.Value > 0.0D)
                                  .FirstOrDefault(e => supported.Keys
                                                  .Contains(e.Value, StringComparer.OrdinalIgnoreCase));

                // Case 1: We can support what client has asked for
                if (headerValue != null)
                {
                    return(GetStreamForSchema(headerValue.Value));
                }


                // Case 2: Client is okay to accept any thing we support except
                // the ones explicitly specified as not preferred by setting q=0
                if (list.Any(e => e.Value == "*" &&
                             (!e.Quality.HasValue || e.Quality.Value > 0.0D)))
                {
                    var encoding = supported.Keys.Where(se =>
                                                        !list.Any(e =>
                                                                  e.Value.Equals(se, StringComparison.OrdinalIgnoreCase) &&
                                                                  e.Quality.HasValue &&
                                                                  e.Quality.Value == 0.0D))
                                   .FirstOrDefault();
                    if (encoding != null)
                    {
                        return(GetStreamForSchema(encoding));
                    }
                }

                // Case 3: Client specifically refusing identity
                if (list.Any(e => e.Value.Equals(IDENTITY, StringComparison.OrdinalIgnoreCase) &&
                             e.Quality.HasValue && e.Quality.Value == 0.0D))
                {
                    throw new NegotiationFailedException();
                }

                // Case 4: Client is not willing to accept one of the encodings
                // we support and is not willing to accept identity
                if (list.Any(e => e.Value == "*" &&
                             (e.Quality.HasValue || e.Quality.Value == 0.0D)))
                {
                    if (!list.Any(e => e.Value.Equals(IDENTITY, StringComparison.OrdinalIgnoreCase)))
                    {
                        throw new NegotiationFailedException();
                    }
                }
            }

            // Settle for the default, which is no transformation whatsoever
            return(null);
        }
 private static IEnumerable <MediaTypeWithQualityHeaderValue> SortByQFactor(HttpHeaderValueCollection <MediaTypeWithQualityHeaderValue> acceptHeaders)
 {
     if (acceptHeaders.Count > 1)
     {
         // Sort accept headers (if more than 1) in descending order based on q factor
         // Use OrderBy() instead of Array.Sort() as it performs fewer comparisons. In this case the comparisons
         // are quite expensive so OrderBy() performs better.
         return(acceptHeaders.OrderByDescending(m => m, MediaTypeWithQualityHeaderValueComparer.QualityComparer));
     }
     else
     {
         return(acceptHeaders);
     }
 }
Exemple #3
0
        public Func <Stream, Stream> GetEncoder(HttpHeaderValueCollection <StringWithQualityHeaderValue> list)
        {
            if (list != null && list.Count() > 0)
            {
                var headerValue = list.OrderByDescending(e => e.Quality ?? 1.0D)
                                  .Where(e => !e.Quality.HasValue ||
                                         e.Quality.Value > 0.0D)
                                  .FirstOrDefault(e => supported.Keys
                                                  .Contains(e.Value, StringComparer.OrdinalIgnoreCase));

                if (headerValue != null)
                {
                    return(GetStreamForSchema(headerValue.Value));
                }

                if (list.Any(e => e.Value == "*" &&
                             (!e.Quality.HasValue || e.Quality.Value > 0.0D)))
                {
                    var encoding = supported.Keys.Where(se =>
                                                        !list.Any(e =>
                                                                  e.Value.Equals(se, StringComparison.OrdinalIgnoreCase) &&
                                                                  e.Quality.HasValue &&
                                                                  e.Quality.Value == 0.0D))
                                   .FirstOrDefault();
                    if (encoding != null)
                    {
                        return(GetStreamForSchema(encoding));
                    }
                }

                if (list.Any(e => e.Value.Equals(IDENTITY, StringComparison.OrdinalIgnoreCase) &&
                             e.Quality.HasValue && e.Quality.Value == 0.0D))
                {
                    throw new NegotiationFailedException();
                }

                // Case 4: Client is not willing to accept one of the encodings
                // we support and is not willing to accept identity
                if (list.Any(e => e.Value == "*" &&
                             (e.Quality.HasValue || e.Quality.Value == 0.0D)))
                {
                    if (!list.Any(e => e.Value.Equals(IDENTITY, StringComparison.OrdinalIgnoreCase)))
                    {
                        throw new NegotiationFailedException();
                    }
                }
            }
            return(null);
        }
        internal static Task <HttpResponseMessage> CreateODataBatchResponseAsync(this HttpRequestMessage request, IEnumerable <ODataBatchResponseItem> responses, ODataMessageQuotas messageQuotas)
        {
            Contract.Assert(request != null);

            ODataVersion               odataVersion     = ResultHelpers.GetODataResponseVersion(request);
            IServiceProvider           requestContainer = request.GetRequestContainer();
            ODataMessageWriterSettings writerSettings   =
                requestContainer.GetRequiredService <ODataMessageWriterSettings>();

            writerSettings.Version       = odataVersion;
            writerSettings.MessageQuotas = messageQuotas;
            HttpHeaderValueCollection <MediaTypeWithQualityHeaderValue> acceptHeaderValues = request.Headers.Accept;

            MediaTypeHeaderValue responseContentType = null;

            foreach (MediaTypeWithQualityHeaderValue acceptHeader in acceptHeaderValues.OrderByDescending(h => h.Quality == null ? 1 : h.Quality))
            {
                if (acceptHeader.MediaType.Equals(ODataBatchHttpRequestMessageExtensions.BatchMediaTypeMime, StringComparison.OrdinalIgnoreCase))
                {
                    responseContentType = MediaTypeHeaderValue.Parse(
                        String.Format(CultureInfo.InvariantCulture, "multipart/mixed;boundary=batchresponse_{0}",
                                      Guid.NewGuid()));
                    break;
                }
                else if (acceptHeader.MediaType.Equals(ODataBatchHttpRequestMessageExtensions.BatchMediaTypeJson, StringComparison.OrdinalIgnoreCase))
                {
                    responseContentType = MediaTypeHeaderValue.Parse(ODataBatchHttpRequestMessageExtensions.BatchMediaTypeJson);
                    break;
                }
            }

            if (responseContentType == null)
            {
                // In absence of accept, if request was JSON then default response to be JSON.
                // Note that, if responseContentType is not set, then it will default to multipart/mixed
                // when constructing the BatchContent, so we don't need to handle that case here
                if (request.Content != null && request.Content.Headers.Any(h => String.Equals(h.Key, ODataBatchHttpRequestMessageExtensions.ContentType, StringComparison.OrdinalIgnoreCase) &&
                                                                           h.Value.Any(v => v.IndexOf(ODataBatchHttpRequestMessageExtensions.BatchMediaTypeJson, StringComparison.OrdinalIgnoreCase) > -1)))
                {
                    responseContentType = MediaTypeHeaderValue.Parse(ODataBatchHttpRequestMessageExtensions.BatchMediaTypeJson);
                }
            }

            HttpResponseMessage response = request.CreateResponse(HttpStatusCode.OK);

            response.Content = new ODataBatchContent(responses, requestContainer, responseContentType);
            return(Task.FromResult(response));
        }
        public Func<Stream, Stream> GetEncoder(HttpHeaderValueCollection<StringWithQualityHeaderValue> list)
        {
            // The following steps will walk you through
            // completing the implementation of this method
            if (list != null && list.Count > 0)
            {
                // More code goes here
                var headerValue = list.OrderByDescending(e => e.Quality ?? 1.0D).Where(e => !e.Quality.HasValue || e.Quality.Value > 0.0D).FirstOrDefault(e => supported.Keys.Contains(e.Value, StringComparer.OrdinalIgnoreCase));
                // Case 1: We can support what client has asked for
                if (headerValue != null)
                    return GetStreamForSchema(headerValue.Value);
                // Case 2: Client will accept anything we support except
                // the ones explicitly specified as not preferred by setting q=0
                if (list.Any(e => e.Value == "*" &&
                (!e.Quality.HasValue || e.Quality.Value > 0.0D)))
                {
                    var encoding = supported.Keys.Where(se =>
                    !list.Any(e =>
                    e.Value.Equals(se, StringComparison.OrdinalIgnoreCase) &&
                    e.Quality.HasValue &&
                    e.Quality.Value == 0.0D))
                    .FirstOrDefault();
                    if (encoding != null)
                        return GetStreamForSchema(encoding);
                }

                // Case 3: Client specifically refusing identity
                if (list.Any(e => e.Value.Equals(IDENTITY, StringComparison.OrdinalIgnoreCase) &&
                e.Quality.HasValue && e.Quality.Value == 0.0D))
                {
                    throw new NegotiationFailedException();
                }
                // Case 4: Client is not willing to accept any of the encodings
                // we support and is not willing to accept identity
                if (list.Any(e => e.Value == "*" &&
                (e.Quality.HasValue || e.Quality.Value == 0.0D)))
                {
                    if (!list.Any(e => e.Value.Equals(IDENTITY, StringComparison.OrdinalIgnoreCase)))
                        throw new NegotiationFailedException();
                }
            }
            // Settle for the default, which is no transformation whatsoever
            return null;
        }
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            HttpHeaderValueCollection <StringWithQualityHeaderValue> acceptedLanguages = request.Headers.AcceptLanguage;

            if (acceptedLanguages.IsNotNullOrEmpty())
            {
                StringWithQualityHeaderValue headerValue = acceptedLanguages.OrderByDescending(e => e.Quality ?? 1.0D)
                                                           .Where(e => !e.Quality.HasValue || e.Quality.Value > 0.0D)
                                                           .FirstOrDefault(e => supportedCultures.Contains(e.Value, StringComparer.OrdinalIgnoreCase));

                string culture = null;
                // Case 1: We can support what client has asked for
                if (headerValue.IsNotNull())
                {
                    culture = headerValue.Value;
                }
                // Case 2: Client is okay to accept any thing we support except
                // the ones explicitly specified as not preferred by setting q=0
                else if (acceptedLanguages.Any(e => e.Value == "*" && (!e.Quality.HasValue || e.Quality.Value > 0.0D)))
                {
                    culture = supportedCultures.Where(sc =>
                                                      !acceptedLanguages.Any(e =>
                                                                             e.Value.Equals(sc, StringComparison.OrdinalIgnoreCase) &&
                                                                             e.Quality.HasValue &&
                                                                             e.Quality.Value == 0.0D))
                              .FirstOrDefault();
                }

                if (culture.IsNotNull())
                {
                    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture);
                    Thread.CurrentThread.CurrentCulture   = Thread.CurrentThread.CurrentUICulture;
                }
            }
            return(await base.SendAsync(request, cancellationToken));
        }