Custom Interceptors in SAP Hybris

Implementing Custom Interceptors in SAP Hybris

SAP Hybris provides a robust platform for customization, and interceptors play a crucial role in extending and modifying the behavior of core models and services. This guide focuses on how to create and configure custom interceptors in Hybris.


What Are Interceptors?

Interceptors are hooks that allow developers to add custom logic at different lifecycle stages of a model, such as:

  • Before Save (PrepareInterceptor)
  • Validation (ValidateInterceptor)
  • After Save (AfterSaveEvent)
  • Remove (RemoveInterceptor)

They are particularly useful for maintaining data integrity, triggering external processes, or implementing custom business rules.


Types of Interceptors in Hybris

  1. PrepareInterceptor: Executes before the model is saved. Ideal for auto-generating data or pre-processing values.
  2. ValidateInterceptor: Validates the data before the save operation.
  3. RemoveInterceptor: Triggered when a model instance is removed.
  4. LoadInterceptor: Executes after loading the model from the database.
  5. AfterSaveEvent Listener: Useful for handling post-save events asynchronously.

Step-by-Step: Creating a Custom Interceptor

Let’s walk through creating a custom ValidateInterceptor for ensuring that a product’s stock value is not negative.


Step 1: Define the Interceptor

Create a new class implementing the ValidateInterceptor interface:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.mycompany.core.interceptors;

import de.hybris.platform.servicelayer.interceptor.InterceptorException;
import de.hybris.platform.servicelayer.interceptor.ValidateInterceptor;
import de.hybris.platform.core.model.product.ProductModel;

public class ProductStockValidateInterceptor implements ValidateInterceptor<ProductModel> {

@Override
public void onValidate(ProductModel product, InterceptorContext ctx) throws InterceptorException {
if (product.getStockLevel() < 0) {
throw new InterceptorException("Stock level cannot be negative for product: " + product.getCode());
}
}
}

Step 2: Configure the Interceptor in Spring

Register the interceptor in your spring.xml:

1
2
3
4
<bean id="productStockValidateInterceptor" class="com.mycompany.core.interceptors.ProductStockValidateInterceptor" />
<bean parent="defaultValidateInterceptorMapping"
p:typeCode="Product"
p:interceptor="productStockValidateInterceptor" />

Step 3: Test Your Interceptor

Write a Spock test to validate the behavior of the interceptor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import spock.lang.Specification
import de.hybris.platform.core.model.product.ProductModel
import de.hybris.platform.servicelayer.interceptor.InterceptorException

class ProductStockValidateInterceptorTest extends Specification {

def "should throw exception for negative stock level"() {
given:
def product = new ProductModel()
product.setStockLevel(-5)

and:
def interceptor = new ProductStockValidateInterceptor()

when:
interceptor.onValidate(product, null)

then:
def e = thrown(InterceptorException)
e.message == "Stock level cannot be negative for product: null"
}
}

Use Cases for Interceptors

  • Data Validation: Enforcing rules such as ensuring a category is assigned to a product.
  • Audit Logging: Recording changes to critical fields like price or stock.
  • Triggering Events: Notifying external systems of data changes.
  • Data Enrichment: Automatically calculating and populating derived fields.

Best Practices

  • Keep It Lightweight: Avoid heavy computations or external API calls in interceptors.
  • Error Handling: Ensure meaningful exception messages are provided.
  • Testing: Always write unit tests for interceptors to prevent unexpected errors.
  • Modular Design: Create separate interceptors for distinct concerns instead of overloading a single interceptor.

Final Thoughts

Custom interceptors are a powerful way to implement business logic within the SAP Hybris ecosystem. By leveraging their capabilities, you can ensure data integrity, streamline workflows, and enforce custom rules efficiently.

Happy Coding!