diff --git a/extensions/hibernate-validator/deployment/src/main/java/io/quarkus/hibernate/validator/deployment/HibernateValidatorProcessor.java b/extensions/hibernate-validator/deployment/src/main/java/io/quarkus/hibernate/validator/deployment/HibernateValidatorProcessor.java index 237bc8739eccf..61048fcf548c7 100644 --- a/extensions/hibernate-validator/deployment/src/main/java/io/quarkus/hibernate/validator/deployment/HibernateValidatorProcessor.java +++ b/extensions/hibernate-validator/deployment/src/main/java/io/quarkus/hibernate/validator/deployment/HibernateValidatorProcessor.java @@ -75,8 +75,8 @@ import io.quarkus.hibernate.validator.runtime.HibernateValidatorRecorder; import io.quarkus.hibernate.validator.runtime.ValidatorProvider; import io.quarkus.hibernate.validator.runtime.interceptor.MethodValidationInterceptor; -import io.quarkus.hibernate.validator.runtime.jaxrs.ResteasyReactiveViolationExceptionMapper; import io.quarkus.hibernate.validator.runtime.jaxrs.ResteasyConfigSupport; +import io.quarkus.hibernate.validator.runtime.jaxrs.ResteasyReactiveViolationExceptionMapper; import io.quarkus.hibernate.validator.spi.BeanValidationAnnotationsBuildItem; import io.quarkus.resteasy.common.spi.ResteasyConfigBuildItem; import io.quarkus.resteasy.common.spi.ResteasyDotNames; @@ -154,6 +154,8 @@ void registerAdditionalBeans(HibernateValidatorRecorder hibernateValidatorRecord // The CDI interceptor which will validate the methods annotated with @JaxrsEndPointValidated additionalBeans.produce(new AdditionalBeanBuildItem( "io.quarkus.hibernate.validator.runtime.jaxrs.ResteasyReactiveEndPointValidationInterceptor")); + additionalBeans.produce(new AdditionalBeanBuildItem( + "io.quarkus.hibernate.validator.runtime.jaxrs.ResteasyReactiveContextLocaleResolver")); } // A constraint validator with an injection point but no scope is added as @Singleton diff --git a/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/AbstractLocaleResolver.java b/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/AbstractLocaleResolver.java new file mode 100644 index 0000000000000..ac5294f80d52a --- /dev/null +++ b/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/AbstractLocaleResolver.java @@ -0,0 +1,42 @@ +package io.quarkus.hibernate.validator.runtime.jaxrs; + +import java.util.List; +import java.util.Locale; +import java.util.Optional; + +import javax.ws.rs.core.HttpHeaders; + +import org.hibernate.validator.spi.messageinterpolation.LocaleResolver; +import org.hibernate.validator.spi.messageinterpolation.LocaleResolverContext; + +abstract class AbstractLocaleResolver implements LocaleResolver { + + protected abstract HttpHeaders getHeaders(); + + @Override + public Locale resolve(LocaleResolverContext context) { + Optional> localePriorities = getAcceptableLanguages(); + if (!localePriorities.isPresent()) { + return context.getDefaultLocale(); + } + + List resolvedLocales = Locale.filter(localePriorities.get(), context.getSupportedLocales()); + if (resolvedLocales.size() > 0) { + return resolvedLocales.get(0); + } + + return context.getDefaultLocale(); + } + + private Optional> getAcceptableLanguages() { + HttpHeaders httpHeaders = getHeaders(); + if (httpHeaders != null) { + List acceptLanguageList = httpHeaders.getRequestHeader("Accept-Language"); + if (acceptLanguageList != null && !acceptLanguageList.isEmpty()) { + return Optional.of(Locale.LanguageRange.parse(acceptLanguageList.get(0))); + } + } + + return Optional.empty(); + } +} diff --git a/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyContextLocaleResolver.java b/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyContextLocaleResolver.java index 2db6f26d506ab..76fdb17a5ef08 100644 --- a/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyContextLocaleResolver.java +++ b/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyContextLocaleResolver.java @@ -1,48 +1,18 @@ package io.quarkus.hibernate.validator.runtime.jaxrs; -import java.util.List; -import java.util.Locale; -import java.util.Locale.LanguageRange; -import java.util.Optional; - import javax.inject.Singleton; import javax.ws.rs.core.HttpHeaders; -import org.hibernate.validator.spi.messageinterpolation.LocaleResolver; -import org.hibernate.validator.spi.messageinterpolation.LocaleResolverContext; import org.jboss.resteasy.core.ResteasyContext; import io.quarkus.arc.DefaultBean; @Singleton @DefaultBean -public class ResteasyContextLocaleResolver implements LocaleResolver { +public class ResteasyContextLocaleResolver extends AbstractLocaleResolver { @Override - public Locale resolve(LocaleResolverContext context) { - Optional> localePriorities = getAcceptableLanguages(); - if (!localePriorities.isPresent()) { - return context.getDefaultLocale(); - } - - List resolvedLocales = Locale.filter(localePriorities.get(), context.getSupportedLocales()); - if (resolvedLocales.size() > 0) { - return resolvedLocales.get(0); - } - - return context.getDefaultLocale(); + protected HttpHeaders getHeaders() { + return ResteasyContext.getContextData(HttpHeaders.class); } - - private Optional> getAcceptableLanguages() { - HttpHeaders httpHeaders = ResteasyContext.getContextData(HttpHeaders.class); - if (httpHeaders != null) { - List acceptLanguageList = httpHeaders.getRequestHeader("Accept-Language"); - if (acceptLanguageList != null && !acceptLanguageList.isEmpty()) { - return Optional.of(LanguageRange.parse(acceptLanguageList.get(0))); - } - } - - return Optional.empty(); - } - } diff --git a/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyReactiveContextLocaleResolver.java b/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyReactiveContextLocaleResolver.java new file mode 100644 index 0000000000000..38366401d34e9 --- /dev/null +++ b/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyReactiveContextLocaleResolver.java @@ -0,0 +1,23 @@ +package io.quarkus.hibernate.validator.runtime.jaxrs; + +import javax.inject.Singleton; +import javax.ws.rs.core.HttpHeaders; + +import io.quarkus.arc.DefaultBean; + +@Singleton +@DefaultBean +public class ResteasyReactiveContextLocaleResolver extends AbstractLocaleResolver { + + private final HttpHeaders headers; + + // automatically injected for RESTEasy Reactive because of org.jboss.resteasy.reactive.server.injection.ContextProducers + public ResteasyReactiveContextLocaleResolver(HttpHeaders headers) { + this.headers = headers; + } + + @Override + protected HttpHeaders getHeaders() { + return headers; + } +}