Adding Context Parameter to Configuration Context

It is possible to decorate the configuration context for every call of configuration load and store. By registering a new configuration context decorator, you can modify the configuration context passed to the configuration service by changing context parameters or adding new ones. You can use it for adding a general aspect to the configuration context of every widget, like the user role of the active user, the active locale or other.

Context

In this tutorial you will create a decorator that adds the currently active language as a configuration context called language.

Procedure

  1. Create a class that implements the WidgetConfigurationContextDecorator interface.

    MyActiveLanguageConfigurationDecorator.java

    public class MyActiveLanguageConfigurationDecorator implements WidgetConfigurationContextDecorator 
    {
    
    private CockpitLocaleService cockpitLocaleService; // Injected via Spring
    
    @Override
    public <CONFIG> ConfigContext decorateContext(final ConfigContext context, final Class<CONFIG> configurationType,
    		final WidgetInstance widgetInstance)
    	{
    		// Copy all context parameters from the original configuration
    		final DefaultConfigContext decorated = new DefaultConfigContext();
    		for (final String name : context.getAttributeNames())
    		{
    			decorated.addAttribute(name, context.getAttribute(name));
    		}
    
    		// Add current language as an additional configuration context
    		final Locale locale = cockpitLocaleService.getCurrentLocale();
    		decorated.addAttribute("language", locale.getLanguage());
    
    		return decorated;
    	}
    }
  2. Register the decorator in the Spring context:

    myextension-spring.xml

    <bean id="defaultWidgetConfigurationContextDecoratorList" class="org.springframework.beans.factory.config.ListFactoryBean">
    	<property name="sourceList">
    		<list value-type="com.hybris.cockpitng.core.config.WidgetConfigurationContextDecorator" merge="true">
    			<bean class="org.myextension.MyActiveLanguageConfigurationDecorator">
    				<property name="cockpitLocaleService" ref="cockpitLocaleService" />
    			</bean>
    		</list>
    	</property>
    </bean>