95-733 internet technologies jaxm java api for xml messaging (processing soap) synchronous messaging...

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

Post on 20-Dec-2015

233 views

Category:

Documents


2 download

TRANSCRIPT

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

95-733 Internet Technologies

• JAXM Java API for XML Messaging

(Processing SOAP)

• Synchronous Messaging

• Asynchronous Message

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

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

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

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");

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

// 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]);

} }}

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

BookTitlesProxy.java// Adapted from Java Web Services by Deitel & Deitel BookTitlesProxy.java

// BookTitlesProxy runs on the client and handles communications

// for wrapping a SOAP documentimport javax.xml.soap.*;

// for sending the SOAP documentimport javax.xml.messaging.*;

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

// Standard Java importsimport 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;

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

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()); } }

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

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; } }

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

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; }

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

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

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

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();

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

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; }}

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

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 }

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

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; }}

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

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.

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

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(); } }

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

/* We need to build a response with the following structure

<soap-env:Envelope xmlns:soap-env= "http://schemas.xmlsoap.org/soap/envelope/"> <soap-env:Header/> <soap-env:Body> <titles bookCount="2"> <title>To Kill A MokingBird</title> <title>Billy Budd</title> </titles> </soap-env:Body> </soap-env:Envelope> */

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

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.

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

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; } }}

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

Running the Client

D:\McCarthy\www\95-733\examples\jaxm\clientcode>java StandAloneClient

Got SOAPconnection factoryGot Message factoryendpoint builtProxy createdGot soap response from serverBook TitlesTo Kill A MokingbirdBilly Budd

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

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;

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

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

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

Asynchronous Messaging

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

JAXM With a Message Provider

1. A JAXM Application sends messages to its own providerrather 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 JAXMServlet (on the server) and receives a response.

An example series of calls…

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

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 JAXMServlet on the client.

An example series of calls…

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

(2)

Message Provider Message Provider

JAXM Servlet

JAXM Servlet

(1)

(3)

(4)(5)

(6)

JAXM Application

In summary, the JAXM applicationplaces a SOAP document in its‘out box’. When the request isprocessed, the response is passed back via the client’s ‘inbox’ whichmakes a call on the client’s JAXM Servlet.

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

XML Messaging JAXM

We entertwo numbersto beadded.

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

We hearback rightaway thatthe computationwill be performed.

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

Checkanotherpage tosee ifour computationhas been completed.

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

Browser Two numbers HTTP ServletDoCalculationServlet

Feedback to browserright away

JAXM Message Provider

JAXM Message ProviderJAXM ServletCalculationHandler

JAXM ServletResultHandler

ResultHolder.java

Browser HTTPServletGetResultsAsHTML

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

Configure the Provider

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

With a URIwenamethe provider

A URLIs theProvider’slocation

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

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

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

Provide two client.xml files

<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE ClientConfig PUBLIC "-//Sun Microsystems, Inc.//DTD JAXM Client//EN" "http://java.sun.com/xml/dtds/jaxm_client_1_0.dtd"><ClientConfig> <Endpoint>urn:edu.cmu.andrew.mm6.serverProvider</Endpoint> <CallbackURL> http://127.0.0.1:8080/jaxmasync/servercode/CalculationHandler </CallbackURL> <Provider> <URI>http://java.sun.com/xml/jaxm/provider</URI> <URL>http://127.0.0.1:8081/jaxm-provider/sender</URL> </Provider></ClientConfig>

On the server side

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

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE ClientConfig

PUBLIC "-//Sun Microsystems, Inc.//DTD JAXM Client//EN"

"http://java.sun.com/xml/dtds/jaxm_client_1_0.dtd">

<ClientConfig>

<Endpoint>urn:edu.cmu.andrew.mm6.clientProvider

</Endpoint>

<CallbackURL>http://127.0.0.1:8080/jaxmasync/clientcode/ResultHandler

</CallbackURL>

<Provider>

<URI>http://java.sun.com/xml/jaxm/provider</URI>

<URL>http://127.0.0.1:8081/jaxm-provider/sender</URL>

</Provider>

</ClientConfig>

On the client side

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

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

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

Client Side DirectoriesD:\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.javaResultHandler.java ResultHolder.java

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

Client Side Web.xml<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"><web-app> <servlet> <servlet-name>GetDataFromHTML </servlet-name> <servlet-class> DoCalculationServlet </servlet-class> </servlet>

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

<servlet> <servlet-name>HandleResult </servlet-name> <servlet-class> ResultHandler </servlet-class> </servlet> <servlet> <servlet-name>HTMLServlet</servlet-name> <servlet-class>GetResultAsHTML</servlet-class> </servlet>

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

<servlet-mapping> <servlet-name>HTMLServlet</servlet-name> <url-pattern>/GetResultAsHTML/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name> GetDataFromHTML </servlet-name> <url-pattern> /DoCalculationServlet/* </url-pattern> </servlet-mapping>

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

<servlet-mapping> <servlet-name> HandleResult </servlet-name> <url-pattern> /ResultHandler/* </url-pattern> </servlet-mapping></web-app>

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

Server Side web.xml<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <servlet> <servlet-name>HandleCalc</servlet-name> <servlet-class>CalculationHandler</servlet-class> <load-on-startup/> </servlet> <servlet-mapping> <servlet-name>HandleCalc</servlet-name> <url-pattern>/CalculationHandler/*</url-pattern> </servlet-mapping></web-app>

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

Browser Two numbers HTTP ServletDoCalculationServlet

Feedback to browserright away

JAXM Provider

JAXM ProviderJAXM ServletCalculationHandler

JAXM ServletResultHandler

ResultHolder.java

Browser HTTPServletGetResultsAsHTML

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

Server Side CalculationHandler.java

// CalculationHandler.java -- Called by provider // -- Get values from message // -- Perform addition and send a message to providerimport 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.*;

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

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;

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

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.

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

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"); }

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

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

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

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); }

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

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);

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

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); } }}

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

Browser Two numbers HTTP ServletDoCalculationServlet

Feedback to browserright away

JAXM Provider

JAXM ProviderJAXM ServletCalculationHandler

JAXM ServletResultHandler

ResultHolder.java

Browser HTTPServletGetResultsAsHTML

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

HTTP ServletDoCalculationServlet

// 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.*;

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

// 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());

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

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";

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

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()); }}

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

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();

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

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); }

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

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

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

Browser Two numbers HTTP ServletDoCalculationServlet

Feedback to browserright away

JAXM Provider

JAXM ProviderJAXM ServletCalculationHandler

JAXM ServletResultHandler

ResultHolder.java

Browser HTTPServletGetResultsAsHTML

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

JAXM Servlet ResultHandler// ResultHandler.java -- Called by provider // -- Get result from message // -- Write result to a shared object or RDBMSimport 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.*;

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

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);

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

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();

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

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()); }}

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

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();

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

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); }

}}

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

Browser Two numbers HTTP ServletDoCalculationServlet

Feedback to browserright away

JAXM Provider

JAXM ProviderJAXM ServletCalculationHandler

JAXM ServletResultHandler

ResultHolder.java

Browser HTTPServletGetResultsAsHTML

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

ResultHolder.javapublic 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; }}

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

Browser Two numbers HTTP ServletDoCalculationServlet

Feedback to browserright away

JAXM Provider

JAXM ProviderJAXM ServletCalculationHandler

JAXM ServletResultHandler

ResultHolder.java

Browser HTTPServletGetResultsAsHTML

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

GetResultsAsHTML// GetResultAsHTML.java -- Get result from ResultHolder object// -- Send it back to client as HTMLimport 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); }

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

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";

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

out.println(docType + "<HTML>\n" + "<HEAD><TITLE> GetResultAsHTML Response" + "</TITLE>" + "</HEAD>\n" + "<BODY>\n" + "<H2> Most Recent Calculation </H2>\n" + "<H2>" + finalString + "</H2>\n" + "</BODY></HTML>"); }

}