Search code examples
soapcxfmulemule-studio

Change the prefix in CXF with interceptor


I am trying to change

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> to

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body>

I am using Mule and CXF. We are exposing a SOAP service and the wsdl is from a legacy system (we imported it and generated the classes). It is necessary to change the prefix from 'soap' to just 's'. I see that this is possible with an Interceptor, but for some reason I cant make it work. Here is the code for my interceptor:

package se.comaround.interceptors;

import java.util.HashMap;
import java.util.Map;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

public class ComAroundSoapResponceInterceptor extends
		AbstractPhaseInterceptor<SoapMessage> {

	public ComAroundSoapResponceInterceptor() {
		super(Phase.PREPARE_SEND);
	}

	public void handleMessage(SoapMessage message) {
		Map<String, String> hmap = new HashMap<String, String>();
		hmap.put("s", "http://schemas.xmlsoap.org/soap/envelope/");
		message.setContextualProperty("soap.env.ns.map", hmap);
		message.setContextualProperty("disable.outputstream.optimization", true);

		System.out.println("Set up");
	}

}

Furthermore, can I change the prefixes for the schemas inside the response?


Solution

  • After some testing around, it worked. It might seem very stupid and my jaw dopped a couple of times, I restarted, cleared all cash, re-built and it seems like the interceptor works WHEN I add an extra line, which is unbelievable, I know:

    package se.comaround.interceptors;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.apache.cxf.binding.soap.SoapMessage;
    import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
    import org.apache.cxf.phase.Phase;
    
    public class ComAroundSoapResponceInterceptor  
            extends  AbstractSoapInterceptor {
    
        public ComAroundSoapResponceInterceptor() {
            super(Phase.PREPARE_SEND);
        }
    
        public void handleMessage(SoapMessage message) {
            Map<String, String> hmap = new HashMap<String, String>();
            hmap.put("s", "http://schemas.xmlsoap.org/soap/envelope/");
                    message.put("soap.env.ns.map", hmap);
                    message.put("disable.outputstream.optimization", true);
        }
    }
    

    I had to drink some coffee, take some time before I got it that it actually is working this way. So, more or less what they suggest here:

    http://cxf.547215.n5.nabble.com/How-to-customize-namespaces-position-and-prefix-in-CXF-response-td3423069.html