3

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?

Stanislav Ivanov
  • 425
  • 2
  • 7
  • 19

1 Answers1

4

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

rtruszk
  • 3,902
  • 13
  • 36
  • 53
Stanislav Ivanov
  • 425
  • 2
  • 7
  • 19
  • 1
    Hi, In fact you don't need to call message.getEnvelopeNs() if you replace the message.setContextualProperty with the message.put. In fact SoapMessage extends java Map and if you call directly the put there will be no check of existing keys. – Mauro Rocco Oct 08 '15 at 13:58