Skip to content

Commit

Permalink
Ignore malformed language ranges when resolving locales for validation
Browse files Browse the repository at this point in the history
(cherry picked from commit 557058a)
  • Loading branch information
marko-bekhta authored and gsmet committed May 9, 2023
1 parent 9481932 commit 7b415fe
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@

import org.hibernate.validator.spi.messageinterpolation.LocaleResolver;
import org.hibernate.validator.spi.messageinterpolation.LocaleResolverContext;
import org.jboss.logging.Logger;

abstract class AbstractLocaleResolver implements LocaleResolver {

private static final Logger log = Logger.getLogger(AbstractLocaleResolver.class);
private static final String ACCEPT_HEADER = "Accept-Language";

protected abstract Map<String, List<String>> getHeaders();

@Override
public Locale resolve(LocaleResolverContext context) {
Optional<List<Locale.LanguageRange>> localePriorities = getAcceptableLanguages();
if (!localePriorities.isPresent()) {
if (localePriorities.isEmpty()) {
return null;
}

List<Locale> resolvedLocales = Locale.filter(localePriorities.get(), context.getSupportedLocales());
if (resolvedLocales.size() > 0) {
if (!resolvedLocales.isEmpty()) {
return resolvedLocales.get(0);
}

Expand All @@ -30,9 +33,17 @@ public Locale resolve(LocaleResolverContext context) {
private Optional<List<Locale.LanguageRange>> getAcceptableLanguages() {
Map<String, List<String>> httpHeaders = getHeaders();
if (httpHeaders != null) {
List<String> acceptLanguageList = httpHeaders.get("Accept-Language");
List<String> acceptLanguageList = httpHeaders.get(ACCEPT_HEADER);
if (acceptLanguageList != null && !acceptLanguageList.isEmpty()) {
return Optional.of(Locale.LanguageRange.parse(acceptLanguageList.get(0)));
try {
return Optional.of(Locale.LanguageRange.parse(acceptLanguageList.get(0)));
} catch (IllegalArgumentException e) {
// this can happen when parsing malformed locale range string
if (log.isDebugEnabled()) {
log.debug("Unable to parse the \"Accept-Language\" header. \"" + acceptLanguageList.get(0)
+ "\" is not a valid language range string.", e);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.hibernate.validator.spi.messageinterpolation.LocaleResolver;
import org.hibernate.validator.spi.messageinterpolation.LocaleResolverContext;
import org.jboss.logging.Logger;

import graphql.schema.DataFetchingEnvironment;
import io.smallrye.graphql.execution.context.SmallRyeContext;
Expand All @@ -20,12 +21,13 @@
@Singleton
public class SmallRyeGraphQLLocaleResolver implements LocaleResolver {

private static final Logger log = Logger.getLogger(SmallRyeGraphQLLocaleResolver.class);
private static final String ACCEPT_HEADER = "Accept-Language";

@Override
public Locale resolve(LocaleResolverContext context) {
Optional<List<Locale.LanguageRange>> localePriorities = getAcceptableLanguages();
if (!localePriorities.isPresent()) {
if (localePriorities.isEmpty()) {
return null;
}
List<Locale> resolvedLocales = Locale.filter(localePriorities.get(), context.getSupportedLocales());
Expand All @@ -41,7 +43,15 @@ private Optional<List<Locale.LanguageRange>> getAcceptableLanguages() {
if (httpHeaders != null) {
List<String> acceptLanguageList = httpHeaders.get(ACCEPT_HEADER);
if (acceptLanguageList != null && !acceptLanguageList.isEmpty()) {
return Optional.of(Locale.LanguageRange.parse(acceptLanguageList.get(0)));
try {
return Optional.of(Locale.LanguageRange.parse(acceptLanguageList.get(0)));
} catch (IllegalArgumentException e) {
// this can happen when parsing malformed locale range string
if (log.isDebugEnabled()) {
log.debug("Unable to parse the \"Accept-Language\" header. \"" + acceptLanguageList.get(0)
+ "\" is not a valid language range string.", e);
}
}
}
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,16 @@ public void testValidationMessageLocale() {
.body(containsString("Vrijednost ne zadovoljava uzorak"));
}

@Test
public void testValidationMessageInvalidAcceptLanguageHeaderLeadsToDefaultLocaleBeingUsed() {
RestAssured.given()
.header("Accept-Language", "invalid string")
.when()
.get("/hibernate-validator/test/test-validation-message-locale/1")
.then()
.body(containsString("Value is not in line with the pattern"));
}

@Test
public void testValidationMessageDefaultLocale() {
RestAssured.given()
Expand Down

0 comments on commit 7b415fe

Please sign in to comment.