Skip to content

Commit 7b415fe

Browse files
marko-bekhtagsmet
authored andcommitted
Ignore malformed language ranges when resolving locales for validation
(cherry picked from commit 557058a)
1 parent 9481932 commit 7b415fe

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/locale/AbstractLocaleResolver.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@
77

88
import org.hibernate.validator.spi.messageinterpolation.LocaleResolver;
99
import org.hibernate.validator.spi.messageinterpolation.LocaleResolverContext;
10+
import org.jboss.logging.Logger;
1011

1112
abstract class AbstractLocaleResolver implements LocaleResolver {
1213

14+
private static final Logger log = Logger.getLogger(AbstractLocaleResolver.class);
15+
private static final String ACCEPT_HEADER = "Accept-Language";
16+
1317
protected abstract Map<String, List<String>> getHeaders();
1418

1519
@Override
1620
public Locale resolve(LocaleResolverContext context) {
1721
Optional<List<Locale.LanguageRange>> localePriorities = getAcceptableLanguages();
18-
if (!localePriorities.isPresent()) {
22+
if (localePriorities.isEmpty()) {
1923
return null;
2024
}
21-
2225
List<Locale> resolvedLocales = Locale.filter(localePriorities.get(), context.getSupportedLocales());
23-
if (resolvedLocales.size() > 0) {
26+
if (!resolvedLocales.isEmpty()) {
2427
return resolvedLocales.get(0);
2528
}
2629

@@ -30,9 +33,17 @@ public Locale resolve(LocaleResolverContext context) {
3033
private Optional<List<Locale.LanguageRange>> getAcceptableLanguages() {
3134
Map<String, List<String>> httpHeaders = getHeaders();
3235
if (httpHeaders != null) {
33-
List<String> acceptLanguageList = httpHeaders.get("Accept-Language");
36+
List<String> acceptLanguageList = httpHeaders.get(ACCEPT_HEADER);
3437
if (acceptLanguageList != null && !acceptLanguageList.isEmpty()) {
35-
return Optional.of(Locale.LanguageRange.parse(acceptLanguageList.get(0)));
38+
try {
39+
return Optional.of(Locale.LanguageRange.parse(acceptLanguageList.get(0)));
40+
} catch (IllegalArgumentException e) {
41+
// this can happen when parsing malformed locale range string
42+
if (log.isDebugEnabled()) {
43+
log.debug("Unable to parse the \"Accept-Language\" header. \"" + acceptLanguageList.get(0)
44+
+ "\" is not a valid language range string.", e);
45+
}
46+
}
3647
}
3748
}
3849

extensions/smallrye-graphql/runtime/src/main/java/io/quarkus/smallrye/graphql/runtime/SmallRyeGraphQLLocaleResolver.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import org.hibernate.validator.spi.messageinterpolation.LocaleResolver;
1111
import org.hibernate.validator.spi.messageinterpolation.LocaleResolverContext;
12+
import org.jboss.logging.Logger;
1213

1314
import graphql.schema.DataFetchingEnvironment;
1415
import io.smallrye.graphql.execution.context.SmallRyeContext;
@@ -20,12 +21,13 @@
2021
@Singleton
2122
public class SmallRyeGraphQLLocaleResolver implements LocaleResolver {
2223

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

2527
@Override
2628
public Locale resolve(LocaleResolverContext context) {
2729
Optional<List<Locale.LanguageRange>> localePriorities = getAcceptableLanguages();
28-
if (!localePriorities.isPresent()) {
30+
if (localePriorities.isEmpty()) {
2931
return null;
3032
}
3133
List<Locale> resolvedLocales = Locale.filter(localePriorities.get(), context.getSupportedLocales());
@@ -41,7 +43,15 @@ private Optional<List<Locale.LanguageRange>> getAcceptableLanguages() {
4143
if (httpHeaders != null) {
4244
List<String> acceptLanguageList = httpHeaders.get(ACCEPT_HEADER);
4345
if (acceptLanguageList != null && !acceptLanguageList.isEmpty()) {
44-
return Optional.of(Locale.LanguageRange.parse(acceptLanguageList.get(0)));
46+
try {
47+
return Optional.of(Locale.LanguageRange.parse(acceptLanguageList.get(0)));
48+
} catch (IllegalArgumentException e) {
49+
// this can happen when parsing malformed locale range string
50+
if (log.isDebugEnabled()) {
51+
log.debug("Unable to parse the \"Accept-Language\" header. \"" + acceptLanguageList.get(0)
52+
+ "\" is not a valid language range string.", e);
53+
}
54+
}
4555
}
4656
}
4757
return Optional.empty();

integration-tests/hibernate-validator/src/test/java/io/quarkus/it/hibernate/validator/HibernateValidatorFunctionalityTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,16 @@ public void testValidationMessageLocale() {
367367
.body(containsString("Vrijednost ne zadovoljava uzorak"));
368368
}
369369

370+
@Test
371+
public void testValidationMessageInvalidAcceptLanguageHeaderLeadsToDefaultLocaleBeingUsed() {
372+
RestAssured.given()
373+
.header("Accept-Language", "invalid string")
374+
.when()
375+
.get("/hibernate-validator/test/test-validation-message-locale/1")
376+
.then()
377+
.body(containsString("Value is not in line with the pattern"));
378+
}
379+
370380
@Test
371381
public void testValidationMessageDefaultLocale() {
372382
RestAssured.given()

0 commit comments

Comments
 (0)