Source: utilities.js

  1. /**
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. Author:Leon
  12. */
  13. var http = require('http'),
  14. xpath = require('xpath'),
  15. SimpleConvertXML = require('simpleConvert-XML'),
  16. dom = require('xmldom').DOMParser;
  17. /**
  18. * To Convert the XMl Response to Json Object
  19. *
  20. * @class Utilities
  21. */
  22. module.exports = {
  23. postCompose: function(url) {
  24. var re = /http:\/\/([^:]*):([^\/]*)([^$]*)/
  25. var tokens = url.match(re);
  26. var theWholeUrl = tokens[0];
  27. var host = tokens[1];
  28. var port = tokens[2];
  29. var path = tokens[3];
  30. return {
  31. host: host,
  32. path: path,
  33. port: port,
  34. method: "POST",
  35. headers: {
  36. 'Cookie': "cookie",
  37. 'Content-Type': 'text/xml',
  38. }
  39. };
  40. },
  41. postRequest: function(post, body, callback) {
  42. var req = http.request(post, function(res) {
  43. var buffer = "";
  44. res.on("data", function(data) {
  45. buffer = buffer + data;
  46. });
  47. res.on("end", function(data) {
  48. callback(buffer);
  49. });
  50. });
  51. req.write(body);
  52. req.end();
  53. },
  54. /**
  55. * Retrieves Json Object from XML Response.
  56. *
  57. * @method getJsObjectByXmlResponse
  58. * @param {object} response
  59. * @return {Json Object}JSON Object
  60. */
  61. getJsObjectByXmlResponse: function(response) {
  62. var parser = new dom().parseFromString(response);
  63. if (xpath.select("//data", parser).length > 0)
  64. response = xpath.select("//data", parser)[0].toString();
  65. parser = new dom().parseFromString(response);
  66. if (xpath.select("//param", parser).length > 0)
  67. response = xpath.select("//param", parser)[0].toString();
  68. var changeAttrToTag = /<member><name>(.*)<\/name><value><(string|int)>((.|[\r\n])*?)<\/\2><\/value><\/member>/;
  69. var nullyfy = /<member><name>.*?<string\/>.*?<\/member>/;
  70. if (nullyfy.test(response))
  71. response = response.replace(/<member><name>.*?<string\/>.*?<\/member>/g, '');
  72. var flag = changeAttrToTag.test(response);
  73. var changedTagResponse = "";
  74. if (flag) {
  75. response = response.replace(/<member><name>(.*)<\/name><value><(string|int)>((.|[\r\n])*?)<\/\2><\/value><\/member>/g, "<$1>$3</$1>");
  76. var recusiveRegx = /<member><name>(.*)?<\/name><value><struct>(((.|[\r\n])(?!<struct>))*?)<\/struct><\/value><\/member>/;
  77. var recursiveFlag = recusiveRegx.test(response);
  78. while (recursiveFlag) {
  79. response = response.replace(/<member><name>(.*)?<\/name><value><struct>(((.|[\r\n])(?!<struct>))*?)<\/struct><\/value><\/member>/, "<_$1>$2</_$1>");
  80. recursiveFlag = recusiveRegx.test(response);
  81. }
  82. response = response.replace(/<member><name>(.*)?<\/name><value><struct>(((.|[\r\n])(?!<struct>))*?)<\/struct><\/value><\/member>/, "<$1>$2</$1>");
  83. }
  84. var xmlNode = new dom().parseFromString(response),
  85. json = SimpleConvertXML.getXMLAsObj(xmlNode);
  86. var returnObject;
  87. if (json.data) {
  88. returnObject = json.data.value;
  89. }
  90. else {
  91. if (json.param) {
  92. returnObject = json.param.value;
  93. }
  94. }
  95. return returnObject;
  96. },
  97. /**
  98. * Converts the Response to Object.
  99. *
  100. * @method getRequestByObject
  101. * @param {object} response
  102. * @return {Json Object}JSON Object
  103. */
  104. getRequestByObject: function(object) {
  105. var xmlResponse = "",
  106. methodCall = "",
  107. parallelTag = "",
  108. oneLabelArray = "";
  109. for (property in object) {
  110. if (property === "methodName") {
  111. methodCall = '<methodName>tl.' + object[property] + '</methodName>\n';
  112. }
  113. else
  114. {
  115. if (Array.isArray(object[property])) {
  116. var recursiveTags = "";
  117. object[property].map(function(tag) {
  118. recursiveTags = recursiveTags + '<member><name>' + tag.type + '</name><value><' + tag.name + '>' + tag.value + '</' + tag.type + '></value></member>\n';
  119. });
  120. oneLabelArray = oneLabelArray + '<member><name>' + property + '</name><value><struct>\n' + recursiveTags + '</struct></value></member>';
  121. }
  122. else {
  123. parallelTag = parallelTag + '<member><name>' + property + '</name><value>' +
  124. '<' + object[property]['type'] + '>' + object[property]['value'] + '</' + object[property]['type'] + '></value></member>\n';
  125. }
  126. }
  127. } //End of loop
  128. xmlResponse = '<?xml version="1.0"?>\n' +
  129. '<methodCall>\n' +
  130. methodCall +
  131. '<params>' +
  132. '<param><value><struct>\n' +
  133. parallelTag +
  134. oneLabelArray +
  135. '</struct></value></param>\n' +
  136. '</params></methodCall>';
  137. return xmlResponse;
  138. }
  139. };