Class PopulateAuditContext

All Implemented Interfaces:
Component, DestructableComponent, InitializableComponent, ProfileAction, Aware, MessageSource, MessageSourceAware, Action

public class PopulateAuditContext extends AbstractProfileAction
Action that populates fields in an AuditContext using injected functions.

Each function is registered against a field specifier, and the result of the function is added to that field in the context's map. This reduces the code footprint required to implement, and extend, the fields logged, instead of requiring a dedicated action for a particular field or set of fields.

The eventual map of formatting strings is also provided in order to recognize which extractors actually need to be run.

Event:
EventIds.PROCEED_EVENT_ID, EventIds.INVALID_PROFILE_CTX
  • Field Details

    • log

      @Nonnull private final org.slf4j.Logger log
      Class logger.
    • auditContextCreationStrategy

      @Nonnull private Function<ProfileRequestContext,AuditContext> auditContextCreationStrategy
      Strategy used to locate or create the AuditContext to populate.
    • fieldExtractors

      @Nonnull @NonnullElements private Map<String,Function<ProfileRequestContext,Object>> fieldExtractors
      Map of fields to extract and the corresponding extraction functions.
    • fieldsToExtract

      @Nonnull @NonnullElements private Set<String> fieldsToExtract
      Fields being audited, to optimize extraction..
    • fieldReplacements

      @Nonnull private Map<String,String> fieldReplacements
      Map allowing substitutions of values during field extraction.
    • dateTimeFormatter

      @Nonnull private DateTimeFormatter dateTimeFormatter
      Formatter for date/time fields.
    • useDefaultTimeZone

      private boolean useDefaultTimeZone
      Convert date/time fields to default time zone.
    • auditCtx

      @Nullable private AuditContext auditCtx
      AuditContext to populate.
  • Constructor Details

    • PopulateAuditContext

      public PopulateAuditContext()
      Constructor.
  • Method Details

    • setAuditContextCreationStrategy

      public void setAuditContextCreationStrategy(@Nonnull Function<ProfileRequestContext,AuditContext> strategy)
      Set the strategy used to locate the AuditContext associated with a given ProfileRequestContext.
      Parameters:
      strategy - lookup strategy
    • setFieldExtractors

      public void setFieldExtractors(@Nonnull @NonnullElements Map<String,Function<ProfileRequestContext,Object>> map)
      Set the map of fields and extraction functions to run.
      Parameters:
      map - map from field name to extraction function
    • setFormattingMapParser

      public void setFormattingMapParser(@Nonnull PopulateAuditContext.FormattingMapParser parser)
      Set the parsed map of logging category to formatting strings for log entries.

      A formatting string consists of tokens prefixed by '%' separated by any non-alphanumeric or whitespace. Tokens can contain any letter or number or a hyphen. Anything other than a token, including whitespace, is a literal.

      The input class exposes the parsed field data for efficiency.

      Parameters:
      parser - the parsed map
    • setFieldReplacements

      public void setFieldReplacements(@Nullable Map<String,String> map)
      Set the map of field values to replace, and the replacement values.
      Parameters:
      map - map of replacements
    • setDateTimeFormat

      public void setDateTimeFormat(@Nullable @NotEmpty String format)
      Set the formatting string to apply when extracting date/time fields.
      Parameters:
      format - formatting string
    • setUseDefaultTimeZone

      public void setUseDefaultTimeZone(boolean flag)
      Convert date/time fields to default time zone.
      Parameters:
      flag - flag to set
    • doInitialize

      protected void doInitialize() throws ComponentInitializationException
      Overrides:
      doInitialize in class AbstractInitializableComponent
      Throws:
      ComponentInitializationException
    • doPreExecute

      protected boolean doPreExecute(@Nonnull ProfileRequestContext profileRequestContext)
      Overrides:
      doPreExecute in class AbstractConditionalProfileAction
    • doExecute

      protected void doExecute(@Nonnull ProfileRequestContext profileRequestContext)
      Overrides:
      doExecute in class AbstractProfileAction
    • addField

      private void addField(@Nonnull @NotEmpty String key, @Nullable Object value)
      Add a non-null field to the audit record.
      Parameters:
      key - field label
      value - value to add