-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce LocaleResolver for RESTEasy Reactive
- Loading branch information
Showing
4 changed files
with
71 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...me/src/main/java/io/quarkus/hibernate/validator/runtime/jaxrs/AbstractLocaleResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
36 changes: 3 additions & 33 deletions
36
...main/java/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyContextLocaleResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...a/io/quarkus/hibernate/validator/runtime/jaxrs/ResteasyReactiveContextLocaleResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |