18

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.

Community
  • 1
  • 1
Denian
  • 737
  • 2
  • 8
  • 17

3 Answers3

23

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

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

VM argument.

adrianboimvaser
  • 2,651
  • 1
  • 22
  • 30
  • I'm already using that SP to dump the request and responses on the log, but what I need is to perform some actions depending on the message, headers, etc. For example, if I receive a response that is a 500 (Internal Server Error) throw an exception. My handler is just an example. – Denian Jan 19 '11 at 18:55
  • 1
    To my understanding this always go to System.out (or .err). Not to a log. – Thorbjørn Ravn Andersen Jan 23 '13 at 08:49
  • This seems to have no effect for me. I guess the right flag must vary depending on which version you're using (and I have no idea which one I'm using.) – Hakanai Oct 16 '13 at 03:25
  • 1
    As of Java 1.7, this seems to have changed. see http://stackoverflow.com/questions/18354201/jax-ws-http-logging-in-java-1-7 – sleske Jul 23 '15 at 13:45
6

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.

andbi
  • 4,426
  • 5
  • 45
  • 70
  • Sorry that I forgot to mention in my question that I already checked this solution but still needs a reference to an XML. – Denian Jan 20 '11 at 00:27
  • BTW, I think that you're right about doing that in the constructor. Having again a look to HandlerChain I've found this: https://www.ibm.com/developerworks/mydeveloperworks/blogs/amsurana/entry/jax_ws_handlers_part_3_of_3 It seems like is possible to do it using a HandlerResolver. I'll try that tomorrow. – Denian Jan 20 '11 at 00:34
  • Right, you could try HandlerResolver interface in a way similar to http://www.javaworld.com/javaworld/jw-02-2007/jw-02-handler.html?page=3 – andbi Jan 20 '11 at 00:50
1

there are 2 tools that you can use to help with this:

Both tools offer a proxy mode, which intercepts, logs and forwards requests and responses.

crowne
  • 8,456
  • 3
  • 35
  • 50
  • 1
    Well, as I said I need to perform some actions in the application itself depending on the response, therefore I can't use external tools. – Denian Jan 20 '11 at 00:18