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:
<?xml version="1.0" encoding="UTF-8"?> <handler-chains xmlns="http://java.sun.com/xml/ns/javaee"> <handler-chain> <handler> <handler-class>lebrijo.handlers.WSHandler</handler-class> </handler> </handler-chain> </handler-chains> |
WSHandler class will implements the SOAPHandler interface:
public class WSHandler implements SOAPHandler<SOAPMessageContext> { public Set<QName> 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 } |