Skip to content

Commit

Permalink
Introduce LocaleResolver for RESTEasy Reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Aug 3, 2021
1 parent 2abe7f0 commit 1ba9e84
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<Locale.LanguageRange>> localePriorities = getAcceptableLanguages();
if (!localePriorities.isPresent()) {
return context.getDefaultLocale();
}

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

return context.getDefaultLocale();
}

private Optional<List<Locale.LanguageRange>> getAcceptableLanguages() {
HttpHeaders httpHeaders = getHeaders();
if (httpHeaders != null) {
List<String> acceptLanguageList = httpHeaders.getRequestHeader("Accept-Language");
if (acceptLanguageList != null && !acceptLanguageList.isEmpty()) {
return Optional.of(Locale.LanguageRange.parse(acceptLanguageList.get(0)));
}
}

return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -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<List<LanguageRange>> localePriorities = getAcceptableLanguages();
if (!localePriorities.isPresent()) {
return context.getDefaultLocale();
}

List<Locale> 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<List<LanguageRange>> getAcceptableLanguages() {
HttpHeaders httpHeaders = ResteasyContext.getContextData(HttpHeaders.class);
if (httpHeaders != null) {
List<String> acceptLanguageList = httpHeaders.getRequestHeader("Accept-Language");
if (acceptLanguageList != null && !acceptLanguageList.isEmpty()) {
return Optional.of(LanguageRange.parse(acceptLanguageList.get(0)));
}
}

return Optional.empty();
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 1ba9e84

Please sign in to comment.