Presentation is loading. Please wait.

Presentation is loading. Please wait.

95-733 Internet Technologies JAXM Java API for XML Messaging (Processing SOAP) Synchronous Messaging Asynchronous Message.

Similar presentations


Presentation on theme: "95-733 Internet Technologies JAXM Java API for XML Messaging (Processing SOAP) Synchronous Messaging Asynchronous Message."— Presentation transcript:

1 95-733 Internet Technologies JAXM Java API for XML Messaging (Processing SOAP) Synchronous Messaging Asynchronous Message

2 Java API for XML Messaging Two main packages SOAP with attachments API for Java (SAAJ) javax.xml.soap Java API for XML Messaging (JAXM) javax.xml.messaging

3 JAXM On the Client // Code adapted from "Java Web Service" // by Deitel StandAloneClient.java import java.io.*; public class StandAloneClient { public static void main(String args[]) throws IOException { // Get a proxy that handles communications BookTitlesProxy service = new BookTitlesProxy(); System.out.println("Proxy created");

4 // Ask the proxy for book titles String response[] = service.getBookTitles(); System.out.println("Book Titles"); for(int i = 0; i < response.length; i++) { System.out.println(response[i]); }

5 BookTitlesProxy.java // Adapted from Java Web Services by Deitel & Deitel BookTitlesProxy.java // BookTitlesProxy runs on the client and handles communications // for wrapping a SOAP document import javax.xml.soap.*; // for sending the SOAP document import javax.xml.messaging.*;

6 // Standard Java imports import java.io.*; import java.net.URL; import java.util.Iterator; public class BookTitlesProxy { // We will need a connection, a message and an endpoint private SOAPConnectionFactory soapConnectionFactory; private URLEndpoint urlEndpoint; private MessageFactory messageFactory;

7 public BookTitlesProxy() throws java.io.IOException { try { // get factories and endpoints soapConnectionFactory = SOAPConnectionFactory.newInstance(); System.out.println("Got SOAPconnection factory"); // get a message factory messageFactory = MessageFactory.newInstance(); System.out.println("Got Message factory"); // establish a url endpoint urlEndpoint = new URLEndpoint( "http://localhost:8080/AnotherSOAPDemo/BookTitles"); System.out.println("endpoint built"); } catch (SOAPException e) { throw new IOException(e.getMessage()); }

8 public String[] getBookTitles() { // invoke web service try { SOAPMessage response = sendSoapRequest(); return handleSoapResponse(response); } catch (SOAPException e) { System.out.println("Mike's Exception " + e); return null; }

9 private SOAPMessage sendSoapRequest() throws SOAPException { // get a SOAPConnection from the factory SOAPConnection soapConnection = soapConnectionFactory.createConnection(); // get a SOAPMessage from the factory SOAPMessage soapRequest = messageFactory.createMessage(); // make a synchronous call on the service // in other words, call and wait for the response SOAPMessage soapResponse = soapConnection.call(soapRequest, urlEndpoint); System.out.println("Got soap response from server"); soapConnection.close(); return soapResponse; }

10 /* The SOAP response has the following structure <soap-env:Envelope xmlns:soap-env= "http://schemas.xmlsoap.org/soap/envelope/"> To Kill A MokingBird Billy Budd */

11 private String[] handleSoapResponse(SOAPMessage sr) throws SOAPException { // We need to take the result from the SOAP message SOAPPart responsePart = sr.getSOAPPart(); SOAPEnvelope responseEnvelope = responsePart.getEnvelope(); SOAPBody responseBody = responseEnvelope.getBody(); Iterator responseBodyChildElements = responseBody.getChildElements(); SOAPBodyElement titlesElement = (SOAPBodyElement) responseBodyChildElements.next();

12 int bookCount = Integer.parseInt( titlesElement.getAttributeValue( responseEnvelope.createName( "bookCount"))); String bookTitles[] = new String[bookCount]; Iterator titleIterator = titlesElement.getChildElements( responseEnvelope.createName("title")); int i = 0; while(titleIterator.hasNext()) { SOAPElement titleElement = (SOAPElement) titleIterator.next(); bookTitles[i++] = titleElement.getValue(); } return bookTitles; }

13 On the Server //BookTitlesImpl.java // code adapted from "Java Web Services" by Deitel import java.io.*; import java.util.*; // when using rdbms import java.sql.*; public class BookTitlesImpl { // this code would normally access a database // private Connection connection public BookTitlesImpl() { // establish a connection to the database }

14 public String[] getBookTitles() { // Use the rdbms connection to get a list of // titles. Extract these titles from the result // set and place in a String array // here we will skip the JDBC work String bookTitles[] = { "To Kill A Mokingbird", "Billy Budd" }; return bookTitles; }

15 A New Kind of Servlet // Code adapted from "Java Web Services" by Deitel // BookTitlesServlet.java import javax.servlet.*; import javax.xml.messaging.*; import javax.xml.soap.*; // This servlet is a JAXMServlet. // It implements ReqRespListener and so the client will wait for // the response. // The alternative is to implement the OneWayListener interface.

16 public class BookTitlesServlet extends JAXMServlet implements ReqRespListener { // we need to create a return message private MessageFactory messageFactory; // we need a class to handle the reques private BookTitlesImpl service; public void init( ServletConfig config) throws ServletException { super.init(config); try { service = new BookTitlesImpl(); messageFactory = MessageFactory.newInstance(); } catch(SOAPException s) { s.printStackTrace(); }

17 /* We need to build a response with the following structure <soap-env:Envelope xmlns:soap-env= "http://schemas.xmlsoap.org/soap/envelope/"> To Kill A MokingBird Billy Budd */

18 public SOAPMessage onMessage( SOAPMessage soapMessage ) { try { String bookTitles[] = null; if(service != null) bookTitles = service.getBookTitles(); if(bookTitles != null) { SOAPMessage message = messageFactory.createMessage(); SOAPPart soapPart = message.getSOAPPart(); SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); SOAPBody soapBody = soapEnvelope.getBody(); SOAPBodyElement titlesElement = soapBody.addBodyElement ( soapEnvelope.createName("titles")); // Like doPost or doGet but here we receive a message from a // SOAP client.

19 titlesElement.addAttribute( soapEnvelope.createName("bookCount"), Integer.toString(bookTitles.length)); for(int i = 0; i < bookTitles.length; i++) { titlesElement.addChildElement( soapEnvelope.createName("title")). addTextNode(bookTitles[i]); } return message; } else return null; } catch(SOAPException s) { return null; }

20 Running the Client D:\McCarthy\www\95-733\examples\jaxm\clientcode> java StandAloneClient Got SOAPconnection factory Got Message factory endpoint built Proxy created Got soap response from server Book Titles To Kill A Mokingbird Billy Budd

21 My Classpath for JAXM d:\jwsdp-1_0_01\common\endorsed\xercesImpl.jar;.; D:\xerces\xmlParserAPIs.jar; d:\jwsdp-1_0_01\common\lib\servlet.jar; d:\jwsdp-1_0_01\common\lib\saaj-api.jar; d:\jwsdp-1_0_01\common\lib\jaxm-api.jar; d:\jwsdp-1_0_01\common\lib\saaj-ri.jar; d:\jwsdp-1_0_01\common\lib\soap.jar; d:\jwsdp-1_0_01\common\lib\jaxm-runtime.jar; d:\jwsdp-1_0_01\common\lib\commons-logging.jar; d:\jwsdp-1_0_01\common\lib\mail.jar; d:\jwsdp-1_0_01\common\lib\activation.jar;

22 d:\jwsdp-1_0_01\common\lib\dom4j.jar; d:\jwsdp-1_0_01\common\lib\jaxp-api.jar; d:\jwsdp-1_0_01\common\endorsed\sax.jar; d:\jwsdp-1_0_01\common\endorsed\xalan.jar; d:\jwsdp-1_0_01\common\endorsed\xercesImpl.jar; d:\jwsdp-1_0_01\common\endorsed\xsltc.jar

23 Asynchronous Messaging

24 JAXM With a Message Provider 1. A JAXM Application sends messages to its own provider rather than directly to a JAXM Servlet. 2. The JAXM Application now goes on to other things. 3. The message provider (on the client) sends a SOAP request to another message provider on the server. 4. In time, the message provider on the server calls a JAXM Servlet (on the server) and receives a response. An example series of calls…

25 JAXM With a Message Provider 5. The message provider (on the server) communicates a SOAP document to the message provider on the client. 6. The message provider on the client calls a JAXM Servlet on the client. An example series of calls…

26 (2) Message Provider JAXM Servlet (1) (3) (4) (5) (6) JAXM Application In summary, the JAXM application places a SOAP document in its ‘out box’. When the request is processed, the response is passed back via the client’s ‘inbox’ which makes a call on the client’s JAXM Servlet.

27 XML Messaging JAXM We enter two numbers to be added.

28 We hear back right away that the computation will be performed.

29 Check another page to see if our computation has been completed.

30 Browser Two numbers HTTP Servlet DoCalculationServlet Feedback to browser right away JAXM Message Provider JAXM Servlet CalculationHandler JAXM Servlet ResultHandler ResultHolder.java Browser HTTPServlet GetResultsAsHTML

31 Configure the Provider

32 With a URI we name the provider A URL Is the Provider’s location

33 Set the classpath.; d:\jwsdp-1_0_01\common\lib\jaxm-api.jar; d:\jwsdp-1_0_01\common\lib\jaxm-runtime.jar; d:\jwsdp-1_0_01\services\jaxm-provider; d:\jwsdp-1_0_01\services\jaxm-provideradmin; d:\jwsdp-1_0_01\common\lib\saaj-api.jar; d:\jwsdp-1_0_01\common\lib\saaj-ri.jar; d:\jwsdp-1_0_01\common\lib\dom4j.jar; d:\jwsdp-1_0_01\common\lib\activation.jar; d:\jwsdp-1_0_01\common\lib\mail.jar; d:\jwsdp-1_0_01\common\lib\commons-logging.jar; d:\jwsdp-1_0_01\common\lib\jaxp-api.jar; d:\jwsdp-1_0_01\common\endorsed\dom.jar; d:\jwsdp-1_0_01\common\endorsed\sax.jar; d:\jwsdp-1_0_01\common\endorsed\xalan.jar; d:\jwsdp-1_0_01\common\endorsed\xercesImpl.jar; d:\jwsdp-1_0_01\common\endorsed\xsltc.jar; d:\xerces\xmlParserAPIs.jar; d:\jwsdp-1_0_01\common\lib\servlet.jar; d:\jwsdp-1_0_01\common\lib\soap.jar; d:\jwsdp-1_0_01\common\lib\providerutil.jar

34 Provide two client.xml files <!DOCTYPE ClientConfig PUBLIC "-//Sun Microsystems, Inc.//DTD JAXM Client//EN" "http://java.sun.com/xml/dtds/jaxm_client_1_0.dtd"> urn:edu.cmu.andrew.mm6.serverProvider http://127.0.0.1:8080/jaxmasync/servercode/CalculationHandler http://java.sun.com/xml/jaxm/provider http://127.0.0.1:8081/jaxm-provider/sender On the server side

35 <!DOCTYPE ClientConfig PUBLIC "-//Sun Microsystems, Inc.//DTD JAXM Client//EN" "http://java.sun.com/xml/dtds/jaxm_client_1_0.dtd"> urn:edu.cmu.andrew.mm6.clientProvider http://127.0.0.1:8080/jaxmasync/clientcode/ResultHandler http://java.sun.com/xml/jaxm/provider http://127.0.0.1:8081/jaxm-provider/sender On the client side

36 Server Side Directories jaxmasync/servercode | ├───build │ └───WEB-INF │ ├───classes │ └───lib ├───docs ├───src -- CalculationHandler.java └───web – index.html | └───WEB-INF – web.xml | └───classes – client.xml | build.properties | build.xml

37 Client Side Directories D:\McCarthy\www\95-733\examples\jaxmasync\clientcode>tree | ├───build │ └───WEB-INF │ ├───classes │ └───lib ├───docs ├───src └───web – index.html └───WEB-INF – web.xml | └───classes – client.xml |build.properties |build.xml DoCalculationServlet.java GetResultAsHTML.java ResultHandler.java ResultHolder.java

38 Client Side Web.xml <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> GetDataFromHTML DoCalculationServlet

39 HandleResult ResultHandler HTMLServlet GetResultAsHTML

40 HTMLServlet /GetResultAsHTML/* GetDataFromHTML /DoCalculationServlet/*

41 HandleResult /ResultHandler/*

42 Server Side web.xml <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> HandleCalc CalculationHandler HandleCalc /CalculationHandler/*

43 Browser Two numbers HTTP Servlet DoCalculationServlet Feedback to browser right away JAXM Provider JAXM Servlet CalculationHandler JAXM Servlet ResultHandler ResultHolder.java Browser HTTPServlet GetResultsAsHTML

44 Server Side CalculationHandler.java // CalculationHandler.java -- Called by provider // -- Get values from message // -- Perform addition and send a message to provider import java.net.*; import java.io.*; import java.util.*; import javax.servlet.http.*; import javax.servlet.*; import javax.xml.messaging.*; import javax.xml.soap.*; import javax.activation.*; import com.sun.xml.messaging.jaxm.ebxml.*; import org.apache.commons.logging.*; import java.math.*;

45 public class CalculationHandler extends JAXMServlet implements OnewayListener { private static final boolean deBug = true; private ProviderConnection serverProvider; private MessageFactory messageFactory; private String from; private String to;

46 public void init(ServletConfig config) throws ServletException { super.init(config); try { // get a connection to the client provider ProviderConnectionFactory providerFactory = ProviderConnectionFactory.newInstance(); serverProvider = providerFactory.createConnection(); // Establish 'from' and 'to' URN's to be placed within the // outgoing message. // The provider must know how these names map to URL's // via the Provider // Administration tool.

47 from = "urn:edu.cmu.andrew.mm6.serverProvider"; ProviderMetaData metaData = serverProvider.getMetaData(); String[] profiles = metaData.getSupportedProfiles(); boolean isProfileSupported = false; for(int i = 0; i < profiles.length; i++) if(profiles[i].equals("ebxml")) isProfileSupported = true; if(isProfileSupported) { // Build an EBXML style message factory messageFactory = serverProvider.createMessageFactory("ebxml"); if(deBug) System.out.println("Server side init complete with no problem"); }

48 else throw new ServletException("CalculationHandler” + “ Profile problem" + new Date()); } catch(Exception e) { throw new ServletException( "CalculationHandler Problems in init()" + e + new Date()); }

49 public void onMessage(SOAPMessage reqMessage) { if(deBug) System.out.println("Hit onMessage() big time " + new Date()); // Build SOAP document to return to the sender try { if(deBug) { System.out.println("The following is the message received” + “ by CalculationHandler onMessage"); reqMessage.writeTo(System.out); }

50 EbXMLMessageImpl resMessage = new EbXMLMessageImpl(reqMessage); to = "urn:edu.cmu.andrew.mm6.clientProvider"; resMessage.setSender(new Party(from)); resMessage.setReceiver(new Party(to)); Iterator i = resMessage.getAttachments(); AttachmentPart operand1 = (AttachmentPart) i.next(); AttachmentPart operand2 = (AttachmentPart) i.next(); String op1 = (String)(operand1.getContent()); String op2 = (String)(operand2.getContent()); BigInteger o1 = new BigInteger(op1); BigInteger o2 = new BigInteger(op2); BigInteger result = o1.add(o2);

51 String answer = result.toString(); AttachmentPart answerPart = resMessage.createAttachmentPart(); answerPart.setContent(answer, "text/plain"); resMessage.addAttachmentPart(answerPart); serverProvider.send(resMessage); if(deBug) System.out.println( "CalculationHandler: Message sent back to” + “ clientProvider"); } catch(IOException ioe) { System.out.println("JAXM exception thrown " + ioe); } catch(JAXMException je) { System.out.println("JAXM exception thrown " + je); } catch(SOAPException se) { System.out.println("Soap exception thrown " + se); } }}

52 Browser Two numbers HTTP Servlet DoCalculationServlet Feedback to browser right away JAXM Provider JAXM Servlet CalculationHandler JAXM Servlet ResultHandler ResultHolder.java Browser HTTPServlet GetResultsAsHTML

53 HTTP Servlet DoCalculationServlet // DoCalculation.java -- Get values from client browser // -- Send SOAP request to ClientProvider Asynchronously import java.net.*; import java.io.*; import java.util.*; import javax.servlet.http.*; import javax.servlet.*; import javax.xml.messaging.*; import javax.xml.soap.*; import javax.activation.*; import com.sun.xml.messaging.jaxm.ebxml.*; import org.apache.commons.logging.*;

54 // Normal servlet. Collects data from the browser, sends a SOAP message // to a JAXM provider and does not wait for a response. Sends an HTML // note of confirmation back to the browser. public class DoCalculationServlet extends HttpServlet { private ProviderConnection clientProvider; private MessageFactory messageFactory; private String from; private String to; private static final boolean deBug = true; public void init(ServletConfig config) throws ServletException { super.init(config); if(deBug) System.out.println( "Running at the top of client side html form init" + new Date());

55 try { // get a connection to the client provider ProviderConnectionFactory providerFactory = ProviderConnectionFactory.newInstance(); clientProvider = providerFactory.createConnection(); // Establish 'from' and 'to' URN's to be placed within the // outgoing message. // The provider must know how these names map to // URL's via the Provider // Administration tool. from = "urn:edu.cmu.andrew.mm6.clientProvider"; to = "urn:edu.cmu.andrew.mm6.serverProvider";

56 ProviderMetaData metaData = clientProvider.getMetaData(); String[] profiles = metaData.getSupportedProfiles(); boolean isProfileSupported = false; for(int i = 0; i < profiles.length; i++) { if(profiles[i].equals("ebxml")) isProfileSupported = true; } if(isProfileSupported) { // Build an EBXML style message factory messageFactory = clientProvider.createMessageFactory( "ebxml"); } else throw new ServletException("Runtime Profile problem" + new Date()); } catch(Exception e) { throw new ServletException("Run Time Problem in “ + “DoCalculationServlet init()" + e + new Date()); }}

57 public void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String finalString = ""; String op1 = req.getParameter("op1"); String op2 = req.getParameter("op2"); // Build SOAP document to send to the provider try { EbXMLMessageImpl message = (EbXMLMessageImpl) messageFactory.createMessage();

58 message.setSender(new Party(from)); message.setReceiver(new Party(to)); AttachmentPart operand1 = message.createAttachmentPart(); operand1.setContent(op1, "text/plain"); AttachmentPart operand2 = message.createAttachmentPart(); operand2.setContent(op2, "text/plain"); message.addAttachmentPart(operand1); message.addAttachmentPart(operand2); clientProvider.send(message); } catch(JAXMException je) { System.out.println( "JAXM exception thrown " + je); } catch(SOAPException se) { System.out.println( "Soap exception thrown " + se); }

59 String docType = "<!DOCTYPE HTML PUBLIC \"//W3C//DTD" + " HTML 4.0 "; docType += "Transitional//EN\">\n"; out.println(docType + " \n" + " Request Response" + " " + " \n" + " Sent Calculation Request to Local Provider \n" + " " + op1 + "+" + op2 + " will be computed \n" + " "); }

60 Browser Two numbers HTTP Servlet DoCalculationServlet Feedback to browser right away JAXM Provider JAXM Servlet CalculationHandler JAXM Servlet ResultHandler ResultHolder.java Browser HTTPServlet GetResultsAsHTML

61 JAXM Servlet ResultHandler // ResultHandler.java -- Called by provider // -- Get result from message // -- Write result to a shared object or RDBMS import java.net.*; import java.io.*; import java.util.*; import javax.servlet.http.*; import javax.servlet.*; import javax.xml.messaging.*; import javax.xml.soap.*; import javax.activation.*; import com.sun.xml.messaging.jaxm.ebxml.*; import org.apache.commons.logging.*; import java.math.*;

62 public class ResultHandler extends JAXMServlet implements OnewayListener { private static final boolean deBug = true; private ProviderConnection clientProvider; private MessageFactory messageFactory; String from, to; public void init(ServletConfig config) throws ServletException { super.init(config);

63 try { // get a connection to the client provider ProviderConnectionFactory providerFactory = ProviderConnectionFactory.newInstance(); clientProvider = providerFactory.createConnection(); // Establish 'from' and 'to' URN's to be placed within // the outgoing message. // The provider must know how these names map to URL's // via the Provider // Administration tool. from = "urn:edu.cmu.andrew.mm6.clientProvider"; ProviderMetaData metaData = clientProvider.getMetaData(); String[] profiles = metaData.getSupportedProfiles();

64 boolean isProfileSupported = false; for(int i = 0; i < profiles.length; i++) if(profiles[i].equals("ebxml")) isProfileSupported = true; if(isProfileSupported) { // Build an EBXML style message factory messageFactory = clientProvider.createMessageFactory("ebxml"); } else throw new ServletException("ResultHandler OnewayListener” + “ Profile problem" + new Date()); } catch(Exception e) { throw new ServletException("ResultHandler OnewayListener”+ “ Problems in init()" + e + new Date()); }

65 public void onMessage(SOAPMessage inMessage) { try { if(deBug) { System.out.println("Message received from “ + “ server appears now"); inMessage.writeTo(System.out); } // Read the data from the SOAP document EbXMLMessageImpl resultMessage = new EbXMLMessageImpl(inMessage); Iterator i = resultMessage.getAttachments(); AttachmentPart operand1 = (AttachmentPart) i.next(); AttachmentPart operand2 = (AttachmentPart) i.next(); AttachmentPart result = (AttachmentPart) i.next();

66 String op1 = (String)(operand1.getContent()); String op2 = (String)(operand2.getContent()); String res = (String)(result.getContent()); // Place the result in a shared object. ResultHolder singleTon = ResultHolder.getInstance(); singleTon.setLastResult(op1 + " + " + op2 + " = " + res); } catch(Exception e) { System.out.println("onMessage in ResultHandler exception " + e); }

67 Browser Two numbers HTTP Servlet DoCalculationServlet Feedback to browser right away JAXM Provider JAXM Servlet CalculationHandler JAXM Servlet ResultHandler ResultHolder.java Browser HTTPServlet GetResultsAsHTML

68 ResultHolder.java public class ResultHolder { private String lastResult; private static ResultHolder instance = new ResultHolder(); private ResultHolder() { lastResult = ""; } public static ResultHolder getInstance() { return instance; } synchronized public String getLastResult() { return lastResult; } synchronized public void setLastResult(String result) { lastResult = result; } synchronized public String toString() { return lastResult; } }

69 Browser Two numbers HTTP Servlet DoCalculationServlet Feedback to browser right away JAXM Provider JAXM Servlet CalculationHandler JAXM Servlet ResultHandler ResultHolder.java Browser HTTPServlet GetResultsAsHTML

70 // GetResultAsHTML.java -- Get result from ResultHolder object // -- Send it back to client as HTML import java.net.*; import java.io.*; import java.util.*; import javax.servlet.http.*; import javax.servlet.*; public class GetResultAsHTML extends HttpServlet { private static final boolean deBug = true; public void init(ServletConfig config) throws ServletException { super.init(config); }

71 public void doGet (HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String finalString = ResultHolder.getInstance().toString(); String docType = "<!DOCTYPE HTML PUBLIC \"//W3C//DTD" + " HTML 4.0 "; docType += "Transitional//EN\">\n";

72 out.println(docType + " \n" + " GetResultAsHTML Response" + " " + " \n" + " Most Recent Calculation \n" + " " + finalString + " \n" + " "); }


Download ppt "95-733 Internet Technologies JAXM Java API for XML Messaging (Processing SOAP) Synchronous Messaging Asynchronous Message."

Similar presentations


Ads by Google