Posts Tagged ‘soap’

CXF Raw SOAP Envelope via Interceptor

November 3rd, 2009 3 comments

CXF SOAPRecently I started using the CXF SOAP library from Apache and was stumped on a simple problem, that being, how to view/modify a raw SOAP envelope. This sounds like a relatively simple task (not too bad when you know how) however it does in fact require some know-how.

The CXF SOAP library gives you access to all parts of the SOAP request and response life-cycle (via HTTP) using CXF Interceptors. The different points in time are referred to as phases and an interceptor can be placed at any one of the defined phases and automatically gain access to the SOAP message object and it’s current state.

For this example I have chosen Phase.RECEIVE which is one of the first phases which is essentially unprocessed by the CXF SOAP stack, which means I have free rain to pre-process the SOAP envelope (if I want to). Particularly handy if you need to maintain backwards compatibility with old SOAP API’s, poorly defined complex SOAP objects, dodgy WSDL definitions or maybe you simply want to view raw SOAP envelope (handy for debugging).

The crux to this problem is manipulating InputStream’s, once an InputStream has been read it is not available, which obviously causes a problem as the rest of the CXF SOAP stack expects it to be available. The solution is to use the CXF provided CachedOutputStream and IOUtils.copy which takes a cache copy of the InputStream making it available for future use :)

Once the Interceptor is implemented all that remains is to add it to the CXF SOAP client pror to usage.

This is an example of gaining access to an incoming client SOAP envelope, the interceptor could quite easily be in the sent phase or even part of the SOAP server implementation…it’s up to you.

package CXFSoap;


import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

public class PacketInterceptor extends AbstractPhaseInterceptor<Message>{

	public PacketInterceptor(){

	public void handleMessage(Message message) throws Fault{
		// Get the supplied SOAP envelope in the form of an InputStream
		InputStream inputStream = message.getContent(InputStream.class);

		if (inputStream != null){
			String processedSoapEnv = "";
			// Cache InputStream so it can be read independently
			CachedOutputStream cachedInputStream = new CachedOutputStream();  
			try {
				InputStream tmpInputStream = cachedInputStream.getInputStream();
					String inputBuffer = "";
					int data;
					while((data = != -1){
						byte x = (byte)data;
						inputBuffer += (char)x;
					  * At this point you can choose to reformat the SOAP
					  * envelope or simply view it just make sure you put
 					  * an InputStream back when you done (see below)
 					  * otherwise CXF will complain.
					processedSoapEnv = fixSoapEnvelope(inputBuffer);
				catch(IOException e){
			catch (IOException e) {
				// TODO Auto-generated catch block

			// Re-set the SOAP InputStream with the new envelope
			message.setContent(InputStream.class,new ByteArrayInputStream(processSoapEnv.getBytes()));
			 * If you just want to read the InputStream and not 
			 * modify it then you just need to put it back where
			 * it was using the CXF cached inputstream
			 * message.setContent(InputStream.class,cachedInputStream.getInputStream());

	private String fixSoapEnvelope(String xml){

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import Stuff.StuffObj;
import CXFSoap.PacketInterceptor;
import CXFSoap.VIPCoreAPI;

public class CXFTest{

	    public static void main(String args[]) throws Exception{
	    		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

		    	 * This is the important part, the interceptor needs
		    	 * to be added to the CXF SOAP client prior to
		    	 * the SOAP call being made.
		    	factory.getInInterceptors().add(new PacketInterceptor());

		    	API client = (API)factory.create();
		    	StuffObj stuff = client.getStuff();

SOAP Debugging with tcpdump

October 27th, 2009 No comments

You are on a Linux box (SOAP client) making a SOAP request to a SOAP server somewhere on port 80, to view the request and response with tcpdump here are the commands execute at shell.

Incoming SOAP envelope (server response)

tcpdump  -nnvvlXSs 4096 src port 80

Outgoing SOAP envelope (client request)

tcpdump  -nnvvlXSs 4096 dst port 80

Of course if you have additional traffic going in and out from this box you can use the “and” operator to fine tune what you are after.

Categories: General, Linux, Web/HTML Tags: ,