How to plug SOAPHandler (JAX-WS)?

2.3k views Asked by At

I want to process SOAP headers with SOAPHandler, i.e. I have created SOAPHandler, but how to plug it to specified service ? In other words to say that this handler should handle messages for this service.. Probably I need to use some annotation or xml configuration ... ?..

1

There are 1 answers

0
Chathuranga Tennakoon On

This can be done with following steps.

1. Defining the Handler

public class CalculatorSOAPHandlerOne implements SOAPHandler<SOAPMessageContext> {

    private static final Logger logger = LoggerFactory.getLogger(CalculatorSOAPHandlerOne.class);

    @Override
    public Set<QName> getHeaders() {
        return null;
    }

    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        if(!(Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)){
            logger.info(" soap message passed through CalculatorSOAPHandlerOne (only for request)");
        }
        return true;
    }

    @Override
    public boolean handleFault(SOAPMessageContext context) {
        return true;
    }

    @Override
    public void close(MessageContext context) {

    }
}

2. declaring the handler chain declaration in XML (handler-chain.xml)

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<javaee:handler-chains
        xmlns:javaee="http://java.sun.com/xml/ns/javaee"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <javaee:handler-chain>
        <javaee:handler>
            <javaee:handler-class>com.chathurangaonline.jaxws.samples.handler.CalculatorSOAPHandlerOne</javaee:handler-class>
        </javaee:handler>

        <javaee:handler>
    </javaee:handler-chain>
</javaee:handler-chains>

3. Adding the handler chain for the JAX-WS service implementation

@WebService
@HandlerChain(file = "handler-chain.xml")
public class CalculatorService{

    private  static final Logger logger = LoggerFactory.getLogger(CalculatorServiceImpl.class);

    @Override
    public double add(double num1, double num2) {
        logger.info("== calling add method ==");
        return num1 + num2;
    }

    @Override
    public double multiply(double num1, double num2) {
        return num1 * num2;
    }
}