Setting context values outside web context

Web application context overview

We know that every web application will have a context within which session and other values will be stored.

In hybris,when we try to fetch some objects which are related to user,carts etc Hybris by default uses session values set within web application context.

Example : when we try to fetch product for a specific product code, then Hybris uses catalog version and other required details from session automatically.

This works very well as long as we are calling getProductForCode() method within web application context,but sometime we need to use this method outside the web application.
Example : We may need to call this method from cronjob,web service etc.

Obviosuly, there is no web application context when we run cron job or make web service call, then in that case, above method call will throw below error

could not translate value expression ‘session.catalogversion’

So in any such scenario, we need to setup the web application context and thankfully Hybris has provided a way for it.

Yes,Hybris provided a context class called “ImpersonationContext” which will be used to set the context values.

We need to do below things to use it.

Step 1

Create ImpersonationContext object

  1. final ImpersonationContext context = new ImpersonationContext();
final ImpersonationContext context = new ImpersonationContext();

Step 2

Set all the context related values using this object

  1.         context.setSite(site);
  2.                 context.setUser(user);
  3.         context.setCatalogVersions(catalogVersionService.getCatalogVersion(“MyStoreCatalog”, "Online"););
  4.         context.setLanguage(language);
  5.         context.setCurrency(currency);
  6.         context.setCatalogVersions(getCatalogVersionService().getSessionCatalogVersions());
		context.setCatalogVersions(catalogVersionService.getCatalogVersion(“MyStoreCatalog”, "Online"););

Note :
Site,user,language,currency can be retrieved using appropriate service methods.

Step 3

Execute your code within this context using impersonationService as below

  1. //Overriding execute method of Executor
  2. impersonationService.executeInContext(context,new ImpersonationService.Executor<Object, ImpersonationService.Nothing>(){
  4. @override
  5. Public Object execute(){
  6. //Write your code here to call other methods as per need , something like below
  7. // getSomething();
  8. }
  9. });
//Overriding execute method of Executor
impersonationService.executeInContext(context,new ImpersonationService.Executor<Object, ImpersonationService.Nothing>(){

Public Object execute(){
//Write your code here to call other methods as per need , something like below
// getSomething();

Alternative way of Step 3 (using Java 8 lambda expression)

  1. impersonationService().executeInContext(context,() -> getSomething());
impersonationService().executeInContext(context,() -> getSomething());

Note :
This way, all the required context values are available to hybris even when call is made from outside web application.

About the Author

Founder of
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

Connect with me on Facebook for more updates

Share this article on