From 2cc7af16e30a5e4cdddec5c7d9951bd5b5026b15 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Tue, 3 Aug 2021 11:16:43 +0300 Subject: [PATCH 1/2] Property name RESTEasy Reactive components in Hibernate Validator extension --- .../validator/deployment/HibernateValidatorProcessor.java | 6 +++--- ...a => ResteasyReactiveEndPointValidationInterceptor.java} | 2 +- ...r.java => ResteasyReactiveViolationExceptionMapper.java} | 2 +- tcks/resteasy-reactive/pom.xml | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) rename extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/{QuarkusRestEndPointValidationInterceptor.java => ResteasyReactiveEndPointValidationInterceptor.java} (88%) rename extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/{QuarkusRestViolationExceptionMapper.java => ResteasyReactiveViolationExceptionMapper.java} (97%) 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 9b27cb7c43053..237bc8739eccf 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,7 +75,7 @@ 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.QuarkusRestViolationExceptionMapper; +import io.quarkus.hibernate.validator.runtime.jaxrs.ResteasyReactiveViolationExceptionMapper; import io.quarkus.hibernate.validator.runtime.jaxrs.ResteasyConfigSupport; import io.quarkus.hibernate.validator.spi.BeanValidationAnnotationsBuildItem; import io.quarkus.resteasy.common.spi.ResteasyConfigBuildItem; @@ -153,7 +153,7 @@ void registerAdditionalBeans(HibernateValidatorRecorder hibernateValidatorRecord } else if (capabilities.isPresent(Capability.RESTEASY_REACTIVE)) { // The CDI interceptor which will validate the methods annotated with @JaxrsEndPointValidated additionalBeans.produce(new AdditionalBeanBuildItem( - "io.quarkus.hibernate.validator.runtime.jaxrs.QuarkusRestEndPointValidationInterceptor")); + "io.quarkus.hibernate.validator.runtime.jaxrs.ResteasyReactiveEndPointValidationInterceptor")); } // A constraint validator with an injection point but no scope is added as @Singleton @@ -348,7 +348,7 @@ NativeImageConfigBuildItem nativeImageConfig() { @BuildStep ExceptionMapperBuildItem mapper() { - return new ExceptionMapperBuildItem(QuarkusRestViolationExceptionMapper.class.getName(), + return new ExceptionMapperBuildItem(ResteasyReactiveViolationExceptionMapper.class.getName(), ValidationException.class.getName(), Priorities.USER + 1, true); } diff --git a/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/QuarkusRestEndPointValidationInterceptor.java b/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyReactiveEndPointValidationInterceptor.java similarity index 88% rename from extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/QuarkusRestEndPointValidationInterceptor.java rename to extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyReactiveEndPointValidationInterceptor.java index c5d023e462043..b139ddd67245d 100644 --- a/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/QuarkusRestEndPointValidationInterceptor.java +++ b/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyReactiveEndPointValidationInterceptor.java @@ -11,7 +11,7 @@ @JaxrsEndPointValidated @Interceptor @Priority(Interceptor.Priority.PLATFORM_AFTER + 800) -public class QuarkusRestEndPointValidationInterceptor extends AbstractMethodValidationInterceptor { +public class ResteasyReactiveEndPointValidationInterceptor extends AbstractMethodValidationInterceptor { @AroundInvoke @Override diff --git a/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/QuarkusRestViolationExceptionMapper.java b/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyReactiveViolationExceptionMapper.java similarity index 97% rename from extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/QuarkusRestViolationExceptionMapper.java rename to extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyReactiveViolationExceptionMapper.java index 49df29c1cbc7c..945552cfe3772 100644 --- a/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/QuarkusRestViolationExceptionMapper.java +++ b/extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyReactiveViolationExceptionMapper.java @@ -12,7 +12,7 @@ import javax.ws.rs.ext.Provider; @Provider -public class QuarkusRestViolationExceptionMapper implements ExceptionMapper { +public class ResteasyReactiveViolationExceptionMapper implements ExceptionMapper { @Override public Response toResponse(ValidationException exception) { diff --git a/tcks/resteasy-reactive/pom.xml b/tcks/resteasy-reactive/pom.xml index 89d69e17b772d..5881a6209de7e 100644 --- a/tcks/resteasy-reactive/pom.xml +++ b/tcks/resteasy-reactive/pom.xml @@ -17,7 +17,7 @@ - e3cd2c5e5b9262ae5d1197005c542fdd02f9d4ff + 8dad67600518d02c8c6b376199efb91d360277b0 ${skipTests} ${exec.skip} From a530db11061e8270144e80461ee3e10f4d4a4d1d Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Tue, 3 Aug 2021 11:29:44 +0300 Subject: [PATCH 2/2] Introduce LocaleResolver for RESTEasy Reactive --- .../HibernateValidatorProcessor.java | 4 +- .../runtime/jaxrs/AbstractLocaleResolver.java | 42 +++++++++++++++++++ .../jaxrs/ResteasyContextLocaleResolver.java | 36 ++-------------- ...ResteasyReactiveContextLocaleResolver.java | 23 ++++++++++ 4 files changed, 71 insertions(+), 34 deletions(-) create mode 100644 extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/AbstractLocaleResolver.java create mode 100644 extensions/hibernate-validator/runtime/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyReactiveContextLocaleResolver.java 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; + } +}