5aad467ce6f83c127919b32d6610b3e13ebc180bPhill Cunnington/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: ISLocaleContext.java,v 1.14 2008/08/19 19:09:00 veiming Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
5aad467ce6f83c127919b32d6610b3e13ebc180bPhill Cunnington * Portions Copyright 2015 ForgeRock AS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.common;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.am.util.AMClientDetector;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.am.util.Misc;
f340ca28e0f04c4b3b878647094bfd9439c0b698David Lunaimport com.iplanet.am.util.SystemProperties;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.cdm.Client;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.cdm.ClientException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.cdm.ClientsManager;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.services.cdm.G11NSettings;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.SSOException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.SSOToken;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.security.AdminTokenAction;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.Constants;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.locale.Locale;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.SMSException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ServiceConfig;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ServiceConfigManager;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ServiceSchema;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.sm.ServiceSchemaManager;
5aad467ce6f83c127919b32d6610b3e13ebc180bPhill Cunningtonimport org.forgerock.json.resource.http.HttpContext;
5aad467ce6f83c127919b32d6610b3e13ebc180bPhill Cunnington
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.security.AccessController;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Map;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.http.HttpServletRequest;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Sets the locale suitable for the given situation. Each response to end-user
8d3140b524c0e28c0a49dc7c7d481123ef3cfe11Chris Lee * must be in a well defined locale. Even if the user has not logged in, OpenAM
8d3140b524c0e28c0a49dc7c7d481123ef3cfe11Chris Lee * should respond in a locale. Hence OpenAM consults
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * various parameter to find out the locale for any given response. The order
8d3140b524c0e28c0a49dc7c7d481123ef3cfe11Chris Lee * and priority of the locale setting is as follows:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Priority 0 - OS_LOCALE - value returned by java.util.Locale.getDefault()
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Priority 1 - PLATFORM_LOCALE - iplanet-am-platform-locale attribute value
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * in iPlanetAMPlatform Service - Global value for entire
8d3140b524c0e28c0a49dc7c7d481123ef3cfe11Chris Lee * OpenAM instance
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Priority 2 - AUTH-LOCALE - iplanet-am-auth-locale attribute value in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * iPlantAMAuth service - Org specific locale value
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Priority 3 - USER_LOCALE - preferredlocale - iPlanetAMUser service and
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * it can be configured per org and user level
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Priority 4 - HTTP_HEADER_LOCALE - Accept-Language header of HTTP Request
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Priority 5 - URL_LOCALE - locale value passed as URL parameter
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Usage: There are three key parameters in the request that can decide the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * locale of a given request. This class expects application to pass all these
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * parameters whenever they are available and use getLocale method to get the
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna * current locale.
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna *
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna * The four key parameters are:
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna *
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna * - HttpServletRequest - to process Accept-language and URL parameter locale.
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna * - HttpContext - to process Accept-language and URL parameter locale.
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna * - UserPreferredLocale - user attribute, if user has sucessful login.
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna * - OrgDN - DN of the org in which the user resides- it can take take core auth locale of this org.
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna *
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna * <code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * ISLocaleContext is = new ISLocaleContext();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * is.setOrgLocale ("o=isp,dc=iplanet,dc=com"); // sets the org locale
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna * is.setLocale(req); // get locale from accept-lang,locale param etc
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * // if user logs in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * is.setUserLocale (loc);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * </code>
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna *
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna * For your response, get the locale using:
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna *
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna * <code>
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna * is.getLocale();
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna * </code>
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna *
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna * Locale with highest priority takes precedence over lower priority.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class ISLocaleContext {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static final int OS_LOCALE = 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static final int PLATFORM_LOCALE = 1;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static final int CORE_AUTH_LOCALE = 2;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static final int USER_PREFERRED_LOCALE = 3;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static final int HTTP_HEADER_LOCALE = 4;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static final int URL_LOCALE = 5;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static int initLocaleLevel; // Default locale level
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Default locale for every request, it will be changed by HTTP_HEADER,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // USER_PREFERRED_LOCALE or URL_LOCALE is defined for any request
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static java.util.Locale initLocale;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static AMClientDetector clientDt;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static Client defaultClient;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Client client;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static G11NSettings g11nSettings;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String initCharset;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private String charset;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private java.util.Locale locale;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private int localeLevel;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String installTime =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SystemProperties.get(AdminTokenAction.AMADMIN_MODE, "false");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (installTime.equalsIgnoreCase("false")) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster clientDt = new AMClientDetector();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((clientDt != null ) && clientDt.isDetectionEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster defaultClient = ClientsManager.getDefaultInstance();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String initLocaleStr;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SSOToken token = (SSOToken) AccessController
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .doPrivileged(AdminTokenAction.getInstance());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String platformLocale = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String authLocale = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceSchemaManager scm = new ServiceSchemaManager(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "iPlanetAMPlatformService", token);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceSchema psc = scm.getGlobalSchema();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map attrs = psc.getAttributeDefaults();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster platformLocale = Misc.getMapAttr(attrs,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "iplanet-am-platform-locale");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException ex) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Ignore the exception and leave platformLocale = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceSchemaManager scm = new ServiceSchemaManager(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "iPlanetAMAuthService", token);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceSchema psc = scm.getOrganizationSchema();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map attrs = psc.getAttributeDefaults();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster authLocale = Misc.getMapAttr(attrs, "iplanet-am-auth-locale");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException ex) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Ignore the exception and leave authLocale = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String tLocale = authLocale;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initLocaleLevel = CORE_AUTH_LOCALE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initLocaleStr = tLocale;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (tLocale == null || tLocale.length() == 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tLocale = platformLocale;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initLocaleLevel = PLATFORM_LOCALE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initLocaleStr = tLocale;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (tLocale == null || tLocale.length() == 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tLocale = java.util.Locale.getDefault().toString();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initLocaleLevel = OS_LOCALE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initLocaleStr = tLocale;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initLocale = Locale.getLocale(initLocaleStr);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException ex) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // unable to get SSOToken hence fallback to OS_LOCALE
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initLocale = java.util.Locale.getDefault();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initLocaleLevel = OS_LOCALE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initCharset = (defaultClient != null) ?
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster defaultClient.getCharset(initLocale) :
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Constants.CONSOLE_UI_DEFAULT_CHARSET;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Initializes <code>ISLocaleContext</code> to default level It examines
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * OS_LOCALE, PLATFORM_LOCALE, AUTH_LOCALE and initialize them based on
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * their priority
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public ISLocaleContext() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster locale = initLocale;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster localeLevel = initLocaleLevel;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster charset = initCharset;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster client = defaultClient;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Initialize <code>ISLocaleContext</code> for a given Org It can look
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * into orgs core auth locale value and set the value if it is available
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public ISLocaleContext(String orgDN) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster setOrgLocale(orgDN);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Set locale to given level.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param level Possible values are <code>OS_LOCALE,PLATFORM_LOCALE</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>AUTH_LOCALE</code>, <code>ACCEPT_LOCALE</code>,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>USER_PREFERRED_LOCALE</code>, <code>URL_LOCALE</code>.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param loc Locale value in string example <code>en</code>,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>ja_JP</ocde>. Warning: This method overrides priority
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * lookup mechanism.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void setLocale(int level, String loc) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (loc != null && loc.length() > 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster setLocale(level, Locale.getLocale(loc));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Set locale to given level.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param level Possible values are <code>OS_LOCALE,PLATFORM_LOCALE</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>AUTH_LOCALE</code>, <code>ACCEPT_LOCALE</code>,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>USER_PREFERRED_LOCALE</code>, <code>URL_LOCALE</code>.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param loc Locale value.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void setLocale(int level, java.util.Locale loc) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (level < 0 || level > URL_LOCALE) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new IllegalArgumentException("Invalid locale level=" + level);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (level >= localeLevel) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster localeLevel = level;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster charset = (client != null) ? charset = client.getCharset(loc) :
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Constants.CONSOLE_UI_DEFAULT_CHARSET;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster locale = loc;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna /**
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna * Returns the locale based on the Http Context supplied.
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna *
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna * @param context The {@link HttpContext} of the request
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna * @return The locale determined by the request
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna */
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna public void setLocale(HttpContext context) {
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna final String superLocale = context.getParameterAsString("locale");
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna final String acceptLangHeader = context.getHeaderAsString("accept-language");
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna if (superLocale != null && !superLocale.isEmpty()) {
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna setLocale(URL_LOCALE, superLocale);
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna } else if (acceptLangHeader != null && !acceptLangHeader.isEmpty()) {
32bd4dbf7dd85069d8cba66ae42fad635cdd34d5David Luna String acclocale = Locale.getLocaleStringFromAcceptLangHeader(acceptLangHeader);
32bd4dbf7dd85069d8cba66ae42fad635cdd34d5David Luna setLocale(HTTP_HEADER_LOCALE, acclocale);
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna }
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna }
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Set locale based on HTTP Servlet Request.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param request Analyze HttpHeader and look for URL parameter called
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * locale . If it is set, it takes high precedence. Else look for
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>accept-language</code> header and set the locale if it
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * is present.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void setLocale(HttpServletRequest request) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (request != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String superLocale = request.getParameter("locale");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String agentType = Client.CDM_DEFAULT_CLIENT_TYPE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((clientDt != null) && clientDt.isDetectionEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster agentType = clientDt.getClientType(request);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster client = ClientsManager.getInstance(agentType);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (ClientException ex) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Unable to determine the client hence we fall back
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // to default client . It is performed at initalization
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (Exception e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Unable to determine the client hence we fall back
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // to default client . It is performed at initalization
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (superLocale != null && superLocale.length() > 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster setLocale(URL_LOCALE, superLocale);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String acceptLangHeader = request.getHeader("Accept-Language");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((acceptLangHeader != null)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster && (acceptLangHeader.length() > 0)) {
f340ca28e0f04c4b3b878647094bfd9439c0b698David Luna String acclocale =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Locale.getLocaleStringFromAcceptLangHeader(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster acceptLangHeader);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster setLocale(HTTP_HEADER_LOCALE, acclocale);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Set the current locale level to <code>USER_LOCALE</code> and sets the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * value if current locale level is greater than <code>USER_LOCALE</code>,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * this setting will be ignored.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param loc Locale.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void setUserLocale(java.util.Locale loc) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster setLocale(USER_PREFERRED_LOCALE, loc);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Set the current locale level to <code>USER_LOCALE</code> and sets the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * value the locale value is separated by underscore character
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>ex:en_US</code>.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param loc Locale.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void setUserLocale(String loc) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (loc == null || loc.length() == 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster setLocale(USER_PREFERRED_LOCALE, Locale.getLocale(loc));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns Locale value that has got highest prioirty
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return locale.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public java.util.Locale getLocale() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return locale;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * get current priority level of locale
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return localeLevel
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public int getLocaleLevel() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return localeLevel;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Update locale context based on org locale user locale takes precedence
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * over this locale
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param orgDN -
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Distinguished Name of Organization
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void setOrgLocale(String orgDN) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (localeLevel > CORE_AUTH_LOCALE) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SSOToken token = (SSOToken) AccessController
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .doPrivileged(AdminTokenAction.getInstance());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfigManager scm = new ServiceConfigManager(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "iPlanetAMAuthService", token);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig sc = scm.getOrganizationConfig(orgDN, null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map attrs = sc.getAttributes();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String locale = Misc.getMapAttr(attrs, "iplanet-am-auth-locale");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (locale != null && locale.length() > 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster setLocale(CORE_AUTH_LOCALE, Locale.getLocale(locale));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException ssoe) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Problems in getting SSOToken which can be safely
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // ignored as we have a fallback locale
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException amex) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Problems in getting attribute from org
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // ignored as we have fallback locale
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * get mime charset to be used for current request This class detectes
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * clientType using Http-Accept-Lang header You should have used
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * setLocale(HttpServletRequest req) before calling this method. Otherwise
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * it will use default clientType configured by client detection service All
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * http headers should have mime charset. For example use this API whenever
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * you set http header using setContentType()
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return mime charset to be used for current request
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getMIMECharset() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return charset;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * get java charset to be used for current request This class detectes
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * clientType using Http-Accept-Lang header You should have used
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * setLocale(HttpServletRequest req) before calling this method. Otherwise
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * it will use default clientType configured by client detection service.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Most of the cases Javas codeset converter understands MIME charset names
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * withsome exceptions. It is recommended to use this API to set form hidden
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * field such as gx_charset
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return java charset to be used for current request
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getJavaCharset() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String jCharset = G11NSettings.JAVA_CHARSET_NAME;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (g11nSettings == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster g11nSettings = G11NSettings.getInstance();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (g11nSettings != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster jCharset = g11nSettings.getJavaCharset(charset);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return jCharset;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}