All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.hibernate.validator.runtime.jaxrs.ResteasyContextLocaleResolver Maven / Gradle / Ivy

Go to download

Validate object properties (field, getter) and method parameters for your beans (REST, CDI, Jakarta Persistence)

There is a newer version: 3.17.5
Show newest version
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 {

    @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 = 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();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy