WebSphere WebSphere Application Server Network Deployment, Version 6.0.x Operating Systems: AIX, HP-UX, Linux, Solaris, Windows

Web Services code example

This topic contains example WSDL and code snippets to 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. It is characterized in Web Services Description Language (WSDL), an XML-based language used to describe the services a business offers and how those services may be accessed.

Based upon this Web service, the rest of the topic shows how to program mediations to work with different parts of the message (described with the SDO representation in Mapping of SDO data graphs for Web services messages.) For each part of the message, you will see an XML description of the message, representing its SDO data graph. To accompany each XML description, you will see some snippets of code that illustrate how to work with that part of the message.

Note that in the following example the SOAP header schema is included in the WSDL. It could alternatively have been included as a separate schema in the SDO repository.

Here is the WSDL description of the message that is used as an illustration for the 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>

Working 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>
You can access the properties of the info node (see Overall Web service message layout) using code snippets like this:
	// Get the info node (a child of the graph's 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");

Working 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>
You can see the properties of the header entry with a list of headers in Header entry. You can work with a header entry and its properties using code like this:
	 	// Get the info node (a child of the graph's 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 which 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 the above, using SDO path expressions that start from the
    // info node.
    mustUnderstand = infoNode.getBoolean("headers[1]/mustUnderstand");
    priority       = infoNode.getInt("headers[1]/any[1]/priority");

Working 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--
You can see the properties of the attachment entry with byte array in Attachment entry. You can work with a header entry and its properties using code like this:
	 // Get the info node (a child of the graph's 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");

Working 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>
You can see the properties of the body in Message body layout. You can work with the contents of the body using code like this:
    // Get the info node (a child of the graph's 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 the above, using a SDO path expression that starts from the
    // info node.
    ticker = infoNode.getString("body/part1/tickerSymbol");
Related tasks
Including SOAP header schemas in the SDO repository

Reference topic

Terms of Use | Feedback

Last updated: 5 Oct 2005
http://publib.boulder.ibm.com/infocenter/ws60help/index.jsp?topic=/com.ibm.websphere.pmc.nd.doc\ref\rjy1112.html

© Copyright IBM Corporation 2004, 2005. All Rights Reserved.
This information center is powered by Eclipse technology. (http://www.eclipse.org)