ContentNegotiationMiddleware extends AbstractWebMiddleware<HttpRequest, NRES> { @Override public HttpResponse handle(HttpRequest request, MiddlewareChain<HttpRequest, NRES, ?, ?> chain) { String accept = (String) request.getHeaders().getOrDefault("Accept", "*/*"); MediaType mediaType = negotiator.bestAllowedContentType(accept, allowedTypes); String acceptLanguage = (String) request.getHeaders().getOrDefault("Accept-Language", "*"); String lang = negotiator.bestAllowedLanguage(acceptLanguage, allowedLanguages); Locale locale = Objects.equals(lang, "*")? null : some(lang, Locale::forLanguageTag).orElse(null); request = MixinUtils.mixin(request, ContentNegotiable.class); ContentNegotiable.class.cast(request).setMediaType(mediaType); ContentNegotiable.class.cast(request).setLocale(locale); return castToHttpResponse(chain.next(request)); } ContentNegotiationMiddleware(); @Override HttpResponse handle(HttpRequest request, MiddlewareChain<HttpRequest, NRES, ?, ?> chain); void setNegotiator(ContentNegotiator negotiator); void setAllowedTypes(Set<String> allowedTypes); void setAllowedLanguages(Set<String> allowedLanguages);  }
@Test void acceptLanguageWithoutAllowedLanguages() { request = builder(new DefaultHttpRequest()) .set(HttpRequest::setHeaders, Headers.of("Accept-Language", "ja,en;q=0.8,en-US;q=0.6")) .build(); MiddlewareChain<HttpRequest, HttpResponse, ?, ?> chain = new DefaultMiddlewareChain<>(new AnyPredicate<>(), null, (Endpoint<HttpRequest, HttpResponse>) req -> { Optional<Locale> locale = Stream.of(req) .map(ContentNegotiable.class::cast) .map(ContentNegotiable::getLocale) .filter(Objects::nonNull) .findFirst(); assertThat(locale).isNotPresent(); return builder(HttpResponse.of("hello")) .set(HttpResponse::setHeaders, Headers.of("Content-Type", "text/html")) .build(); }); middleware.handle(request, chain); }