Implementing Interceptor in Hybris

As we learned in Interceptors-in-Hybris article, We have different types of interceptors in Hybris which will be called automatically during specific step of model life cycle.

Hybris has provided 5 interfaces to intercept at specific step of model lifecycle.

We need to create a new class and implement the suitable interface out of those 5 interfaces as per our requirement

If we want to intercept model while saving it, we can use PrepareInterceptor

If we want to perform some logic before model is being removed from DB, we can use RemoveInterceptor

Similarly, other interfaces also can be chosen based on where exactly we need to intercept the model lifecycle.

Let’s implement PrepareInterceptor


We know that, Prepare interceptor will be called while saving the model

Requirement :
Save consignment carrier code to Consignment model while saving it. Carrier code is available in CarrierModel


Step 1

Create a new Java class which implements PrepareInterceptor and override onPrepare() method.

  1. package de.hybris.platform.consignmenttrackingservices.interceptor;
  2.  
  3. import de.hybris.platform.consignmenttrackingservices.model.CarrierModel;
  4. import de.hybris.platform.ordersplitting.model.ConsignmentModel;
  5. import de.hybris.platform.servicelayer.interceptor.InterceptorContext;
  6. import de.hybris.platform.servicelayer.interceptor.InterceptorException;
  7. import de.hybris.platform.servicelayer.interceptor.PrepareInterceptor;
  8.  
  9. public class ConsignmentPrepareInterceptor implements PrepareInterceptor<ConsignmentModel>
  10. {
  11.  
  12.     @Override
  13.     public void onPrepare(ConsignmentModel consignment, InterceptorContext ctx) throws InterceptorException
  14.     {
  15.         final CarrierModel carrier = consignment.getCarrierDetails();
  16.         final String carrierCode = carrier == null ? null : carrier.getCode();
  17.         consignment.setCarrier(carrierCode);
  18.     }
  19.  
  20. }
package de.hybris.platform.consignmenttrackingservices.interceptor;

import de.hybris.platform.consignmenttrackingservices.model.CarrierModel;
import de.hybris.platform.ordersplitting.model.ConsignmentModel;
import de.hybris.platform.servicelayer.interceptor.InterceptorContext;
import de.hybris.platform.servicelayer.interceptor.InterceptorException;
import de.hybris.platform.servicelayer.interceptor.PrepareInterceptor;

public class ConsignmentPrepareInterceptor implements PrepareInterceptor<ConsignmentModel>
{

	@Override
	public void onPrepare(ConsignmentModel consignment, InterceptorContext ctx) throws InterceptorException
	{
		final CarrierModel carrier = consignment.getCarrierDetails();
		final String carrierCode = carrier == null ? null : carrier.getCode();
		consignment.setCarrier(carrierCode);
	}

}


In this class, we have taken carrier code from CarrierModel and setting it to ConsignmentModel explicitly.

Step 2

Register and declare the ConsignmentPrepareInterceptor as spring bean

  1. <!-- Defining Spring bean for Interceptor -->
  2. <bean id="consignmentPrepareInterceptor" class="de.hybris.platform.consignmenttrackingservices.interceptor.ConsignmentPrepareInterceptor"/>
<!-- Defining Spring bean for Interceptor -->
<bean id="consignmentPrepareInterceptor" class="de.hybris.platform.consignmenttrackingservices.interceptor.ConsignmentPrepareInterceptor"/>

Step 3

Map this interceptor with corresponding model

  1. <!-- Mapping Interceptor and model class-->
  2. <bean id="consignmentPrepareInterceptorMapping" class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">
  3.         <property name="interceptor" ref="consignmentPrepareInterceptor"/>
  4.         <property name="typeCode" value="Consignment"/>
  5.     </bean>
<!-- Mapping Interceptor and model class-->
<bean id="consignmentPrepareInterceptorMapping" class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">
		<property name="interceptor" ref="consignmentPrepareInterceptor"/>
		<property name="typeCode" value="Consignment"/>
	</bean>


This mapping makes sure that, mapped interceptor will be intercepting only the mapped model’s lifecycle.

In this case, consignmentPrepareInterceptor will be intercepting the lifecycle of ConsignmentModel only

Step 4

We just need to build and start the server

Note :
If we implement ValidateInterceptor also for the same Model then ValidateInterceptor is always invoked after PrepareInterceptor. All the above steps are same to implement any of the above 5 interceptors provided by Hybris. We just need to override corresponding method and provide the logic as per the requirement

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on