Class AbstractContext

java.lang.Object
org.forgerock.services.context.AbstractContext
All Implemented Interfaces:
Context
Direct Known Subclasses:
AdviceContext, ApiVersionRouterContext, AttributesContext, ClientContext, MessageContextImpl, OAuth2Context, RequestAuditContext, RootContext, SecurityContext, SelfServiceContext, SessionContext, TransactionIdContext, UriRouterContext

public abstract class AbstractContext extends Object implements Context
A base implementation of the Context interface. Derived Contexts MUST support persistence by providing See the documentation for more details.

Here is an example of the JSON representation of the core attributes of all contexts:

 {
   "id"     : "56f0fb7e-3837-464d-b9ec-9d3b6af665c3",
   "class"  : ...Java class name..,
   "parent" : {
       ...
   }
 }
 
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final JsonValue
    The Context data.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    AbstractContext(String id, String name, Context parent)
    Constructs a new AbstractContext.
     
    AbstractContext(JsonValue savedContext, ClassLoader classLoader)
    Creates a new context from the JSON representation of a previously persisted context.
    protected
    Constructs a new AbstractContext with a null id.
  • Method Summary

    Modifier and Type
    Method
    Description
    final <T extends Context>
    Optional<T>
    as(Class<T> clazz)
    Returns an @{link Optional} which contains the first context in the chain whose type is a sub-type of the provided Context class if one exists, or an empty optional if none is present.
    final <T extends Context>
    T
    asContext(Class<T> clazz)
    Returns the first context in the chain whose type is a sub-type of the provided Context class.
    final boolean
    containsContext(Class<? extends Context> clazz)
    Returns true if there is a context in the chain whose type is a sub-type of the provided Context class.
    final boolean
    containsContext(String contextName)
    Returns true if there is a context in the chain whose name matches the provided context name.
    get(String contextName)
    Returns an Optional containing the first context in the chain whose context name matches the provided name, or an empty Optional if none is present.
    final Context
    getContext(String contextName)
    Returns the first context in the chain whose context name matches the provided name.
    final String
    Get this Context's name.
    final String
    Returns the unique ID identifying this context, usually a UUID.
    final Context
    Returns the parent of this context.
    Returns the unique ID of the root context, usually a UUID.
    final boolean
    Returns true if this context is a root context.
    Return this Context as a JsonValue (for persistence).
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • data

      protected final JsonValue data
      The Context data.
  • Constructor Details

    • AbstractContext

      protected AbstractContext(Context parent, String name)
      Constructs a new AbstractContext with a null id.
      Parameters:
      parent - The parent context.
      name - The name of the context.
    • AbstractContext

      protected AbstractContext(String id, String name, Context parent)
      Constructs a new AbstractContext.
      Parameters:
      id - The id of the context.
      parent - The parent context.
      name - The name of the context.
    • AbstractContext

      public AbstractContext(JsonValue savedContext, ClassLoader classLoader)
      Creates a new context from the JSON representation of a previously persisted context.

      Sub-classes MUST provide a constructor having the same declaration as this constructor in order to support persistence. Implementations MUST take care to invoke the super class implementation before parsing their own context attributes. Below is an example implementation for a security context which stores the user name and password of the authenticated user:

       protected SecurityContext(JsonValue savedContext, ClassLoader classLoader) {
           // Invoke the super-class implementation first.
           super(savedContext, classLoader);
      
           // Now parse the attributes for this context.
           this.username = savedContext.get("username").required().asString();
           this.password = savedContext.get("password").required().asString();
       }
       
      In order to create a context's persisted JSON representation, implementations must override.
      Parameters:
      savedContext - The JSON representation from which this context's attributes should be parsed.
      classLoader - The ClassLoader which can properly resolve the persisted class-name.
  • Method Details

    • getContextName

      public final String getContextName()
      Description copied from interface: Context
      Get this Context's name.
      Specified by:
      getContextName in interface Context
      Returns:
      this object's name
    • asContext

      public final <T extends Context> T asContext(Class<T> clazz)
      Description copied from interface: Context
      Returns the first context in the chain whose type is a sub-type of the provided Context class. The method first checks this context to see if it has the required type, before proceeding to the parent context, and then continuing up the chain of parents until the root context is reached.
      Specified by:
      asContext in interface Context
      Type Parameters:
      T - The context type.
      Parameters:
      clazz - The class of context to be returned.
      Returns:
      The first context in the chain whose type is a sub-type of the provided Context class.
    • as

      public final <T extends Context> Optional<T> as(Class<T> clazz)
      Description copied from interface: Context
      Returns an @{link Optional} which contains the first context in the chain whose type is a sub-type of the provided Context class if one exists, or an empty optional if none is present. The method first checks this context to see if it has the required type, before proceeding to the parent context, and then continuing up the chain of parents until the root context is reached.
      Specified by:
      as in interface Context
      Type Parameters:
      T - The context type.
      Parameters:
      clazz - The class of context to be returned.
      Returns:
      An Optional containing the first context in the chain whose type is a sub-type of the provided Context class, or an empty optional if no such Context exists in the chain.
    • getContext

      public final Context getContext(String contextName)
      Description copied from interface: Context
      Returns the first context in the chain whose context name matches the provided name.
      Specified by:
      getContext in interface Context
      Parameters:
      contextName - The name of the context to be returned.
      Returns:
      The first context in the chain whose name matches the provided context name.
    • get

      public Optional<Context> get(String contextName)
      Description copied from interface: Context
      Returns an Optional containing the first context in the chain whose context name matches the provided name, or an empty Optional if none is present.
      Specified by:
      get in interface Context
      Parameters:
      contextName - The name of the context to be returned.
      Returns:
      An Optional containing the first context in the chain whose name matches the provided context name, an empty optional if none is present.
    • containsContext

      public final boolean containsContext(Class<? extends Context> clazz)
      Description copied from interface: Context
      Returns true if there is a context in the chain whose type is a sub-type of the provided Context class. The method first checks this context to see if it has the required type, before proceeding to the parent context, and then continuing up the chain of parents until the root context is reached.
      Specified by:
      containsContext in interface Context
      Parameters:
      clazz - The class of context to be checked.
      Returns:
      true if there is a context in the chain whose type is a sub-type of the provided Context class.
    • containsContext

      public final boolean containsContext(String contextName)
      Description copied from interface: Context
      Returns true if there is a context in the chain whose name matches the provided context name.
      Specified by:
      containsContext in interface Context
      Parameters:
      contextName - The name of the context to locate.
      Returns:
      true if there is a context in the chain whose context name matches contextName.
    • getId

      public final String getId()
      Description copied from interface: Context
      Returns the unique ID identifying this context, usually a UUID. If no ID has been defined then the ID of the parent context will be returned.
      Specified by:
      getId in interface Context
      Returns:
      The unique ID identifying this context. If no ID has been defined then the ID of the parent context will be returned.
    • getRootId

      public String getRootId()
      Description copied from interface: Context
      Returns the unique ID of the root context, usually a UUID.
      Specified by:
      getRootId in interface Context
      Returns:
      The unique ID of the root context.
    • getParent

      public final Context getParent()
      Description copied from interface: Context
      Returns the parent of this context.
      Specified by:
      getParent in interface Context
      Returns:
      The parent of this context, or null if this context is a root context.
    • isRootContext

      public final boolean isRootContext()
      Description copied from interface: Context
      Returns true if this context is a root context.
      Specified by:
      isRootContext in interface Context
      Returns:
      true if this context is a root context.
    • toJsonValue

      public JsonValue toJsonValue()
      Description copied from interface: Context
      Return this Context as a JsonValue (for persistence).
      Specified by:
      toJsonValue in interface Context
      Returns:
      the Context data as a JsonValue.
    • toString

      public String toString()
      Overrides:
      toString in class Object