Wednesday, 14 February 2018

Add session ID in SOAP header at the message payload

Handling session in the SOAP header at request payload.

Login request and get the session ID as response.

Received Session Id has to be added in the header as below.

This is displayed in soap UI but when receiving to HCI it receives only body data, at that time header need to be added by below script.

Groovy Script:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.cxf.binding.soap.SoapHeader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.sap.it.api.ITApiFactory;
import com.sap.it.api.securestore.SecureStoreService;
import com.sap.it.api.securestore.UserCredential;
import groovy.util.XmlSlurper;

def Message processData(Message message) {

   // To get a session ID
   def propertyMap = message.getProperties()
   String sessionID = propertyMap.get("sessionID");
   
   // To add the header to the actual data
   // The following lines create the DOM Element.
   //<CventSessionHeader xmlns=http://www.Test.com/><CventSessionValue>"+sessionID+"</CventSessionValue></CventSessionHeader>
   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   dbf.setNamespaceAware(true);
   dbf.setIgnoringElementContentWhitespace(true);
   dbf.setValidating(false);
   DocumentBuilder db = dbf.newDocumentBuilder();
   Document doc = db.newDocument();
   Element authHeader = doc.createElementNS("http://xxx/2006-11", "CventSessionHeader");
   doc.appendChild(authHeader);
   Element cventSessionValue = doc.createElementNS("http://xxx/2006-11", "CventSessionValue");
   cventSessionValue.setTextContent(sessionID);
   authHeader.appendChild(cventSessionValue);
 
   // Create SOAP header instance.
   SoapHeader header = new SoapHeader(new QName(authHeader.getNamespaceURI(), authHeader.getLocalName()), authHeader);


   // Add the SOAP header to the header list and set the list to the message header "org.apache.cxf.headers.Header.list".
   List  headersList  = new ArrayList<SoapHeader>();
   headersList.add(header);
   message.setHeader("org.apache.cxf.headers.Header.list", headersList);
  
   return message;
}

After that the operation call is performed like create, update event etc..

Happy Learning!!!





5 comments:

  1. Hi,
    awesome post! Can you tell me how you did the Login SOAP call? I use the same API but wasn't successfull.
    Thanks

    ReplyDelete
  2. Hi Phosmet,
    If it is SOAP UI, you need to make a 2 different call, one for getting SessionID and other for pushing data. As like in SOAP UI,Using the login details from content modifier and hit the system and get the SessionID. After that add the script and add the Session Id in the header and send the actual data with it. Hope this helps. Sorry for the delay in response.

    ReplyDelete
  3. Hi guys! thank's for the post...

    I'm facing an problem with HTTP call, i got a response HTTP Status code "400", but we have a client limitation that we need to return this HTTP Status Code 200 to the caller... i build the body message but that's now enought. I need to change the header with the HTTP Status Code 400 to 200, and i don't kow how to do... Any idea?

    ReplyDelete
  4. sorry for the delay. Still not found an solution try this. Use the script to set the header parameter for HTTP status as 200, this will overwrite the old HTTP code to the newly added one. If you had done already with other solutions, then let us know the solution please. Thanks

    ReplyDelete
  5. Hi guys, i have a Problem. Currently I use "user credentials" for user and passwort. Now I want to change to a multiple call without the user crendetials. I want to determine the runtime on the basis of certain criteria which user / password I use. Who can tell me how I do this. I use SOAP.

    regards

    ReplyDelete