Search in sources :

Example 1 with LocaleCharsetMap

use of org.glassfish.web.deployment.runtime.LocaleCharsetMap in project Payara by payara.

the class DynamicWebServletRegistrationImpl method mapLocalesToCharset.

/**
 * Matches the given request locales against the charsets specified in
 * the locale-charset-map of this web module's sun-web.xml, and returns
 * the first matching charset.
 *
 * @param locales Request locales
 *
 * @return First matching charset, or null if this web module does not
 * specify any locale-charset-map in its sun-web.xml, or no match was
 * found
 */
@Override
public String mapLocalesToCharset(Enumeration locales) {
    String encoding = null;
    LocaleCharsetMap[] locCharsetMap = getLocaleCharsetMap();
    if (locCharsetMap != null && locCharsetMap.length > 0) {
        /*
             * Check to see if there is a match between the request
             * locales (in preference order) and the locales in the
             * locale-charset-map.
             */
        boolean matchFound = false;
        while (locales.hasMoreElements() && !matchFound) {
            Locale reqLoc = (Locale) locales.nextElement();
            for (int i = 0; i < locCharsetMap.length && !matchFound; i++) {
                String language = locCharsetMap[i].getAttributeValue(LocaleCharsetMap.LOCALE);
                if (language == null || "".equals(language)) {
                    continue;
                }
                String country = null;
                int index = language.indexOf('_');
                if (index != -1) {
                    country = language.substring(index + 1);
                    language = language.substring(0, index);
                }
                Locale mapLoc = null;
                if (country != null) {
                    mapLoc = new Locale(language, country);
                } else {
                    mapLoc = new Locale(language);
                }
                if (mapLoc.equals(reqLoc)) {
                    /*
                         * Match found. Get the charset to which the
                         * matched locale maps.
                         */
                    encoding = locCharsetMap[i].getAttributeValue(LocaleCharsetMap.CHARSET);
                    matchFound = true;
                }
            }
        }
    }
    return encoding;
}
Also used : Locale(java.util.Locale) LocaleCharsetMap(org.glassfish.web.deployment.runtime.LocaleCharsetMap) UserDataConstraint(com.sun.enterprise.deployment.web.UserDataConstraint) SecurityConstraint(com.sun.enterprise.deployment.web.SecurityConstraint) WebServiceEndpoint(com.sun.enterprise.deployment.WebServiceEndpoint)

Aggregations

WebServiceEndpoint (com.sun.enterprise.deployment.WebServiceEndpoint)1 SecurityConstraint (com.sun.enterprise.deployment.web.SecurityConstraint)1 UserDataConstraint (com.sun.enterprise.deployment.web.UserDataConstraint)1 Locale (java.util.Locale)1 LocaleCharsetMap (org.glassfish.web.deployment.runtime.LocaleCharsetMap)1