javax.mvc.locale.LocaleResolver Java Examples

The following examples show how to use javax.mvc.locale.LocaleResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocaleResolverChain.java    From krazo with Apache License 2.0 5 votes vote down vote up
public Locale resolve(ContainerRequestContext requestContext) {

        // prepare context instance
        LocaleResolverContext context = new LocaleResolverContextImpl(configuration, requestContext);

        List<LocaleResolver> resolvers = CdiUtils.getApplicationBeans(LocaleResolver.class);

        // candidates as sorted list
        List<LocaleResolver> candidates = StreamSupport.stream(resolvers.spliterator(), false)
                .sorted((resolver1, resolver2) -> {
                    final Priority prio1 = getAnnotation(resolver1.getClass(), Priority.class);
                    final Priority prio2 = getAnnotation(resolver2.getClass(), Priority.class);
                    final int value1 = prio1 != null ? prio1.value() : 1000;
                    final int value2 = prio2 != null ? prio2.value() : 1000;
                    return value2 - value1;
                })
                .collect(Collectors.toList());

        // do the resolving
        for (LocaleResolver candidate : candidates) {
            Locale locale = candidate.resolveLocale(context);
            if (locale != null) {
                return locale;
            }
        }

        throw new IllegalStateException("Could not resolve locale with any of the " + candidates.size()
                + " resolver implementations");

    }
 
Example #2
Source File: LocaleResolverChain.java    From ozark with Apache License 2.0 5 votes vote down vote up
public Locale resolve(ContainerRequestContext requestContext) {

        // prepare context instance
        LocaleResolverContext context = new LocaleResolverContextImpl(configuration, requestContext);

        List<LocaleResolver> resolvers = CdiUtils.getApplicationBeans(LocaleResolver.class);

        // candidates as sorted list
        List<LocaleResolver> candidates = StreamSupport.stream(resolvers.spliterator(), false)
                .sorted((resolver1, resolver2) -> {
                    final Priority prio1 = getAnnotation(resolver1.getClass(), Priority.class);
                    final Priority prio2 = getAnnotation(resolver2.getClass(), Priority.class);
                    final int value1 = prio1 != null ? prio1.value() : 1000;
                    final int value2 = prio2 != null ? prio2.value() : 1000;
                    return value2 - value1;
                })
                .collect(Collectors.toList());

        // do the resolving
        for (LocaleResolver candidate : candidates) {
            Locale locale = candidate.resolveLocale(context);
            if (locale != null) {
                return locale;
            }
        }

        throw new IllegalStateException("Could not resolve locale with any of the " + candidates.size()
                + " resolver implementations");

    }
 
Example #3
Source File: MvcContextImpl.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void postConstruct() {

	SpringCsrfToken springCsrfToken = new SpringCsrfToken(portletRequest.getAttribute(
				"org.springframework.security.web.csrf.CsrfToken"));

	csrf = new CsrfImpl(springCsrfToken.getName(), springCsrfToken.getValue());

	List<LocaleResolver> localeResolvers = localeResolverChain.getLocaleResolvers();

	Map<String, javax.ws.rs.core.Cookie> cookieMap = new HashMap<>();

	Cookie[] cookies = portletRequest.getCookies();

	for (Cookie cookie : cookies) {
		cookieMap.put(cookie.getName(), new javax.ws.rs.core.Cookie(cookie.getName(), cookie.getValue()));
	}

	Map<String, String> headerMap = new HashMap<>();

	Enumeration<String> propertyNames = portletRequest.getPropertyNames();

	while (propertyNames.hasMoreElements()) {
		String header = propertyNames.nextElement();
		headerMap.put(header, portletRequest.getProperty(header));
	}

	LocaleResolverContextImpl localeResolverContext = new LocaleResolverContextImpl(Collections.list(
				portletRequest.getLocales()), configuration, cookieMap, headerMap, new UriInfoImpl());

	for (LocaleResolver localeResolver : localeResolvers) {
		locale = localeResolver.resolveLocale(localeResolverContext);

		if (locale != null) {
			break;
		}
	}
}
 
Example #4
Source File: LocaleResolverChain.java    From portals-pluto with Apache License 2.0 4 votes vote down vote up
public List<LocaleResolver> getLocaleResolvers() {
	return localeResolvers;
}
 
Example #5
Source File: LocaleResolverChain.java    From portals-pluto with Apache License 2.0 3 votes vote down vote up
@PostConstruct
private void postConstruct() {

	localeResolvers = BeanUtil.getBeanInstances(_beanManager, LocaleResolver.class);

	Collections.sort(localeResolvers, new DescendingPriorityComparator(1000));
}