Download NewJAXM - Andrew.cmu.edu

Document related concepts
no text concepts found
Transcript
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
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");
// 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]);
}
}
}
BookTitlesProxy.java
// Adapted from Java Web Services by Deitel & Deitel
BookTitlesProxy.java
// BookTitlesProxy runs on the client and handles communications
// for wrapping a SOAP document
import javax.xml.soap.*;
// for sending the SOAP document
import javax.xml.messaging.*;
// Standard Java imports
import java.io.*;
import java.net.URL;
import java.util.Iterator;
public class BookTitlesProxy {
// We will need a connection, a message and an endpoint
private SOAPConnectionFactory soapConnectionFactory;
private URLEndpoint urlEndpoint;
private MessageFactory messageFactory;
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());
}
}
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;
}
}
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;
}
/* 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>
*/
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();
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;
}
}
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
}
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;
}
}
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.
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();
}
}
/* 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>
*/
// Like doPost or doGet but here we receive a message from a
// SOAP client.
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"));
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;
}
}
}
Running the Client
D:\McCarthy\www\95-733\examples\jaxm\clientcode>
java StandAloneClient
Got SOAPconnection factory
Got Message factory
endpoint built
Proxy created
Got soap response from server
Book Titles
To Kill A Mokingbird
Billy Budd
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;
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
Asynchronous Messaging
JAXM With a Message Provider
An example series of calls…
1. A JAXM Application sends messages to its own provider
rather than directly to a JAXM Servlet.
2. The JAXM Application now goes on to other things.
3. The message provider (on the client) sends a SOAP request
to another message provider on the server.
4. In time, the message provider on the server calls a JAXM
Servlet (on the server) and receives a response.
JAXM With a Message Provider
An example series of calls…
5. The message provider (on the server) communicates a
SOAP document to the message provider on the client.
6. The message provider on the client calls a JAXM
Servlet on the client.
JAXM Application
(2)
JAXM Servlet
(1)
(4)
(5)
Message Provider
Message Provider
(3)
(6)
JAXM Servlet
In summary, the JAXM application
places a SOAP document in its
‘out box’. When the request is
processed, the response is passed
back via the client’s ‘inbox’ which
makes a call on the client’s
JAXM Servlet.
XML Messaging JAXM
We
enter
two
numbers
to be
added.
We hear
back right
away that
the computation
will be performed.
Check
another
page to
see if
our
computation
has been
completed.
ResultHolder.java
Browser
JAXM Servlet
ResultHandler
HTTPServlet
GetResultsAsHTML
Browser Two numbers
HTTP Servlet
DoCalculationServlet
Feedback to browser
right away
JAXM Message Provider
JAXM Servlet
CalculationHandler
JAXM Message Provider
Configure the Provider
With a URI
we
name
the provider
A URL
Is the
Provider
location
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
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
<?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
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
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
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>
<servlet>
<servlet-name>HandleResult
</servlet-name>
<servlet-class>
ResultHandler
</servlet-class>
</servlet>
<servlet>
<servlet-name>HTMLServlet</servlet-name>
<servlet-class>GetResultAsHTML</servlet-class>
</servlet>
<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>
<servlet-mapping>
<servlet-name>
HandleResult
</servlet-name>
<url-pattern>
/ResultHandler/*
</url-pattern>
</servlet-mapping>
</web-app>
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>
ResultHolder.java
Browser
JAXM Servlet
ResultHandler
HTTPServlet
GetResultsAsHTML
Browser Two numbers
HTTP Servlet
DoCalculationServlet
Feedback to browser
right away
JAXM Provider
JAXM Servlet
CalculationHandler
JAXM Provider
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.*;
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;
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.
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");
}
else
throw new ServletException("CalculationHandler” +
“ Profile problem" + new Date());
}
catch(Exception e) {
throw new ServletException(
"CalculationHandler Problems in init()" + e + new Date());
}
}
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);
}
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();
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); }
}}
ResultHolder.java
Browser
JAXM Servlet
ResultHandler
HTTPServlet
GetResultsAsHTML
Browser Two numbers
HTTP Servlet
DoCalculationServlet
Feedback to browser
right away
JAXM Provider
JAXM Servlet
CalculationHandler
JAXM Provider
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.*;
// 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());
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();
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,
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));
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); }
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>");
}
}
ResultHolder.java
Browser
JAXM Servlet
ResultHandler
HTTPServlet
GetResultsAsHTML
Browser Two numbers
HTTP Servlet
DoCalculationServlet
Feedback to browser
right away
JAXM Provider
JAXM Servlet
CalculationHandler
JAXM Provider
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
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);
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;
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("ebxm
}
else
throw new ServletException("ResultHandler OnewayListener” +
“ Profile problem" + new Date());
}
catch(Exception e) {
throw new ServletException("ResultHandler OnewayListener”+
“ Problems in init()" + e + new Date()
}
}
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();
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);
}
}
}
ResultHolder.java
Browser
JAXM Servlet
ResultHandler
HTTPServlet
GetResultsAsHTML
Browser Two numbers
HTTP Servlet
DoCalculationServlet
Feedback to browser
right away
JAXM Provider
JAXM Servlet
CalculationHandler
JAXM Provider
ResultHolder.java
public class ResultHolder {
private String lastResult;
private static ResultHolder instance = new ResultHolder();
private ResultHolder() {
lastResult = "";
}
public static ResultHolder getInstance() { return instance; }
synchronized public String getLastResult() { return lastResult; }
synchronized public void setLastResult(String result) { lastResult
= result; }
synchronized public String toString() { return lastResult; }
}
ResultHolder.java
Browser
JAXM Servlet
ResultHandler
HTTPServlet
GetResultsAsHTML
Browser Two numbers
HTTP Servlet
DoCalculationServlet
Feedback to browser
right away
JAXM Provider
JAXM Servlet
CalculationHandler
JAXM Provider
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);
}
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";
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>");
}
}
Related documents