Home » Interceptor

Interceptor

As we have discussed that models and item are essential part while we are doing implementation in Commerce cloud.

  • Interceptor provides a way to interrupt the models during its life-cycle, while we need to do some modifications or validations in models.
  • Every model has its own life cycle.
  • In Every life cycle there are five stages:
    • Create and Initialize 
    • Modify
    • Save
    • Load 
    • Remove
  • During these 5 stages you can interrupt or modify the models explicitly using Interceptors.

Types Of Interceptor:

There are 5 types of interceptors according to their use listed below:

 

1. Init Defaults Interceptor:

  • The Init Defaults Interceptor is called when a model is filled with its default values.
  • You can create a model with one of the following methods:
    • modelService.create method
    • modelService.initDefaults method 

         When these methods are executed, Init Defaults Interceptor is called.

  • You can use this interceptor to fill the model with additional default values, apart from the values defined in the items.xml file.

2. Prepare Interceptor:

  • The Prepare Interceptor is called before a model is saved to the database before it is validated by Validate interceptors.
  • Use this to add values to the model or modify existing ones before they are saved. 

 3. Validate Interceptor :

  • The Validate Interceptor is called before a model is saved to the database after is been prepared by the Prepare interceptors, above.
  • You can use Validate Interceptors to validate values of the model and raise an InterceptorException if any values are not valid.

4. Load Interceptor:

  • The Load Interceptor is called when a model is being loaded from the database.
  • If you want to change values of the model after loading from database You can use this Interceptor. 

5. Remove Interceptor:

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

How to create Interceptors? Let’s See