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<SOAPMessageContext> {

	@Override
	public Set<QName> 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<Handler> 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 ×
Binu GeorgeJAX-WSjax-ws logging,jax-ws tutorial,jax-ws web service request response loggingHow 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...Daily Dose of Java