Search code examples
javaapache-axis

How to add soap header information to a java soap service request using apache axis2


I am working on a web service using apache axis2 and i have generate the required client code. The client request should look like this:

`<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:v2="http://www.huawei.com.cn/schema/common/v2_1"
xmlns:loc="http://www.csapi.org/schema/parlayx/sms/notification_manager/v2_3/local">
<soapenv:Header>
    <RequestSOAPHeader xmlns="http://www.huawei.com.cn/schema/common/v2_1">
        <spId>35000001</spId>
        <spPassword>de96d901b3bad1db2aab76b7b0b202f2</spPassword>
        <serviceId>0003062000001100</serviceId>
        <timeStamp>20120731064245</timeStamp>
    </RequestSOAPHeader>
</soapenv:Header>
<soapenv:Body>
    <loc:startSmsNotification>
        <loc:reference>
            <endpoint>>http://10.138.30.153:9080/notify</endpoint>
            <interfaceName>notifySmsReception</interfaceName>
            <correlator>1232</correlator>
        </loc:reference>
        <loc:smsServiceActivationNumber>23424
        </loc:smsServiceActivationNumber> <!--Optional: -->
        <loc:criteria>Love</loc:criteria>
    </loc:startSmsNotification>
</soapenv:Body>
</soapenv:Envelope>`

I have been able to add body message to my request but not soap header information. The header information is required for validation of the request. How can i add soap header information to my soap request in the generated client code?


Solution

  • Hi Martin, SDP material is not readily available on the internet but try following.

        OMFactory fac = OMAbstractFactory.getOMFactory();
        SOAPFactory sfac = OMAbstractFactory.getSOAP11Factory();
    
        OMNamespace omNs = fac.createOMNamespace(nameSpace, "ns1");
    
        SOAPHeaderBlock block_RequestSOAPHeader = sfac.createSOAPHeaderBlock(
        SOAPHEADER_RequestSOAPHeader, omNs);
    
        try
        {
            SOAPHeaderBlock block_spId = sfac.createSOAPHeaderBlock(SOAPHEADER_SPID, omNs);
            block_spId.addChild(sfac.createOMText(spId));
    
            String md_password = md5Digest(spId, spPassword, timeStamp);
            SOAPHeaderBlock block_spPassword = sfac.createSOAPHeaderBlock(SOAPHEADER_SPPASSWORD, omNs);
            block_spPassword.addChild(sfac.createOMText(md_password));
    
            SOAPHeaderBlock block_timeStamp = sfac.createSOAPHeaderBlock(SOAPHEADER_TIMESTAMP, omNs);
            block_timeStamp.addChild(sfac.createOMText(timeStamp));
    
            SOAPHeaderBlock block_serviceId = sfac.createSOAPHeaderBlock(SOAPHEADER_SERVICEID, omNs);
            block_serviceId.addChild(sfac.createOMText("your text"));
    
    
            SOAPHeaderBlock block_linkId = sfac.createSOAPHeaderBlock(SOAPHEADER_LINKID, omNs);
            block_linkId.addChild(sfac.createOMText(linkID));
    
            SOAPHeaderBlock block_OA = sfac.createSOAPHeaderBlock(SOAPHEADER_OA, omNs);
            block_OA.addChild(sfac.createOMText(OA));
    
            SOAPHeaderBlock block_FA = sfac.createSOAPHeaderBlock(SOAPHEADER_FA, omNs);
            block_FA.addChild(sfac.createOMText(FA));
    
            block_RequestSOAPHeader.addChild(block_spId);
            block_RequestSOAPHeader.addChild(block_spPassword);
            block_RequestSOAPHeader.addChild(block_serviceId);
            block_RequestSOAPHeader.addChild(block_timeStamp);
            block_RequestSOAPHeader.addChild(block_linkId);
            block_RequestSOAPHeader.addChild(block_OA);
            block_RequestSOAPHeader.addChild(block_FA);
    
    
            /*
             *
             *  <soapenv:Header>
             *    <ns1:RequestSOAPHeader xmlns:ns1="http://www.xxxxxxxx.xxxxxx.xxxxxx/common/v2_1">
             *      <ns1:spId>your spid</tns:spId>
             *      <ns1:spPassword>your password</tns:spPassword>
             *      <ns1:timeStamp>your timestamp</tns:timeStamp>
             *      <ns1:linkId>your linkid</tns:linkId>
             *      <ns1:OA>tel:your msisdn1</tns:OA>
             *      <ns1:FA>tel:msisdn1</tns:FA>
             *    </ns1:RequestSOAPHeader>
             *  </soapenv:Header>
             */
    
            serviceClient.addHeader(block_RequestSOAPHeader);