Java Reference
In-Depth Information
The handleMessage method is invoked at two different times in a typical SOAP request/re-
sponse message exchange pattern: when sending the message and when receiving the request.
More specifically, outbound messages are processed by handlers immediately following bind-
ing provider processing, and inbound messages are processed by handlers immediately before
binding provider processing.
Implementing a simple handler
Let's implement a simple handler to see how they work. This protocol handler class will
simply write all SOAP messages to a file output stream.
Do a quick check of the message context to find out if the current message is a request (out-
bound) or a response (inbound), and write a new file based on that. Note that in a production
system, you wouldn't want to use a static name for all inbound and outbound messages, as
each message would get overwritten with the new set. The thing to do is to set a unique mes-
sage ID in the header on an outbound message (you could do this using a handler too, using
SAAJ), and then use that as the filename to help you look it up later. Or, more simply, you
could append to the file instead of creating a new one with each invocation. But that's not the
point here. (See Example 6-10 .)
Example6-10.SaveMessageHandler.java
package com.soacookbook.ch03.handler;
import java.io.File;
import java.io.FileOutputStream;
import static java.lang.System.out;
import java.io.IOException;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import org.apache.log4j.Logger;
public class SaveMessageHandler implements
SOAPHandler<SOAPMessageContext> {
private static final Logger LOGGER =
Logger.getLogger(SaveMessageHandler.class);
p ublic boolean handleMessage(SOAPMessageContext ctx) {
LOGGER.debug("Handling SOAP MESSAGE.");
Search WWH ::




Custom Search