JAX-WS Handler Chain: messages interceptor

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:

public class Hello {
    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">  

WSHandler class will implements the SOAPHandler interface:

public class WSHandler implements SOAPHandler<SOAPMessageContext> {  
     public Set<QName> getHeaders() {  
         return null;  
     public boolean handleFault(SOAPMessageContext context) {  
         return true;  
     public boolean handleMessage(SOAPMessageContext 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 {  
         } catch (Exception e) {  
             System.out.println("Exception in handler: " + e);  
	public void close(MessageContext context) {
		// TODO Auto-generated method stub

2 thoughts on “JAX-WS Handler Chain: messages interceptor

Leave a Reply

Your email address will not be published. Required fields are marked *