package ora.jwsnut.chapter4.ebXMLecho;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import java.io.ByteArrayOutputStream;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.xml.messaging.Endpoint;
import javax.xml.messaging.JAXMServlet;
import javax.xml.messaging.OnewayListener;
import javax.xml.messaging.ProviderConnection;
import javax.xml.messaging.ProviderConnectionFactory;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPMessage;
import com.sun.xml.messaging.jaxm.ebxml.EbXMLMessageImpl;
/**
* A servlet that recieves an ebXML message and
* returns it to its sender.
*/
public class EBXMLEchoServlet extends JAXMServlet implements OnewayListener {
/**
* Output stream used to save a SOAP message
* for logging.
*/
private ByteArrayOutputStream os = new ByteArrayOutputStream();
/**
* ProviderConnection used to send reply messages
*/
private ProviderConnection conn;
/**
* MessageFactory for ebXML messages
*/
private MessageFactory messageFactory;
/**
* Initialize by installing the appropriate MessageFactory
*/
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
// Workaround for hang when deploying in J2EE container.
// Do not connect to the provider here - defer to another thread.
new Thread(new Runnable() {
public void run() {
try {
// Create the connection to the provider
conn = ProviderConnectionFactory.newInstance().createConnection();
// Work around for a JAXM bug
conn.getMetaData();
// Install the message factory for the ebXML profile
messageFactory = conn.createMessageFactory("ebxml");
setMessageFactory(messageFactory);
} catch (Exception e) {
log("Exception when initializing", e);
}
}
}).start();
}
/**
* Handles a received SOAP message
*/
public void onMessage(SOAPMessage message) {
// Convert the message to string representation
// and log it.
try {
message.writeTo(os);
log("Received SOAP message:\n" + os.toString());
os.reset();
// Create a copy of the message with the same body
// and with the to and from addresses exchanged.
EbXMLMessageImpl soapMsg = (EbXMLMessageImpl)message;
// Create a reply message
EbXMLMessageImpl replyMsg = (EbXMLMessageImpl)messageFactory.createMessage();
// Move the body content from the received message to the source.
SOAPBody body = soapMsg.getSOAPPart().getEnvelope().getBody();
SOAPBody replyBody = replyMsg.getSOAPPart().getEnvelope().getBody();
Iterator iter = body.getChildElements();
while (iter.hasNext()) {
SOAPElement element = (SOAPElement)iter.next();
replyBody.addChildElement(element);
}
// Add an element after the body that contains the date.
SOAPElement element =
replyMsg.getSOAPPart().getEnvelope().addChildElement("Date", "tns", "urn:ebXMLEcho");
element.addTextNode(new Date().toString());
// Copy any attachments
iter = soapMsg.getAttachments();
while (iter.hasNext()) {
replyMsg.addAttachmentPart((AttachmentPart)iter.next());
}
// Get the message ID and install it as the "RefToMessageId" value
replyMsg.setRefToMessageId(soapMsg.getMessageId());
// Get the the "Reciever" address and install it as the "Sender" address
replyMsg.setSender(soapMsg.getReceiver());
// Get the "Sender" address an install it as the "Receiver" address
replyMsg.setReceiver(soapMsg.getSender());
// Copy values from the ebXML MessageHeader
replyMsg.setAction(soapMsg.getAction());
replyMsg.setConversationId(soapMsg.getConversationId());
replyMsg.setCPAId(soapMsg.getCPAId());
replyMsg.setManifest(soapMsg.getManifest());
replyMsg.setMessageHeaderVersion(soapMsg.getMessageHeaderVersion());
replyMsg.setService(soapMsg.getService());
// Send the reply message
conn.send(replyMsg);
} catch (Exception ex) {
log("Exception", ex);
}
}
} |