Chapter 18. Auditing
18.1. SwitchYard Auditing
META-INF/beans.xml file) to run. The auditing functionality also works in a test environment.
18.2. Enable Custom Auditors
@Named annotation. It helps Apache Camel component to recognize all the auditor implementations. Camel Exchange Bus, a default implementation used by SwitchYard, look up for bean definitions with @Audit annotation.
@Audit
@Named("custom auditor")
public class SimpleAuditor implements Auditor
{
@Override
public void beforeCall(Processors processor, Exchange exchange)
{
System.out.println("Before " + processor.name());
}
@Override
public void afterCall(Processors processor, Exchange exchange)
{
System.out.println("After " + processor.name());
}
}
Note
18.3. Mediation State
18.4. List of Mediation States
- Domain handlers
- In this state all the handlers defined in
switchyard.xmlare executed. This is an early phase of mediation where you can either implement own logic or choose the service provider logic to use. - Addressing
- If this is not specified by the domain handlers then the addressing handler will determine what to do by using the consumer contract.
- Transaction
- If the service is required to run a transaction this handler starts it.
- Security
- This state verifies constraints related to authentication and authorization.
- General policy
- This executes checks other than those for security and transactions.
- Validation
- This executes custom validators.
- Transformation
- This prepares the payload by calling a provider.
- Validation (2)
- This validates the transformed payload.
- Provider call
- This calls the provisional service.
- Transaction (2)
- This commits or, if necessary, rolls back the transaction.
- Domain handlers
- These are called when a response is generated by a provider service.
- Validation
- This verifies the output generated by the provider.
- Transformation
- This converts the payload to the structure required by the consumer.
- Validation
- This checks the output after the transformation has occurred.
- Consumer callback
- This returns the exchange to the service consumer.
18.5. Create a Custom Auditor
Prerequisities
- CDI Runtime
- Annotate your auditor implementations with @Named in order to have Camel recognize them.
Note
The Camel Exchange Bus looks for bean definitions with the @Audit annotation.Here is code that shows what a very simple auditor would look like:@Audit @Named("custom auditor") public class SimpleAuditor implements Auditor { @Override public void beforeCall(Processors processor, Exchange exchange) { System.out.println("Before " + processor.name()); } @Override public void afterCall(Processors processor, Exchange exchange) { System.out.println("After " + processor.name()); } }Important
Be aware that the afterCall method is not called if the step it surrounds throws an exception. If this happens, afterCall will be skipped.
You can see many statements like 'Before DOMAIN_HANDLERS' and 'Before ADDRESSING' appearing in the server console. This is because every step of mediation is surrounded by this SimpleAuditor class.
18.6. Determine Location for Audit Assignment
@Audit(Processors.VALIDATION)
- The validation is executed twice for in-only exchanges and four times for in-out exchanges.
- The validation occurs before and after transformation of inbound messages.
- When SwitchYard sends outgoing messages, the validation occurs before and after transformation of outbound messages.
- Transformation is executed once for in-only exchanges and twice for in-out exchanges.
- Transaction phase is always executed twice.
@Audit(Processors.PROVIDER_CALLBACK).Here, the auditor is executed just before sending exchange to service implementation. You can also implement one auditor instance with few mediation steps. For example, a bean with annotation following:
@Audit({Processors.PROVIDER_CALLBACK, Processors.CONSUMER_CALLBACK})
This bean is executed twice. One pair of before or after call for provider service and second pair for outgoing response.
18.7. Use Exchange Properties
@Named("custom auditor")
public class SimpleAuditor implements Auditor {
private Logger _logger = Logger.getLogger(SimpleAuditor.class);
@Override
public void beforeCall(Processors processor, Exchange exchange) {
exchange.setProperty("time", System.currentTimeMillis());
}
@Override
public void afterCall(Processors processor, Exchange exchange) {
long time = System.currentTimeMillis() - exchange.getProperty("time", 0, Long.class);
_logger.info("Step " + processor.name() + " took " + time + "ms");
}
}

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.