Logging or Tracing Web Service XML Request/Response with JAX-WS

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- Filament.io 0 Flares ×

How to Log Request and Response With Java JAX-WS web-service Server and Client?

We have discussed many examples to develop secure web services with JAX-WS API and using the Metro web service stack. We also discussed how to enable Message Transmission Optimization Mechanism (MTOM) to optimize the data transfer between services and clients. In our previous examples we also created web services which are secured using signature, encryption and also with Transport Level Security (TLS).
When you try all those examples, you may want to see how the SOAP message requests and responses are transferred between clients. Let us see some of the tools and mechanisms to log and dump the JAX-WS SOAP message request and responses.

Tools to Trace XML request/responses with JAX-WS

There are tools such as SoapUI allows you to provide endpoint and the tool will generates sample requests for you. You can provide required inputs to test the service methods. SoapUI is an open source web service testing application.
Tools such as Wireshark monitors and capture raw network traffic from an interface. You can configure filters and see the network traffic and data from your client to server machines.
There are other tools such as TCP Monitor and WSMonitor to Monitor SOAP traffic.

Configure your Server to Dump SOAP Requests and Response With JAX-WS

You can monitor the the request and response messages using system property com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true. This will print both request and response messages at client side.
If you are using Tomcat as web server you can use set JAVA_OPTS as below

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

At server side, you can dump incoming requests and responses to System.out using the system property com.sun.xml.ws.transport.http.HttpAdapter.dump=true.
In both scenario you will see the actual bytes that are sent over the wire and the transport specific information such as HTTP headers.
See a sample request and response here.

Using SOAPHandler to Develop Your Own Logger to Print JAX-WS Request and Response XML Messages

SOAPHandler class provide typesafety for the message context parameter and it gives access to the SOAPMessageContext. The SOAPMessageContext provides access to the SOAP message.
See the example below

package com.globinch.client;

import java.io.IOException;
import java.util.Set;

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

public class MyServiceLogHandler implements SOAPHandler {

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

	@Override
	public void close(MessageContext arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean handleFault(SOAPMessageContext arg0) {
		SOAPMessage message= arg0.getMessage();
		try {
			message.writeTo(System.out);
		} catch (SOAPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean handleMessage(SOAPMessageContext arg0) {
		SOAPMessage message= arg0.getMessage();
		boolean isOutboundMessage=  (Boolean)arg0.get (MessageContext.MESSAGE_OUTBOUND_PROPERTY);
		if(isOutboundMessage){
			System.out.println("OUTBOUND MESSAGE\n");
			
		}else{
			System.out.println("INBOUND MESSAGE\n");
		}
		try {
			message.writeTo(System.out);
		} catch (SOAPException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
}

You need to add the above MyServiceLogHandler to the Binding. Binding interface is the base interface for JAX-WS protocol bindings and can be obtained from BindingProvider.
Example source code is given below.

….
private static URL url = null;
private static Service service = null;
..............
url = new URL("https://localhost:8443/MyService/?wsdl");
QName qname = new QName("http://service.globinch.com/",
			"MyWebServiceService");
service = Service.create(url, qname);
MyWebService mtomService = service.getPort(MyWebService.class);
BindingProvider bindProv = (BindingProvider) mtomService;
java.util.List handlers = bindProv.getBinding().getHandlerChain();
handlers.add(new MyServiceLogHandler());
bindProv.getBinding().setHandlerChain(handlers);
……………..

This will print both SOAP XML request and response to “System.out” PrintWiter.

References:

Incoming search terms:

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- Filament.io 0 Flares ×

Related Posts

This Post Has 3 Comments

  1. Nice one..!!
    How can I do it if multiple threads are calling or using the service at a same time..??

  2. For dumping SOAP requests and responses, I believe the correct system property is as follows:


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

    It looks like you may have forgotten “internal”.

  3. Hi All,

    Writing to you seeking solution for the below problem:

    As part of automation initiative, we are Currently evaluating Web Services using Java. We successfully generated the request. Upon Posting the request, we are getting an error as

    no SOAPAction header!

    We are not passing any SOAPAction in our XML request since it is not defined in our END Point URL. In our Endpoint URL it shows as mentioned below.

    Please help me in solving this issue.

    Note: we placed the same request generated with JAVA in SOAP UI manually and received the proper response.

Leave a Reply

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

Paged comment generated by AJAX Comment Page

© 2024 Globinch Java Forum - Theme by WPEnjoy · Powered by WordPress