6

I'm triing to implement a SOAP Handler to a client. I'm using Wildfly8.2 java8 and JAX-WS and Maven

I have generated the client interface class with eclipse from the endpoint WSDL

The handler-chain.xml file is placed in the same package as the client interface.

When I call the web service it executes ok but the handler is not invoked. If I put a brake point in the handler it is never invoked

the client interface is like this:

@WebService(targetNamespace = "********************", name = "WorkflowEditor")
@XmlSeeAlso({ ObjectFactory.class })
@HandlerChain(file = "handler-chain.xml")
public interface WorkflowEditor {

I have tried also to put the xml in resources and call it in the annotation with the url I verified is working for example :

    @WebService(targetNamespace = "**************", name = "WorkflowEditor")
@XmlSeeAlso({ ObjectFactory.class })
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@HandlerChain(file = "http://cloudflow-backend-local.arctur.net:8080/resources/handler-chain.xml")
public interface WorkflowEditor {

the handler is like this:

package si.arctur.services.handlers;

import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPMessageContext;

public class PrintEnvelopeHandler implements javax.xml.ws.handler.soap.SOAPHandler<SOAPMessageContext> {

    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        System.out.println("Client : handleMessage()......");
        SOAPMessage soapMessage = context.getMessage();
        return true;
    }

    @Override
    public boolean handleFault(SOAPMessageContext context) {
        System.out.println("Client : handleFault()......");
        return true;
    }

    @Override
    public void close(MessageContext context) {
        System.out.println("Client : close()......");
    }

    @Override
    public Set<QName> getHeaders() {
        // TODO Auto-generated method stub
        return null;
    }

}

and the handler-chain.xml file is like this:

<?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>si.arctur.services.handlers.PrintEnvelopeHandler</javaee:handler-class>
</javaee:handler>
 </javaee:handler-chain>
simonC
  • 4,101
  • 10
  • 50
  • 78

3 Answers3

8

(NOTE: The guide specified by other answers here is flawed. It asks you to edit an automatically-generated file. This never ends well.)

There is (currently) no standard way to attach a handler chain to a Web Service client using annotations only. To add handlers, you'll need to use the JAX-WS API:

// 'sei' is assumed to be the service endpoint interface
BindingProvider bp = (BindingProvider) sei;

// Get the handler chain
List<Handler> handlerChain = bp.getBinding().getHandlerChain();

// Add your handler
handlerChain.add(myHandler);

// Re-set the handler chain (needed because getHandlerChain() returns
// a copy of the handlers' list.
bp.getBinding().setHandlerChain(handlerChain);
Isaac
  • 16,458
  • 5
  • 57
  • 81
4

@HandlerChain annotation can be placed at the client class (annotated with @WebServiceClient) as described in this tutorial.

sanastasiadis
  • 1,182
  • 1
  • 15
  • 23
  • 3
    A tutorial that tells you to edit a generated file, is most likely missing something. – Isaac Jun 26 '17 at 06:48
  • That annoyed me as well, @Isaac. We're told do subclass it instead on https://stackoverflow.com/a/39990569/770254 . – bruno Oct 07 '20 at 12:19
  • i am using handler chain annotation and it is working fine, however when i run the project this annotation disappear from java class. what can be the reason? – user666 Dec 30 '20 at 12:21
0

As I can understand, the client interface and the client class are located in 2 different packages. Suspecting that handler-chain.xml can not be located.

  • The most correct solution would be to place the handler-chain.xml file in the main/resources folder of your maven project.

  • Alternatively, try to give the full package path of the location of the file on the file attribute of the @HandlerChain annotation.

sanastasiadis
  • 1,182
  • 1
  • 15
  • 23
  • If I put the handler-chain.xml in resources I suppose that I need still put the annotation on the interface? With the correct path to the resource foulder? – simonC Sep 01 '15 at 08:52
  • Yes, as the problem seems to be how the handler-chain.xml is located, the annotation is still needed. Moving the file in src/main/resources the file is in the classpath and it is in the root folder as it is already declared on the annotation. – sanastasiadis Sep 01 '15 at 12:01
  • Looks like it does not help, I have put the handler-chain.xml to resources and I have put the @HandlerChain with the url to the xml file like this @HandlerChain(file = "http://cloudflow-backend-local.arctur.net:8080/resources/handler-chain.xml") ... I have verified the url and it works, it returns the xml ok, but anyway the handler is not initiated when the web service method is called...do you have an Idea how to chack why the handler is not called? – simonC Sep 01 '15 at 19:47
  • 2
    Please have a look at [this](http://www.mkyong.com/webservices/jax-ws/jax-ws-soap-handler-in-client-side/) tutorial where it is described that the `@HandlerChain` annotation is assigned on the class with `@WebServiceClient`. – sanastasiadis Sep 01 '15 at 20:11
  • yes that was the problem...I was putting the annotation on interface instead on the class ... can you write an answer an I will accept it as the correct answer – simonC Sep 01 '15 at 20:35