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

io.quarkus.smallrye.graphql.runtime.SmallRyeGraphQLLocaleResolver Maven / Gradle / Ivy

There is a newer version: 3.17.0.CR1
Show newest version
package io.quarkus.smallrye.graphql.runtime;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;

import javax.inject.Singleton;

import org.hibernate.validator.spi.messageinterpolation.LocaleResolver;
import org.hibernate.validator.spi.messageinterpolation.LocaleResolverContext;

import graphql.schema.DataFetchingEnvironment;
import io.smallrye.graphql.execution.context.SmallRyeContext;
import io.smallrye.graphql.execution.context.SmallRyeContextManager;

/**
 * Resolving BV messages for SmallRye GraphQL
 */
@Singleton
public class SmallRyeGraphQLLocaleResolver implements LocaleResolver {

    private static final String ACCEPT_HEADER = "Accept-Language";

    @Override
    public Locale resolve(LocaleResolverContext context) {
        Optional> localePriorities = getAcceptableLanguages();
        if (!localePriorities.isPresent()) {
            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()) {
                return Optional.of(Locale.LanguageRange.parse(acceptLanguageList.get(0)));
            }
        }
        return Optional.empty();
    }

    @SuppressWarnings("unchecked")
    private Map> getHeaders() {
        SmallRyeContext smallRyeContext = SmallRyeContextManager.getCurrentSmallRyeContext();
        if (smallRyeContext != null) {
            DataFetchingEnvironment dfe = smallRyeContext.unwrap(DataFetchingEnvironment.class);
            return (Map>) dfe.getGraphQlContext().get("httpHeaders");
        } else {
            return null;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy