msgbuilder.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #pragma warning (disable : 4786)
  14. #include "esphttp.hpp"
  15. //Jlib
  16. #include "jliball.hpp"
  17. //SCM Interface definition includes:
  18. #include "esp.hpp"
  19. #include "espthread.hpp"
  20. //ESP Bindings
  21. #include "http/platform/httpprot.hpp"
  22. #include "http/platform/httptransport.ipp"
  23. #include "http/platform/httpservice.hpp"
  24. #include "SOAP/Platform/soapservice.hpp"
  25. //openssl
  26. #include <openssl/rsa.h>
  27. #include <openssl/crypto.h>
  28. #ifdef WIN32
  29. #define MSGBUILDER_EXPORT _declspec(dllexport)
  30. #else
  31. #define MSGBUILDER_EXPORT
  32. #endif
  33. #include "msgbuilder.hpp"
  34. // ===========================================================================
  35. CSoapMsgBuilder::CSoapMsgBuilder(const char * xml)
  36. {
  37. m_properties.setown(createPTreeFromXMLString(xml));
  38. }
  39. void CSoapMsgBuilder::setPropertyValue(const char * key, const char * val)
  40. {
  41. assertex(m_properties->hasProp(key));
  42. m_properties->setProp(key, val);
  43. }
  44. void CSoapMsgBuilder::setPropertyValueInt(const char * key, unsigned int val)
  45. {
  46. char buff[64];
  47. itoa(val, buff, 10);
  48. setPropertyValue(key, buff);
  49. }
  50. void CSoapMsgBuilder::setPropertyValueBool(const char * key, bool val)
  51. {
  52. setPropertyValueInt(key, val == true ? 1 : 0);
  53. }
  54. StringBuffer & CSoapMsgBuilder::getSoapResponse(StringBuffer & soapResponse)
  55. {
  56. IPropertyTreeIterator * itr = m_properties->getElements("*");
  57. itr->first();
  58. while(itr->isValid())
  59. {
  60. IPropertyTree & prop = itr->query();
  61. StringBuffer key, val;
  62. prop.getName(key);
  63. m_properties->getProp(key.str(), val);
  64. soapResponse.appendf("<%s>%s</%s>", key.str(), val.str(), key.str());
  65. itr->next();
  66. }
  67. itr->Release();
  68. return soapResponse;
  69. }
  70. CSoapMsgArrayBuilder::CSoapMsgArrayBuilder(CSoapMsgXsdBuilder * xsd)
  71. {
  72. m_xsd.set(xsd);
  73. }
  74. unsigned CSoapMsgArrayBuilder::ordinality()
  75. {
  76. return m_array.ordinality();
  77. }
  78. CSoapMsgBuilder * CSoapMsgArrayBuilder::item(unsigned i)
  79. {
  80. return &m_array.item(i);
  81. }
  82. CSoapMsgBuilder * CSoapMsgArrayBuilder::newMsgBuilder()
  83. {
  84. CSoapMsgBuilder * retVal = m_xsd->newMsgBuilder();
  85. retVal->Link();
  86. m_array.append(*retVal);
  87. return retVal;
  88. }
  89. StringBuffer & CSoapMsgArrayBuilder::getSoapResponse(StringBuffer & soapResponse)
  90. {
  91. StringBuffer label;
  92. m_xsd->getLabel(label);
  93. ForEachItemIn(i, m_array)
  94. {
  95. CSoapMsgBuilder &msg = m_array.item(i);
  96. soapResponse.appendf("<%s>", label.str());
  97. msg.getSoapResponse(soapResponse);
  98. soapResponse.appendf("</%s>", label.str());
  99. }
  100. return soapResponse;
  101. }
  102. CSoapMsgXsdBuilder::CSoapMsgXsdBuilder(const char * structLabel, const char * var)
  103. {
  104. m_properties.set(createPTree(structLabel));
  105. m_structLabel.append(structLabel);
  106. m_var.append(var);
  107. }
  108. void CSoapMsgXsdBuilder::appendProperty(const char * prop, XSD_TYPES type)
  109. {
  110. m_properties->setPropInt(prop, type);
  111. }
  112. StringBuffer & CSoapMsgXsdBuilder::getLabel(StringBuffer & label)
  113. {
  114. label.append(m_structLabel.str());
  115. return label;
  116. }
  117. StringBuffer & CSoapMsgXsdBuilder::getXsd(StringBuffer & wsdlSchema)
  118. {
  119. wsdlSchema.appendf("<%s:complexType name=\"%s\" >", m_var.str(), m_structLabel.str());
  120. wsdlSchema.appendf("<%s:sequence>", m_var.str());
  121. IPropertyTreeIterator * itr = m_properties->getElements("*");
  122. itr->first();
  123. while(itr->isValid())
  124. {
  125. IPropertyTree & prop = itr->query();
  126. StringBuffer name;
  127. prop.getName(name);
  128. wsdlSchema.appendf("<%s:element minOccurs=\"0\" maxOccurs=\"1\" name=\"%s\" type=\"xsd:%s\"/>", m_var.str(), name.str(), getXsdTypeLabel(static_cast<XSD_TYPES>(m_properties->getPropInt(name.str()))));
  129. itr->next();
  130. }
  131. itr->Release();
  132. wsdlSchema.appendf("</%s:sequence>", m_var.str());
  133. wsdlSchema.appendf("</%s:complexType>", m_var.str());
  134. wsdlSchema.appendf("<%s:complexType name=\"ArrayOf%s\" >", m_var.str(), m_structLabel.str());
  135. wsdlSchema.appendf("<%s:sequence>", m_var.str());
  136. wsdlSchema.appendf("<%s:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"%s\" type=\"tns:%s\"/>", m_var.str(), m_structLabel.str(), m_structLabel.str());
  137. wsdlSchema.appendf("</%s:sequence>", m_var.str());
  138. wsdlSchema.appendf("</%s:complexType>", m_var.str());
  139. wsdlSchema.appendf("<%s:element name=\"%s\" nillable=\"true\" type=\"tns:%s\" />", m_var.str(), m_structLabel.str(), m_structLabel.str());
  140. wsdlSchema.appendf("<%s:element name=\"ArrayOf%s\" nillable=\"true\" type=\"tns:ArrayOf%s\" />", m_var.str(), m_structLabel.str(), m_structLabel.str());
  141. return wsdlSchema;
  142. }
  143. const char * const XSD_STRING_DESC = "string";
  144. const char * const XSD_INT_DESC = "int";
  145. const char * const XSD_BOOL_DESC = "boolean";
  146. const char * CSoapMsgXsdBuilder::getXsdTypeLabel(XSD_TYPES type)
  147. {
  148. switch(type)
  149. {
  150. case XSD_STRING:
  151. return XSD_STRING_DESC;
  152. case XSD_INT:
  153. return XSD_INT_DESC;
  154. case XSD_BOOL:
  155. return XSD_BOOL_DESC;
  156. default:
  157. return XSD_STRING_DESC;
  158. }
  159. }
  160. CSoapMsgBuilder * CSoapMsgXsdBuilder::newMsgBuilder()
  161. {
  162. Owned<IPropertyTree>newXml = createPTree(m_structLabel.str());
  163. IPropertyTreeIterator * itr = m_properties->getElements("*");
  164. itr->first();
  165. while(itr->isValid())
  166. {
  167. IPropertyTree & prop = itr->query();
  168. StringBuffer name;
  169. prop.getName(name);
  170. switch(m_properties->getPropInt(name.str()))
  171. {
  172. case XSD_INT:
  173. newXml->setProp(name.str(), "0");
  174. break;
  175. case XSD_BOOL:
  176. newXml->setProp(name.str(), "0");
  177. break;
  178. default:
  179. newXml->setProp(name.str(), "");
  180. }
  181. itr->next();
  182. }
  183. itr->Release();
  184. StringBuffer xml;
  185. return new CSoapMsgBuilder(toXML(newXml, xml).str());
  186. }
  187. CSoapMsgArrayBuilder * CSoapMsgXsdBuilder::newMsgArrayBuilder()
  188. {
  189. return new CSoapMsgArrayBuilder(this);
  190. }
  191. // ===========================================================================