FileDocCategorySizeDatePackage
EBXMLSenderServlet.javaAPI DocExample8526Mon Sep 02 10:29:02 BST 2002ora.jwsnut.chapter4.ebXMLsender

EBXMLSenderServlet

public class EBXMLSenderServlet extends javax.xml.messaging.JAXMServlet implements javax.xml.messaging.OnewayListener
A servlet that creates an ebXML message on demand and sends it to a remote echo service.

Fields Summary
private SOAPMessage
replyMessage
Message returned by the echo service.
private SOAPFactory
soapFactory
Factory used to create parts of SOAP messages
private javax.xml.messaging.ProviderConnection
conn
ProviderConnection used to send reply messages
private MessageFactory
msgFactory
Factory used to create messages
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
Handles a request from a client to send a message.

                                            
        // Only allow gets on the "request" handler
        String path = req.getServletPath();
        if (!path.equals("/request")) {
            resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Cannot use get on this URL");
            return;
        }
        
        // Build and send a message
        boolean sent = sendMessage();
        
        // Wait until the echo service has replied,
        // for a maximum of 30 seconds
        if (sent) {
            synchronized (this) {
                replyMessage = null;
                try {
                    if (replyMessage == null) {
                        wait(30000L);                
                    }
                } catch (InterruptedException ex) {
                }
            }
        }
        
        // Now send the reply to the caller.
        try {
            if (replyMessage == null) {
                resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "No reply received");
                return;
            }

            OutputStream os = resp.getOutputStream();
            resp.setContentType("text/html");
            resp.setStatus(HttpServletResponse.SC_OK);
            os.write("<html><P><XMP>".getBytes());
            replyMessage.writeTo(os);
            os.write("</XMP></html>".getBytes());
            os.flush();
        } catch (Exception ex) {
            log("Exception in doGet", ex);
            resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "Exception: " + ex.getMessage());
        }
        
        replyMessage = null; 
    
public voiddoPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
Handles a POST either from a client or as a callback from the provider.

        // Only allow posts to the "message" handler
        String path = req.getServletPath();
        if (path.equals("/message")) {
            // This is allowed
            super.doPost(req, resp);
        } else {
            // Cannot post to the request path
            resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Cannot post to this URL");
        }
    
public voidinit(javax.servlet.ServletConfig servletConfig)
Initialize by installing the appropriate MessageFactory

        super.init(servletConfig);
	try {
            // Create the connection to the provider
            conn = ProviderConnectionFactory.newInstance().createConnection();            
            soapFactory = SOAPFactory.newInstance();            
           
            // Check that the ebXML profile is supported
            ProviderMetaData metaData = conn.getMetaData();
            String[] profiles = metaData.getSupportedProfiles();
            boolean found = false;
            for (int i = 0; i < profiles.length; i++) {
                if (profiles[i].equals("ebxml")) {
                    found = true;
                    break;
                }
            }
            
            if (!found) {
                // No ebXML profile
                log("ebxml profile not supported");
                throw new ServletException("ebxml profile not supported");
            }
             
            // Get the message factory and build the message
            msgFactory = conn.createMessageFactory("ebxml");
            
            // Install the factory to use when receiving messages
	    setMessageFactory(msgFactory);
	} catch (Exception e) {
	    e.printStackTrace();
                throw new ServletException(
	    	"Failed to initialize ebXML sender servlet " + e.getMessage());
	}
    
public voidonMessage(javax.xml.soap.SOAPMessage message)
Handles a received SOAP message - this is the asynchronous reply from the echo service.

        try {
            synchronized (this) {
                // Save the message for the benefit
                // of the client.
                replyMessage = message;

                // Wake up the client
                notify();
            }            
        } catch (Exception ex) {
            log("Exception", ex);
        }
    
private booleansendMessage()
Builds a message and sends it to the service

        try {            
            // Build the ebXML message 
            EbXMLMessageImpl message = (EbXMLMessageImpl)msgFactory.createMessage();
            
            // Set attributes held in the MessageHeader
            message.setAction("ECHO");
            message.setService(new Service("urn:ECHOSERVICE", "URI"));
            message.setCPAId("urn:EchoCollaborationAgreement");
            message.setConversationId("1");
            
            // Set the sending and receiving parties.
            message.setReceiver(new Party("urn:ebXMLEcho"));
            message.setSender(new Party("urn:ebXMLSender"));
            
            // Add a Manifest with two references to external locations
            Manifest manifest = new Manifest("ID1", "1.0");
            Reference ref = new Reference("ID2", "http://www.ora.com", null);
            Description desc = new Description("en");
            desc.setText("O'Reilly Home Page");
            ref.setDescription(desc);
            manifest.addReference(ref);
            
            ref = new Reference("ID3", "http://www.amazon.com", null);
            desc = new Description("en");
            desc.setText("Online bookstore");
            ref.setDescription(desc);
            manifest.addReference(ref);
            
            message.setManifest(manifest);
            
            // Add some data to the body.
            SOAPElement element = 
                message.getSOAPPart().getEnvelope().getBody().addBodyElement(
                            soapFactory.createName("Sent", "tns", "urn:ebXMLSender"));
            element.addTextNode("This is the content");
            
            // Send the message to the echo service
            conn.send(message);
            
            // Return indicating that the message was sent.
            return true;
        } catch (Exception ex) {
            log("Failed when sending message", ex);
        }
        
        return false;