Standalone Client Implementation

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

Java APIs for XML Kick Start
By Aoyon Chowdhury, Parag Choudhary

Table of Contents
Chapter 8.  Creating Client Implementations


Standalone Client Implementation

A standalone client is essentially a J2SE application that uses the JAXM APIs to create and consume the SOAP messages. In this section, you will create a standalone JAXM client called MyJAXMClient that will send a SOAP message invoking the find_business method described in the InquiryAPI to the UDDI registry to get information about an organization.

The SOAP message that MyJAXMClient will send to the UDDI registry will be of the following form:

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> <soap-env:Header/> <soap-env:Body>     <find_business xmlns="urn:uddi-org:api" generic="1.0" maxRows="100">              <name>oracle</name>     </find_business> </soap-env:Body> </soap-env:Envelope> 

To create a standalone client, you need to do the following:

  1. Import the necessary packages and classes.

  2. Get an instance of the SOAPConnectionFactory class. This is a factory class that enables an application to create a connection to the Web service.

  3. Get the SOAPConnection object. A SOAPConnection object represents a point-to-point connection. A standalone client can use only a point-to-point connection and engage in a request-response messaging. You will use the SOAPConnectionFactory's createConnection() method to get the SOAPConnection object.

  4. Create the SOAP message using the combination of MessageFactory and SOAPMessage classes.

  5. Access the empty SOAP message and add the necessary elements.

  6. Create a URLEndpoint object to connect to the UDDI registry.

  7. Use the SOAPConnection object to send the SOAP message to the registry.

  8. Display the SOAP message that is sent and the reply that is received in response to the query to the registry.

Importing the Packages and Classes

Like all Java programs, the MyJAXMClient application will also begin with importing the required packages and classes. To do so, open a text editor of your choice and enter the following lines of code:

import javax.xml.soap.*; import javax.xml.messaging.URLEndpoint; import java.io.*; public class MyJAXMClient{     public static void main(String[] args) {         try { 

The javax.xml.soap package contains the necessary classes and interfaces that enable an application to create a SOAP message. The javax.xml.messaging.URLEndpoint class provides the URL required by the application to provide a direct connection to the Web service. As said before, JAXM clients that do not use a messaging provider can only do a point-to-point connection with a Web service. The classes of java.io package enable the application to do I/O operations.

Other than importing the required classes and packages, the class declaration for the program is also provided. Next, you need the JAXM client application to get a connection.

Getting a Connection

A JAXM client can get a connection by creating either a SOAPConnection or a ProviderConnection object. However, the clients that do not use a messaging provider can only use the SOAPConnection object.

To get a SOAPConnection object, enter the following lines of code shown in bold:

public static void main(String[] args) {         try { // Create the connection and the message factory.     SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();     SOAPConnection connection = scf.createConnection(); } catch (Exception ex) {             ex.printStackTrace();         }     } } 

The SOAPConnectionFactory is a factory class that enables the creation of a SOAPConnection object. Using a factory class to create a class instance provides the necessary loose coupling between the application and the selection of a particular class to instantiate at runtime. This allows you to write code once and then use a vendor-specific class at runtime. To get an instance of the SOAPConnectionFactory, use the newInstance() method. This method returns a SOAPConnectionFactory instance. Once you have the instance, call the createConnection() method of the SOAPConnectionFactory instance. Next, you will learn how to create the SOAP message.

Creating the SOAP Message

A SOAP message is created using the MessageFactory class. To do so, add the lines of code shown in bold in the following:

SOAPConnection connection = scf.createConnection(); // Create a message       MessageFactory msgFactory = MessageFactory.newInstance();       SOAPMessage msg = msgFactory.createMessage(); 

You use the createMessage() method of the MessageFactory class to create a SOAPMessage object. A SOAPMessage object has the following parts:

  • A SOAPPart The object that represents the SOAP-specific portion of a SOAP message.

  • A SOAPEnvelope The object that represents the envelope element of the SOAP messages.

  • A SOAPHeader The object that represents the Header element of a SOAP message. By default this object is empty when a SOAPMessage is created from MessageFactory.

  • A SOAPBody The object that represents the body element of a SOAP message. Similar to the SOAPHeader object, this object is created empty.

After the SOAPMessage object has been created, the next step is to populate the object.

Populating the SOAP Message

Populating the SOAP message implies adding necessary content to the Header and Body elements. To populate the empty SOAPMessage object, you need to do the following:

  1. Get the SOAPEnvelope object.

  2. From the SOAPEnvelope object, get the SOAPBody object.

  3. In the SOAPBody object, add the SOAPBody elements and attributes, if any.

  4. Under the SOAPBody element, add the child elements.

To do the previously mentioned tasks, enter the lines of code shown in bold in the following:

SOAPMessage msg = msgFactory.createMessage();             // Get the SOAP envelope in the message             SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();             // Get the soap body             SOAPBody body = envelope.getBody();         //Add Body element         Name bodyName = envelope.createName ("find_business", "","urn:uddi-org:api");         Name genericAttribute = envelope.createName("generic");         Name maxRowsAttribute = envelope.createName("maxRows");         SOAPBodyElement bodyElement = body.addBodyElement(bodyName);         // Add attributes          bodyElement.addAttribute(genericAttribute, "1.0");          bodyElement.addAttribute(maxRowsAttribute, "100");            //Adding Child Elements and its text node            Name companyName = envelope.createName("name");            bodyElement.addChildElement(companyName).addTextNode(args[1]);            msg.saveChanges();            System.out.println("The Outgoing SOAP Message :\n");            msg.writeTo(System.out); 

First, the getEnvelope() method is used to get the SOAPEnvelope object from the SOAPPart object. Next, you create the Name objects for the body element and its attributes.

Note that in our example, we are actually executing an rpc call on the UDDI registry, by executing the find_business method of the UDDI specification. This method takes the maxRows and generic as its two parameters. The value of the maxRows parameter limits the number of results returned. The generic parameter defines the version of the UDDI specification. Therefore, you created the Name objects with find_business, generic, and maxRows as parameters.

NOTE

To learn more about the Publish and the Inquiry APIs, see http://www.uddi.org/pubs/ProgrammersAPI-V2.00-Open-20010608.pdf.


Next, you add the name child element. This element is another parameter of the find_business method. The value of this parameter of the find_business method is the name of the business that you want the information for. For example, if you want information about Microsoft, the value of the name element will be Microsoft. The find_business method returns the businessList structure for the company that the query was run for. The value of the name element will be picked up at runtime as a command-line argument.

After populating the SOAP message, you called the saveChanges() method to save the SOAP message, and used the writeTo() method of the SOAPMessage object to write out the SOAP message on the screen. Next, you need to create the URLEndpoint object for the URL that points to the UDDI registry, which is implemented as a Web service.

Creating the URLEndpoint Object

The registry that will be used is a test registry located at http://www-3.ibm.com/services/uddi/testregistry/inquiryapi. To create a URLEndpoint object from this URL, enter the following line of code shown in bold:

msg.writeTo(System.out); URLEndpoint endpoint = new URLEndpoint ("http://www-3.ibm.com/services/uddi/testregistry/ graphics/ccc.gifinquiryapi"); 

Next, the SOAP message needs to be sent.

Sending the Message and Getting the Response

The SOAP message is sent to the specified Web service by using the call() method of the SOAPConnection object. Because a standalone client can only do request-response type messaging, the call() method returns a SOAPMessage object that contains the response to the query. The application is blocked until the call method returns a SOAPMessage object. After the SOAPMessage object is obtained, it is a good idea to close the connection, because connections tend to be resource-intensive.

Add the lines of code shown in bold to send the message, get the reply, print the received reply, and then close the connection:

URLEndpoint endpoint = new URLEndpoint ("http://www-3.ibm.com/services/uddi/testregistry/ graphics/ccc.gifinquiryapi"); SOAPMessage reply = connection.call(msg, endpoint);           System.out.println("\n Received reply from: "+endpoint);             reply.writeTo(System.out);             connection.close();         } catch (Exception ex) {             ex.printStackTrace();         }     } } 

You have now successfully created a standalone JAXM client to connect to a registry, send the registry a find_business query, and display the information the registry returns about an organization.

NOTE

The code discussed so far is available in the example1001 folder.


To compile and run the application to get information about companies that have oracle in their name, use the following command:

java MyJAXMClient oracle 

The output is displayed in Listing 8.1.

Listing 8.1 Output of MyJAXMClient Application
The Outgoing SOAP Message : <soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> <soap-env:Header/> <soap-env:Body>     <find_business xmlns="urn:uddi-org:api" generic="1.0" maxRows="100">         <name>oracle</name>     </find_business> </soap-env:Body> </soap-env:Envelope> Received reply from: http://www-3.ibm.com/services/uddi/testregistry/inquiryapi <?xml version="1.0" encoding="UTF-8" ?> <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> <Body> <businessList generic="1.0" xmlns="urn:uddi-org:api" operator="www.ibm.com/services/uddi"  graphics/ccc.giftruncated="false"> <businessInfos> <businessInfo businessKey="26B46510-81E8-11D5-A4A5-0004AC49CC1E"> <name>Oracle</name> <description xml:lang="en">oracle powers the internet </description> <serviceInfos> </serviceInfos> </businessInfo> <businessInfo businessKey="2ACAA2D0-82A7-11D5-A4A5-0004AC49CC1E"> <name>Oracle Corporation</name> <description xml:lang="en"> Oracle Corporation provides the software and services for e-business. </description> <serviceInfos> <serviceInfo serviceKey="82757F80-82A9-11D5-A4A5-0004AC49CC1E"  graphics/ccc.gifbusinessKey="2ACAA2D0-82A7-11D5-A4A5-0004AC49CC1E"> <name>Oracle.com</name> </serviceInfo> <serviceInfo serviceKey="3AD5A9B0-82AA-11D5-A4A5-0004AC49CC1E"  graphics/ccc.gifbusinessKey="2ACAA2D0-82A7-11D5-A4A5-0004AC49CC1E"> <name>Oracle Store</name> </serviceInfo> <serviceInfo serviceKey="0735C300-82AB-11D5-A4A5-0004AC49CC1E"  graphics/ccc.gifbusinessKey="2ACAA2D0-82A7-11D5-A4A5-0004AC49CC1E"> <name>Oracle Technology Network</name> </serviceInfo> <serviceInfo serviceKey="4EDB6FC0-82AB-11D5-A4A5-0004AC49CC1E"  graphics/ccc.gifbusinessKey="2ACAA2D0-82A7-11D5-A4A5-0004AC49CC1E"> <name>E-Business Network</name> </serviceInfo> </serviceInfos> </businessInfo> </businessInfos> </businessList> </Body> </Envelope> 

Notice that the result is a SOAP message that contains the businessList structure for two companies that have oracle in their name.

You learned to create a standalone JAXM client that does JAXM messaging without a messaging provider. Next, you will learn to create a JAXM application that uses a messaging provider.


printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
Top

[0672324342/ch08lev1sec1]

 
 


JavaT APIs for XML Kick Start
JAX: Java APIs for XML Kick Start
ISBN: 0672324342
EAN: 2147483647
Year: 2002
Pages: 133

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