JAXM

download JAXM

of 76

Transcript of JAXM

  • 8/12/2019 JAXM

    1/76

    Internet Technologies 1

    Internet Technologies

    XML Messaging

    A PowerWarning application using servlets and SAX

    Asynchronous Messages using JAXM and SOAP

    The PowerWarning Application is from XML andJava by Maruyama, Tamura, and Uramoto, Addison-Wesley.The JAXM example was adapted with major changes from Java Web Services by Deitel

    and Deitel

  • 8/12/2019 JAXM

    2/76

    Internet Technologies 2

    XML Messaging Part I

    The PowerWarning application allows users to register

    their geographical position and their temperature concerns.

    Users will receive e-mail when the temperature exceeds the user

    specified parameters.

    This example is from

    XML and Java by Maruyama, Tamura, and Uramoto, Addison-Wesley.

    The web container is called Jigsaw from the W3C.

  • 8/12/2019 JAXM

    3/76

    Internet Technologies 3

    [1]

    [2]

    [3] Weather Report

    [4]

    [5] [6] Weather Report -- White Plains, NY

    [7]

    [8] Date/Time11 AM EDT Sat Jul 25

    1998

    [9] Current Tem.70[10] Todays High82

    [11] Todays Low62

    [12]

    [13]

    [14]

    Suppose that we know that the weather information is available

    from the web at

    http://www.xweather.com/White_Plains_NY_US.html.

  • 8/12/2019 JAXM

    4/76

    Internet Technologies 4

    Strategy 1:

    For the current temperature of White Plains, go to line 9,column 46 of the page and continue until reaching the next

    ampersand.

    Strategy 2:

    For the current temperature of the White Plains, go to the

    first tag, then go to the second tag within thetable, and then go to the second tag within the row.

    Neither of these seems very appealing

  • 8/12/2019 JAXM

    5/76

    Internet Technologies 5

    //www.xweather.com/WeatherReport.dtd>

    White Plains

    NY

    Sat Jul 25 1998

    11 AM EDT

    70

    82

    62

    XML would help

  • 8/12/2019 JAXM

    6/76

    Internet Technologies 6

    Strategy 3:

    For the current temperature of White Plains, N.Y., go

    to the tag.

  • 8/12/2019 JAXM

    7/76

    Internet Technologies 7

    XML

    Mobile users

    PC usersHttp://www.xweather.com

    WeatherReport

    application

    WML

    HTML

    PowerWarning

    application

    Application

    programs

    XML

    Email notifications

    Registrations

    XML

    XSLT

  • 8/12/2019 JAXM

    8/76

    Internet Technologies 8

    The XML Describing the Weather

    Pittsburgh

    PA

    Wed. April 11, 2001

    3

    70

    8262

    This file is behind

    Jigsaw in the file

    Www/weather/

    weather.xml.

    Perhaps this isbeing served up

    by www.xweather.com

    for cents per hit.

    http://www.xweather.com/http://www.xweather.com/
  • 8/12/2019 JAXM

    9/76

    Internet Technologies 9

    Serving the weather// This servlet file is stored in Www/Jigsaw/servlet/GetWeather.java

    // This servlet returns a user selected xml weather file from

    // the Www/weather directory and returns it to the client.

    import java.io.*;

    import java.util.*;

    import javax.servlet.*;

    import javax.servlet.http.*;

    public class GetWeather extends HttpServlet {

    This data would not

    normally be retrieved

    from a file.

  • 8/12/2019 JAXM

    10/76

    Internet Technologies 10

    public void doGet(HttpServletRequest req, HttpServletResponse res)

    throws ServletException, IOException {

    String theData = "";

    /* For simplicity we get the users request from the path. */String extraPath = req.getPathInfo();

    extraPath = extraPath.substring(1);

    // read the file

    try {

    // open file and create a DataInputStreamFileInputStream theFile =

    new FileInputStream("c:\\Jigsaw\\Jigsaw\\+Jigsaw\\Www\\weather\\"+extraPath);

  • 8/12/2019 JAXM

    11/76

    Internet Technologies 11

    InputStreamReader is = new InputStreamReader(theFile);

    BufferedReader br = new BufferedReader(is);

    // read the file into the string theDataString thisLine;

    while((thisLine = br.readLine()) != null) {

    theData += thisLine + "\n";

    }

    }

    catch(Exception e) {

    System.err.println("Error " + e);

    }

    PrintWriter out = res.getWriter();out.write(theData);

    System.out.println("Wrote document to client");

    out.close();

    }}

  • 8/12/2019 JAXM

    12/76

    Internet Technologies 12

    XML

    Mobile users

    PC usersHttp://www.xweather.com

    WeatherReport

    application

    WML

    HTML

    PowerWarning

    application

    Application

    programs

    XML

    Email notifications

    Registrations

    XML

    XSLT

  • 8/12/2019 JAXM

    13/76

    Internet Technologies 13

    Registrations (HTML)

    PowerWarning

    E-Mail

    State

    City

    Temperature

    Duration

  • 8/12/2019 JAXM

    14/76

    Internet Technologies 14

    Registrations (Servlet)

    On servlet initialization, we will start up an object whose

    responsibility it is to periodically wake up and tell the watcher

    objects to check the weather.

    The servlet will create a watcher object for each registered

    user. The watcher object will be told of each users locationand temperature requirements. Each watcher object will run

    in its own thread and may or may not notify its assigneduser by email.

  • 8/12/2019 JAXM

    15/76

    Internet Technologies 15

    Registrations (Servlet)

    /* This servlet is called by an HTML form. The form passes

    the user email, state, city, temperature and duration.

    */

    import java.io.*;

    import java.util.*;

    import javax.servlet.*;

    import javax.servlet.http.*;

    public class PowerWarn extends HttpServlet {

  • 8/12/2019 JAXM

    16/76

    Internet Technologies 16

    static Hashtable userTable; /* Holds (email,watcher) pairs */

    public void init(ServletConfig conf) throws ServletException {

    super.init(conf);

    PowerWarn.userTable = new Hashtable();Scheduler scheduler = new Scheduler();

    scheduler.start(); /* Run the scheduler */

    } /* The scheduler can see the hash

    table. It has package access.*/

  • 8/12/2019 JAXM

    17/76

    Internet Technologies 17

    public void doPost(HttpServletRequest req, HttpServletResponse res)

    throws ServletException, IOException {

    /* Collect data from the HTML form */

    String par_user = req.getParameter("User");

    String par_state = req.getParameter("State");

    String par_city = req.getParameter("City");

    int par_temp = Integer.parseInt(

    req.getParameter("Temperature"));

    int par_duration = Integer.parseInt(req.getParameter("Duration"));

  • 8/12/2019 JAXM

    18/76

    Internet Technologies 18

    /* Assign a watcher to this user. */

    Watcher watcher = new Watcher(par_user, par_state, par_city,

    par_temp, par_duration);

    /* Place the (email,watcher) pair in the hash table. */

    PowerWarn.userTable.put(par_user, watcher);

    res.setContentType("text/html");

    PrintWriter writer = res.getWriter();

    writer.print(" +You'll be notified by email");writer.close();

    }

    }

  • 8/12/2019 JAXM

    19/76

    Internet Technologies 19

    [email protected]

    [email protected]

    PowerWarn.userTable

    User

    data

    User

    data

    Watcher

    Watcher

    Scheduler

    Http Request

    Http Request

    Email

    Email

    Servlet

  • 8/12/2019 JAXM

    20/76

    Internet Technologies 20

    The Schedulerimport java.util.Enumeration;

    public class Scheduler extends Thread {

    public void run() {

    System.out.println("Running scheduler");

    while(true) {

    Enumeration en = PowerWarn.userTable.elements();

    while(en.hasMoreElements()) {Watcher wa = (Watcher)en.nextElement();

    new Thread(wa).start();

    }

  • 8/12/2019 JAXM

    21/76

    Internet Technologies 21

    try { /* put this thread to sleep for 15 seconds */

    Thread.sleep(1000 * 15);

    }catch(InterruptedException ie) {

    // ignore

    }

    } /* end while */

    }

    public Scheduler() {

    super();}

    }

    Fifteen seconds fortesting.

  • 8/12/2019 JAXM

    22/76

    Internet Technologies 22

    The Watcher Class

    The Watcher objects make HTTP requests to get XML.

    SAX.

    JavaMail.

    How should we handle the XML? SAX or DOM?

    How do we send email?

  • 8/12/2019 JAXM

    23/76

    Internet Technologies 23

    import org.xml.sax.*;

    import org.xml.sax.helpers.ParserFactory;

    import java.io.*;import java.net.*;

    import org.w3c.dom.Document;

    import javax.xml.parsers.SAXParserFactory;

    import javax.xml.parsers.ParserConfigurationException;

    import javax.xml.parsers.SAXParser;

  • 8/12/2019 JAXM

    24/76

    Internet Technologies 24

    public class Watcher extends HandlerBase implements Runnable {

    String user, state, city;

    int temp, duration, overTemp;

    public Watcher(String user, String state, String city, int temp,

    int duration) {

    super();

    this.user = user;

    this.state = state;

    this.city = city;

    this.temp = temp;

    this.duration = duration;

    this.overTemp = 0;

    }

  • 8/12/2019 JAXM

    25/76

    Internet Technologies 25

    public void run() { // called by scheduler

    System.out.println("Running watcher");

    /* Set up to call the weather service. */

    String weatheruri =

    http://mccarthy.heinz.cmu.edu:8001/servlet/GetWeather+/weather.xml";

    /* For simplicity we wont take the appropriate approach. *//* String weatheruri =

    "http://mccarthy.heinz.cmu.edu:8001/servlet/GetWeather/?city="

    + URLEncoder.encode(this.city);*/

    /* Create an InputSource object for the parser to use. */

    InputSource is = new InputSource(weatheruri);

    http://mccarthy.heinz.cmu.edu:8001/servlet/GetWeatherhttp://mccarthy.heinz.cmu.edu:8001/servlet/GetWeather
  • 8/12/2019 JAXM

    26/76

    Internet Technologies 26

    try { /* Set up to handle incoming XML */

    SAXParserFactory factory = SAXParserFactory.newInstance();

    factory.setValidating(true);

    SAXParser parser = factory.newSAXParser();parser.parse(is, this); /* The parser makes the calls. */

    }

    catch(Exception e) {

    e.printStackTrace();

    return;}

    /* The parsing and callbacks are done by this time. */

    int currentTempNumber;

    try {

    currentTempNumber = Integer.parseInt(this.currentTemp.trim());

    }

    catch( NumberFormatException e) {e.printStackTrace(); return; }

  • 8/12/2019 JAXM

    27/76

    Internet Technologies 27

    /* See if the user wants to be alerted. */

    if(currentTempNumber > this.temp) {

    this.overTemp++;

    if(this.overTemp >= this.duration) {warning();

    }

    }

    else {

    this.overTemp = 0;}

    }

    /* Send email via JavaMail. The Mailer class is based on the JavaMail API. */

    public void warning() {

    System.out.println("Sending email");

    Mailer mailman = new Mailer(this.user,

    "[email protected]", "It's hot");

    mailman.send();

    }

  • 8/12/2019 JAXM

    28/76

    Internet Technologies 28

    /* Handle SAX events. */

    StringBuffer buffer;

    String currentTemp;

    public void startDocument() throws SAXException {

    this.currentTemp = null;

    }

    public void startElement(String name, AttributeList aMap) throws SAXException {

    if(name.equals("CurrTemp")) { /* Prepare for next event. */

    this.buffer = new StringBuffer();

    }}

  • 8/12/2019 JAXM

    29/76

    Internet Technologies 29

    public void endElement(String name) throws SAXException {

    if(name.equals("CurrTemp")) {

    this.currentTemp = this.buffer.toString();

    this.buffer = null;

    }

    }

    public void characters(char[] ch, int start, int length) throws SAXException {

    if(this.buffer != null) this.buffer.append(ch,start,length);

    }

    }

  • 8/12/2019 JAXM

    30/76

    Internet Technologies 30

    XML

    Mobile users

    PC usersHttp://www.xweather.com

    WeatherReport

    application

    WML

    HTML

    PowerWarning

    application

    Application

    programs

    XML

    Email notifications

    Registrations

    XML

  • 8/12/2019 JAXM

    31/76

    Internet Technologies 31

    XML Messaging JAXM

    We

    enter

    twonumbers

    to be

    added.

  • 8/12/2019 JAXM

    32/76

    Internet Technologies 32

    We hear

    back right

    away that

    the computationwill be performed.

  • 8/12/2019 JAXM

    33/76

    Internet Technologies 33

    Check

    another

    page to

    see if

    our

    computation

    has been

    completed.

  • 8/12/2019 JAXM

    34/76

    Internet Technologies 34

    BrowserTwo numbers HTTP Servlet

    DoCalculationServlet

    Feedback to browser

    right away

    JAXM Message Provider

    JAXM Message Provider

    JAXM Servlet

    CalculationHandler

    JAXM Servlet

    ResultHandler

    ResultHolder.java

    BrowserHTTPServletGetResultsAsHTML

  • 8/12/2019 JAXM

    35/76

    Internet Technologies 35

    Configure the Provider

  • 8/12/2019 JAXM

    36/76

    Internet Technologies 36

    With a URI

    we

    name

    the provider

    A URL

    Is the

    Provider

    location

  • 8/12/2019 JAXM

    37/76

    Internet Technologies 37

    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

  • 8/12/2019 JAXM

    38/76

    Internet Technologies 38

    Provide two client.xml files

    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

  • 8/12/2019 JAXM

    39/76

    Internet Technologies 39

    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

  • 8/12/2019 JAXM

    40/76

    Internet Technologies 40

    Server Side Directoriesjaxmasync/servercode

    |

    build WEB-INF

    classes libdocssrc -- CalculationHandler.java

    web index.html| WEB-INFweb.xml| classes client.xml| build.properties

    | build.xml

  • 8/12/2019 JAXM

    41/76

    Internet Technologies 41

    Client Side DirectoriesD:\McCarthy\www\95-733\examples\jaxmasync\clientcode>tree

    |

    build

    WEB-INF classes libdocs

    srcweb index.htmlWEB-INFweb.xml

    | classes client.xml|build.properties

    |build.xml

    DoCalculationServlet.javaGetResultAsHTML.java

    ResultHandler.java

    ResultHolder.java

  • 8/12/2019 JAXM

    42/76

    Internet Technologies 42

    Client Side Web.xml

    GetDataFromHTML

    DoCalculationServlet

  • 8/12/2019 JAXM

    43/76

    Internet Technologies 43

    HandleResult

    ResultHandler

    HTMLServlet

    GetResultAsHTML

  • 8/12/2019 JAXM

    44/76

    Internet Technologies 44

    HTMLServlet/GetResultAsHTML/*

    GetDataFromHTML

    /DoCalculationServlet/*

  • 8/12/2019 JAXM

    45/76

    Internet Technologies 45

    HandleResult

    /ResultHandler/*

  • 8/12/2019 JAXM

    46/76

    Internet Technologies 46

    Server Side web.xml

    HandleCalc

    CalculationHandler

    HandleCalc

    /CalculationHandler/*

  • 8/12/2019 JAXM

    47/76

    Internet Technologies 47

    BrowserTwo numbers HTTP Servlet

    DoCalculationServlet

    Feedback to browser

    right away

    JAXM Provider

    JAXM Provider

    JAXM Servlet

    CalculationHandler

    JAXM Servlet

    ResultHandler

    ResultHolder.java

    BrowserHTTPServletGetResultsAsHTML

  • 8/12/2019 JAXM

    48/76

    Internet Technologies 48

    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.*;im ort ava.math.*

  • 8/12/2019 JAXM

    49/76

    Internet Technologies 49

    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;

  • 8/12/2019 JAXM

    50/76

    Internet Technologies 50

    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.

  • 8/12/2019 JAXM

    51/76

    Internet Technologies 51

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

    }

  • 8/12/2019 JAXM

    52/76

    Internet Technologies 52

    else

    throw new ServletException("CalculationHandler +

    Profile problem" + new Date());}catch(Exception e) {

    throw new ServletException(

    "CalculationHandler Problems in init()" + e + new Date());

    }

    }

    bli id ( )

  • 8/12/2019 JAXM

    53/76

    Internet Technologies 53

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

    }

  • 8/12/2019 JAXM

    54/76

    Internet Technologies 54

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

    String answer = result.toString();

  • 8/12/2019 JAXM

    55/76

    Internet Technologies 55

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

    }}

  • 8/12/2019 JAXM

    56/76

    Internet Technologies 56

    BrowserTwo numbers HTTP Servlet

    DoCalculationServlet

    Feedback to browser

    right away

    JAXM Provider

    JAXM Provider

    JAXM Servlet

    CalculationHandler

    JAXM Servlet

    ResultHandler

    ResultHolder.java

    BrowserHTTPServletGetResultsAsHTML

    HTTP S l t

  • 8/12/2019 JAXM

    57/76

    Internet Technologies 57

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

    // N l l t C ll t d t f th b d SOAP

  • 8/12/2019 JAXM

    58/76

    Internet Technologies 58

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

  • 8/12/2019 JAXM

    59/76

    Internet Technologies 59

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

    ProviderMetaData metaData = clientProvider.getMetaData();

    i fil d fil ()

  • 8/12/2019 JAXM

    60/76

    Internet Technologies 60

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

    }}

    public void doPost(HttpServletRequest req

  • 8/12/2019 JAXM

    61/76

    Internet Technologies 61

    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

    message.setSender(new Party(from));

    R i ( P ( ))

  • 8/12/2019 JAXM

    62/76

    Internet Technologies 62

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

  • 8/12/2019 JAXM

    63/76

    Internet Technologies 63

    String docType = "\n";

    out.println(docType +"\n" +

    "Request Response" + "" +

    "\n" +

    "\n" +" Sent Calculation Request to Local Provider \n" +

    "" + op1 + "+" + op2 + " will be computed\n" +

    "");

    }

    }

  • 8/12/2019 JAXM

    64/76

    Internet Technologies 64

    BrowserTwo numbers HTTP Servlet

    DoCalculationServlet

    Feedback to browser

    right away

    JAXM Provider

    JAXM Provider

    JAXM Servlet

    CalculationHandler

    JAXM Servlet

    ResultHandler

    ResultHolder.java

    BrowserHTTPServletGetResultsAsHTML

  • 8/12/2019 JAXM

    65/76

    Internet Technologies 65

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

    public class ResultHandler extends JAXMServlet

  • 8/12/2019 JAXM

    66/76

    Internet Technologies 66

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

  • 8/12/2019 JAXM

    67/76

    Internet Technologies 67

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

    boolean isProfileSupported = false;

  • 8/12/2019 JAXM

    68/76

    Internet Technologies 68

    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("ebx

    }

    else

    throw new ServletException("ResultHandler OnewayListener + Profile problem" + new Date());

    }

    catch(Exception e) {

    throw new ServletException("ResultHandler OnewayListener+ Problems in init()" + e + new Date( }

    }

  • 8/12/2019 JAXM

    69/76

    Internet Technologies 69

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

  • 8/12/2019 JAXM

    70/76

    Internet Technologies 70

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

    }

    }

  • 8/12/2019 JAXM

    71/76

    Internet Technologies 71

    BrowserTwo numbers HTTP Servlet

    DoCalculationServlet

    Feedback to browser

    right away

    JAXM Provider

    JAXM Provider

    JAXM Servlet

    CalculationHandler

    JAXM Servlet

    ResultHandler

    ResultHolder.java

    Browser

    HTTPServletGetResultsAsHTML

  • 8/12/2019 JAXM

    72/76

    Internet Technologies 72

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

    }

  • 8/12/2019 JAXM

    73/76

    Internet Technologies 73

    BrowserTwo numbers HTTP Servlet

    DoCalculationServlet

    Feedback to browser

    right away

    JAXM Provider

    JAXM Provider

    JAXM Servlet

    CalculationHandler

    JAXM Servlet

    ResultHandler

    ResultHolder.java

    Browser

    HTTPServletGetResultsAsHTML

  • 8/12/2019 JAXM

    74/76

    Internet Technologies 74

    GetResultsAsHTML

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

    bli id d G t (Htt S l tR t

  • 8/12/2019 JAXM

    75/76

    Internet Technologies 75

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

  • 8/12/2019 JAXM

    76/76

    out.println(docType +

    "\n" +

    "

    GetResultAsHTML Response" + "" +"\n" +

    "\n" +

    " Most Recent Calculation \n" +

    "" + finalString + "\n" +"");

    }

    }