From 5ad6ccb4956c23ce919a8bd5357686c5c3870682 Mon Sep 17 00:00:00 2001 From: koplas <54645365+koplas@users.noreply.github.com> Date: Fri, 1 Nov 2024 14:58:21 +0100 Subject: [PATCH 1/2] Fix HttpHeaders.getAcceptableLanguages() on empty headers `jakarta.ws.rs.core.HttpHeaders.getAcceptableLanguages` requires that a wildcard locale is returned, if no acceptable language is specified. The previous behavior returned an empty list. Closes #44253 --- .../org/jboss/resteasy/reactive/common/headers/HeaderUtil.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/headers/HeaderUtil.java b/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/headers/HeaderUtil.java index 15239f7aa1cc5..0324278db92ed 100644 --- a/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/headers/HeaderUtil.java +++ b/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/headers/HeaderUtil.java @@ -32,6 +32,7 @@ */ @SuppressWarnings({ "rawtypes", "unchecked" }) public class HeaderUtil { + private static final List LANGUAGE_WILDCARD = List.of(Locale.ROOT); private static final ClassValue> HEADER_DELEGATE_CACHE = new ClassValue<>() { @Override @@ -293,7 +294,7 @@ public static List getAcceptableMediaTypes(MultivaluedMap getAcceptableLanguages(MultivaluedMap headers) { List accepts = headers.get(HttpHeaders.ACCEPT_LANGUAGE); if (accepts == null || accepts.isEmpty()) - return Collections.emptyList(); + return LANGUAGE_WILDCARD; List languages = new ArrayList(); for (Object obj : accepts) { if (obj instanceof Locale) { From c55d0bef59caad15ae8359554d3025480da7a74c Mon Sep 17 00:00:00 2001 From: koplas <54645365+koplas@users.noreply.github.com> Date: Tue, 5 Nov 2024 13:55:10 +0100 Subject: [PATCH 2/2] Add HeaderUtil.getAcceptableLanguages test and use '*' as wildcard locale --- .../reactive/common/headers/HeaderUtil.java | 2 +- .../common/headers/HeaderUtilTest.java | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 independent-projects/resteasy-reactive/common/runtime/src/test/java/org/jboss/resteasy/reactive/common/headers/HeaderUtilTest.java diff --git a/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/headers/HeaderUtil.java b/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/headers/HeaderUtil.java index 0324278db92ed..4aa5847e58c06 100644 --- a/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/headers/HeaderUtil.java +++ b/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/headers/HeaderUtil.java @@ -32,7 +32,7 @@ */ @SuppressWarnings({ "rawtypes", "unchecked" }) public class HeaderUtil { - private static final List LANGUAGE_WILDCARD = List.of(Locale.ROOT); + private static final List LANGUAGE_WILDCARD = List.of(new Locale("*")); private static final ClassValue> HEADER_DELEGATE_CACHE = new ClassValue<>() { @Override diff --git a/independent-projects/resteasy-reactive/common/runtime/src/test/java/org/jboss/resteasy/reactive/common/headers/HeaderUtilTest.java b/independent-projects/resteasy-reactive/common/runtime/src/test/java/org/jboss/resteasy/reactive/common/headers/HeaderUtilTest.java new file mode 100644 index 0000000000000..663c9ce99e0ac --- /dev/null +++ b/independent-projects/resteasy-reactive/common/runtime/src/test/java/org/jboss/resteasy/reactive/common/headers/HeaderUtilTest.java @@ -0,0 +1,28 @@ +package org.jboss.resteasy.reactive.common.headers; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Locale; + +import org.jboss.resteasy.reactive.common.util.MultivaluedTreeMap; +import org.junit.jupiter.api.Test; + +class HeaderUtilTest { + + @Test + void getAcceptableLanguages() { + MultivaluedTreeMap emptyHeaders = new MultivaluedTreeMap<>(); + Locale[] wildcardLocale = new Locale[] { new Locale("*") }; + assertArrayEquals(wildcardLocale, HeaderUtil.getAcceptableLanguages(emptyHeaders).toArray()); + + MultivaluedTreeMap singleLanguage = new MultivaluedTreeMap<>(); + singleLanguage.add("Accept-Language", "de"); + Locale[] singleLocale = new Locale[] { Locale.GERMAN }; + assertArrayEquals(singleLocale, HeaderUtil.getAcceptableLanguages(singleLanguage).toArray()); + + MultivaluedTreeMap multipleWeightedLanguages = new MultivaluedTreeMap<>(); + multipleWeightedLanguages.add("Accept-Language", "da, en-gb;q=0.8, en;q=0.7"); + Locale[] multipleWeightedLocales = new Locale[] { new Locale("da"), Locale.UK, Locale.ENGLISH }; + assertArrayEquals(multipleWeightedLocales, HeaderUtil.getAcceptableLanguages(multipleWeightedLanguages).toArray()); + } +}