Skip to content

Commit

Permalink
Merge pull request #4094 from jamezp/RESTEASY-3471-6.2
Browse files Browse the repository at this point in the history
[RESTEASY-3471] Use the LocaleHelper where appropriate and allow the …
  • Loading branch information
jamezp committed Mar 19, 2024
2 parents e63e25b + 3c6319d commit cc0a115
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jboss.resteasy.util.CaseInsensitiveMap;
import org.jboss.resteasy.util.DateUtil;
import org.jboss.resteasy.util.HeaderHelper;
import org.jboss.resteasy.util.LocaleHelper;
import org.jboss.resteasy.util.MediaTypeHelper;
import org.jboss.resteasy.util.WeightedLanguage;

Expand Down Expand Up @@ -59,7 +60,7 @@ public void setLanguage(Locale language) {
}

public void setLanguage(String language) {
setLanguage(new Locale(language));
setLanguage(LocaleHelper.extractLocale(language));
}

public void setMediaType(MediaType mediaType) {
Expand Down Expand Up @@ -199,7 +200,7 @@ public Locale getLanguage() {
return null;
if (obj instanceof Locale)
return (Locale) obj;
return new Locale(obj.toString());
return LocaleHelper.extractLocale(obj.toString());
}

public int getLength() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.jboss.resteasy.resteasy_jaxrs.i18n.LogMessages;
import org.jboss.resteasy.resteasy_jaxrs.i18n.Messages;
import org.jboss.resteasy.util.LocaleHelper;

/**
* @author Pascal S. de Kloe
Expand Down Expand Up @@ -94,16 +95,11 @@ public static Map<Locale, QualityValue> getLocaleQualityValues(String header) {
String value = entry.getKey();
if (value != null) {
int length = value.length();
if (length == 2) {
locale = new Locale(value);
} else if (length == 5 && value.charAt(2) == '-') {
String language = value.substring(0, 2);
String country = value.substring(3, 5);
locale = new Locale(language, country);
} else {
if (length > 5 || (length > 2 && value.charAt(2) != '-')) {
LogMessages.LOGGER.ignoringUnsupportedLocale(value);
continue;
}
locale = LocaleHelper.extractLocale(value);
}
result.put(locale, quality);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.jboss.resteasy.util.CookieParser;
import org.jboss.resteasy.util.DateUtil;
import org.jboss.resteasy.util.LocaleHelper;
import org.jboss.resteasy.util.MediaTypeHelper;
import org.jboss.resteasy.util.WeightedLanguage;

Expand Down Expand Up @@ -153,7 +154,7 @@ public Locale getLanguage() {
String obj = requestHeaders.getFirst(HttpHeaders.CONTENT_LANGUAGE);
if (obj == null)
return null;
return new Locale(obj);
return LocaleHelper.extractLocale(obj);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public static Locale extractLocale(String lang) {
* @return converted language format string
*/
public static String toLanguageString(Locale value) {
return value.toLanguageTag();
// This is somewhat of a hack to fix an issue in the TCK, see https://github.com/jakartaee/rest/issues/1239.
// However, if the Locale was created incorrectly, we should likely continue to return the string value of the
// locale.
String languageTag = value.toLanguageTag();
if ("und".equals(languageTag)) {
languageTag = value.getLanguage().toLowerCase(Locale.ROOT);
if (value.getCountry() != null && !value.getCountry().isBlank()) {
languageTag += "-" + value.getCountry().toLowerCase(Locale.ROOT);
}
}
return languageTag;
}
}

0 comments on commit cc0a115

Please sign in to comment.