Extending a virtual service in Sentinet with custom messages processing

When you create a Web API you can use it for your own applications but maybe you also want to expose it to the outside world. In that case you probably want to change the behavior of the Web API. For example add security or hide properties. You can use BizTalk to create a virtual service in front of your Web API to modify the behavior but is that the smartest choice?  Because BizTalk writes every message that it receives a couple of times to the MessageBox database. This increases heavily the execution time and especially with Web APIs it’s important to have a really low execution time!

Another option is to use Sentinet from Nevatech to create a virtual service in front of your Web API.
The virtual service runtime in Sentinet can also be extended with custom messages processing. Sentinet provides a number of build-in message processing components but it is also possible to create custom message processing components.

Let’s have a look at the following example where an Order Web API is used inside an organization to process orders. This Web API must also be exposed to external customers to show the status of an order but if an error occurs in the Web API or in a background process, the OrderStatus property must be modified that the order is still in progress and the ErrorMessage property with the error must not be shown to the customers. Furthermore the output of the Web API must be in XML instead of JSON.

Send request with Postman to Order Web API

 

 

 

 

 

 

 


The following steps in Sentinet are necessary to make it work:

  • Add the REST service (or API) to the the Sentinet Repository
  • Create a Virtual Service
  • Add custom Pipeline Processing
  • Test the Virtual REST Service

 

Add the REST service (or API) to the the Sentinet Repository

To register a REST service in the Repository, right click on Repository and select Add / Service / REST to add a REST service.

Add REST service to the Sentinet Repository

 

The process of registering REST service consists of the following required steps (at minimum):
- Add a new REST service to a Repository folder.
- Add a new service version to a service.
- Add one or more service endpoints along with their policies to a service version.
- Add one or more service operations to a service version.

 
 

Create a Virtual Service

Select the Repository folder where virtual service will be registered and select the Add / Virtual Service / REST toolbar menu option.

Add Virtual service to the Sentinet Repository
 

A typical workflow when creating Sentinet Virtual Service consists of several independent steps:
- Design a virtual service structure that includes virtual service interfaces and virtual operations.
- Assign a virtual service hosting environment, by creating a virtual endpoint.
- Assign transport and policy requirements for each virtual endpoint.
- Promote virtual service to the Active state.

Virtual Service
 
 

Add custom Pipeline Processing

Sentinet provides a graphical designer for the Pipeline configuration. The designer consists of the graphical representation of the Pipeline configuration and the Designer Toolbox with predefined (built-in) and custom message processing components.
 
In the Response Pipeline are we going to perform the following steps:
- Convert the JSON output to XML.
- Use the the IF component in combination with XPATH.
- Add XSL transformation after the “true” area from the If condition
 
In the Processing tab select the Pipeline sub tab and click on the Modify button.
Modify Pipeline
 
In the Designer Toolbox drag the JSON Transformation component to the Response-Inbound pipeline.
Add JSON
 
Click on the Conversion Type drop down box to change to conversation from JSON to XML and click on OK.
JSON to XML
 
In the Designer Toolbox drag the IF component after the Json Transformation.
Add IF
 
Drag the XPath Condition to the Expression field.
Add xpath condition
 
Type “/root/object[OrderStatus = 'Error']” to check if the OrderStatus property contains an error and click on OK.
xpath expression
 
In the Designer Toolbox drag the XSL transformation after the “true” area from the If condition.
Add XSLT
 
Add the custom XSLT to modify the XML output and remove the ErrorMessage field.
xslt
 
Click on the Save button to save the custom processing.
pipeline
 
 

Test the Virtual REST Service

Testing of a Virtual REST Service is similar to testing the Web API. You only have to send a request to the URL in the browser or in a tool like Postman. In the Monitoring tab of the created Virtual Service you can also monitor real-time and historical transactions.
Monitoring
 
Click on the Control sub tab to modify the Monitoring Profile if you also want to save the messages.
Modify Monitoring
 
In the Logs you can search for a specific request and see the response from the original Web API.
Orders - Result JSON
 
It is also possible to see the modified response from the Pipeline in the Virtual Service.
Virtual Orders - Result XML
 
In Postman you now see the modified response from the Virtual Service.
Virtual OrderApp - Result
 

Conclusion

Sentinet makes it really easy to modify the response of a Web API. You only have to create a Virtual Service in front of it and add custom Pipeline Processing! Especially if you are already familiar with XPATH and XSLT, the example above doesn’t take long to implement.

Download:
Sentinet version 4.5

About the author

Tomasso Groenendijk lives in Netherlands and is a senior integration consultant at Motion10. He has over 15 years’ experience in software development and software design. Tomasso is specialized in application integration with a strong interest in the Windows Azure cloud platform. In 2014, 2015 and 2016 Tomasso was awarded with the Microsoft Azure MVP award. He is an active contributor to the integration community through his work on his blog, MSDN samples, TechNet Wiki and also speaking on events.

 
 
 
MVP profile Tomasso Groenendijk
 
 
Sentinet Product Specialist
 
 
BizTalk360 Product Specialist

Month List