Sunday, 8 January 2017

Webservices in Hybris

Hybris provides a great way to expose all of its functionality via webservices, We can provide all the data available in the hybris database (products, promotions, vouchers , wishlist etc) to third party applications which are not built using Hybris.


Some webservices are exposed via http and 9001 port where-as others where spring-security is applied are exposed via https and 9002 port

Accessing normal webservices is pretty straight forward. But there is a way to access webservices where spring-security is applied.

Most of the features are provided by Hybris Out-Of-The-Box (OOTB). In case of customization we only have to Fetch data either byFlexible Search Query or from services, add them to a list and return the list to the calling application.

Rest of the things will be taken care by Hybris automatically :)   

 --------------------------------------------------------------------------------------------------------------------

Follow these steps to make your own custom extension for webservices -> 
1) Go to platform directory of hybris and type the modulegen command.

Hybris5.4\hybris\bin\platform> ant modulegen 
2) You will be provided the following templates to select.

[input] Press [Enter] to use the default value ([accelerator], b2baccelerator, telcoaccelerator, commercewebservices)

Select commercewebservices 
3) You will be asked to name your extension.

Press [Enter] to use the default value [training]

Example Name -> myWebService 
4) You will be asked to enter package name.

Press [Enter] to use the default value [org.training]

Example Package -> org.webservice 
---------------------------------------------------------------------------------------------------------------------
Add the following entries to localextensions.xml file ->
<path dir="${HYBRIS_BIN_DIR}" />
<!-- ext-platform -->
<extension name="admincockpit" />
<extension name="backoffice" />
<extension name="cockpit" />
<extension name="hmc" />
<extension name="mcc" />
<extension name="platformhmc" />
<!-- ext-commerce -->
<extension name="btg" />
<extension name="btgcockpit" />
<extension name="commercesearch" />
<extension name="commercesearchbackoffice" />
<extension name="commercesearchhmc" />
<extension name="commerceservices" />
<extension name="basecommerce" />
<extension name="payment" />
<extension name="promotions" />
<extension name="voucher" />
<extension name="customerreview" />
<extension name="ticketsystem" />
<extension name="solrfacetsearch" />
<extension name="solrfacetsearchhmc" />
<extension name="oci" />
<extension name="wishlist" />
<extension name="commercefacades" />
<!-- ext-data -->
<extension name="commercesearchsampledata" />
<extension name="apparelstore" />
<extension name="electronicsstore" />
<extension name="instoresampledata" />
<!-- ext-content -->
<extension name="productcockpit" />
<extension name="cms2" />
<extension name="cms2lib" />
<extension name="cmscockpit" />
<extension name="bmecat" />
<extension name="bmecathmc" />
<extension name="importcockpit" />
<extension name="classificationsystems" />
<extension name="classificationsystemshmc" />
<!-- ext-channel -->
<extension name="cscockpit" />
<extension name="mobileoptionals" />
<extension name="mobileservices" />
<extension name="instore" />
<!-- ext-addon -->
<extension name="addonsupport" />
<!-- ext-print -->
<extension name="print" />
<extension name="printcockpit" />
<extension name="printhmc" />
<!-- ext-accelerator -->
<extension name="acceleratorservices" />
<extension name="acceleratorfacades" />
<extension name="acceleratorcms" />
<extension name="acceleratorstorefrontcommons" />
<!-- custom-webstore -->
<extension name="myWebStorecockpits" />
<extension name="myWebStorecore" />
<extension name="myWebStorefacades" />
<extension name="myWebStorefulfilmentprocess" />
<extension name="myWebStoreinitialdata" />
<extension name="myWebStorestorefront" />
<extension name="myWebStoretest" />
<!-- custom-sebservice -->
<extension name="myWebService" />


Note : In some versions of Hybris, you make have to include one more extension -> <extension name="platformwebservices" />
 ------------------------------------------------------------------------------------------------------------------------
Go to extensioninfo.xml file of the newly created extension -> myWebService.

You will see the following entries->
<extension abstractclassprefix="Generated" classprefix="Ycommercewebservices" name="myWebService" usemaven="false">
<requires-extension name="commercefacades"/>
<requires-extension name="commerceservices"/>
<requires-extension name="commercewebservicescommons"/>
<coremodule generated="true" manager="de.hybris.platform.jalo.extension.GenericManager" packageroot="org.webservice.core"/>
<webmodule jspcompile="false" webroot="/myWebService"/>
<meta key="extgen-template-extension" value="true"/>
</extension>


Here this line is very important  ->

<webmodule jspcompile="false" webroot="/myWebService"/>.

You will be calling your webservices with the root as /myWebService. Here you can give any name you want to give but you need to use the same name mentioned in this file for calling your webservices.
 ---------------------------------------------------------------------------------------------------------------------

Time to check the webservice. Build the code and start hybrisserver.

Now open Rest-Client and execute the following webservice request to fetch product details from Hybris.

URL - http://localhost:9001/myWebService/v1/electronics/products

Method : GET

You can view the response in the body of rest client as shown below.

Now if you try to make this call - https://localhost:9002/myWebService/v1/electronics/customers/current

You will get the following error.

It is because this call has been secured and requires authentication.

Steps for Authentication : 
Type the following URL in RestClient.

http://localhost:9001/myWebService/oauth/token?client_id=mobile_android&client_secret=secret&grant_type=password&[email protected]&[email protected]

Method = POST

Output =

Note: You need to be a registered user, else it will give "Bad Credentials" error.  
 
Authorization entries are present in security-spring.xml located at the following location.  
myWebService/web/webroot/WEB-INF/config/security-spring.xml<oauth:client-details-service id="clientDetails">
<oauth:client client-id="client-side" resource-ids="hybris" authorized-grant-types="implicit,client_credentials"
authorities="ROLE_CLIENT" secret="secret" redirect-uri="
http://localhost:8080/oauth2_implicit_callback" />
<oauth:client client-id="mobile_android" resource-ids="hybris"
authorized-grant-types="authorization_code,refresh_token,password,client_credentials" authorities="ROLE_CLIENT" secret="secret"
redirect-uri="
http://localhost:8080/oauth2_callback" />
<oauth:client client-id="trusted_client" resource-ids="hybris"
authorized-grant-types="authorization_code,refresh_token,password,client_credentials" authorities=" ROLE_TRUSTED_CLIENT”
secret="secret" /> </oauth:client-details-service>



Now add the access token in the header of Rest-Client as shown below.
 
 
From now onwards whenever you make any webservice calls, you will no longer be an anonymous user but will be logged in with the user-id of whom the credentials were provided while acquiring the access_token above.

Now again if you try to make this call - https://localhost:9002/myWebService/v1/electronics/customers/current 

You will still get the following error. 
 
 
To solve this go to SSL tab of Rest-Client  

Here enter the following->
 
Trust store file : Select keystore file located at the following path of your Hybris Commerce Suite.
Path = \hybris-commerce-suite-5.4.0.3\hybris\bin\platform\tomcat\lib\keystore 
 
Trust store password : 123456 
 
Hostname verifier : Allow All
 
Now if you try to make this call -https://localhost:9002/myWebService/v1/electronics/customers/current

You will get the following output ->
  

If you observe there was no need to pass customerID, from the access_token it automatically passes the customerID.

Note : If you want to use this logic from JAVA code, then you have to write code to append access_token in the header of request. 
 --------------------------------------------------------------------------------------------------------------------------------
By default Hybris provides various webservice calls, related to various functionalities which are located in the following files->
 

 
 
If you want to expose something which is not provided in the above files then you need to do further customization.

Steps: 

1) Create your own class and extend it from the BaseController 

@Controller("wishlistControllerV1")
@RequestMapping(value = "/{baseSiteId}/wishlist")
public class WishlistController extends BaseController
 

2) Write your own method, fetch the required data, add it to a list and return the list. 
@RequestMapping(value = "/getDefaultWishlist", method = RequestMethod.GET)
@ResponseBody
public List<WishlistData> getDefaultWishlist() throws Exception
{
final List<WishlistData> listFinal = new ArrayList<WishlistData>();
final Wishlist2Model wishlist2Model = wishlistService.getDefaultWishlist(userService.getCurrentUser());
final List<Wishlist2EntryModel> list = wishlist2Model.getEntries();
final Iterator iterator = list.iterator();
while (iterator.hasNext())
{
Wishlist2EntryModel productModel = new Wishlist2EntryModel();
productModel = (Wishlist2EntryModel) iterator.next();
final WishlistData wishlistData = new WishlistData();
wishlistData.setCode(productModel.getProduct().getCode());
wishlistData.setName(productModel.getProduct().getName());
wishlistData.setPriceQuantity(productModel.getProduct().getPriceQuantity());
wishlistData.setPriority(String.valueOf(productModel.getPriority()));
wishlistData.setAddedDate(productModel.getAddedDate());
listFinal.add(wishlistData);
}
}
return listFinal;
}


3) If the data is not accessible via available services then you can write Flexible Search Query.  
final FlexibleSearchQuery query = new FlexibleSearchQuery("select {pk} from {voucher}"); 
Add this result to a list and return the list.

Note: 
If you don’t want to customize webservice and use the default webservices provided use /rest instead of your custom name/myWebService. 

Example : 
 
Custom : http://localhost:9001/myWebService/v1/electronics/cart  
Default : http://localhost:9001/rest/v1/electronics/cart  
----------------------------------------------------------------------------------------------------------------------------------
Some sample webservice calls provided by Hybris OOTB. 

Authenticaiton 
http://localhost:9001/myWebService/oauth/token?client_id=mobile_android&client_secret=secret&grant_type=password&[email protected]&[email protected] 

Get the current logged in user https://localhost:9002/myWebService/v1/electronics/customers/current 
 
Get the current session cart 
http://localhost:9001/myWebService/v1/electronics/cart 
 
Add to cart 
http://localhost:9001/myWebService/v1/electronics/cart/entry?code=107701&qty=2 
 
Logout current user http://localhost:9001/myWebService/v1/electronics/customers/current/logout 
 
Promotions list 
https://localhost:9002/myWebService/v1/electronics/promotions?type=all 
(type can be all,order,product) 
 
Product list 
https://localhost:9002/myWebService/v1/electronics/products 

Get Voucher Information 
https://localhost:9002/myWebService/v1/electronics/vouchers/999-965M-4Y95-34GY 
 
Apply voucher to cart 
https://localhost:9002/myWebService/v1/electronics/cart/voucher/999-965M-4Y95-34GY 

Release voucher from cart 
https://localhost:9002/myWebService/v1/electronics/vouchers/999-965M-4Y95-34GY 

1 comment :

  1. Hi Kishore,

    There is one requirement from web services is to add product to anonymous user cart and while making checkout, it will ask for login and after login anonymous and logged in user cart should be merged like storefront.

    But here, I am unable to get anonymous user cart id and not able to add product to anonymous user by using web service.

    Please help.

    ReplyDelete