Hybris provide below 5 type interceptors
Init Defaults interceptor
Prepare interceptor
Validate interceptor
Load interceptor
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;
}
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;
}
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;
}
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;
}
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
Create a Java class with implements specific interceptor and override interceptor method.
Register the java class as a bean in Spring.
Declare and configure your interceptor using InterceptorMapping.
interceptor will call on specific model life cycle state.
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.