perfectxml.com
 Basic Search  Advanced Search   
Topics Resources Free Library Software XML News About Us
  You are here: home Info Bank Articles » SOAP Interoperability with Microsoft and Apache Toolkits - A step by step guide Monday, 3 March 2008
 

Back to Articles Page      

        

SOAP Interoperability with Microsoft and Apache Toolkits - A step by step guide

  • Introduction
    SOAP Interoperability - a recent very hot topic on various SOAP mailing/discussion lists, is really an interesting area to focus on. My previous article was a brief introduction to write Web services with SOAP, and we wrote a simple Hello SOAP Web service. There are atleast forty toolkits available today for writing SOAP Web services and clients including those from Microsoft, Apache, SOAP::Lite, IdooXoap, DevelopMentor, etc. So do they keep the promise of SOAP and permit write server using one while client using other toolkit? Let's take a look. This article series will focus on such interoperability issues, and with simple illustrations.

    This series begins with an article on writing a simple SOAP application, where I'll be writing the server using Microsoft SOAP Toolkit Beta 2.0 in Microsoft Visual Basic 6.0 and the client in Java using Apache SOAP 2.1 toolkit. We'll write a COM DLL in Visual Basic, deploy it using Microsoft SOAP Toolkit, then use this Web service from a Java program that uses Apache SOAP Toolkit. I'll also talk about some issues and their workarounds with programming these two toolkits to interoperate.

    This article assumes you are familiar with both the toolkits. To learn more about downloading these toolkits see perfectxml.com's SOAP Software section.


  • SOAP Server - A Visual Basic 6.0 COM DLL
    Let's look at the steps for creating SOAP Server - a VB 6.0 COM DLL deployed using Microsoft SOAP Toolkit:

    •  Create a new ActiveX DLL Project. Name the project as Hello2Server and the class as Hello2.


    •  The directory where this project will be saved and DLL created, needs to be a IIS virtual directory. Use IIS Internet Service Manager and make the directory where you are saving project files, a IIS virtual directory named Hello2.


    •  Add following code under the Hello2 Class Module:
      Public Function addNumbers(ByVal NumberOne As Double, ByVal NumberTwo As Double) As Double
            addNumbers = NumberOne + NumberTwo
      End Function
    •  Build (File | Make Hello2Server.dll...) the DLL.


    •  To deploy this COM object as a SOAP service, let's now run the WSDLGEN utility that comes along with the Microsoft SOAP toolkit. This utility presents you with a nice wizard with following five easy steps:

      •  Enter a name for the Web service and choose the COM Components that implements the web service.


      •  Select the methods that will be exposed by the web service. Click on the check box against to the name of the COM component, this will select all the methods that the COM object supports.


      •  Give the URL of the listener of your web service ( ex: http://127.0.0.1/Hello2/Hello2.asp ). Here Hello2 is the virtual directory that we created above.


      •  Store all the files in the same directory where we created the Visual Basic project.


      •  Finally, click Finish, the wizard will generate the .WSDL, .WSML, and the .ASP files required for the web service to run.




  • Client - using Java and Apache Toolkit
    Let's now write the client for the above Web service. We'll use Java and Apache SOAP Toolkit 2.1 here. Make sure you've downloaded and installed the Apache SOAP 2.1 Toolkit (http://xml.apache.org/soap/index.html). Also ensure that your Java environment is set properly - jars for Xerces, Java Mail, Java Activation Framework and Apache SOAP 2.1 are in your CLASSPATH.

    The client code looks like:
    import java.io.*;
    import java.util.*;
    import java.net.*;
    import org.w3c.dom.*;
    import org.apache.soap.util.xml.*;
    import org.apache.soap.*;
    import org.apache.soap.encoding.*;
    import org.apache.soap.encoding.soapenc.*;
    import org.apache.soap.rpc.*;
    import org.apache.soap.transport.http.SOAPHTTPConnection;

    public class testClient 
    {

    public static void main(String[] args) throws Exception 
    {

        URL url = new URL ("http://127.0.0.1:8080/Hello2/Hello2.asp");

        SOAPMappingRegistry smr = new SOAPMappingRegistry ();
        StringDeserializer sd = new StringDeserializer ();
        smr.mapTypes (Constants.NS_URI_SOAP_ENC,    new QName ("", "Result"), null, null, sd);

        // create the transport and set parameters
       SOAPHTTPConnection st = new SOAPHTTPConnection();

       // build the call.
       Call call = new Call ();
       call.setSOAPTransport(st);
       call.setSOAPMappingRegistry (smr);

       call.setTargetObjectURI ("http://tempuri.org/message/");
       call.setMethodName("addNumbers");
       call.setEncodingStyleURI ("http://schemas.xmlsoap.org/soap/encoding/");

      Vector params = new Vector();
      params.addElement(new Parameter("NumberOne", Double.class, "10", null));
      params.addElement(new Parameter("NumberTwo", Double.class, "25", null));
      call.setParams(params); 

       Response resp = null;
       try 
      {
          resp = call.invoke (url, "http://tempuri.org/action/Hello2.addNumbers"); 
      } 
       catch (SOAPException e) 
      {
       System.err.println("Caught SOAPException (" + e.getFaultCode () + "): " + e.getMessage ()); 
       return;
      }

      // check response 
      if (resp != null && !resp.generatedFault()) 
      {
           Parameter ret = resp.getReturnValue();
          Object value = ret.getValue();

          System.out.println ("Answer--> " + value);
      }
      else 
     {
        Fault fault = resp.getFault ();
        System.err.println ("Generated fault: ");
        System.out.println (" Fault Code = " + fault.getFaultCode()); 
        System.out.println (" Fault String = " + fault.getFaultString());
      }
    }
    }


    Note that I am using port 8080 and not port 80 (which is the port at which IIS is listening). I have done this so that I can use the MSoapT application. MSoapT is an excellent debugging utility for SOAP applications. By the help of this utility you can observe all the requests that are sent to the server and all the responses that the server generates.

    Run MSoapT and configure it to listen on 8080, and forwarding calls to port 80.
    Finally, Compile and run above client application.

    When the client application is run, you will see following request in the MSoapT application, that the client posts to the server:
    <?xml version='1.0' encoding='UTF-8'?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema">
    <SOAP-ENV:Body>
    <ns1:addNumbers xmlns:ns1="http://tempuri.org/message/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <NumberOne xsi:type="xsd:double">10</NumberOne>
    <NumberTwo xsi:type="xsd:double">25</NumberTwo>

    </ns1:addNumbers>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>


    And you will also be able to see the output that the server has generated.
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
    <m:addNumbersResponse xmlns:m="http://tempuri.org/message/">
    <Result>35</Result>
    </m:addNumbersResponse>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>


    On careful observation of the request that the Apache SOAP implementation posted and the response what the Microsoft SOAP Toolkit generated, you'll notice a fundamental difference between the two. The Apache SOAP always puts the xsi:type elements for all the parameters in the request (NumberOne and NumberTwo). However, Microsoft SOAP Toolkit does not put the xsi:type of element in the Response (Result).

    If we do not handle this difference, the Apache SOAP implementation would reject the response that the Microsoft SOAP Toolkit generated, because of the absence of the xsi:type attribute of the Result element.

    We'll handle this situation by instructing Apache SOAP Client to not to expect the xsi:type attribute in the response received. This is done by following lines from the above client code:
    SOAPMappingRegistry smr = new SOAPMappingRegistry ();
    StringDeserializer sd = new StringDeserializer ();
    smr.mapTypes (Constants.NS_URI_SOAP_ENC, new QName ("", "Result"), null, null, sd);


  • Summary
    In this article, we learnt how to invoke a Web service deployed using Microsoft SOAP Toolkit from the Java client that uses Apache SOAP Toolkit. This article just started with a discussion on the differences between the two SOAP toolkits and the ways to work around those to achieve interoperability. Future articles will bring more details in this respect, so watch this space for interesting interoperatibility tutorials.

    Download Download source code files

If you have any questions or comments, feel free to contact author of this article, Abhishek Srivastava at  abhisheks@india.hp.com Email.

  

Back to Articles Page      

All information on this site is for training only. We do not warrant its correctness or its fitness to be used. The risk of using it remains entirely with the user. 

 

  Contact Us | E-mail Us | Site Guide | About PerfectXML | Advertise ©2004 perfectxml.com. All rights reserved. | Privacy