这节课我们一起学习利用Mirth Connect的HTTP Listener源通道与JavaScript Writer目的通道搭建自定义Restful风格webapi服务。

1.新建名为‘Custom  Restful api’的信道,指定源通道与目的通道的输入输出消息格式

2.设置HTTP Listener类型源通道参数

  1. 把 "Response" 响应指定为 destination 1
  2. 输入‘Base  context path’ 为 /myrestservice
  3. 设置 "Message Content" 为 XML Body
  4. 设置默认"Response Content Type" 为text/plain; charset=UTF-8我们将在目的通道中通过channel map重写它的值为application/xml或application/json
  5. 设置 "Response Status Code" 响应码为 ${responseStatusCode}我们将在目的通道中通过channel map重写它的值为200(成功)或500(失败)
  6. 在 "Response Header" 中添加一个变量 "Content-Type" ,指定其值为 ${responseContentType}我们将在目的通道中通过channel map重写它的值为application/xml或application/json

3.设置JavaScript Writer目的通道参数并编写JS实现脚本

// Mirth strings don't support startsWith() in Mirth 3
// If necessary, add a method to the String prototype.
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position){
position = position || 0;
return this.substr(position, searchString.length) === searchString;
};
} /*
Incoming message looks like this: <HttpRequest>
<RemoteAddress>71.127.40.115</RemoteAddress>
<RequestUrl>http://www.example.com:8080/myrestservice</RequestUrl>
<Method>GET</Method>
<RequestPath>foo=bar</RequestPath>
<RequestContextPath>/myrestservice/param1/param2</RequestContextPath>
<Parameters>
<foo>bar</foo>
</Parameters>
<Header>
<Host>www.example.com:8080</Host>
<Accept-Encoding>identity</Accept-Encoding>
<User-Agent>Wget/1.18 (darwin15.5.0)</User-Agent>
<Connection>keep-alive</Connection>
<Accept>application/xml</Accept>
</Header>
<Content/>
</HttpRequest> <HttpRequest>
<RemoteAddress>71.127.40.115</RemoteAddress>
<RequestUrl>http://www.example.com:8080/myrestservice</RequestUrl>
<Method>GET</Method>
<RequestPath>foo=bar</RequestPath>
<RequestContextPath>/myrestservice/param1/param2</RequestContextPath>
<Parameters>
<foo>bar</foo>
</Parameters>
<Header>
<Host>www.example.com:8080</Host>
<Accept-Encoding>identity</Accept-Encoding>
<User-Agent>Wget/1.18 (darwin15.5.0)</User-Agent>
<Connection>keep-alive</Connection>
<Accept>application/json</Accept>
</Header>
<Content/>
</HttpRequest>
*/ // Just in case we fail, set a sane responseContentType
channelMap.put('responseContentType', 'text/plain'); var msg = XML(connectorMessage.getRawData());
logger.info(msg);
// Get the REST data from the "context path" which is actually
// the "path info" of the request, so it will start with '/myrestservice'.
var rest = msg['RequestContextPath'];
logger.info(rest);
var myServicePrefix = '/myrestservice';
var minimumURLParameterCount = 4; // This is the minimum you require to do your work
var maximumExpectedURLParameterCount = 5; // however many you expect to get
var params = rest.substring(myServicePrefix).split('/', maximumExpectedURLParameterCount);
if(params.length < minimumURLParameterCount)
return Packages.com.mirth.connect.server.userutil.ResponseFactory.getErrorResponse('Too few parameters in request');
var mrn = params[1]; // params[0] will be an empty string
logger.info(mrn);
// Now, determine the client's preference for what data type to return (XML vs. JSON).
// We will default to XML.
var clientWantsJSON = false;
var responseContentType = 'text/xml'; // If we see any kind of JSON before any kind of XML, we'll use
// JSON. Otherwise, we'll use XML.
//
// Technically, this is incorrect resolution of the "Accept" header,
// but it's good enough for an example.
var mimeTypes = msg['Header']['Accept'].split(/\s*,\s*/);
for(var i=0; i<mimeTypes.length; ++i) {
var mimeType = mimeTypes[i].toString();
if(mimeType.startsWith('application/json')) {
clientWantsJSON = true;
responseContentType = 'application/json';
break;
} else if(mimeType.startsWith('application/xml')) {
clientWantsJSON = false;
responseContentType = 'application/xml';
break;
} else if(mimeType.startsWith('text/xml')) {
clientWantsJSON = false;
responseContentType = 'text/xml';
break;
}
} var xml;
var json; if(clientWantsJSON)
json = { status : '' };
else
xml = new XML('<response></response>'); try {
/*
Here is where you do whatever your service needs to actually do.
*/ if(clientWantsJSON) {
json.data = { foo: 1,
bar: 'a string',
baz: [ 'list', 'of', 'strings']
};
} else {
xml['@foo'] = 1;
xml['bar'] = 'a string';
xml['baz'][0] = 'list';
xml['baz'][1] = 'of';
xml['baz'][3] = 'strings';
} // Set the response code and content-type appropriately.
// http://www.mirthproject.org/community/forums/showthread.php?t=12678 channelMap.put('responseStatusCode', 200); if(clientWantsJSON) {
json.status = 'success';
var content = JSON.stringify(json);
channelMap.put('responseContent', content);
channelMap.put('responseContentType', responseContentType);
return content;
} else {
channelMap.put('responseContentType', responseContentType);
var content = xml.toString();
channelMap.put('responseContent', content);
return content;
}
}
catch (err)
{
channelMap.put('responseStatusCode', '500');
if(clientWantsJSON) {
json.status = 'error';
if(err.javaException) {
// If you want to unpack a Java exception, this is how you do it:
json.errorType = String(err.javaException.getClass().getName());
json.errorMessage = String(err.javaException.getMessage());
} channelMap.put('responseContentType', responseContentType); // Return an error with our "error" JSON
return Packages.com.mirth.connect.server.userutil.ResponseFactory.getErrorResponse(JSON.stringify(json));
} else {
if(err.javaException) {
xml['response']['error']['@type'] = String(err.javaException.getClass().getName());
xml['response']['error']['@message'] = String(err.javaException.getMessage());
} channelMap.put('responseContentType', responseContentType); // Return an error with our "error" XML
return Packages.com.mirth.connect.server.userutil.ResponseFactory.getErrorResponse(xml.toString());
}
}

我们通过目的通道以上JS脚本,学习到以下特别重要的知识:

  1. 获取输入请求的原始消息并自动格式化为XML格式: var xml = new XML(connectorMessage.getRawData())
  2. 设置响应类型,如:channelMap.put('responseContentType', 'application/json')
  3. 设置响应码,如:channelMap.put('responseStatusCode', '200')
  4. 设置响应内容并通过JS脚本返回XML实体或者Json实体的字符串格式值
  5. 异常处理通过JS脚本调用Mirth的API函数Packages.com.mirth.connect.server.userutil.ResponseFactory.getErrorResponse(string)返回字符串格式错误消息

4.部署信道并测试 

发送消息要区分application/json和application/xml,可以看到响应值格式会相应变化

<HttpRequest>
<RemoteAddress>71.127.40.115</RemoteAddress>
<RequestUrl>http://www.example.com:8080/myrestservice</RequestUrl>
<Method>GET</Method>
<RequestPath>foo=bar</RequestPath>
<RequestContextPath>/myrestservice/param1/param2</RequestContextPath>
<Parameters>
<foo>bar</foo>
</Parameters>
<Header>
<Host>www.example.com:8080</Host>
<Accept-Encoding>identity</Accept-Encoding>
<User-Agent>Wget/1.18 (darwin15.5.0)</User-Agent>
<Connection>keep-alive</Connection>
<Accept>application/json</Accept>
</Header>
<Content/>
</HttpRequest>

  

<HttpRequest>
<RemoteAddress>71.127.40.115</RemoteAddress>
<RequestUrl>http://www.example.com:8080/myrestservice</RequestUrl>
<Method>GET</Method>
<RequestPath>foo=bar</RequestPath>
<RequestContextPath>/myrestservice/param1/param2</RequestContextPath>
<Parameters>
<foo>bar</foo>
</Parameters>
<Header>
<Host>www.example.com:8080</Host>
<Accept-Encoding>identity</Accept-Encoding>
<User-Agent>Wget/1.18 (darwin15.5.0)</User-Agent>
<Connection>keep-alive</Connection>
<Accept>application/xml</Accept>
</Header>
<Content/>
</HttpRequest>
 

大功告成!!!

本课程总结:

通过JS脚本编程,自定义实现Restful风格webapi.

欢迎大家持续关注潤沁網路大學本系列Mirth Connect课程的教学

第11課-Channel Study For Create Custom Restful Service的更多相关文章

  1. How to Create Custom Filters in AngularJs

    http://www.codeproject.com/Tips/829025/How-to-Create-Custom-Filters-in-AngularJs Introduction Filter ...

  2. create custom launcher icon 细节介绍

    create custom launcher icon 是创建你的Android app的图标 点击下一步的时候,出现的界面就是创建你的Android的图标 Foreground: ” Foregro ...

  3. [转]How to Create Custom Filters in AngularJs

    本文转自:http://www.codeproject.com/Tips/829025/How-to-Create-Custom-Filters-in-AngularJs Introduction F ...

  4. How to: Create Custom Configuration Sections Using ConfigurationSection

    https://msdn.microsoft.com/en-us/library/2tw134k3.aspx You can extend ASP.NET configuration settings ...

  5. java中如何创建自定义异常Create Custom Exception

    9.创建自定义异常 Create Custom Exception 马克-to-win:我们可以创建自己的异常:checked或unchecked异常都可以, 规则如前面我们所介绍,反正如果是chec ...

  6. Custom Data Service Providers

    Custom Data Service Providers Introduction Data Services sits above a Data Service Provider, which i ...

  7. Unable to create Azure Mobile Service: Error 500

    I had to go into my existing azure sql database server and under the configuration tab select " ...

  8. Cannot create container for service peer1.org2.example.com: Conflict. 解决方案

    I have a docker-compose.yaml file defining 5 services: orderer.example.com peer0.org1.example.com pe ...

  9. docker启动报错解决及分析(Cannot create container for service *******: cannot mount volume over existing file, file exists /var/lib/docker/overlay2/)

    现象: Cannot create container for service *******: cannot mount volume over existing file, file exists ...

  10. [转]Create Custom Exception Filter in ASP.NET Core

    本文转自:http://www.binaryintellect.net/articles/5df6e275-1148-45a1-a8b3-0ba2c7c9cea1.aspx In my previou ...

随机推荐

  1. 将谷歌chrome浏览器主题变黑的方法

    两个步骤: 第一: 桌面找到google chrome图标右键->属性,在后面加上: --force-dark-mode (注意有空格) 第二: 1.浏览器地址输入chrome://flags/ ...

  2. pip 安装requirements.txt 的问题

    用新环境 在进行pip 安装的时候, 如果出现不进行安装 ,但是不报错就是满足条件,这个时候重新起一个shell,然后进行pip的安装.

  3. Android 多module情况下module依赖aar问题处理

    原文: Android 多module情况下module依赖aar问题处理 - Stars-One的杂货小窝 问题描述 负责一个大项目Android工程项目,新增了一个module,而此module由 ...

  4. vue基础知识和原理(一)

    1. vue基础知识和原理 1.1 初识Vue 想让Vue工作,就必须创建一个Vue实例,且要传入一个配置对象 demo容器里的代码依然符合html规范,只不过混入了一些特殊的Vue语法 demo容器 ...

  5. django(模版层)

    一 模板语法传值 {{}}: 变量相关.过滤器 {%%}: 逻辑相关(静态文件配置.反向解析.标签相关.模版继承和 导入) def index(request): # 模板语法可以传递的后端pytho ...

  6. JQ实现音乐插件并自动播放

    这里分享我最近写出来的一个小东西,基于jq的音乐播放器,可以嵌套到网站 效果截图: 具体首页代码如下: <!DOCTYPE html> <html> <head> ...

  7. Docker Commands Diagram

  8. verilog之状态机

    verilog之状态机设计 1.状态机的原理 状态机,就是基于状态变化而设计的硬件模块,是一种常见的设计思路.掌握状态机的使用,是初步建立复杂逻辑设计能力的开始.所谓的状态机,和高级语言程序的流程图十 ...

  9. OWOD:开放世界目标检测,更贴近现实的检测场景 | CVPR 2021 Oral

    不同于以往在固定数据集上测试性能,论文提出了一个更符合实际的全新检测场景Open World Object Detection,需要同时识别出未知类别和已知类别,并不断地进行增量学习.论文还给出了OR ...

  10. QT 自定义插件问题 error: LNK2001: 无法解析的外部符号

    为了重复利用已有的代码,我使用自定义插件进行开发.当每个插件独立开发时没有遇到问题,但是当插件B引用了插件A时就会在编译时报错 error: LNK2001: 无法解析的外部符号. 例如,先定义一个插 ...