Creating Web-Service Clients

Creating Web-Service Clients

Creating web-service clients is a simple jobthe toolset that you're using usually takes care of much of the tedious SOAP and HTTP plumbing. All the SOAP and HTTP interaction code is generated automatically for the three different approaches that we'll show you.

Building the Apache Axis Client

The Apache Axis project contains both Java and C++ implementations of the SOAP Protocol and other associated web-services goodies . We're only going to scratch the surface of Axis here, mainly to illustrate just how simple it is to build a Java client with no help from Oracle 10g AS at all.

First, you need to get the latest version of Axis from the website at http://ws.apache.org/axis/ . For this book we used version 1.1. Once you've obtained the Axis libraries you're ready to build the web-service client. There isn't much to the code, so we'll simply show it in a block and explain it afterward.

 package com.apress.oracle10g.webservices.client; import javax.xml.namespace.QName; import javax.xml.rpc.Call; import org.apache.axis.client.Service; public class SimpleJavaClient {     private static final String SERVICE_URL =                "http://localhost:8888/ws/echoStateful";     public static void main(String[] args) throws Exception {         // create the axis service         Service service = new Service();         Call call = service.createCall();         call.setTargetEndpointAddress(SERVICE_URL);         call.setOperationName(new QName("echo"));         call.setProperty(Call.SESSION_MAINTAIN_PROPERTY, Boolean.TRUE);         Object[] params = new Object[] {"Hello World"};         System.out.println(call.invoke(params));         System.out.println(call.invoke(params));         System.out.println(call.invoke(params));     } } 

The basis of the code is the creation of a Service object. Using this Service object, you can create an implementation of the javax.xml.rpc.Call interface. Using the Call object, we set the endpoint, that is, the URL of the web service. In this case, we're using the stateful service.

When setting the operation name you need to use the QName class, which allows you to specify names that are qualified by a namespace, although in this case it isn't relevant.

The call to the setProperty() method using the SESSION_MAINTAIN_PROPERTY constant, strangely enough, instructs the Call object to maintain state between calls. State in web services is implemented using cookies (in the same way as for web applications), so if the client library wants to access the state it must remember to send any cookies with subsequent requests .

Finally, we assembled the operation arguments, as an Object[] , and invoked the method three times. Running this code results in the following output:

 Hello World0 Hello World1 Hello World2 

As you would imagine, you may change the SERVICE_URL constant to point at the stateless service and the client will continue to function, though without any state. That's about as difficult as it gets. As you'll see in the following sections, our other two approachesbuilding a Java application using the Oracle 10g AS-generated proxy, and building a .NET application using the Visual Studio-generated proxyare even simpler.

Building the Proxy Client

We mentioned earlier that Oracle 10g AS autogenerates a proxy JAR file for your web service. Using this proxy, you can access the web service as though it were a local object. In fact, with just two lines of code, you can create the proxy and invoke the echo operation.

Obtaining the Proxy

To get access to the proxy, visit the home page for your service and click the Proxy JAR link. Save the JAR file to the disk, in a location that's accessible to your code. That's itproxy obtained!

Note 

Using the proxy requires you to have a selection of classes available on your classpath. Rather than repeating those classes here, we suggest that you check out the documentation at http://download-uk.oracle.com/docs/cd/B10464_02/web.904/b10447/useservices.htm#CHDFEGAE , which will take you directly to the list of required JAR files.

Using the Proxy

The proxy encapsulates all the logic that's required to call the web service. It provides you with a simple interface to work with, as shown here:

 package com.apress.oracle10g.webservices.client; import com.apress.oracle10g.webservices.proxy.EchoWebServiceProxy; public class ProxyClient {     public static void main(String[] args) throws Exception {         EchoWebServiceProxy proxy = new EchoWebServiceProxy();         System.out.println("Using URL: " + proxy._getSoapURL());         System.out.println(proxy.echo("Hello World"));         System.out.println(proxy.echo("Hello World"));         System.out.println(proxy.echo("Hello World"));     } } 

You really couldn't hope for it to be much simpler than that. As you can see, the Proxy class has a method, _getSoapURL() , which returns the URL of the service it's configured to access. It also has the corresponding setter, _setSoapURL(String) , which allows you to use the services with the same interface hosted at different URLs.

Building the .NET Client

So far the client applications we've discussed have been built solely in Java. A much-touted benefit of web services is the ease of interoperability between different platforms. In this section, you'll see just how easy it is to create a client for the echo web service using the Microsoft .NET platform.

Creating a Proxy

The common IDE for creating .NET-based applications is Microsoft's Visual Studio .NET 2003. Because this is book about Oracle 10g AS, which is a J2EE application server, we aren't going to spend too much time on Visual Studio .NET or the .NET Framework, but you should be able to follow the example without any problems. For more information on Visual Studio .NET read Pro Visual Studio .NET: From Professional to Expert (Apress, 2004) by David Deloveh et al.

For this client we created a simple C# console application, which is the equivalent of a simple Java command-line application. From within the Solution Explorer in Visual Studio .NET you can add a reference to a web application, as shown in Figure 13-5.

image from book
Figure 13-5: The Visual Studio .NET Solution Explorer

Clicking Add Reference displays the Add Web Reference dialog box, which allows you to enter the address of the web service or browse to it as necessary. As you can see in Figure 13-6, we've entered the URL for the main service page.

image from book
Figure 13-6: Service home page in Visual Studio .NET

Because that page doesn't contain the WSDL definition of the service, you should navigate to the stateful service page and then click the Service Description link to open the WSDL file in Visual Studio .NET. Once the WSDL file is located, Visual Studio .NET informs you that it has found a web service, as shown in Figure 13-7.

image from book
Figure 13-7: The WSDL file inside Visual Studio .NET

Clicking the Add Reference button completes the proxy generation process and makes the proxy available to the project.

Creating the Client

The actual C# code required to invoke the echo service is trivial, with the proxy taking care of the communication and SOAP serialization, as follows :

 using System; using System.Net; using Apress.Oracle10g.WebServices; namespace Apress {     class Driver     {         static void Main(string[] args)         {             EchoWebService ws = new EchoWebService();             ws.Url = "http://python:8888/ws/echoStateful";             // support stateful calls             ws.CookieContainer = new CookieContainer();             Console.WriteLine(ws.echo("Hello .NET World"));             Console.WriteLine(ws.echo("Hello .NET World"));             Console.ReadLine();         }     } } 

Even if you aren't familiar with C# you should be able to decipher what's going on, and you can certainly appreciate just how little code is required to interact with your Oracle 10g AShosted web service.



Oracle Application Server 10g. J2EE Deployment and Administration
Oracle Application Server 10g: J2EE Deployment and Administration
ISBN: 1590592352
EAN: 2147483647
Year: 2004
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net