Class UserContext
- java.lang.Object
-
- com.filenet.api.util.UserContext
-
public class UserContext extends java.lang.ObjectUsed to override the locale and authentication for the requests a given thread makes to the server. It is not required if your application is already running in a Java™ Authentication and Authorization Service (JAAS) context or if the JVM's default locale is sufficient. When a subject is specified, a request to the server is limited, through authorization access control, to the operations that have been granted to the specified principal. Correspondingly, if a locale is specified, the server attempts to return localizable messages, such as exceptions, according to the language code specified by the locale.As noted above,
UserContextoperations are performed on a per-thread basis. When working with multiple locales, for example when servicing requests via a thread pool, you must explicitly callsetLocaleat the beginning of each new thread (request). The following code snippet illustrates the call to make at the startup phase of every request, wherereqLocaleis the locale to be used for this request:UserContext uc = UserContext.get(); uc.setLocale(reqLocale);Calls to the
pushSubjectandpopSubjectmethods must always be balanced. That is, a call topushSubjectmust eventually be followed by a balancing call topopSubject, particularly for Java EE applications or other situations in which threads are reused. TheUserContextobject, and therefore its internal stack ofSubjects, is thread local. If thepushSubjectandpopSubjectcalls are not balanced, an incorrectSubjectmight be used by a component that is reusing the thread. The coding pattern shown below is a good way to ensure that the calls are balanced:UserContext.get().pushSubject(mySubject); try { // do work } finally { UserContext.get().popSubject(); }To ensure that an ambient JAAS
Subjectis used for Content Engine API server roundtrips, you can employ the coding pattern below. You can view this as defensive coding against the possibility that another component has neglected to call the matchingpopSubject()method. Because the stack ofSubjectsis affiliated with a thread, a defect in one application can lead to problems in other applications (typically thread-pool environments).boolean ambient = true; //true means container-managed authentication UserContext origUC = UserContext.get(); UserContext tempUC = new UserContext(); tempUC.setLocale(origUC.getLocale()); try { if (ambient) UserContext.set(tempUC); // execute code using ambient Subject } finally { if (ambient) UserContext.set(origUC); }Note: The style of programming using push/popSubject is no longer preferred. Instead the
doAs(javax.security.auth.Subject, java.security.PrivilegedExceptionAction<T>)method should be used or, as of 5.5.9,SubjectCredentials.doAs(java.security.PrivilegedExceptionAction<T>).
-
-
Constructor Summary
Constructors Constructor and Description UserContext()Constructs a new instance of aUserContextobject with uninitialized properties.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method and Description static javax.security.auth.SubjectcreateSubject(Connection conn, java.lang.String user, java.lang.String password, java.lang.String optionalJAASStanzaName)Uses standard JAAS calls to authenticate with the application server and return a JAASSubjectobject.static <T> java.lang.ObjectdoAs(javax.security.auth.Subject subject, java.security.PrivilegedExceptionAction<T> pea)Deprecated.As of version 5.5.9. TheSubjectCredentials.doAs(java.security.PrivilegedExceptionAction<T>)method should be used instead.static UserContextget()This method returns theUserContextobject associated with the current thread.static javax.security.auth.SubjectgetAmbientSubject()Returns the current ambient JAASSubject, if any.java.util.LocalegetLocale()Returns the locale identifier for this user context.javax.security.auth.SubjectgetSubject()Returns the currentSubjectassociated with the thread.javax.security.auth.SubjectpopSubject()Removes and returns the currently activeSubject, making the previously activeSubjectthe current one.voidpushSubject(javax.security.auth.Subject sub)Saves any previously activeSubjectand makes the specifiedSubjectactive.static voidset(UserContext uc)This method associates the current thread with aUserContextobject.voidsetLocale(java.util.Locale locale)Sets the locale identifier for this user context.
-
-
-
Constructor Detail
-
UserContext
public UserContext()
Constructs a new instance of aUserContextobject with uninitialized properties.
-
-
Method Detail
-
getSubject
public javax.security.auth.Subject getSubject()
Returns the currentSubjectassociated with the thread. To associate theSubjectwith the thread, call thepushSubject()method. This method does not consider any ambient JAAS Subjects. For that, use the methodgetAmbientSubject().- Returns:
- A JAAS
Subjectobject. - See Also:
getAmbientSubject()
-
doAs
public static <T> java.lang.Object doAs(javax.security.auth.Subject subject, java.security.PrivilegedExceptionAction<T> pea)Deprecated. As of version 5.5.9. TheSubjectCredentials.doAs(java.security.PrivilegedExceptionAction<T>)method should be used instead.Performs thePrivilegedExceptionActionwith the given ambientSubject. It is preferable to use this method instead of thepushSubject()andpopSubject()methods because it eliminates the chance for unmatched push/pop. It also places theSubjectinto the ambient JAAS context, so it will be in effect for all software, not just the CE API.- Returns:
- The value returned by the
PrivilegedExceptionAction.run()method. - Throws:
EngineRuntimeException- if thePrivilegedExceptionAction.run()method throws anyThrowable. In contrast to the standard JDKSubject.doAs()method, anyThrowablethrown will be wrapped in an uncheckedEngineRuntimeExceptionrather than a checkedPrivilegedActionException.
-
getAmbientSubject
public static javax.security.auth.Subject getAmbientSubject()
Returns the current ambient JAASSubject, if any. Ambient means put into the thread context via JAAS APIs, including container-managed authentication in J2EE. This method ignores anySubjects pushed onto the stack viaUserContext.pushSubject()but considers to be ambient aSubjectin effect viaUserContext.doAs().- Returns:
- A JAAS
Subjectobject. - See Also:
getSubject()
-
pushSubject
public void pushSubject(javax.security.auth.Subject sub)
Saves any previously activeSubjectand makes the specifiedSubjectactive. To restore the previousSubject, call thepopSubject()method.- Parameters:
sub- The JAASSubjectobject to be associated with the thread. Cannot benull.- Throws:
EngineRuntimeException- if the parameter is not specified or is an invalid value.
-
popSubject
public javax.security.auth.Subject popSubject()
Removes and returns the currently activeSubject, making the previously activeSubjectthe current one. Note that this method does not return the restoredSubject. To retrieve the restoredSubject, you must callgetSubject().- Returns:
- The currently active JAAS
Subjectfor the thread.
-
setLocale
public void setLocale(java.util.Locale locale)
Sets the locale identifier for this user context. For more information, see the LocaleName property.- Parameters:
locale- The JavaLocaleobject to be associated with the current user context. Can benull, in which case the default locale for the JVM is used.
-
getLocale
public java.util.Locale getLocale()
Returns the locale identifier for this user context. If no locale has been set, this method returns the default locale defined for the JVM. For more information, see the LocaleName property.- Returns:
- A Java
Localeobject.
-
createSubject
public static javax.security.auth.Subject createSubject(Connection conn, java.lang.String user, java.lang.String password, java.lang.String optionalJAASStanzaName)
Uses standard JAAS calls to authenticate with the application server and return a JAASSubjectobject. The client must be properly configured for JAAS. The calling code can optionally provide a JAAS configuration stanza name or pass in anullvalue, in which case the stanza name defaults to "FileNetP8". Note that the returnedSubjecthas no association to the current user context. It is up to the caller to use theSubjectwith theUserContextor to use the JAASSubject.doAsmethod.Note: As of version 5.5.9, if using the WSI transport the
UsernameCredentialsclass can be used to execute actions using username/password authentication, avoiding the need to perform a JAAS login.- Parameters:
conn- TheConnectionobject for this thread. Cannot benull.user- A string containing the user name to be authenticated.password- A string containing the user's password.optionalJAASStanzaName- A string containing the JAAS configuration stanza name. Can benull, in which case the stanza name defaults to "FileNetP8".- Returns:
- A JAAS
Subjectobject. - Throws:
EngineRuntimeException- if the connection isnullor an invalid value.
-
get
public static UserContext get()
This method returns theUserContextobject associated with the current thread. When no user context has been set, a default one exists containing the current locale default from the JVM.- Returns:
- The
UserContextobject associated with the current thread.
-
set
public static void set(UserContext uc)
This method associates the current thread with aUserContextobject. In general, aUserContextobject should not be shared between threads. So this operation should only be invoked from the same thread that created the given object. If an attempt is made to set aUserContextobject on a different thread than the one from which it was created, a clone of theUserContextis made and set to the current thread instead. This clone will contain a shallow copy of the Subjects in the givenUserContextso that the underlying Subjects are shared between cloned instances, however the push/pop list of Subjects is unique per instance ofUserContext.- Parameters:
uc- AUserContextobject.
-
-