Unable to set SOAP header when calling web service via Camel using dataFormat as POJO

advertisements

I am using Camel in our project and requesting WebServices, the dataFormat is POJO. I was able to request when my SOAP message did not contain SOAP headers, but when it had Headers, I was unable to set those. I looked at the documentation but was not able to understand and have several questions.

I want to create a message like the below:

<soapenv:Envelope`enter code here`
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Header>
        <platformMsgs:documentInfo
            xmlns:platformMsgs="urn:messages_2015_1.platform.webservices.netsuite.com">
            <platformMsgs:nsId>WEBSERVICES_3479023</platformMsgs:nsId>
        </platformMsgs:documentInfo>
    </soapenv:Header>
    <soapenv:Body>
        <addListResponse
            xmlns="">
            <platformMsgs:writeResponseList
                xmlns:platformMsgs="urn:messages_2015_1.platform.webservices.netsuite.com">
                <platformCore:status isSuccess="true"
                    xmlns:platformCore="urn:core_2015_1.platform.webservices.netsuite.com"/>
                    <platformMsgs:writeResponse>
                        <platformCore:status isSuccess="false"
                            xmlns:platformCore="urn:core_2015_1.platform.webservices.netsuite.com">
                            <platformCore:statusDetail type="ERROR">
                                <platformCore:code>DUP_ENTITY</platformCore:code>
                                <platformCore:message>This entity already exists.</platformCore:message>
                            </platformCore:statusDetail>
                        </platformCore:status>
                    </platformMsgs:writeResponse>
                </platformMsgs:writeResponseList>
            </addListResponse>`enter code here`
        </soapenv:Body>
    </soapenv:Envelope>

I will be able to send the message if there was only Body, but can someone give me a code snippet for including the header section? The dataFormat is POJO.


When using CXF endpoint with dataFormat as POJO, body in Camel Exchange object is an object of org.apache.cxf.message.MessageContentsList. It is an extension of java.util.ArrayList<Object> and it contains parts of SOAP Message in order as defined in WSDL and corresponding method in WebService class. Element 0 there is a Body.

So, one way to do that with Java is to create a Processor class implementing org.apache.camel.Processor interface and in its process method set your SOAP header. Something like:

@Override
public void process(Exchange camelExchange) throws Exception {

  MessageContentsList messageBody = (MessageContentsList) camelExchange.getIn().getBody(); 

   DocumentInfo docInfoHeader = new DocumentInfo();
   ... set docInfoHeader properties ...
   messageBody.add(docInfoHeader);

}

(sample is not tested. It is just an idea, how to handle that...)

Other answer on similar question you can find here: Setting Custom Soap Header-To Pojo Message In Camel Cxf

It describes how to use Camel Exchange headers as SOAP Headers.

I'm not sure for 100% which way will work for you and which one is better... I guess, it depends on WSDL you use.

UPD: second choice is to use pure CXF solution by using CxfMessageSoapHeaderOutInterceptor custom implementation. It may look like:

public class MyCxfInterceptor extends CxfMessageSoapHeaderOutInterceptor {
   @Override
   public void handleMessage( org.apache.cxf.binding.soap.SoapMessage message) {

      org.apache.cxf.binding.soap.SoapHeader myCustomHeader = new org.apache.cxf.binding.soap.SoapHeader(new QName(
                {custom name space}, {custom local name}), {Custom content object}));

        myCustomHeader.setMustUnderstand(true);

        message.getHeaders().add(myCustomHeader);

   }

and set Interceptor in Camel Cxf Endpoint as :

<cxfEndpoint ...>
    <outInterceptors>
        <spring:bean class="MyCxfInterceptor"/>
    </outInterceptors>
...