Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trace SOAP request/responses with JAX-WS on the client side

I'm using JAX-WS reference implementation (2.1.7) and I want to trace SOAP request/responses on the client side. Actually, what I need is to examine some Http headers when I receive the response.

Following these previous questions ( Tracing XML request/responses with JAX-WS and Java JAX-WS web-service client: how log request & response xml? ), I've created my own handler to log when I send a request and receive a response:

public class SHandler implements SOAPHandler<SOAPMessageContext>
{

  private static final Logger log = Logger.getLogger(SHandler.class);

  @Nullable
  @Override
  public Set<QName> getHeaders()
  {    
    log.debug(">>>>>>>>>>> GetHeaders");
    return null;    
  }

  @Override
  public boolean handleMessage(SOAPMessageContext soapMessageContext)
  {
    log.debug(">>>>>>>>>>> HandleMessage");
    return true;
  }

  @Override
  public boolean handleFault(SOAPMessageContext soapMessageContext)
  {
    log.debug(">>>>>>>>>>> HandleFault");
    return true;
  }

  @Override
  public void close(MessageContext messageContext)
  {
    log.debug(">>>>>>>>>>> Close");    
  }
}

and I add the handler to the handler chain during the service initialisation:

@WebServiceClient(name = "MyService", targetNamespace = "http://www.whatever.com/", wsdlLocation = "file:/path/to/wsdl")
public class MyService extends Service
{

    public MyService(URL wsdlLocation) {
        super(...);
        initializeBinding();
    }

    @WebEndpoint(name = "MyOperation")
    public MyPort getMyPort() {
        return super.getPort(new QName("http://www.whatever.com/", "MyPort"), MyPort.class);
    }

    private void initializeBinding() {        
        MyPort port = getMyPort();
        BindingProvider bindingProvider = ((BindingProvider) port);
        List handlerChain = bindingProvider.getBinding().getHandlerChain();
        handlerChain.add(new SHandler());
        bindingProvider.getBinding().setHandlerChain(handlerChain);        
    }

    ...

}

The problem is that this doesn't work at all on the client side. I don't see any logs and my handler is never executed when I send a request and receive a response.

Notice that there is no specific WSDL related to this issue because I work on an MDA platform that generates client/server artifacts from any WSDL. In addition, I cannot do this at configuration level as all is generated, so I can only do it programmatically (I've been googling this and all the solutions that I find are either the one in the original post or using the handler-chain.xml configuration file).

Am I missing something? Is there any other way of doing this?

Thanks in advance.

like image 830
Denian Avatar asked Jan 19 '11 18:01

Denian


People also ask

How do you trace a SOAP request and response?

In SOAP web service, each HTTP request or response encapsulates a SOAP envelope, these messages are easy to trace by using Eclipse IDE, build-in “TCP/IP monitor” tool. The idea is host another server in between the client and server to perform port forward function to intercept the HTTP traffic.

What is JAX-WS how it is useful for describing SOAP web services?

JAX-WS is a technology for building web services and clients that communicate using XML. JAX-WS allows developers to write message-oriented as well as RPC-oriented web services. In JAX-WS, a web service operation invocation is represented by an XML-based protocol such as SOAP.

What is the contract first approach to building a SOAP Web service with JAX-WS?

The Contract-first approach tells us to create first WSDL and then create end-point interface and implementation class. The Contract-last approach tells us to create first end-point interface and implementation class then create WSDL file.


2 Answers

If you only want to look at the SOAP messages run with

-Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true

VM argument.

like image 55
adrianboimvaser Avatar answered Oct 20 '22 21:10

adrianboimvaser


Why not use @HandlerChain(file = "....") annotation?

From my pov, you can not mix constructor- and annotation-based configurations as on-deploy webservice initialization and creating new instance of your service class are performed in absolutely different contexts.

like image 32
andbi Avatar answered Oct 20 '22 22:10

andbi