예제 #1
0
        public Func <Stream, Stream> GetEncoder(HttpHeaderValueCollection <StringWithQualityHeaderValue> list)
        {
            if (list.IsNotNullOrEmpty())
            {
                StringWithQualityHeaderValue 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.IsNotNull())
                {
                    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)))
                {
                    string 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.IsNotNull())
                    {
                        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);
        }
        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));
        }