SHARE
TWEET

REST2SOAP with node.js

cawoodm Sep 11th, 2011 403 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var sys  = require('util');
  2. var http = require('http');
  3. var fs  = require('fs');
  4. var url = require('url');
  5.  
  6. var srv = http.createServer(function (request, response) {
  7.   SOAP2REST(request.url,
  8.     function(d) {
  9.       var headers = {
  10.         "Content-Type": d.response.headers["Content-Type"]
  11.         };
  12.       response.writeHead(d.response.statusCode, headers);
  13.       response.end(d.data);
  14.     },
  15.     function(e) {
  16.     }
  17.   );
  18. }).listen(8080);
  19.  
  20. function SOAP2REST(URL, success, error) {
  21.  
  22.   URL = url.parse(URL, true);
  23.  
  24.   // Load Service Definition
  25.   try {
  26.     var soapJSON = fs.readFileSync('./soap' + URL.pathname + '.json', 'UTF-8');
  27.   } catch (e) {
  28.     return die({msg: "Service description " + URL.pathname + ".json could not be found!",
  29.                 err: e}, 404);
  30.   }
  31.  
  32.   // Load SOAP Request
  33.   var soapXML = '';
  34.   try {
  35.     soapXML = fs.readFileSync('./soap' + URL.pathname + '.xml', 'UTF-8');
  36.   } catch (e) {
  37.     // No problem
  38.   }
  39.  
  40.   // Parse SOAP Configuration
  41.   try {
  42.     var soapJSON = JSON.parse(soapJSON);
  43.     var soapURL = url.parse(soapJSON.url, true);
  44.   } catch (e) {
  45.     return die({
  46.       msg: "Service JSON could not be parsed!",
  47.       name: e.name,
  48.       message: e.message}, 500
  49.     );
  50.   }
  51.  
  52.   // Set Parameters
  53.   for (p in URL.query)
  54.     soapXML = soapXML.replace('##' + p.toUpperCase() + '##', URL.query[p]);
  55.  
  56.   // Prepare SOAP Request Headers
  57.   soapJSON.headers = soapJSON.headers || {};
  58.   soapJSON.headers["Content-Length"] = soapXML.length;
  59.   soapJSON.headers["Connection"] = "close";
  60.  
  61.   // Do SOAP Call
  62.   var httpOptions = {
  63.     host:     soapURL.hostname,
  64.     post:     soapURL.port || 80,
  65.     method:   soapJSON.method || 'POST',
  66.     path:     soapURL.pathname,
  67.     headers:  soapJSON.headers,
  68.   };
  69.  
  70.   httpOptions.body = soapXML;
  71.  
  72.   doHTTP(httpOptions,
  73.     function(d) {
  74.       success(d);
  75.     },
  76.     function(e){
  77.       error(e, 500);
  78.     }
  79.   );
  80.  
  81. }
  82.  
  83. function doHTTP(options, success, error) {
  84.  
  85.   var req = http.request(options, function(res) {
  86.     var data = '';
  87.     res.setEncoding(options.encoding || 'utf8');
  88.     res.on('data', function (chunk) { data += chunk; });
  89.     res.on('end', function () { success({ data: data, response: res }); });
  90.   });
  91.  
  92.   if (typeof options.body != 'undefined') req.write(options.body);
  93.   req.end();
  94.  
  95.   req.on('error', function(e) { error('HTTP ERROR: ' + e.message); });
  96. }
  97.  
  98. function out(d) {
  99.  if (typeof global.response != 'undefined') global.response.write(d); else console.log(d);
  100. }
  101. function dp(d, t) {
  102.  console.log(t || "DEBUG:", d);
  103. }
  104. function dpe(d) {
  105.  console.error("ERROR:", d);
  106. }
  107. function die(d,s) {
  108.   if (typeof global.response != 'undefined') {
  109.     global.response.statusCode = s || 500;
  110.     global.response.setHeader('Content-Type', 'text/plain');
  111.     global.response.write(d);
  112.     response.end();
  113.   }
  114.   out(sys.inspect(d));
  115.   if (s==500) process.exit(1);
  116.   return false;
  117. }
RAW Paste Data
Top