Use Interceptors in SAP Hybris

Hybris featured image

1. Overview

A Model in Hybris has a lifecycle managed by the ModelService (Create, Update, Load, Remove).

Hybris offers interceptors that provide the possibility to Hook into model lifecycle to be able to act when an event occurs.

Overview of interceptors Hybris

In this article, I will show you the various types of interceptors in Hybris and how to create them.

In the next section of this article we will create interceptors for UserModel.

2. Implementation

2.1. Create InitDefaultsInterceptor

The InitDefaultsInterceptor is called when ever you create a new model using modelService.initDefaults() or modelService.create().

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

package com.stackextend.core.interceptors;

// imports...

public class UserInitDefaultInterceptor implements InitDefaultsInterceptor<UserModel> {

    @Override
    public void onInitDefaults(UserModel userModel, InterceptorContext interceptorContext) throws InterceptorException {

        // do what ever business logic you want here...
        System.out.println("You are trying to create/init UserModel: " + userModel.toString());

    }
}

2. Register the UserInitDefaultInterceptor as a bean in Spring.

<!-- ...\hybris\bin\custom\training\trainingcore\resources\trainingcore-spring.xml -->
<bean id="userInitDefaultInterceptor" class="com.stackextend.core.interceptors.UserInitDefaultInterceptor">
	
	<!--  inject beans here if needed -->
	
</bean>

3. Declare and configure your interceptor using InterceptorMapping.

<bean id="userInitDefaultInterceptorMapping" class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">
   <property name="interceptor" ref="userInitDefaultInterceptor"/>
   <property name="typeCode" value="User"/>
</bean>

4. Create a new instance of UserModel to see your interceptor being executed.

// 1. This is one way to invoke the InitDefaultInterceptor
UserModel newUser = new UserModel();
modelService.initDefaults(newUser);

// 2. This is another way to invoke the InitDefaultInterceptor
UserModel newUser = modelService.create(UserModel.class);

2.2. Create LoadInterceptor

The LoadInterceptor is invoked just after a model is being retrieved from the database, using modelService.get() for example.

1. Create a Java class implements LoadInterceptor and override onLoad() method.

package com.stackextend.core.interceptors;

// imports....

public class UserLoadInterceptor implements LoadInterceptor<UserModel> {

    @Override
    public void onLoad(UserModel userModel, InterceptorContext interceptorContext) throws InterceptorException {
		
        // do what ever business logic you want here...
        System.out.println("This UserModel is just being loaded: " + userModel.toString());
		
    }
}

2. Register the UserLoadInterceptor as a bean in Spring.

<!-- ...\hybris\bin\custom\training\trainingcore\resources\trainingcore-spring.xml -->
<bean id="userLoadInterceptor" class="com.stackextend.core.interceptors.UserLoadInterceptor">

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

3. Declare and configure your interceptor using InterceptorMapping.

<!-- ...\hybris\bin\custom\training\trainingcore\resources\trainingcore-spring.xml -->
<bean id="userLoadInterceptorMapping" class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">
	<property name="interceptor" ref="userLoadInterceptor"/>
	<property name="typeCode" value="User"/>
</bean>

4. The UserLoadInterceptor will be invoked when ever a UserModel is being retrieved from the database.

// The userLoadInterceptor will be invoked here
UserModel userModel = modelService.get(null);

2.3. Create RemoveInterceptor

The RemoveInterceptor is called before a model is being removed from the database.

1. Create a Java class implements RemoveInterceptor and override onRemove() method.

package com.stackextend.core.interceptors;

// imports...

public class UseRemoveInterceptor implements RemoveInterceptor<UserModel> {

    @Override
    public void onRemove(UserModel userModel, InterceptorContext interceptorContext) throws InterceptorException {
        
        // do what ever business logic you want here...
        System.out.println("This UserModel will be removed from the database: " + userModel.toString());
        
    }
}

2. Register and declare the UseRemoveInterceptor as bean in Spring like the previous examples.

3. The UserRemoveInterceptor will be invoked when ever you remove a UserModel from the database.

// The UserRemoveInterceptor will be invoked right here
modelService.remove(userToBeRemove);

2.4. Generalization

There are also PrepareInterceptor and ValidateInterceptor, they are called just before a model is being saved to the database, for example when modelService.save() is used.

The ValidateInterceptor is always invoked after PrepareInterceptor.

 

6
Leave a Reply

avatar
3 Comment threads
3 Thread replies
3 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
wang sallyNickMouad EL FakirAshish Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of
Ashish
Guest
Ashish

Thanks for sharing. It’s very helpful.

Nick
Guest
Nick

Hey Mouad!
Could you please explain the PersistenceOperation approach?

What is the difference between PersistenceOperation.SAVE and just writing modelService.save()?

Thank you!

wang sally
Guest
wang sally

Hi Mouad,
I run into a problem, I add some logical code in CustomerLoadInterceptor to modify the attribute value of customer model, when I call customer.getXX() by groovy in hac, it’ll execute the logical code in CustomerLoadInterceptor, but when I call it by postman, the code I added in CustomerLoadInterceptor do not execute. why?