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