Interceptors

We are aware about Hybris models that represent to database tables in Hybris. we often use models to save data in database and to load data from Database. These models use own lifecycle in Hybris for managed by ModelService. To intercept the behavior of life cycles of models, various types of interceptors have been developed. Each such interceptor addresses a particular step of the life cycle.

Interceptors can interrupt the lifecycle of a model and execute some logic and then lifecycle of the model can continue its process. Hybris offers interceptors that provide the possibility to Hook into model lifecycle to be able to act when an event occurs.

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.

Types of interceptors

Hybris provide below 5 type interceptors

  1. Init Defaults interceptor

  2. Prepare interceptor

  3. Validate interceptor

  4. Load interceptor

  5. Remove interceptor

Init Defaults Interceptor

The Init Defaults Interceptor is used for provide default values to model. This will called either when model is created via the modelService.create method or when the modelService.initDefaults method is called. we can use this interceptor to provide model with additional default values, apart from the values defined in the items.xml file

package de.hybris.platform.servicelayer.interceptor;


public interface InitDefaultsInterceptor extends Interceptor

{

void onInitDefaults(Object model, InterceptorContext ctx) throws InterceptorException;

}


Prepare interceptor

The Prepare Interceptor is called before a model is saved to the database and Validate interceptors, It is use to add values to the model or modify existing ones before model saved. in OOTB, Prepare interceptor is called before the impex translators. we should not use this interceptor to perform validation.

package de.hybris.platform.servicelayer.interceptor;


public interface PrepareInterceptor extends Interceptor

{

void onPrepare(Object model, InterceptorContext ctx) throws InterceptorException;

}

Validate Interceptor

The Validate Interceptor is called before a model is saved to the database and after Prepare interceptors. we can use Validate Interceptors to validate values of the model and raise an InterceptorException if any validation fail.

package de.hybris.platform.servicelayer.interceptor;


public interface ValidateInterceptor extends Interceptor

{

void onValidate(Object model, InterceptorContext ctx) throws InterceptorException;

}

Load interceptor

The Load Interceptor is called when model is loaded from the database. we can use this interceptor if we want to change any values of the model after load. An exception occur during execution to prevents model from being loaded.

package de.hybris.platform.servicelayer.interceptor;

public interface LoadInterceptor extends Interceptor

{

void onLoad(Object model, InterceptorContext ctx) throws InterceptorException;

}


Remove interceptor

The Remove Interceptor is called before a model is removed from the database. we can use this interceptor


package de.hybris.platform.servicelayer.interceptor;

public interface RemoveInterceptor extends Interceptor

{

void onRemove(Object model, InterceptorContext ctx) throws InterceptorException;

}



Interceptor Implementation

Below are four step need to follow for interceptor implementation

  1. Create a Java class with implements specific interceptor and override interceptor method.

  2. Register the java class as a bean in Spring.

  3. Declare and configure your interceptor using InterceptorMapping.

  4. interceptor will call on specific model life cycle state.

Example

InitDefaultsInterceptor

1. Create a Java class implements InitDefaultsInterceptor and override onInitDefaults() method.

package com.javaoops.interceptors;

//add required import


public class CustomerInitDefaultInterceptor implements InitDefaultsInterceptor<CustomerModel> {

@Override

public void onInitDefaults(CustomerModel customerModel, InterceptorContext interceptorContext) throws InterceptorException {


// add business logic whatever you want

System.out.println("You are createing or init CustomerModel: " + customerModel.toString());


}

}

2. Register the CustomerInitDefaultInterceptor as a bean in Spring. (myextensioncore-spring.xml)

<bean id="customerInitDefaultInterceptor" class="com.javaoops.interceptors.CustomerInitDefaultInterceptor">


<!-- inject beans here if needed -->


</bean>

3. Declare and configure interceptor using InterceptorMapping.

<bean id="customerInitDefaultInterceptorMapping" class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">

<property name="interceptor" ref="customerInitDefaultInterceptor"/>

<property name="typeCode" value="Customer"/>

</bean>

4. Create a new instance of CustomerModel by using below code then interceptor will be executed.

// 1. This is one way to invoke the InitDefaultInterceptor

CustomerModel customer = new CustomerModel();

modelService.initDefaults(customer);


// 2. This is another way to invoke the InitDefaultInterceptor

CustomerModel customer = modelService.create(CustomerModel.class);

In same way , we can implement other interceptor as well.