Custom WS Publisher
The custom publisher (custom class) mechanism can be used for the following purposes:
- formatting or inserting data to the request before publishing
- taking full control of the publishing
Sample Custom Publisher for Inserting Data
Consider a scenario where you need to pass data that is not captured on the form or data while creating a request or a micro-flow. Let's assume that the response data from a web service needs to be sent to the request or a micro-flow. To pass the response data in the request and for publishing the updated response data values to the web service, you can make use of a custom web service publisher to send the response data of a web service to a request. You can achieve this using the custom publisher class as shown in the following sample code snippet:
In this example, the form data is to be sent to web service, hence the custom publisher class must be extended from CanvasReqModelWSPublisher class. If the form data is to be sent to database, the custom publisher class must be extended from CanvasReqModelDBPublisher. The default publisher class to send the form data to a database is CanvasReqModelDBPublisher, whereas the default publisher class to send the form data to a web service is CanvasReqModelWSPublisher. You can even set your own custom publisher class to send the data for both web services and database individually. To process the response data that needs to be sent to the web service, you can override the prepareDataToBePublished method in the custom publisher class, whereas if you want to set the context for the response data before publishing the data to the web service, then you can override the publish method while specifying the custom publisher class.
/* This is a sample custom publisher Java class that inserts the form data to the web service. */ import com.intellectdesign.canvas.config.ConfigurationManager; import com.intellectdesign.canvas.config.SystemPreferenceDescriptor; import com.intellectdesign.canvas.connectors.ICanvasConnector; import com.intellectdesign.canvas.contextmanager.ExecutionContext; import com.intellectdesign.canvas.logger.Logger; import com.intellectdesign.canvas.model.StateModelException; import com.intellectdesign.canvas.reqmodel.external.CanvasPublishContext; import com.intellectdesign.canvas.reqmodel.external.CanvasReqModelWSPublisher; import com.intellectdesign.canvas.utils.ResourceLoaderUtils; import com.intellectdesign.canvas.utils.StringUtils; import org.json.JSONObject; import java.util.Map; /* In this example, the form data is to be sent to web service, hence the custom publisher class must be extended from CanvasReqModelWSPublisher class. If the form data is to be sent to database, the custom publisher class must be extended from CanvasReqModelDBPublisher. */ public class SampleWSPublisher extends CanvasReqModelWSPublisher { @Override public boolean publish(CanvasPublishContext publishContext) throws StateModelException { boolean status = false; try { ExecutionContext context = (ExecutionContext)publishContext.getData().get("EXECUTION_CONTEXT"); Map datasourceData = context.getDataSourceData(); Map detailObject = (Map)publishContext.getFormattedData(); JSONObject processedData = new JSONObject(detailObject); // setting the prepared data in execution context context.setRequestData(processedData); context.setDataSource((String) datasourceData.get("DATA_SRC_ID")); SystemPreferenceDescriptor sysPrefDesc = ConfigurationManager.getInstance().getSystemPrefDescriptor(); String canvasConnectorClass = sysPrefDesc.getCanvasConnectorClass(); ICanvasConnector connector = null; if (!StringUtils.isEmpty(canvasConnectorClass)) { connector = (ICanvasConnector) ResourceLoaderUtils.createInstance(canvasConnectorClass); } // fetching data using the connector context = connector.execute(context); JSONObject responseWSData = context.getResponseData(); Object respObj = responseWSData.get("responseData"); //This is a sample where the publisher sets any response from // the web service in the context and sends it to the client. //The context of the response data can be set here. //Any logic to process the response can also be done here. context.setResponseData((JSONObject)respObj); status = true; } catch (Exception e) { String debugInfo = publishContext.toString(); StateModelException se = new StateModelException("CTREQM0221", "Exception while publishing data for web service : " + debugInfo, e); logger.cterror("CTREQM0221", se); throw se; } return status; } private static Logger logger = Logger.getLogger(SampleWSPublisher.class); }
You must specify the custom publisher class in the Additional Params field in the request configuration. Request Modeler will then publish the data as per the provided custom publisher class.