Sunday, March 19, 2017

Creating custom rest service using databeans

Introduction
Custom rest services will be required in multiple scenarios. WCS provides three ways to create new REST services.

  • Using BOD mapping framework
  • Using controller command framework
  • Using databean mapping framework

Let us create a new rest service using a databean. The url of the service is https://localhost/wcs/resources/store/{storeId}/myPath/customDetails

Steps
1. Create a custom handler to service your request.
A new java class must be created (say MyCustomHandler) and it should extend the class com.ibm.commerce.rest.classic.core.AbstractConfigBasedClassicHandler.

In the handler set the path and create a method to service the request

Path("store/{storeId}/myPath")
@Encoded
@Description("This class provides RESTful services to get some custom details")
public class MyCustomHandler extends AbstractConfigBasedClassicHandler{
@POST
@Path("customDetails")
@Description("Gets custom details")
public Response findCustomDetails(
@PathParam("storeId")
@ParameterDescription(description = "storeId in parameter", valueProviderClass = StoreIdProvider.class, required = true) String storeId,
@QueryParam("responseFormat") @ParameterDescription(description = "responseFormat description", valueProviderClass = ResponseType.class) String responseFormat) {
//params is a HashMap containing the parameters to be set for bean
Map params = new HashMap<String, String>();
params.put("storeId", storeId);

//call executeConfigBasedBeanWithContext method to execute the databean and to get the response
//calling with profile name Custom_Profile_1
Response result =
executeConfigBasedBeanWithContext("com.mycompany.beans.MyCustomDataBean", "Custom_Profile_1", responseFormat,params);

}
}

2. Add the handler to resource file
Navigate to Rest\WebContent\WEB-INF\config\resources-ext.properties and add the fully qualified name of the handler in the file. If the file is not there, create it

3. Create the data bean mapping xml 
Navigate to Rest\WebContent\WEB-INF\config\beanMapping-ext
Create an xml file with complete name of Databean. com.mycompany.beans.MyCustomDataBean.xml
Configure the input-output profiles in this xmls. We can have multiple profiles in the same xml. A simple example is given below. It will have the input/output parameter name and the corresponding method in data bean used for setting/getting them. The profile name used on the handler will choose what profile will be called.

<?xml version="1.0" encoding="UTF-8"?>
<bean>
<profiles>
<profile name="Custom_Profile_1">
<inputs>
<input inputName="storeId" methodName="setStoreId" />
</inputs>

<outputs>
<output methodName="getDetails" outputName="Details" />
</outputs>
</profile>
                       <!--Add more profiles if needed   -->
</profiles>
</bean>

4. Create the databean
Create the data bean. Use the same steps that we use for creating databeans. The changes specific for rest service bean are

  • It must implement com.ibm.commerce.security.Delegator
  • It must override the getDelegate() method. We can just return null in that method.
  • It must have all methods specified in the mapping xml
Restart and republish is required for reflecting the changes



Cross transactional cache invalidation across WCS and Search applications

Introduction Cache invalidation is one of the key and tedious part of software development. It is really important that we purge the inval...