Access an external web service using SOAP

From AgileApps Support Wiki

Jump to: navigation, search

This sample shows how to make a SOAP request to an external service. It 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.


Sample WSDL

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

<definitions xmlns=""
     <xs:schema targetNamespace=""
      elementFormDefault="unqualified" attributeFormDefault="unqualified">
         <xs:complexType name="CalculatorInput">
               <xs:element name="x" type="xs:double"/>
               <xs:element name="y" type="xs:double"/>
         <xs:complexType name="CalculatorOutput">
               <xs:element name="result" type="xs:double"/>
         <xs:element name="Add" type="CalculatorInput"/>
         <xs:element name="AddResponse" type="CalculatorOutput"/>
   <message name="AddMessage">
      <part name="parameters" element="ns:Add"/>
   <message name="AddResponseMessage">
      <part name="parameters" element="ns:AddResponse"/>
   <portType name="CalculatorInterface">
      <operation name="Add">
         <input message="y:AddMessage"/>
         <output message="y:AddResponseMessage"/>
   <binding name="CalculatorSoapHttpBinding" type="y:CalculatorInterface">
      <soap:binding style="document"
      <operation name="Add">
         <soap:operation soapAction=""/>
            <soap:body use="literal"/>
            <soap:body use="literal"/>
   <service name="CalculatorService">
      <port name="CalculatorEndpoint" binding="y:CalculatorSoapHttpBinding">
         <soap:address location=""/>

Sample Code

Here is the code that accesses the remote service. The WSDL definition was used to format the SOAP request and to determine the response format. To use this code, you would place it into a Calculator class. You could then invoke the add() method it defines from a Rule or from other platform code.

// These libraries are part of the platform.
// Import them here to make them available to the class.
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 an external web service to add two values.
    public void add(Parameters p) throws Exception
      // Here, the record parameters are assumed to be "order_total" and "shipping".
      // When you invoke a method in the platform, it will always be for some record.
      // The fields defined for that record are available in the Parameters object.
      // Use lines of code like these to extract them for use in the SOAP request.
      String order_amount = (String)p.get("order_total");
      String shipping_cost = (String)p.get("shipping");
      // The WSDL describes the format of the SOAP request. Here, the extracted
      // params are converted to variables "x" and "y" the expected format.
      String soapRequest =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
    "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"\" xmlns:soap=\"\" xmlns:http=\"\" xmlns:xs=\"\" xmlns:soapenc=\"\" xmlns:mime=\"\" xmlns:y=\"\" xmlns:ns=\"\" xmlns:xsi=\"\" xmlns:xsd=\"\">"+
        "   <SOAP-ENV:Body>"+
    "      <mns1:Add xmlns:mns1=\"\">"+
    "         <x>" + order_amount  + "</x>"+
    "         <y>" + shipping_cost + "</y>"+
    "      </mns1:Add>"+
    "   </SOAP-ENV:Body>"+
      // Create an HTTP connection to use for the SOAP request,
      // using the built-in HttpConnection class
      HttpConnection con = new HttpConnection(
      // Send the request and get the response
      int code = con.execute();
      String response = con.getResponse();

       Sample Response, as determined by the WSDL
    <?xml version=\"1.0\" encoding=\"UTF-8\"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV=\"\" xmlns:soap=\"\" xmlns:http=\"\" xmlns:xs=\"\" xmlns:soapenc=\"\" xmlns:mime=\"\" xmlns:y=\"\" xmlns:ns=\"\" xmlns:xsi=\"\" xmlns:xsd=\"\">"+
          <mns1:AddResponse xmlns:mns1=\"\">

      // Extract the result element from the response:
      //  1. Convert XML response to JSON using the org.json package
      //  2. Extract the result element from the JSON structure
      JSONObject jo = org.json.XML.toJSONObject(response);        
      String resultElement = jo.getJSONObject("SOAP-ENV:Envelope")
      // The data returned from the SOAP request can be used here. For
      // example, the Java API could be used to update the current record
      // with the retrieved value.

    } // add() method
} // Calculator class

Learn More

Personal tools