How to throw a SOAP Fault from Camel - 6.3

Talend ESB Mediation Developer Guide

EnrichVersion
6.3
EnrichProdName
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
task
Design and Development
EnrichPlatform
Talend ESB

If you are using a camel-cxf endpoint to consume the SOAP request, you may need to throw the SOAP Fault from the Camel context. Basically, you can use the throwFault DSL to do that; it works for POJO, PAYLOAD and MESSAGE data format. You can define the soap fault like this

SOAP_FAULT = new SoapFault(EXCEPTION_MESSAGE, SoapFault.FAULT_CODE_CLIENT);
Element detail = SOAP_FAULT.getOrCreateDetail();
Document doc = detail.getOwnerDocument();
Text tn = doc.createTextNode(DETAIL_TEXT);
detail.appendChild(tn);

Then throw it as you like:

from(routerEndpointURI).setFaultBody(constant(SOAP_FAULT));

If your CXF endpoint is working in the MESSAGE data format, you could set the SOAP Fault message in the message body and set the response code in the message header.

from(routerEndpointURI).process(new Processor() {

   public void process(Exchange exchange) throws Exception {
      Message out = exchange.getOut();
      // Set the message body with the 
      out.setBody(this.getClass().
         getResourceAsStream("SoapFaultMessage.xml"));
      // Set the response code here
      out.setHeader(
         org.apache.cxf.message.Message.RESPONSE_CODE, 
         new Integer(500));
   }
});

Same for using POJO data format. You can set the SOAPFault on the out body and also indicate it is a fault by calling Message.setFault(true):

from("direct:start").onException(SoapFault.class)
   .maximumRedeliveries(0).handled(true)
   .process(new Processor() {
      public void process(Exchange exchange) throws Exception {
         SoapFault fault = exchange
            .getProperty(Exchange.EXCEPTION_CAUGHT, SoapFault.class);
         exchange.getOut().setFault(true);
         exchange.getOut().setBody(fault);
      }
   }
).end().to(SERVICE_URI);