Example usage for javax.servlet.http HttpServletRequest getLocales

List of usage examples for javax.servlet.http HttpServletRequest getLocales

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getLocales.

Prototype

public Enumeration<Locale> getLocales();

Source Link

Document

Returns an Enumeration of Locale objects indicating, in decreasing order starting with the preferred locale, the locales that are acceptable to the client based on the Accept-Language header.

Usage

From source file:ch.entwine.weblounge.common.impl.language.LanguageUtils.java

/**
 * Returns the language out of <code>choices</code> that matches the client's
 * requirements as indicated through the <code>Accept-Language</code> header.
 * If no match is possible, <code>null</code> is returned.
 * // w  w  w .ja va 2 s .com
 * @param choices
 *          the available locales
 * @param request
 *          the http request
 */
public static Language getPreferredLanguage(Set<Language> choices, HttpServletRequest request) {
    if (request.getHeader("Accept-Language") != null) {
        Enumeration<?> locales = request.getLocales();
        while (locales.hasMoreElements()) {
            try {
                Language l = getLanguage((Locale) locales.nextElement());
                if (choices.contains(l))
                    return l;
            } catch (UnknownLanguageException e) {
                // never mind, some clients will send stuff like "*" as the locale
            }

        }
    }
    return null;
}

From source file:ch.entwine.weblounge.common.impl.language.LanguageUtils.java

/**
 * Returns the preferred one out of of those languages that are requested by
 * the client through the <code>Accept-Language</code> header and are
 * supported by the site. If there is no match, the site's default language is
 * returned.//from  w  w w .  ja v  a2s .c o m
 * <p>
 * The preferred one is defined by the following priorities:
 * <ul>
 * <li>Requested by the client</li>
 * <li>The site default language</li>
 * </ul>
 * 
 * @param request
 *          the http request
 * @param site
 *          the site
 */
public static Language getPreferredLanguage(HttpServletRequest request, Site site) {

    // Accept-Language header
    if (request.getHeader("Accept-Language") != null) {
        Enumeration<?> locales = request.getLocales();
        while (locales.hasMoreElements()) {
            try {
                Language l = getLanguage((Locale) locales.nextElement());
                if (site.supportsLanguage(l))
                    return l;
            } catch (UnknownLanguageException e) {
                // never mind, some clients will send stuff like "*" as the locale
            }
        }
    }

    return site.getDefaultLanguage();
}

From source file:ch.entwine.weblounge.common.impl.language.LanguageUtils.java

/**
 * Returns the preferred one out of of those languages that are requested by
 * the client through the <code>Accept-Language</code> header and are
 * supported by both the localizable and the site.
 * <p>/*from  w  w  w.ja v a  2  s.  c om*/
 * The preferred one is defined by the following priorities:
 * <ul>
 * <li>Requested by the client</li>
 * <li>The localizable's original language</li>
 * <li>The site default language</li>
 * <li>The first language of what is supported by both the localizable and the
 * site</li>
 * </ul>
 * 
 * @param localizable
 *          the localizable
 * @param request
 *          the http request
 * @param site
 *          the site
 */
public static Language getPreferredLanguage(Localizable localizable, HttpServletRequest request, Site site) {

    // Path
    String[] pathElements = StringUtils.split(request.getRequestURI(), "/");
    for (String element : pathElements) {
        for (Language l : localizable.languages()) {
            if (l.getIdentifier().equals(element)) {
                return l;
            }
        }
    }

    // Accept-Language header
    if (request.getHeader("Accept-Language") != null) {
        Enumeration<?> locales = request.getLocales();
        while (locales.hasMoreElements()) {
            try {
                Language l = getLanguage((Locale) locales.nextElement());
                if (localizable != null && !localizable.supportsLanguage(l))
                    continue;
                if (!site.supportsLanguage(l))
                    continue;
                return l;
            } catch (UnknownLanguageException e) {
                // never mind, some clients will send stuff like "*" as the locale
            }
        }
    }

    // The localizable's original language
    if (localizable != null && localizable instanceof Resource) {
        Resource<?> r = (Resource<?>) localizable;
        if (r.getOriginalContent() != null) {
            if (site.supportsLanguage(r.getOriginalContent().getLanguage()))
                return r.getOriginalContent().getLanguage();
        }
    }

    // Site default language
    if (localizable != null && localizable.supportsLanguage(site.getDefaultLanguage())) {
        return site.getDefaultLanguage();
    }

    // Any match
    if (localizable != null) {
        for (Language l : site.getLanguages()) {
            if (localizable.supportsLanguage(l)) {
                return l;
            }
        }
    }

    return null;
}

From source file:org.smigo.user.UserController.java

@RequestMapping(value = "/locales", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody/* w w w .ja v a2  s  .  co  m*/
public java.util.Map<String, String> getLocales(HttpServletRequest request, Locale locale) {
    return Language.getLanguagesForDisplay(request.getLocales(), locale);
}

From source file:com.ibm.util.merge.web.rest.servlet.RequestData.java

private List<Locale> getAllRequestLocales(HttpServletRequest request) {
    Enumeration<Locale> allLocales = request.getLocales();
    List<Locale> tlocales = new ArrayList<>();
    while (allLocales.hasMoreElements()) {
        tlocales.add(allLocales.nextElement());
    }/*from w ww.ja v  a  2 s  .c o m*/
    return tlocales;
}

From source file:com.ctc.storefront.filters.StorefrontFilter.java

protected void initDefaults(final HttpServletRequest request) {
    getStoreSessionFacade().initializeSession(Collections.list(request.getLocales()));
}

From source file:ch.entwine.weblounge.common.impl.language.LanguageUtils.java

/**
 * Returns the preferred one out of of those languages that are requested by
 * the client through the <code>Accept-Language</code> header and are
 * supported by both the resource in that there is resource content in that
 * language and the site./*from  w  ww  .ja v  a 2s .  c  o  m*/
 * <p>
 * The preferred one is defined by the following priorities:
 * <ul>
 * <li>Requested by the client</li>
 * <li>The resource's original language</li>
 * <li>The site default language</li>
 * <li>The first language of what is supported by both the resource and the
 * site</li>
 * </ul>
 * 
 * @param resource
 *          the resource
 * @param request
 *          the http request
 * @param site
 *          the site
 */
public static Language getPreferredContentLanguage(Resource<?> resource, HttpServletRequest request,
        Site site) {

    if (resource == null)
        throw new IllegalArgumentException("Resource must not be null");

    // Path
    String[] pathElements = StringUtils.split(request.getRequestURI(), "/");
    for (String element : pathElements) {
        for (Language l : resource.contentLanguages()) {
            if (l.getIdentifier().equals(element)) {
                return l;
            }
        }
    }

    // Accept-Language header
    if (request.getHeader("Accept-Language") != null) {
        Enumeration<?> locales = request.getLocales();
        while (locales.hasMoreElements()) {
            try {
                Language l = getLanguage((Locale) locales.nextElement());
                if (l == null)
                    continue;
                if (!resource.supportsContentLanguage(l))
                    continue;
                if (!site.supportsLanguage(l))
                    continue;
                return l;
            } catch (UnknownLanguageException e) {
                // never mind, some clients will send stuff like "*" as the locale
            }
        }
    }

    // Original content
    if (resource.getOriginalContent() != null) {
        if (site.supportsLanguage(resource.getOriginalContent().getLanguage()))
            return resource.getOriginalContent().getLanguage();
    }

    // Site default language
    if (resource.supportsContentLanguage(site.getDefaultLanguage())) {
        return site.getDefaultLanguage();
    }

    // Any match
    for (Language l : site.getLanguages()) {
        if (resource.supportsContentLanguage(l)) {
            return l;
        }
    }

    return null;
}

From source file:com.epam.cme.storefront.filters.StorefrontFilter.java

protected void initDefaults(final HttpServletRequest request) {
    final StoreSessionFacade storeSessionFacade = getStoreSessionFacade();
    storeSessionFacade.initializeSession(Collections.list(request.getLocales()));
}

From source file:com.exxonmobile.ace.hybris.storefront.filters.StorefrontFilter.java

protected void initDefaults(final HttpServletRequest request) {
    final StoreSessionFacade storeSessionFacade = getStoreSessionFacade();

    storeSessionFacade.initializeSession(Collections.list(request.getLocales()));
}

From source file:com.aurel.track.dbase.ReadyTesterServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    prepareResponse(resp);/*w  w  w  .ja  v a2  s  .  co m*/
    PrintWriter out = resp.getWriter();
    Enumeration<Locale> locales = req.getLocales();
    execute(out, locales);
}