Exforsys

Home arrow Reviews arrow Java SOA Training

Access WSDL

Author : Exforsys Inc.     Published on: 20th Jun 2008

Access WSDL and Generate Axis Stubs to Access the Web Service Remotely

Now for the really cool stuff. As we discussed earlier, we have set up the ServiceMix as a separate web service gateway in front of the actual web service deployment. Now we have to check whether we can access the WSDL from the ServiceMix. For this, we can point our browser using the standard WSDL query string, like:

Ads

http://localhost:8081/services/HelloWebService/?wsdl
or
http://localhost:8081/services/HelloWebService/main.wsdl

Note that, the above URL points to the locationURI attribute configured for the consumer component, which is http://localhost:8081/services/HelloWebService. The WSDL placed in location ch10\ServiceMixHttpBinding\HelloWebService-esb.wsdl, matches the following code:

Sample Code
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <wsdl:definitions
  3. xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
  4. xmlns:apachesoap="http://xml.apache.org/xml-soap"
  5. xmlns:impl="http://AxisEndToEnd.axis.apache.binildas.com"
  6. xmlns:intf="http://AxisEndToEnd.axis.apache.binildas.com"
  7. xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
  8. xmlns:xsd="http://www.w3.org/2001/XMLSchema" target="_blank" rel="nofollow"
  9. targetNamespace="http://AxisEndToEnd.axis.apache.binildas.com">
  10. <wsdl:types>
  11. <schema xmlns="http://www.w3.org/2001/XMLSchema" target="_blank" rel="nofollow"
  12. elementFormDefault="qualified"
  13. targetNamespace="http://AxisEndToEnd.axis.
  14. apache.binildas.com">
  15. <element name="hello">
  16. <complexType>
  17. <sequence>
  18. <element name="in0" type="xsd:string"/>
  19. </sequence>
  20. </complexType>
  21. </element>
  22. <element name="helloResponse">
  23. <complexType>
  24. <sequence>
  25. <element name="helloReturn" type="xsd:string"/>
  26. </sequence>
  27. </complexType>
  28. </element>
  29. </schema>
  30. </wsdl:types>
  31. <wsdl:message name="helloRequest">
  32. <wsdl:part element="impl:hello" name="parameters">
  33. </wsdl:part>
  34. </wsdl:message>
  35. <wsdl:message name="helloResponse">
  36. <wsdl:part element="impl:helloResponse" name="parameters">
  37. </wsdl:part>
  38. </wsdl:message>
  39. <wsdl:portType name="IHelloWeb">
  40. <wsdl:operation name="hello">
  41. <wsdl:input message="impl:helloRequest" name="helloRequest">
  42. </wsdl:input>
  43. <wsdl:output message="impl:helloResponse"
  44. name="helloResponse">
  45. </wsdl:output>
  46. </wsdl:operation>
  47. </wsdl:portType>
  48. <wsdl:binding name="HelloWebServiceBinding" type="impl:IHelloWeb">
  49. <wsdlsoap:binding style="document"
  50. transport="http://schemas.xmlsoap.org/
  51. soap/http"/>
  52. <wsdl:operation name="hello">
  53. <wsdlsoap:operation soapAction=""/>
  54. <wsdl:input name="helloRequest">
  55. <wsdlsoap:body use="literal"/>
  56. </wsdl:input>
  57. <wsdl:output name="helloResponse">
  58. <wsdlsoap:body use="literal"/>
  59. </wsdl:output>
  60. </wsdl:operation>
  61. </wsdl:binding>
  62. <wsdl:service name="MyConsumerService">
  63. <wsdl:port binding="impl:HelloWebServiceBinding"
  64. name="HelloWebService">
  65. <wsdlsoap:address location="http://localhost:8081/
  66. services/HelloWebService/"/>
  67. </wsdl:port>
  68. </wsdl:service>
  69. </wsdl:definitions>
Copyright exforsys.com


If we compare the two WSDL, the major difference is in the service description section. Here, ServiceMix forms the service and port name taking values from service and endpoint attributes of the consumer service?MyConsumerService and HelloWebService respectively.

If we are able to retrieve the WSDL, the next step is to use the Apache Axis tools to auto-generate the client-side stubs and binding classes, using which we can write simple Java client code to access the service through HTTP channel. The Axis client classes are placed in the directory ch10\ServiceMixHttpBinding\03_AxisClient.

To do that, we have to use the wsdl2java ant task. Let us first declare the task definition and execute that task to generate the stub classes.

Sample Code
  1. <taskdef name="wsdl2java"
  2. classname="org.apache.axis.tools.ant.wsdl.Wsdl2javaAntTask"
  3. loaderref="axis" >
  4. <classpath refid="classpath"/>
  5. </taskdef>
  6. <target name="wsdl2java">
  7. <java classname="org.apache.axis.wsdl.WSDL2Java"
  8. fork="true"
  9. failonerror="true">
  10. <arg value="-o"/>
  11. <arg value="${src}"/>
  12. <arg value="-x"/>
  13. <arg value="http://io.java"/>
  14. <arg value="http://localhost:8081/services/HelloWebService/
  15. main.wsdl"/>
  16. <classpath>
  17. <path refid="classpath"/>
  18. <pathelement location="${build}"/>
  19. </classpath>
  20. </java>
  21. </target>
Copyright exforsys.com


The task will extract the WSDL from the specified location and generate the following client-side artifacts:

com\binildas\apache\axis\AxisEndToEnd\HelloWebServiceBindingStub.java
com\binildas\apache\axis\AxisEndToEnd\IHelloWeb.java
com\binildas\apache\axis\AxisEndToEnd\MyConsumerService.java
com\binildas\apache\axis\AxisEndToEnd\MyConsumerServiceLocator.java

The Client Java class can be written against these generated files as follows:

Sample Code
  1. public class Client
  2. {
  3. private static String wsdlUrl = "http://localhost:8081/services/
  4. HelloWebService/main.wsdl";
  5. private static String namespaceURI = "http://AxisEndToEnd.
  6. axis.apache.binildas.com";
  7. private static String localPart = "MyConsumerService";
  8. protected void executeClient(String[] args)throws Exception
  9. {
  10. MyConsumerService myConsumerService = null;
  11. IHelloWeb iHelloWeb = null;
  12. if(args.length == 3)
  13. {
  14. myConsumerService = new MyConsumerServiceLocator(args[0],
  15. new QName(args[1], args[2]));
  16. }
  17. else
  18. {
  19. myConsumerService = new MyConsumerServiceLocator(wsdlUrl,
  20. new QName(namespaceURI, localPart));
  21. }
  22. iHelloWeb = myConsumerService.getHelloWebService();
  23. }
  24. public static void main(String[] args)throws Exception
  25. {
  26. Client client = new Client();
  27. client.executeClient(args);
  28. }
  29. }
Copyright exforsys.com


To build the entire Axis client codebase, assuming that the ServiceMix is up and running, change directory to ch10\ServiceMixHttpBinding\03_AxisClient, which contains a build.xml file. Execute ant as shown as follows:

cd ch10\ServiceMixHttpBinding\03_AxisClient
ant

This will generate the required Axis client-side stubs and compile the client classes. Now to run the client, execute the following command:

    ant run

Summary

We started this chapter by introducing the servicemix-http JBI component. Then we looked at the samples of binding web services to ESB using the servicemix-http binding component. By doing so, we have, in fact, implemented a complete functional web services gateway at the ESB.

A lot of times, we utilize this pattern to expose useful web services hosted deep inside your corporate networks protected by multiple levels of firewall. When we do so, the web services gateway is the access point for any external client. It should mock the actual web service not only in providing the functionality but also in exposing the web services contract (WSDL). Now, do you want to improve the QOS attributes of your web service?

Ads

The next chapter will take you through a similar exercise by demonstrating how to access your HTTP-based web services through an MOM channel like JMS.

 
This tutorial is part of a Java SOA Training tutorial series. Read it from the beginning and learn yourself.

Java SOA Training

 

Comments