io.quarkus.smallrye.graphql.runtime.SmallRyeGraphQLLocaleResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-smallrye-graphql Show documentation
Show all versions of quarkus-smallrye-graphql Show documentation
Create GraphQL Endpoints using the code-first approach from MicroProfile GraphQL
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