Difference between revisions of "Access an external web service using SOAP"

From AgileApps Support Wiki
imported>Aeric
imported>Aeric
Line 1: Line 1:
{{TBD|In Progress}}
This sample is based on a working example, but it has been simplified to make it easier to follow. (It therefore will not work "as is". Use it as a template to guide your own developments.)
 
Here is the sample {{^WSDL}} (Web Service Description Language) the code is based on:
:<syntaxhighlight lang="xml" enclose="div">
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
  xmlns:y="http://mydomain.com/calculator/"
  xmlns:ns="http://mydomain.com/calculator/types/"
  targetNamespace="http://mydomain.com/calculator/">
  <types>
    <xs:schema targetNamespace="http://mydomain.com/calculator/types/"
      xmlns="http://mydomain.com/calculator/types/"
      elementFormDefault="unqualified" attributeFormDefault="unqualified">
        <xs:complexType name="CalculatorInput">
            <xs:sequence>
              <xs:element name="x" type="xs:double"/>
              <xs:element name="y" type="xs:double"/>
            </xs:sequence>
        </xs:complexType>
        <xs:complexType name="CalculatorOutput">
            <xs:sequence>
              <xs:element name="result" type="xs:double"/>
            </xs:sequence>
        </xs:complexType>
        <xs:element name="Add" type="CalculatorInput"/>
        <xs:element name="AddResponse" type="CalculatorOutput"/>
      </xs:schema>
  </types>
  <message name="AddMessage">
      <part name="parameters" element="ns:Add"/>
  </message>
  <message name="AddResponseMessage">
      <part name="parameters" element="ns:AddResponse"/>
  </message>
  <portType name="CalculatorInterface">
      <operation name="Add">
        <input message="y:AddMessage"/>
        <output message="y:AddResponseMessage"/>
      </operation>
  </portType>
  <binding name="CalculatorSoapHttpBinding" type="y:CalculatorInterface">
      <soap:binding style="document"
        transport="http://schemas.xmlsoap.org/soap/http"/>
      <operation name="Add">
        <soap:operation soapAction="http://mydomain.com/calculator/#Add"/>
        <input>
            <soap:body use="literal"/>
        </input>
        <output>
            <soap:body use="literal"/>
        </output>
      </operation>
  </binding>
  <service name="CalculatorService">
      <port name="CalculatorEndpoint" binding="y:CalculatorSoapHttpBinding">
        <soap:address location="http://mydomain.com/Calculator/Calculator.asmx"/>
      </port>
  </service>
</definitions>
</syntaxhighlight>
 
And here is the code that uses that WSDL to access the remote service and connect to it. To use this code, you would place it into a <tt>Calculator</tt> class, and invoke the <tt>add()</tt> method from a Rule or from other platform code.
:<syntaxhighlight lang="xml" enclose="div">
import com.platform.api.*;
import org.json.*;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
 
public class Calculator
{
    //This method makes a request to
    public void add(Parameters p) throws Exception
    {
      String x = (String)p.get("x");
      String y = (String)p.get("y");
     
      String soapRequest =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:http=\"http://schemas.xmlsoap.org/wsdl/http/\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:mime=\"http://schemas.xmlsoap.org/wsdl/mime/\" xmlns:y=\"http://mydomain.com/calculator/\" xmlns:ns=\"http://mydomain.com/calculator/types/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"+
"  <SOAP-ENV:Body>"+
"      <mns1:Add xmlns:mns1=\"http://mydomain.com/calculator/\">"+
"        <x>" + x + "</x>"+
"        <y>" + y + "</y>"+
"      </mns1:Add>"+
"  </SOAP-ENV:Body>"+
"</SOAP-ENV:Envelope>";
     
      HttpConnection con = new HttpConnection(CONSTANTS.HTTP.METHOD.POST, "http://mydomain.com/Calculator/Calculator.asmx");
     
      con.addHeader("SOAPAction","http://mydomain.com/calculator/#Add");
     
      con.addHeader("Content-Type","text/xml");
     
      con.setRequestBody(soapRequest);
     
      int code = con.execute();
     
      String response = con.getResponse();
 
      /*
Sample Response
 
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:http=\"http://schemas.xmlsoap.org/wsdl/http/\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:mime=\"http://schemas.xmlsoap.org/wsdl/mime/\" xmlns:y=\"http://mydomain.com/calculator/\" xmlns:ns=\"http://mydomain.com/calculator/types/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"+
  <SOAP-ENV:Body>"+
      <mns1:AddResponse xmlns:mns1=\"http://mydomain.com/calculator/\">
        <result>1</result>
      </mns1:AddResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
 
 
 
      */
      //Extracting the result element from the response
      //First convert xml response to JSON using org.json package
      //Then extract the result element
      JSONObject jo = org.json.XML.toJSONObject(response);
     
      String resultElement = jo.getJSONObject("SOAP-ENV:Envelope")
.getJSONObject("SOAP-ENV:Body")
      .getJSONObject("mns1:AddResponse")
      .getString("result");
     
    }
}
</syntaxhighlight>

Revision as of 22:32, 2 July 2013

This sample is based on a working example, but it has been simplified to make it easier to follow. (It therefore will not work "as is". Use it as a template to guide your own developments.)

Here is the sample WSDL (Web Service Description Language) the code is based on:

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" 
  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
  xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
  xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" 
  xmlns:y="http://mydomain.com/calculator/" 
  xmlns:ns="http://mydomain.com/calculator/types/" 
  targetNamespace="http://mydomain.com/calculator/">
   <types>
     <xs:schema targetNamespace="http://mydomain.com/calculator/types/" 
       xmlns="http://mydomain.com/calculator/types/" 
       elementFormDefault="unqualified" attributeFormDefault="unqualified">
         <xs:complexType name="CalculatorInput">
            <xs:sequence>
               <xs:element name="x" type="xs:double"/>
               <xs:element name="y" type="xs:double"/>
            </xs:sequence>
         </xs:complexType>
         <xs:complexType name="CalculatorOutput">
            <xs:sequence>
               <xs:element name="result" type="xs:double"/>
            </xs:sequence>
         </xs:complexType>
         <xs:element name="Add" type="CalculatorInput"/>
         <xs:element name="AddResponse" type="CalculatorOutput"/>
      </xs:schema>
   </types>
   <message name="AddMessage">
      <part name="parameters" element="ns:Add"/>
   </message>
   <message name="AddResponseMessage">
      <part name="parameters" element="ns:AddResponse"/>
   </message>
   <portType name="CalculatorInterface">
      <operation name="Add">
         <input message="y:AddMessage"/>
         <output message="y:AddResponseMessage"/>
      </operation>
   </portType>
   <binding name="CalculatorSoapHttpBinding" type="y:CalculatorInterface">
      <soap:binding style="document" 
         transport="http://schemas.xmlsoap.org/soap/http"/>
      <operation name="Add">
         <soap:operation soapAction="http://mydomain.com/calculator/#Add"/>
         <input>
            <soap:body use="literal"/>
         </input>
         <output>
            <soap:body use="literal"/>
         </output>
      </operation>
   </binding>
   <service name="CalculatorService">
      <port name="CalculatorEndpoint" binding="y:CalculatorSoapHttpBinding">
         <soap:address location="http://mydomain.com/Calculator/Calculator.asmx"/>
      </port>
   </service>
</definitions>

And here is the code that uses that WSDL to access the remote service and connect to it. To use this code, you would place it into a Calculator class, and invoke the add() method from a Rule or from other platform code.

import com.platform.api.*;
import org.json.*;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;

public class Calculator
{
    //This method makes a request to 
    public void add(Parameters p) throws Exception
    {
      String x = (String)p.get("x");
      String y = (String)p.get("y");
      
      String soapRequest =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
	"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:http=\"http://schemas.xmlsoap.org/wsdl/http/\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:mime=\"http://schemas.xmlsoap.org/wsdl/mime/\" xmlns:y=\"http://mydomain.com/calculator/\" xmlns:ns=\"http://mydomain.com/calculator/types/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"+
	"   <SOAP-ENV:Body>"+
	"      <mns1:Add xmlns:mns1=\"http://mydomain.com/calculator/\">"+
	"         <x>" + x + "</x>"+
	"         <y>" + y + "</y>"+
	"      </mns1:Add>"+
	"   </SOAP-ENV:Body>"+
	"</SOAP-ENV:Envelope>";
      
      HttpConnection con = new HttpConnection(CONSTANTS.HTTP.METHOD.POST, "http://mydomain.com/Calculator/Calculator.asmx");
      
      con.addHeader("SOAPAction","http://mydomain.com/calculator/#Add");
      
      con.addHeader("Content-Type","text/xml");
      
      con.setRequestBody(soapRequest);
      
      int code = con.execute();
      
      String response = con.getResponse();

      /*
	Sample Response

	<?xml version=\"1.0\" encoding=\"UTF-8\"?>
	<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:http=\"http://schemas.xmlsoap.org/wsdl/http/\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:mime=\"http://schemas.xmlsoap.org/wsdl/mime/\" xmlns:y=\"http://mydomain.com/calculator/\" xmlns:ns=\"http://mydomain.com/calculator/types/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"+
	   <SOAP-ENV:Body>"+
	      <mns1:AddResponse xmlns:mns1=\"http://mydomain.com/calculator/\">
	         <result>1</result>
	      </mns1:AddResponse>
	   </SOAP-ENV:Body>
	</SOAP-ENV:Envelope>



      */
 
      //Extracting the result element from the response
      //First convert xml response to JSON using org.json package
      //Then extract the result element
      JSONObject jo = org.json.XML.toJSONObject(response);
	      
      String resultElement = jo.getJSONObject("SOAP-ENV:Envelope")
				.getJSONObject("SOAP-ENV:Body")
	    		  	.getJSONObject("mns1:AddResponse")
	    		  	.getString("result");
      
    }
}