io.quarkus.hibernate.validator.runtime.locale.AbstractLocaleResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-hibernate-validator Show documentation
Show all versions of quarkus-hibernate-validator Show documentation
Validate object properties (field, getter) and method parameters for your beans (REST, CDI, Jakarta Persistence)
package io.quarkus.hibernate.validator.runtime.locale;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import org.hibernate.validator.spi.messageinterpolation.LocaleResolver;
import org.hibernate.validator.spi.messageinterpolation.LocaleResolverContext;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.common.util.CaseInsensitiveMap;
abstract class AbstractLocaleResolver implements LocaleResolver {
private static final Logger log = Logger.getLogger(AbstractLocaleResolver.class);
private static final String ACCEPT_HEADER = "Accept-Language";
/**
* @return case-insensitive map
* @see CaseInsensitiveMap
*/
protected abstract Map> getHeaders();
@Override
public Locale resolve(LocaleResolverContext context) {
Optional> localePriorities = getAcceptableLanguages();
if (localePriorities.isEmpty()) {
return null;
}
List resolvedLocales = Locale.filter(localePriorities.get(), context.getSupportedLocales());
if (!resolvedLocales.isEmpty()) {
return resolvedLocales.get(0);
}
return null;
}
private Optional> getAcceptableLanguages() {
Map> httpHeaders = getHeaders();
if (httpHeaders != null) {
List acceptLanguageList = httpHeaders.get(ACCEPT_HEADER);
if (acceptLanguageList != null && !acceptLanguageList.isEmpty()) {
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();
}
}