UDDI Request Authoring in Java


Having seen the UDDI's XML structures for publishing, let's consider a Java code sample to author a UDDI request. Refer to Listing 13.1, which authors the save_service method call that we presented earlier.

Listing 13.1 SOAPRequest.java: A UDDI Request Authoring Example
 import org.w3c.dom.*; import javax.xml.parsers.*; import org.apache.crimson.tree.XmlDocument; import java.io.*; public class SOAPRequest  {        // Keeps reference of the complete XML document.        private Document ownerDoc;        // Keeps reference of the SOAP Envelope element.        private Element soapEnvelope;        // Keeps reference of the SOAP Body element.        private Element soapBody;        // We will author SOAPEnvelope        // and an empty SOAP Body in the constructor.        public SOAPRequest () {               try {                      // Create a Document Builder Factory,                      // then create a Document Builder using the Factory,                      // then create a Document using the Builder.                      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();                      DocumentBuilder db = dbf.newDocumentBuilder();                      ownerDoc = db.newDocument();               }//try               catch (ParserConfigurationException pce) {                        System.out.println ("ParserConfigException:"+ pce.getMessage());               }//catch               try {                      // Create the Envelope.                     soapEnvelope = ownerDoc.createElement("SOAP-ENV:Envelope");                      // Set namespaces.                      soapEnvelope.setAttributeNS( "http://www.w3.org/2000/xmlns/", "xmlns: graphics/ccc.gifSOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/" );                      soapEnvelope.setAttributeNS( "http://www.w3.org/2000/xmlns/", "xmlns: graphics/ccc.gifxsi", "http://www.w3.org/1999/XMLSchema-instance" );                      soapEnvelope.setAttributeNS( "http://www.w3.org/2000/xmlns/", "xmlns: graphics/ccc.gifxsd", "http://www.w3.org/1999/XMLSchema" );                      // Create an empty SOAP Body and                      // add it to the Envelope.                      soapBody = ownerDoc.createElement ("SOAP-ENV:Body");                      soapEnvelope.appendChild(soapBody);                      ownerDoc.appendChild (soapEnvelope);               }//try               catch (DOMException de){                        System.out.println ( "DOMException: "+de.getMessage());               }//catch        }// Constructor        public void setBodyMethod (Node bodyMethod) {               // bodyMethod belongs to some other owner document.               // We will import the Node into our document               // and append it to the soapBody.               Node importedNode = ownerDoc.importNode(bodyMethod, true);               soapBody.appendChild (importedNode);               // Now save the SOAP request XML as SOAPRequest.xml.               // This saving is only for demonstration.               XmlDocument xmlDocument = (XmlDocument)ownerDoc;               try{                      FileOutputStream fout =                             new FileOutputStream( new File(".\\SoapRequest.xml"));                      xmlDocument.write(fout);                      fout.close();               }//try               catch(Throwable th){th.printStackTrace();}        }//setBodyMethod()        // UDDI request authoring.        public static void main (String args[]){               SOAPRequest soap = new SOAPRequest();               Document doc = null;               Element method;               Element businessService;               Element authInfo;               Element name;               Element description;               Element categoryBag;               Element keyedReference;               try{               // We will author the UDDI part               // independently of the SOAP part of the request.               // Therefore, we have to create a new XML document.               // The steps of creating a new XML document are:               // Create a Document Builder Factory,               // then create a Document Builder using the Factory,               // then create a Document using the Builder.               DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();               DocumentBuilder db = dbf.newDocumentBuilder();               doc = db.newDocument();               }//try               catch (ParserConfigurationException pce) {                      System.out.println( "ParserConfigException: "+ pce.getMessage());               }//catch               try{                      // The newly created empty XML document                      // is stored in the object named doc.                      // We can start authoring the UDDI structure.                      // The first step is to author the root element.                      method = doc.createElement("save_service");                      method.setAttributeNS( "http://www.w3.org/2000/xmlns/", "xmlns","urn: graphics/ccc.gifuddi-org:api_v2");                      method.setAttribute("generic", "2.0");                      // We'll now author root element's child elements.                      // We will perform the following steps for each                      // child of the save_service element:                      // First create the new element                      // using the createElement method,                      // then set the attributes (if any),                      // then append the element to its immediate parent                      // using the appendChild method.                      // Text nodes are created using the createTextNode method.                      authInfo = doc.createElement("authInfo");                      authInfo.appendChild( doc.createTextNode( "An authorization token  graphics/ccc.gifstring."));                      method.appendChild(authInfo);                      businessService = doc.createElement("businessService");                      businessService.setAttribute ("serviceKey","");                      businessService.setAttribute ("businessKey","F5E65…");                      method.appendChild(businessService);                      name = doc.createElement("name");                      name.appendChild( doc.createTextNode( "The name of the service."));                      businessService.appendChild(name);                      description = doc.createElement("description");                      description.appendChild(                             doc.createTextNode( "Textual description of the Binding  graphics/ccc.gifTemplate."                                                ));                      businessService.appendChild(description);                      categoryBag = doc.createElement("categoryBag");                      businessService.appendChild(categoryBag);                      keyedReference = doc.createElement("keyedReference");                      keyedReference.setAttribute("keyName","UNSPSC");                      keyedReference.setAttribute("keyValue","UNSPSC code");                      categoryBag.appendChild(keyedReference);                      doc.appendChild (method);                      // Now save the UDDI request as UDDIRequest.xml.                      XmlDocument xmlDocument = (XmlDocument)doc;                      try{                      FileOutputStream fout = new FileOutputStream( new File(".\\ graphics/ccc.gifUDDIRequest.xml"));                      xmlDocument.write(fout);                      fout.close();                      }//try                      catch(Throwable th){th.printStackTrace();}                      // Finally call setBodyMethod,                      // which will copy the UDDI part inside the                      // SOAP part.                      soap.setBodyMethod (method);               }//try               catch (DOMException de){                      System.out.println( "Method DOMException: "+de.getMessage());               }//catch        }//main }//class 

The code in Listing 13.1 builds on the class SOAPRequest that we developed in Chapter 12, "Messaging and Java APIs for XML" (refer to Listing 12.5). There is no change in the SOAPRequest constructor. The main() function has been rewritten to demonstrate that UDDI requests are actually SOAP requests. The UDDI part of the request appears inside the SOAP Body element.

The UDDI Inquiry API

The UDDI API offers ten methods for inquiry, or searching through the registry. The ten methods jointly form the UDDI Inquiry API. Five of the ten methods are used for general search and are named find_XX methods. The other five are used for specific (drill-down) search and are called get_XX methods.

The find_XX methods will take the search criteria as search parameters from the user and return a list of required objects. On the other hand, the drill-down (or get) methods simply take the key of an object and return the complete structure representing that object. The 10 methods are find_binding, find_business, find_relatedBusinesses, find_service, find_tModel, get_bindingDetail, get_businessDetail, get_businessDetailExt, get_serviceDetail, and get_tModelDetail. All the method names are self-explanatory, except find_relatedBusinesses and get_businessDetailExt. The find_relatedBusinesses method will find all businessEntities, which are related to a particular businessEntity through publisher assertions. The get_businessDetailExt method returns information about business entities with some additional attributes. These additional attributes are not defined by the UDDI specification, and a UDDI-compatible registry is allowed to choose what extra information it would like to provide. Refer to the UDDI API documentation for complete details.



JavaT P2P Unleashed
JavaT P2P Unleashed
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 209

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