Announcement: You can find the guides for Commerce 7.5 and later on the new Elastic Path Documentation site. This Developer Center contains the guides for Commerce 6.13.0 through 7.4.1.Visit new site

This version of Elastic Path Commerce is no longer supported or maintained. To upgrade to the latest version, contact your Elastic Path representative.

Including audit metadata

Including audit metadata

When Object Auditing is enabled, the CM client passes transaction-related metadata to the server's persistence layer, such as the CM client user ID, so that it can be included in the audit records. This is done by adding information to the Spring remoting invocation.

AuditHttpInvokerProxyFactoryBean extends Spring's HttpInvokerProxyFactoryBean class. It overrides the executeRequest method and adds an attribute to the RemoteInvocation containing a map of key/value pairs of auditing metadata (i.e. CM user GUID and Change Sets GUID).

To include audit metadata when a service is invoked from the CM client, find the service bean definition in com.elasticpath.cmclient/com.elasticpath.cmclient.core/conf/spring/service/serviceCMClient.xml and set its class to AuditHttpInvokerProxyFactoryBean. The following example shows how you would change the product service bean definition to support passing metadata:

<bean id="productService" class="com.elasticpath.cmclient.core.AuditHttpInvokerProxyFactoryBean">
  <property name="serviceUrl">
    <value>productService.remote</value>
  </property>
  <property name="serviceInterface">
    <value>com.elasticpath.service.catalog.ProductService</value>
  </property>
</bean>

By default, the AuditHttpInvokerProxyFactoryBean class's getMetadata() method is implemented as follows:

protected HashMap<String, String> getMetadata() {
  HashMap<String, String> auditingMetadata = new HashMap<String, String>();
  auditingMetadata.put("userGuid", Application.getInstance().getCmUser().getGuid()); //$NON-NLS-1$
  return auditingMetadata;
}

This can be extended to include additional audit metadata. Simply add key/value pairs to the map. This map will be passed in the remote invocation.

You'll also need to make appropriate changes to the CM server to receive and process that metadata. AuditHttpInvokerServiceExporter extends Spring's HttpInvokerServiceExporter class. It overrides the readRemoteInvocation method and gets the map of auditing metadata from the RemoteInvocation. It adds this metadata to a ThreadLocalMap that can be accessed by the persistence auditing listener.

To retrieve audit metadata from a remote service invocation, locate the service bean definition in com.elasticpath.cm/WEB-INF/conf/spring/service/serviceHttp.xml and set its class to AuditHttpInvokerServiceExporter. The following example shows how you would change the product service bean definiton:

<bean id="httpProductService" class="com.elasticpath.service.audit.impl.AuditHttpInvokerServiceExporter">
  <property name="service" ref="productService"/commerce-legacy/>
  <property name="serviceInterface">
    <value>com.elasticpath.service.catalog.ProductService</value>
  </property>
</bean>

The thread local map bean is defined as follows:

<bean id="persistenceListenerMetadataMap" class="com.elasticpath.commons.ThreadLocalMap" scope="singleton" />

The ThreadLocalMap class is an implementation of the Map interface that is local to the current thread. It will contain key/value pairs of audit metadata that can be consumed by the auditing listener and stored as part of the audit records.