Calling SOAP Web services

From Toolsverse Knowledge Base
Jump to: navigation, search

The ETL Framework natively supports SOAP Web services over HTTP. You will need to:

  1. Set source or destination connection to the one linked to the SOAP endpoint
  2. Define SOAP request in the connection CDATA attribute
<connection alias="Cars">
            <url>http://www.vendor.com/soapendpoint</url>
            <CDATA><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.vendor.com/soapendpoint/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <SOAP-ENV:Header>
    <ns1:AuthenticationHeader>
      <vendorUserId>{requestUserId}</mktowsUserId>
      <vendorSignature>{requestSignature}</requestSignature>
      <vendorTimestamp>{requestTimestamp}</requestTimestamp>
    </ns1:AuthenticationHeader>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <ns1:paramsGetMultipleCars>
      <leadSelector xsi:type="ns1:KeySelector">
        <keyType>MAKE</keyType>
        <keyValues>
          <stringItem>Ford</stringItem>
          <stringItem>Acura</stringItem>
        </keyValues>
      </leadSelector>
      <batchSize>100</batchSize>
    </ns1:paramsGetMultipleCars>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
 
]]></CDATA>
            <params>method=post;exceptions=500;file=false;header_Content-Type=text/xml</params>
            <connector>com.toolsverse.etl.connector.xml.XmlObjectConnector</connector>
            <transport>com.toolsverse.io.HttpProcessor</transport>
        </connection>

You can tokenize any part of the CDATA or URL using {token}. In the example above there are 3 tokens:

      <vendorUserId>{requestUserId}</mktowsUserId>
      <vendorSignature>{requestSignature}</requestSignature>
      <vendorTimestamp>{requestTimestamp}</requestTimestamp>

Simple example below demonstrates how to set tokens, link source to the SOAP and point and transform result web service execution into the destination:

<?xml version="1.0" encoding="UTF-8" ?>
<scenario>
     <name>SOAP</name>
     <script>soap</script>
     <driver name="com.toolsverse.etl.driver.GenericFileDriver" />
     <beforetasks>
          <task noconnection="true">
               <name>security</name>
               <class>com.toolsverse.etl.core.task.common.ScriptTask</class>
               <code>
                    <![CDATA[
                    importPackage(java.text);
                    importPackage(java.util);
 
                    importPackage(javax.crypto);
                    importPackage(javax.crypto.spec);
 
                    importPackage(org.apache.commons.codec.binary);
 
                    importPackage(com.toolsverse.config);
 
                    var vendorUserId = SystemConfig.instance().getSystemProperty('requestUserId');
                    var vendorSecretKey = SystemConfig.instance().getSystemProperty('vendorSecretKey');
 
                    var df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
                    var text = df.format(new java.util.Date());
                    var requestTimestamp = text.substring(0, 22) + ":" + text.substring(22);           
                    var encryptString = requestTimestamp + vendorUserId;
 
                    var secretKey = new SecretKeySpec(new java.lang.String(vendorSecretKey).getBytes(), "HmacSHA1");
                    var mac = Mac.getInstance("HmacSHA1");
                    mac.init(secretKey);
                    var rawHmac = mac.doFinal(new java.lang.String(encryptString).getBytes());
                    var hexChars = Hex.encodeHex(rawHmac);
                    var signature = new java.lang.String(hexChars); 
 
                    SystemConfig.instance().setSystemProperty('requestSignature', signature);
                    SystemConfig.instance().setSystemProperty('requestTimestamp', requestTimestamp);
                    SystemConfig.instance().setSystemProperty('requestUserId', vendorUserId);
                    ]]>
               </code>
               <variables>
                    <LANG value="JavaScript" />
               </variables>
          </task>
     </beforetasks>
     <sources>
          <source>
               <name>cars</name>
          </source>
     </sources>     
     <destinations>
          <destination >
               <name>cars</name>
          </destination>
     </destinations>
</scenario>