We want to intercept the WS input and output. In order to:
- Logging any part in the message
- Registering sent messages
- Add security controls
- ....
Here we are going to do a input/output WS example log.
We annotate the WS with Handler Chain descriptor:
@WebService
@HandlerChain(file="src/META-INF/handlerChain.xml")
public class Hello {
@WebMethod
public String sayHello(String name) {
return "Hello, "+name+".";
}
}
It will have the following contents:
lebrijo.handlers.WSHandler
WSHandler class will implements the SOAPHandler interface:
public class WSHandler implements SOAPHandler {
public Set getHeaders() {
return null;
}
public boolean handleFault(SOAPMessageContext context) {
logToSystemOut(context);
return true;
}
public boolean handleMessage(SOAPMessageContext context) {
logToSystemOut(context);
return true;
}
private void logToSystemOut(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
System.out.println("Outgoing message:");
} else {
System.out.println("Incoming message:");
}
SOAPMessage message = smc.getMessage();
try {
message.writeTo(System.out);
} catch (Exception e) {
System.out.println("Exception in handler: " + e);
}
}
public void close(MessageContext context) {
// TODO Auto-generated method stub
}