Interceptors in SAP Commerce(Hybris)

Nuray Fahri
5 min readMar 13, 2021

--

Interceptors are called when we perform an operation on a particular object/model. They can be called at different lifecycles of ModelService object.

As we know the Models have it’s own lifecycle managed by ModelService. The predefined methods which represents the Model life cycle are:

Create: to create model instance;

Update: to update/modify the properties of the model;

Save: when we create or update Model we save back the model to update the database. If we have used a new model, a new record is created in the database, otherwise, the existing record is updated;

Load: to get existing model from the database;

Remove: to delete no longer needed records from the database.

We can use interceptors to modify the model, raise an exception, to interrupt the current step or publish an event. Interceptors can interrupt the lifecycle of a model and execute some logic and then lifecycle of the model can continue its process.

Types of interceptors in Hybris

1) Init Defaults Interceptor

This Interceptor is called when we create a new model filled with its default values calling modelService.initDefaults() or modelService.create(). We can use this interceptor to fill the model with additional default values.

To use this interceptor we must implement InitDefaultsInterceptor interface and then override onInitDefaults() method.

After implementing an interceptor, register it as a Spring bean:

1. In customextension-spring.xml add the following line

2. And add this line in the same file to do Interceptor mapping:

2) Prepare Interceptor

This Interceptor is called before a model is saved to the database(modelService.save()) and before it is validated by Validate interceptors. We can use this Interceptor to add values to the model or modify existing ones before they are saved. It is always called before the impex translators.

To use this interceptor we must implement PrepareInterceptor interface and then override onPrepare() method.

Then register and map the CustomProductPrepareInterceptor as bean in Spring like the first example.

3) Validate Interceptor

This Interceptor is called before a model is saved to the database(modelService.save()) after is been prepared by the Prepare interceptors,. You can use Validate Interceptors to validate values of the model and raise an InterceptorException if any values are not valid. An exception raised during execution prevents the model from being saved.

To use this interceptor we must implement ValidateInterceptor interface and then override onValidate() method.

Then register and map the ProductValidateInterceptor as bean in Spring like the first example.

4) Load Interceptor

This Interceptor is called after a model is retrieved from the database using modelService.get(). We can use this interceptor if we want to change to change the values of the model even after being loaded from the Database

To use this interceptor we must implement LoadInterceptor interface and then override onLoad() method.

Then register and map the ProductLoadInterceptor as bean in Spring like the first example.

5) Remove Interceptor

This Interceptor is called before a model is removed from the database using modelService.remove(). We can use remove interceptor whenever we need to remove models that are related to the model but are not in the model context or to prevent the removal of the model by raising an InterceptorException.

To use this interceptor we must implement RemoveInterceptor interface and then override onRemove() method.

Then register and map the ProducRemoveInterceptor as bean in Spring like the first example.

How can we disable Interceptors?

We can disable interceptors in session programmatically using sessionSercice class or via ImpEx Import.

Or we can disable interceptors for data integration scenarious. Disabling interceptors is useful from performance point of view.

Disable Interceptors in the Code

To disable interceptors in code, use the sessionService.executeInLocalViewWithParams method. It is an implementation of a template method pattern and takes a map of attributes that are set in a session before execution of the provided callback method, and reverted afterwards.

The following code snippet shows how to disable on a per-bean basis B2BUnitModelValidateInterceptor during creation of a new B2BUnit:

final Map<String, Object> params = ImmutableMap.of(InterceptorExecutionPolicy.DISABLED_INTERCEPTOR_BEANS,
ImmutableSet.of("B2BUnitModelValidateInterceptor"));

getSessionService().executeInLocalViewWithParams(params, new SessionExecutionBody()
{
@Override
public void executeWithoutResult()
{
getModelService().save(newUnit);
}
});jj

We can also disable all validate interceptors:

final Map<String, Object> params = ImmutableMap.of(InterceptorExecutionPolicy.DISABLED_INTERCEPTOR_TYPES,
ImmutableSet.of(InterceptorExecutionPolicy.InterceptorType.VALIDATE));

getSessionService().executeInLocalViewWithParams(params, new SessionExecutionBody()
{
@Override
public void executeWithoutResult()
{
getModelService().save(newUnit);
}
});jj

Disable Interceptors via ImpEx

To disable all validator type interceptors, use the disable.interceptor.types=validate header attribute:


INSERT_UPDATE B2BUnit[disable.interceptor.types=validate];name[unique=true];uid
;Custom Retail;0001010512_1010_10_00

To disable specific interceptors, specify a comma-separated bean-IDs list for the disable.interceptor.beans header attribute. The following impex specifies only one such interceptor:

INSERT_UPDATE B2BUnit[disable.interceptor.beans='B2BUnitModelValidateInterceptor'];name[unique=true];uid
;Custom Retail;0001010512_1010_10_00

--

--