Disabling Interceptors

You can disable interceptors in a session if necessary. You can do it programmatically by using the sessionService class, or declaratively via ImpEx import.

You can disable interceptors for data integration scenarios to relax certain constraints imposed on a data model. Disabling interceptors may also be useful from the performance point of view. If you are sure that your data doesn't violate a particular interceptor, you can disable it for a given operation.

Disable Interceptors Programmatically

To disable interceptors in code, use the sessionService.executeInLocalViewWithParams method. It is an implementation of a template method pattern and takes a map of attributes that are set in a session before execution of the provided callback method, and reverted afterwards.

There are three attributes that allow you to disable specific interceptors during execution of callback:
  • disable.interceptor.beans (InterceptorExecutionPolicy#DISABLED_INTERCEPTOR_BEANS constant) This attribute takes a set of Spring bean IDs.

  • disable.interceptor.types (InterceptorExecutionPolicy#DISABLED_INTERCEPTOR_TYPES constant) This attribute takes a set of interceptor types (de.hybris.platform.servicelayer.interceptor.impl.InterceptorExecutionPolicy.InterceptorType) that you want to disable.

  • disable.UniqueAttributesValidator.for.types (InterceptorExecutionPolicy#DISABLED_UNIQUE_ATTRIBUTE_VALIDATOR_FOR_ITEM_TYPES) This attribute takes a set of item types for which you want to disable UniqueAttributesValidator.

The following example of a currency model has the out-of-the box defined ValidateInterceptor. This interceptor checks if a currency digit is not a negative number. If it is, the following code fails:
final CurrencyModel currency = modelService.create(CurrencyModel.class);
currency.setSymbol("$");
currency.setIsocode("USD");
currency.setDigits(-1);
modelService.save(currency); // throws ModelSavingException caused by InterceptorException
The following code snippet shows how to disable all validate interceptors so that you can save the data successfully. Currently, this method only supports the validator type interceptors:
final Map<String, Object> params = ImmutableMap.of(InterceptorExecutionPolicy.DISABLED_INTERCEPTOR_TYPES,
                ImmutableSet.of(InterceptorExecutionPolicy.InterceptorType.VALIDATE));
sessionService.executeInLocalViewWithParams(params, new SessionExecutionBody()
{
    @Override
    public void executeWithoutResult()
    {
        final CurrencyModel currency = modelService.create(CurrencyModel.class);
        currency.setSymbol("$");
        currency.setIsocode("Dollar");
        currency.setDigits(-1);
         
        modelService.save(currency);    // save successful - all validate interceptors are disabled
    }
});
You can also choose to be more granular and disable specific interceptors on a per-bean basis:
final Map<String, Object> params = ImmutableMap.of(InterceptorExecutionPolicy.DISABLED_INTERCEPTOR_BEANS, ImmutableSet.of("validateCurrencyDataInterceptor"));
         
sessionService.executeInLocalViewWithParams(params, new SessionExecutionBody()
{
    @Override
    public void executeWithoutResult()
    {
        final CurrencyModel currency = modelService.create(CurrencyModel.class);
        currency.setSymbol("$");
        currency.setIsocode("Dollar");
        currency.setDigits(-1);
        modelService.save(currency);    // save successful - validateCurrencyDataInterceptor interceptor is disabled
    }
});

In case we know that there is no Currency with the Dollar IsoCode, we can disable UniqueAttributesValidator for the Currency type, as shown in the code:

final Map<String, Object> params = ImmutableMap.of(InterceptorExecutionPolicy.DISABLED_UNIQUE_ATTRIBUTE_VALIDATOR_FOR_ITEM_TYPES, ImmutableSet.of("Currency"));
		
sessionService.executeInLocalViewWithParams(params, new SessionExecutionBody()
{
	@Override
	public void executeWithoutResult()
	{
		final CurrencyModel currency = modelService.create(CurrencyModel.class);
		currency.setSymbol("$");
		currency.setIsocode("Dollar");
		currency.setDigits(-1);
		modelService.save(currency);	// save successful - UniqueAttributesValidator not called
	}
});

Disable Interceptors via ImpEx

Similarly, the following import fails:
INSERT_UPDATE Currency;isocode[unique=true];digits;symbol
;EUR_Test;-2;€

with the following exception:

[de.hybris.platform.servicelayer.i18n.interceptors.ValidateCurrencyDataInterceptor@13097d1b]: Number of digits for CurrencyModel must be greater than 0.;EUR;-2;€

To disable all validator type interceptors, use the disable.interceptor.types=validate header attribute:
INSERT_UPDATE Currency[disable.interceptor.types=validate];isocode[unique=true];symbol;digits;
;EUR_Test2;$;-2;
To disable specific interceptors, specify a comma-separated bean-IDs list for the disable.interceptor.beans header attribute. The following impex specifies only one such interceptor:
INSERT_UPDATE Currency[disable.interceptor.beans='validateCurrencyDataInterceptor'];isocode[unique=true];symbol;digits;
;EUR_Test;$;-2;

To disable UniqueAttributesValidator, specify a comma-separated item types for which you want to disable it:

INSERT_UPDATE Currency[disable.UniqueAttributesValidator.for.types='Currency'];isocode[unique=true];symbol;digits;
;EUR_Test;$;-2;

If you want to specify more than one ID, put the bean IDs in apostrophes: <'beanID'>.