Parse responses in response tab
Imagine that we are sending certain data to external sources and we are getting response from those source. Now the received response has to be parsed inside the response tab.
APPROACH 1:
Imagine we are receiving an HTML response like this as provided below:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″/>
<title>401 – Unauthorized: Access is denied due to invalid credentials.</title>
<style type=”text/css”>
<!–
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:”trebuchet MS”, Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
–>
</style>
</head>
<body>
Server Error
401 – Unauthorized: Access is denied due to invalid credentials.
You do not have permission to view this directory or page using the credentials that you supplied.
</div>
</body>
</html>
We need to get the status that we are receiving on this and make business logic accordingly. For example, here on this HTML response we are supposed to parse the status value 401, and if it is 401 then we need to perform actions accordingly.
This can be achieved by using external library called JSOUP that is used for parsing HTML via JAVA. download the jsoup jar library and then test it in the JAVA first using any version of eclipse. Find the JAVA code below:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class TestHtml {
public void htmlDataReader() {
String htmlData =””; // your html string
Document doc = Jsoup.parse(htmlData);
Document doc = Jsoup.parse(htmlData);
Elements tds = doc.getElementsByTag(“h2”);
String text = tds.text();
System.out.println(“Data : “+ text.substring(0,3));
}
public static void main (String args[]) {
TestHtml val = new TestHtml();
val.htmlDataReader();
}
}
Output : Data: 401
The corresponding Mirth code for the above JAVA code will be as follows:
importPackage(org.jsoup);
var doc = Jsoup.parse(msg);
var tds = doc.getElementsByTag(“h2”);
var text = tds.text();
var responseStatusValue = text.substring(0,3);
if(responseStatusValue==’401′){
logger.debug(“Data : “+ text.substring(0,3));
// Business logic below of your choice
}
APPROACH 2:
If we are going to parse the HTML data through mirth itself without using any external library, then it is little challenging because the tag <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”> is expected to come in HTML, but ex4 will not be considering this element as a XML tag. So mirth will not take this data as a XML and allow us to parse the message as normal XML.
Make your source listener as RAW instead of XML then only it will be consumed inside Mirth. In the pre-processor of the mirth write the following code:
var pattern = $gc(‘pattern’);
if (!pattern) {
pattern = java.util.regex.Pattern.compile(‘^(\\s*(<\\?xml.*\\?>)?\\s*(<!DOCTYPE[^\\[>]*(\\[\\s*(<![^>]*>\\s*)*\\])?[^>]*>)?\\s*)’);
$gc(‘pattern’, pattern);
}
var prolog = ”;
var matcher = pattern.matcher(message);
if (matcher.find()) {
prolog = matcher.group(1);
message = message.substring(0, matcher.start(1)) + message.substring(matcher.end(1));
}
$c(‘prolog’, prolog);
return message;
Inside the source transformer provide the following code. you can use ‘msg’ inside the source transformer since, those DOCTYPE! will be removed inside the pre-processor. Your pre-processor is the first place where your code reaches at first followed by other stages of mirth interface engine.
var responseData = msg[‘body’][‘div’][1][‘div’][‘fieldset’][‘h2’].toString();
var responseData = msg[‘body’][‘div’][1][‘div’][‘fieldset’][‘h2’].toString();
logger.debug(msg[‘body’][‘div’][1][‘div’][‘fieldset’][‘h2’].toString());
var responseDataStatus = responseData.substring(0,3);
if(responseDataStatus==’401′){
logger.debug(“RESPONSE DATA : “+responseDataStatus);
// Your business Logic
}
APPROACH 3:
You can also perform this activity in the summary of the channel. As shown in the picture below:
Click on the properties tab, and provide the following code. This code is basically the replace code that we will be using to remove the incoming namespace.
message.replace(‘<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>’,”);
return message;
Similar to the scripts tab, we should be using message here instead of msg. From 3.4 version mirth has made this javascript properties tab available in the summary of the channel, which will be executed at first instead of the pre-processor.
Inside transformer we can use the code that we have used for Approach 2 directly. it will work fine.
APPROACH 4:
If we want the same to be achieved in response tab, then we cannot use approach 2 or approach 3. In that case we can use the inbuilt function $(‘responseStatusLine’) which will contain the corresponding HTTP responses such as 500,401 or 200.
We can call this function directly and use it in response transformer as provided below:
if($(‘responseStatusLine’).contains(‘401’)){
//Your business logic goes here…
}