XML Messaging - PowerPoint PPT Presentation

About This Presentation
Title:

XML Messaging

Description:

Title: PowerPoint Presentation Author: mm6 Last modified by: mm6 Created Date: 1/25/2001 5:23:17 PM Document presentation format: On-screen Show Company – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 31
Provided by: mm6
Category:

less

Transcript and Presenter's Notes

Title: XML Messaging


1
Internet Technologies
XML Messaging A PowerWarning application
using servlets and SAX
The PowerWarning Application is from XML and
Java by Maruyama, Tamura, and Uramoto,
Addison-Wesley.
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. Th
e web container is called Jigsaw from the W3C.
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 lthtmlgt 2 ltheadgt 3 lttitlegtWeather
Reportlt/titlegt 4 lt/headgt 5 ltbodygt 6
lth2gtWeather Report -- White Plains, NY lt/h2gt 7
lttable border1gt 8 lttrgtlttdgtDate/Timelt/tdgtlttd
aligncentergt11 AM EDT Sat Jul 25
1998lt/tdgtlt/trgt 9 lttrgtlttdgtCurrent Tem.lt/tdgtlttd
aligncentergt70176lt/tdgtlt/trgt 10 lttrgtlttdgtToday
s Highlt/tdgtlttd aligncentergt82176lt/tdgtlt/trgt 1
1 lttrgtlttdgtTodays Lowlt/tdgtlttd aligncentergt6217
6lt/tdgtlttrgt 12 lt/tablegt 13 lt/bodygt 14
lt/htmlgt
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 lttablegt tag, then go to the second lttrgt tag
    within the
  • table, and then go to the second lttggt tag within
    the row.

Neither of these seems very appealing
5
lt?xml version1.0?gt lt!DOCTYPE WeatherReport
SYSTEM httpgt//www.xweather.com/WeatherReport.dtd
gt ltWeatherReportgt ltCitygtWhite Plainslt/Citygt ltSta
tegtNYlt/Stategt ltDategtSat Jul 25
1998lt/Dategt ltTimegt11 AM EDTlt/Timegt ltCurrTemp
unitFarenheitgt70lt/CurrTempgt ltHigh
unitFarenheitgt82lt/Highgt ltLow
unitFarenheitgt62lt/Lowgt lt/Weather Reportgt
XML would help
6
  • Strategy 3
  • For the current temperature of White Plains,
    N.Y., go
  • to the ltCurrTempgt tag.

7
WeatherReport application
Mobile users
XSLT
WML
HTML
XML
PC users
Http//www.xweather.com
PowerWarning application
XML
Email notifications
Registrations
Application programs
XML
8
The XML Describing the Weather
lt?xml version"1.0" encoding"UTF-8"?gt ltWeatherRep
ortgt ltCitygtPittsburghlt/Citygt
ltStategtPAlt/Stategt ltDategtWed. April 11,
2001lt/Dategt ltTimegt3lt/Timegt
ltCurrTemp Unit "Farenheit"gt70lt/CurrTempgt
ltHigh Unit "Farenheit"gt82lt/Highgt ltLow
Unit "Farenheit"gt62lt/Lowgt
lt/WeatherReportgt
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.
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.
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 DataInputStream
FileInputStream theFile
new FileInputStream("c\\Jigsaw\\Jigsa
w\\
Jigsaw\\Www\\weather\\"extraPath)

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()
12
WeatherReport application
Mobile users
XSLT
WML
HTML
XML
PC users
Http//www.xweather.com
PowerWarning application
XML
Email notifications
Registrations
Application programs
XML
13
Registrations (HTML)
lt!-- PowerWarningForm.html --gt lthtmlgt ltheadgt lttitl
egtPowerWarninglt/titlegt lt/headgt ltbodygt ltform
method"post" action"/servlet/PowerWarn"gt
E-Mail ltinput type"text" name
"User"gt ltpgt State ltinput
type"text" name "State"gt ltpgt City
ltinput type"text" name "City"gt ltpgt
Temperature ltinput type"text" name
"Temperature"gt ltpgt Duration
ltinput type"text" name "Duration"gt ltpgt
ltinput type "submit"gt lt/formgt lt/bodygt lt/htm
lgt
14
Registrations (Servlet)
The servlet will create a watcher object for each
registered user. The watcher object will be told
of each users 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.
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. pub
lic class PowerWarn extends HttpServlet
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.
/
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("St
ate") String par_city
req.getParameter("City") int
par_temp Integer.parseInt(
req.getParameter("Tempera
ture")) int par_duration
Integer.parseInt(
req.getParameter("Duration"))
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("lthtmlgtltheadgtlt/headgtltbodygtltbgt
You'll be notified by
emaillt/bgtlt/bodygtlt/htmlgt")
writer.close()
19
Servlet
Http Request
PowerWarn.userTable
Watcher
mm6_at_andrew.cmu.edu
User data
Email
Http Request
Watcher
User data
Email
w_at_whitehouse.gov
Scheduler
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()

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.
22
The Watcher Class
The Watcher objects make HTTP requests to get
XML.
SAX.
How should we handle the XML? SAX or DOM?
JavaMail.
How do we send email?
23
import org.xml.sax. import org.xml.sax.helpers.P
arserFactory import java.io. import
java.net. import org.w3c.dom.Document import
javax.xml.parsers.SAXParserFactory import
javax.xml.parsers.ParserConfigurationException im
port javax.xml.parsers.SAXParser
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
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.edu80
01/servlet/GetWeather /weather.xml" /
For simplicity we wont take the appropriate
approach. / / String weatheruri
"http//mccarthy.heinz.cmu.edu8001/servlet/GetWea
ther/?city" URLEncoder.encode(this.city)
/ / Create an InputSource object for the
parser to use. / InputSource is new
InputSource(weatheruri)
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
27
/ See if the user wants to be alerted.
/ if(currentTempNumber gt this.temp)
this.overTemp if(this.overTemp gt
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_at_andrew.cmu.edu", "It's hot")
mailman.send()
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()

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,star
t,length)
30
WeatherReport application
Mobile users
WML
HTML
XML
PC users
Http//www.xweather.com
PowerWarning application
XML
Email notifications
Registrations
Application programs
XML
Write a Comment
User Comments (0)
About PowerShow.com