+

Search Tips   |   Advanced Search

Web Services code example

This Web Services Description Language (WSDL) example and code snippets show how to access fields within a Web services message for programming a mediation.


Web services message definition

This topic contains an example of a web services message. The example is characterized in WSDL, an XML-based language used to describe the services a business offers and how those services might be accessed.

This topic shows how to program mediations to work with different parts of a web services message, which are described with the Service Data Objects (SDO) Version 1 representation in Mapping of SDO data graphs for web services messages. For each part of the message, there is an XML description of the message, representing its SDO data graph. Each XML description is accompanied by code snippets that illustrate how to work with that part of the message.

In the following example, the SOAP header schema is included in the WSDL. Alternatively, it can be included as a separate schema in the SDO repository.

This is a WSDL description of the message used as an example for subsequent code snippets:


companyInfo web service message description

<wsdl:definitions targetNamespace="http://example.companyInfo"
 xmlns:tns="http://example.companyInfo"
 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
 xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
 xmlns:wsdlmime="http://schemas.xmlsoap.org/wsdl/mime/"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema">

 <wsdl:types>
  <xsd:schema elementFormDefault="qualified"
   targetNamespace="http://example.header">

   <xsd:element name="sampleHeader">
    <xsd:complexType>
     <xsd:all>
      <xsd:element name="priority" type="xsd:int"/>
     </xsd:all>
    </xsd:complexType>
   </xsd:element>
  </xsd:schema>


  <xsd:schema elementFormDefault="qualified"
   targetNamespace="http://example.companyInfo">

   <xsd:element name="getCompanyInfo">
    <xsd:complexType>
     <xsd:all>
      <xsd:element name="tickerSymbol" type="xsd:string"/>
     </xsd:all>
    </xsd:complexType>
   </xsd:element>

   <xsd:element name="getCompanyInfoResult">
    <xsd:complexType>
     <xsd:all>
      <xsd:element name="result" type="xsd:float"/>
     </xsd:all>
    </xsd:complexType>
   </xsd:element>
  </xsd:schema>
  
  
 </wsdl:types>

 <wsdl:message name="getCompanyInfoRequest">
   <wsdl:part name="part1" element="tns:getCompanyInfo"/>
 </wsdl:message>

 <wsdl:message name="getCompanyInfoResponse">
  <wsdl:part name="part1" element="tns:getCompanyInfoResult"/>
  <wsdl:part name="part2" type="xsd:string"/>
  <wsdl:part name="part3" type="xsd:base64Binary"/>
 </wsdl:message>

 <wsdl:portType name="CompanyInfo">
  <wsdl:operation name="GetCompanyInfo">
   <wsdl:input message="tns:getCompanyInfoRequest"
               name="getCompanyInfoRequest"/>
   <wsdl:output message="tns:getCompanyInfoResponse"
                name="getCompanyInfoResponse"/>
  </wsdl:operation>
 </wsdl:portType>

 <wsdl:binding name="CompanyInfoBinding" type="tns:CompanyInfo">
  <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

  <wsdl:operation name="GetCompanyInfo">
   <wsdlsoap:operation soapAction=""/>
   <wsdl:input name="getCompanyInfoRequest">
    <wsdlsoap:body use="literal"/>
   </wsdl:input>
   <wsdl:output name="getCompanyInfoResponse">
    <wsdlsoap:body use="literal"/>
   </wsdl:output>
  </wsdl:operation>
 </wsdl:binding>

 <wsdl:service name="CompanyInfoService">
  <wsdl:port binding="tns:CompanyInfoBinding" name="SOAPPort">
   <wsdlsoap:address location="http://somewhere/services/CompanyInfoService"/>
  </wsdl:port>
 </wsdl:service>

</wsdl:definitions>


Work with the info node

This is an example of a simple SOAP request:

<env:Envelope
  xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'
  xmlns:ns1='http://example.companyInfo'>
    <env:Body>
        <ns1:getCompanyInfo>
            <ns1:tickerSymbol>IBM</ns1:tickerSymbol>
        </ns1:getCompanyInfo>
    </env:Body>
</env:Envelope>

We can access the properties of the info node (see Overall layout of a web services message) using code snippets such as this:

	// Get the info node (a child of the graph root object)
	DataObject rootNode = graph.getRootObject();
	DataObject infoNode = rootNode.getDataObject("Info");
  
  // Query the operationName, and messageType.
  String opName = infoNode.getString("operationName");
  String type   = infoNode.getString("messageType");


Work with a header

This is an example of a SOAP request including a header:

<env:Envelope
  xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'
  xmlns:ns1='http://example.companyInfo'>
    <env:Header>
        <example:sampleHeader
          env:mustUnderstand='1'
          xmlns:example='http://example.header'>
            <example:priority>4</example:priority>
        </example:sampleHeader>
    </env:Header>
    <env:Body>
        <ns1:getCompanyInfo>
            <ns1:tickerSymbol>IBM</ns1:tickerSymbol>
        </ns1:getCompanyInfo>
    </env:Body>
</env:Envelope>

For a description of the properties of the header entry with a list of headers, see Header entry. To work with a header entry and its properties, use code such as this:

	 	// Get the info node (a child of the graph root object)
    DataObject rootNode = graph.getRootObject();
    DataObject infoNode = rootNode.getDataObject("Info");
    
    // Access the list of headers
    List headerEntries = infoNode.getList("headers");
    
    // Get the first entry from the list
    DataObject headerEntry = (DataObject) headerEntries.get(0);
    
    // Query the mustUnderstand property of the header entry
    boolean mustUnderstand = headerEntry.getBoolean("mustUnderstand");
    
    // Get the Sequence that holds the content of the header entry
    Sequence headerContent = headerEntry.getSequence("any");
    
    // Get the first piece of content from the Sequence
    DataObject header = (DataObject) headerContent.getValue(0);
    
    // Read the priority from the header
    int priority = header.getInt("priority");
    
    // Shorthand for this code, using SDO path expressions that start
    // from the info node.
    mustUnderstand = infoNode.getBoolean("headers[1]/mustUnderstand");
    priority       = infoNode.getInt("headers[1]/any[1]/priority");


Work with an attachment

This is an example of a SOAP request including an XML attachment:

Content-Type: multipart/related; start="<start>"; boundary="boundary"


--boundary
Content-Type: text/xml
Content-Transfer-Encoding: 7bit
Content-ID: <start>

<env:Envelope
  xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'
  xmlns:ns1='http://example.companyInfo'>
    <env:Body>
        <ns1:getCompanyInfo>
            <ns1:tickerSymbol>IBM</ns1:tickerSymbol>
        </ns1:getCompanyInfo>
    </env:Body>
</env:Envelope>
--boundary
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: binary
Content-ID: <myAttachment>

<info>Some attached information</info>
--boundary--

For a description of the properties of the attachment entry with byte array, see Attachment entry. To work with a header entry and its properties, use code such as this:

	 // Get the info node (a child of the graph root object)
    DataObject rootNode = graph.getRootObject();
    DataObject infoNode = rootNode.getDataObject("Info");
    
    // Access the list of attachments
    List attachmentEntries = infoNode.getList("attachments");
    
    // Get the first entry from the list
    DataObject attachmentEntry = (DataObject) attachmentEntries.get(0);
    
    // Query the contentId property of the header entry
    String contentId = attachmentEntry.getString("contentId");
    
    // Get the data contained in the attachment
    byte[] data = attachmentEntry.getBytes("data");


Work with the message body

This is an example of a simple SOAP request:

<env:Envelope
  xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'
  xmlns:ns1='http://example.companyInfo'>
    <env:Body>
        <ns1:getCompanyInfo>
            <ns1:tickerSymbol>IBM</ns1:tickerSymbol>
        </ns1:getCompanyInfo>
    </env:Body>
</env:Envelope>

To see the properties of the body, see Message body layout. To work with the contents of the body, use code such as this:

    // Get the info node (a child of the graph root object)
    DataObject rootNode = graph.getRootObject();
    DataObject infoNode = rootNode.getDataObject("Info");

    // Get hold of the body node
    DataObject bodyNode = infoNode.getDataObject("body");
    
    // Get hold of the data object for the first part of the body
    DataObject part1Node = bodyNode.getDataObject("part1");
    
    // Query the tickerSymbol
    String ticker = part1Node.getString("tickerSymbol");
    
    // Shorthand for this code, using a SDO path expression that 
    // starts from the info node.
    ticker = infoNode.getString("body/part1/tickerSymbol");