03.02 javascript 示例

// Mirth strings don't support startsWith() in Mirth 3.0.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;
  };
}
 

 
// Just in case we fail, set a sane responseContentType
channelMap.put('responseContentType', 'text/plain');
 
var msg = XML(connectorMessage.getRawData());
if(msg.length ==0)
 return Packages.com.mirth.connect.server.userutil.ResponseFactory.getErrorResponse('message is null');
 
// 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'];

var myServicePrefix = '/myrestservice';
return Packages.com.mirth.connect.server.userutil.ResponseFactory.getErrorResponse('rest is :'+myServicePrefix);
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.substr(myServicePrefix.length).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
 
// 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';
  }
 

 
  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());
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值