1 / 76

Internet Technologies

Internet Technologies. XML Messaging A PowerWarning application using servlets and SAX Asynchronous Messages using JAXM and SOAP. The PowerWarning Application is from “XML and Java” by Maruyama, Tamura, and Uramoto, Addison-Wesley.

nirav
Download Presentation

Internet Technologies

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Internet Technologies • XML Messaging • A PowerWarning application using servlets and SAX • Asynchronous Messages using JAXM and SOAP The PowerWarning Application is from “XML and Java” by Maruyama, Tamura, and Uramoto, Addison-Wesley. The JAXM example was adapted with major changes from “Java Web Services” by Deitel and Deitel 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. Internet Technologies

  3. Suppose that we know that the weather information is available from the web at http://www.xweather.com/White_Plains_NY_US.html. [1] <html> [2] <head> [3] <title>Weather Report</title> [4] </head> [5] <body> [6] <h2>Weather Report -- White Plains, NY </h2> [7] <table border=1> [8] <tr><td>Date/Time</td><td align=center>11 AM EDT Sat Jul 25 1998</td></tr> [9] <tr><td>Current Tem.</td><td align=center>70&#176;</td></tr> [10] <tr><td>Today’s High</td><td align=center>82&#176;</td></tr> [11] <tr><td>Today’s Low</td><td align=center>62&#176;</td><tr> [12] </table> [13] </body> [14] </html> 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 <table> tag, then go to the second <tr> tag within the • table, and then go to the second <tg> tag within the row. Neither of these seems very appealing… Internet Technologies

  5. <?xml version=“1.0”?> <!DOCTYPE WeatherReport SYSTEM “http>//www.xweather.com/WeatherReport.dtd”> <WeatherReport> <City>White Plains</City> <State>NY</State> <Date>Sat Jul 25 1998</Date> <Time>11 AM EDT</Time> <CurrTemp unit=“Farenheit”>70</CurrTemp> <High unit=“Farenheit”>82</High> <Low unit=“Farenheit”>62</Low> </Weather Report> XML would help Internet Technologies

  6. Strategy 3: • For the current temperature of White Plains, N.Y., go • to the <CurrTemp> tag. Internet Technologies

  7. WeatherReport application Mobile users XSLT WML HTML XML PC users Http://www.xweather.com PowerWarning application XML Email notifications Registrations Application programs XML Internet Technologies

  8. The XML Describing the Weather <?xml version="1.0" encoding="UTF-8"?> <WeatherReport> <City>Pittsburgh</City> <State>PA</State> <Date>Wed. April 11, 2001</Date> <Time>3</Time> <CurrTemp Unit = "Farenheit">70</CurrTemp> <High Unit = "Farenheit">82</High> <Low Unit = "Farenheit">62</Low> </WeatherReport> This file is behind Jigsaw in the file Www/weather/ weather.xml. Perhaps this is being served up by www.xweather.com for ½ cents per hit. 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. Internet Technologies

  10. public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String theData = ""; /* For simplicity we get the user’s request from the path. */ String extraPath = req.getPathInfo(); extraPath = extraPath.substring(1); // read the file try { // open file and create a DataInputStream FileInputStream theFile = new FileInputStream("c:\\Jigsaw\\Jigsaw\\”+ “Jigsaw\\Www\\weather\\"+extraPath); Internet Technologies

  11. InputStreamReader is = new InputStreamReader(theFile); BufferedReader br = new BufferedReader(is); // read the file into the string theData String 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(); } } Internet Technologies

  12. WeatherReport application Mobile users XSLT WML HTML XML PC users Http://www.xweather.com PowerWarning application XML Email notifications Registrations Application programs XML Internet Technologies

  13. Registrations (HTML) <!-- PowerWarningForm.html --> <html> <head> <title>PowerWarning</title> </head> <body> <form method="post" action="/servlet/PowerWarn"> E-Mail <input type="text" name = "User"> <p> State <input type="text" name = "State"> <p> City <input type="text" name = "City"> <p> Temperature <input type="text" name = "Temperature"> <p> Duration <input type="text" name = "Duration"> <p> <input type = "submit"> </form> </body> </html> Internet Technologies

  14. Registrations (Servlet) The servlet will create a watcher object for each registered user. The watcher object will be told of each user’s location and temperature requirements. Each watcher object will run in its own thread and may or may not notify its assigned user by email. 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. 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 { 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. */ 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")); 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("<html><head></head><body><b>” + “You'll be notified by email</b></body></html>"); writer.close(); } } Internet Technologies

  19. Servlet Http Request PowerWarn.userTable Watcher mm6@andrew.cmu.edu User data Email Http Request Watcher User data Email w@whitehouse.gov Scheduler Internet Technologies

  20. The Scheduler import 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(); } 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 for testing. Internet Technologies

  22. The Watcher Class The Watcher objects make HTTP requests to get XML. How should we handle the XML? SAX or DOM? SAX. How do we send email? JavaMail. 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; 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; } 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 won’t 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); 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; } 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, "mm6@andrew.cmu.edu", "It's hot"); mailman.send(); } 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(); } } 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); } } Internet Technologies

  30. WeatherReport application Mobile users WML HTML XML PC users Http://www.xweather.com PowerWarning application XML Email notifications Registrations Application programs XML Internet Technologies

  31. XML Messaging JAXM We enter two numbers to be added. Internet Technologies

  32. We hear back right away that the computation will be performed. Internet Technologies

  33. Check another page to see if our computation has been completed. Internet Technologies

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

  35. Configure the Provider Internet Technologies

  36. With a URI we name the provider A URL Is the Provider’s location 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 Internet Technologies

  38. 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 Internet Technologies

  39. <?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 Internet Technologies

  40. 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 Internet Technologies

  41. 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 Internet Technologies

  42. 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> Internet Technologies

  43. <servlet> <servlet-name>HandleResult </servlet-name> <servlet-class> ResultHandler </servlet-class> </servlet> <servlet> <servlet-name>HTMLServlet</servlet-name> <servlet-class>GetResultAsHTML</servlet-class> </servlet> Internet Technologies

  44. <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> Internet Technologies

  45. <servlet-mapping> <servlet-name> HandleResult </servlet-name> <url-pattern> /ResultHandler/* </url-pattern> </servlet-mapping> </web-app> Internet Technologies

  46. 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> Internet Technologies

  47. ResultHolder.java JAXM Servlet ResultHandler Browser HTTPServlet GetResultsAsHTML Two numbers HTTP Servlet DoCalculationServlet Browser Feedback to browser right away JAXM Provider JAXM Servlet CalculationHandler JAXM Provider 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.*; import java.math.*; 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; 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. Internet Technologies

More Related