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
- final ImpersonationContext context = new ImpersonationContext();
final ImpersonationContext context = new ImpersonationContext();
Step 2
Set all the context related values using this object
- context.setSite(site);
- context.setUser(user);
- context.setCatalogVersions(catalogVersionService.getCatalogVersion(“MyStoreCatalog”, "Online"););
- context.setLanguage(language);
- context.setCurrency(currency);
- context.setCatalogVersions(getCatalogVersionService().getSessionCatalogVersions());
context.setSite(site); context.setUser(user); context.setCatalogVersions(catalogVersionService.getCatalogVersion(“MyStoreCatalog”, "Online");); context.setLanguage(language); context.setCurrency(currency); context.setCatalogVersions(getCatalogVersionService().getSessionCatalogVersions());
Note :
Site,user,language,currency can be retrieved using appropriate service methods.
Step 3
Execute your code within this context using impersonationService as below
- //Overriding execute method of Executor
- impersonationService.executeInContext(context,new ImpersonationService.Executor<Object, ImpersonationService.Nothing>(){
- @override
- Public Object execute(){
- //Write your code here to call other methods as per need , something like below
- // getSomething();
- }
- });
//Overriding execute method of Executor impersonationService.executeInContext(context,new ImpersonationService.Executor<Object, ImpersonationService.Nothing>(){ @override 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)
- 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.
Hey KB ,
How are you ?
Can’t we achieve the above by a single line below ?
userService.setCurrentUser(userService.getAdminUser());
Regards
Sankalp Sharma
Hi Sankalp,
I am good, How are you doing ?
Coming to your question,
Below line will set the current user in the session as admin user
userService.setCurrentUser(userService.getAdminUser());
But Please note that, assigning the session to an admin user has the side effect of granting the session access to every CatalogVersion in the hybris Multichannel Suite.
And Admin user will remove all the restrictions, so it is very dangerous to do so.
We should set only the right user and details to session.