Java 2EE and XML Development

Скачать в pdf «Java 2EE and XML Development»


Listing 4.4 A JAXM Business Delegate for SOAP RPC


import


import


import


import


import


import


javax.xml.messaging.*;


javax.xml.soap.*;


org.jdom.*;    JAXM is built on


java.net.*;    ltoP of JDOM


java.io.*;


java.util.*;


т


public class ProductPriceDelegate { О Business delegate private SOAPConnection con = null; e Cache connection public ProductPriceDelegate(SOAPConnectionFactory cf) {


try {    ProductPriceDelegate


// Create a connection    constructor


// from the connection factory looked up. con = cf.createConnection();


} catch(Exception e) { throw new IllegalStateException( «Unable to obtain JAXM connection.»);


}



}


/** Business method to obtain current pricing for a product.


*    @param productId The product’s ID


*    @return Current price as a double */


public double getProductPrice(String productId) throws ProductPricingException { try {


// Create a message factory from the connection MessageFactory mf = con.createMessageFactory();


// Create a message from the message factory. SOAPMessage msg = mf.createMessage();



// Message creation takes care of creating the SOAPPart SOAPPart sp = msg.getSOAPPart();



// Retrieve the envelope from the soap part SOAPEnvelope envelope = sp.getSOAPEnvelope();



// Create a soap header from the envelope. SOAPHeader hdr = envelope.createSOAPHeader();


// Create a soap body from the envelope. SOAPBody bdy = envelope.createSOAPBody();



Adds SOAP e



header


element



SOAPHeaderElement


quoteDate = hdr.createSOAPHeaderElement(«QuoteDate», Namespace.getNamespace(«q»,


«http://www.mycompany.com/PriceQuote«)); quoteDate.setMustUnderstand(true); quoteDate.addContent((new Date()).toString());



// Add a soap body element to the soap body


SOAPBodyElement gpp


= bdy.createSOAPBodyElement(«GetProductPrice», Namespace.getNamespace(«m», «MyCompany-URI»)); gpp.createSOAPElement(«productId»,


Namespace.getNamespace(«m», «MyCompany-URI»)) .addContent(productId);

Скачать в pdf «Java 2EE and XML Development»