Skip to Content
author's profile photo Former Member
Former Member

SOAP header authentication for external webservice to use in WebDynpro Java

Hi All,

We are working on SAP EP 7.31 and Adaptive Web Service Model.

The requirement is to develop a SSO application by passing portal user-id to external webservice which will grant the access to third party system. But to access that external webservice, authentication is required thru SOAP header. As shown in attached image, the parameters are <web:User> and <web:Pwd> that needs to be set for authentication.

I understand, I need to use interface 'IWDWSInvocationModifier' and method 'doModifyInvocation' to set the SOAP header. But not able to understand how to set the parameters programatically.

Please help, how do I need to authenticate using SOAP header in webdynpro java.

Thanks,

Amol.

SOAP.png (6.4 kB)
Add comment
10|10000 characters needed characters exceeded

3 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Oct 21, 2014 at 07:00 AM

    Hi Daniel,

    I have already gone thru the document, also I checked FAQ - Models - Adaptive Web Service - Web Dynpro Java - SCN Wiki and blog . Below is the code, I used for setting header. But not sure, if it is correct or not. I am waiting for my VPN connection, to test the below code. Once done, will get back with result.

    public void doModifyInvocation(Object port) {
      // TODO Auto-generated method stub
      // Creating the SOAP Header node.
               DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
               docBuilderFactory.setNamespaceAware(true);
               DocumentBuilder docBuilder;
               try {
                 docBuilder = docBuilderFactory.newDocumentBuilder();
               } catch (ParserConfigurationException e) {
                 // TODO Log the Exception
                 return;
               }
                Document soapDoc = docBuilder.newDocument();
                 Element soapEnv = soapDoc.createElementNS("http://schemas.xmlsoap.org/soap/envelop","soapenv:Envelope");
                 Element soapHeader = soapDoc.createElement("soapenv:Header");
                 Element webAuthHeader = soapDoc.createElement("web:AuthHeader");
                 Element webUser = soapDoc.createElement("web:User");
                 webUser.setTextContent("User_Code from FS");
                 
                 Element webPwd = soapDoc.createElement("web:Pwd");
                 webPwd.setTextContent("Pwd_code from FS");
                 
                 webAuthHeader.appendChild(webUser);
                 webAuthHeader.appendChild(webPwd);
                 
                 soapHeader.appendChild(webAuthHeader);
                 soapEnv.appendChild(soapHeader);
                 
    //            soapDoc.appendChild(soapEnv);
                 
      SOAPHeaderInterface headerInterface = SOAPHeaderIFactory.getInterface(port);
    
      // Adds the output SOAP Header to the next proxy request.
      try {
      headerInterface.setOutputHeader(null, soapEnv);
    // headerInterface.setOutputHeader(null, soapHeader);
      } catch (MarshalException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
              
      }
    

    Thanks,

    Amol.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      A bit late in reply, but finally I am able to set the SOAP header in Webdynpro Java. Below are the high level steps I followed:

      1) Import the webservice WSDL and create AWS model. While importing, mention the service group name, that will be used in step 5 below.

      2) In execute method of component controller write below code (consider above WSDL snippet). Note, that after creating model, proxies are not created for <SOAP:Header> part, but created only for <SOAP:Body> part of WSDL.

      Request_ChangePasswordNoOldPassword request_ChangePasswordNoOldPassword = wdContext.nodeRequest_ChangePasswordNoOldPassword().createAndAddRequest_ChangePasswordNoOldPasswordElement().modelObject();
      request_ChangePasswordNoOldPassword.wdSetInvocationModifier(new IWDWSInvocationModifier() {   
        
          SOAPHeaderInterface headerInterface = null;
          public void doModifyInvocation(Object port) {
          // TODO Auto-generated method stub
          // Creating the SOAP Header node.   
                   DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                   docBuilderFactory.setNamespaceAware(false);
                   DocumentBuilder docBuilder;
                   try {
                     docBuilder = docBuilderFactory.newDocumentBuilder();
                   } catch (ParserConfigurationException e) {
                     // TODO Log the Exception
                   wdComponentAPI.getMessageManager().reportException("Error in Executing webservice. Error Description is: " + e.getLocalizedMessage());
                     return;
                   }
                    
                    Document soapDoc = docBuilder.newDocument();
      
      
                     Element AuthHeader = soapDoc.createElementNS("http://www.tmwsystems.com/WebFramework/","web:AuthHeader"); //Set the URI as mentioned in WSDL            
                     Element User = soapDoc.createElement("web:User");             
                     User.setTextContent(wdContext.currentContextElement().getUser_code());//Set the user
                    
                     Element Pwd = soapDoc.createElement("web:Pwd");//               
                     Pwd.setTextContent(wdContext.currentContextElement().getPwd_code());//Set the password
                                    
                     AuthHeader.appendChild(User);
                     AuthHeader.appendChild(Pwd);
                 headerInterface = SOAPHeaderIFactory.getInterface(port);
      
      
          // Adds the output SOAP Header to the request.
          try {
          headerInterface.setOutputHeader(null, AuthHeader);   
          } catch (MarshalException e) {
          // TODO Auto-generated catch block
          wdComponentAPI.getMessageManager().reportException("Error in Executing webservice. Error Description is: " + e.getLocalizedMessage());
          e.printStackTrace();
          }
      
      
          }
        
          @Override
          public void doModifyAfterInvocation() {
          // TODO Auto-generated method stub
      
      
        
          }
          });
      
      

      3) Deploy the webdynpro DC .

      4) Create a provider system. Navigate to NWA->SOA->Technical Configurations->System Connections and click on ‘New’.

      5) Create Service Group.Before creating service group, the webdynpro application needs to be deployed on the server.

      Go to NWA-> SOA->Application and Scenario Communication->Application Communication

      Select the application name (Webdynpro Project) -> Select Service group and click on Edit and select above created Provider system & Save.

      Thanks,

      Amol.

  • Posted on Oct 20, 2014 at 12:46 PM

    hi anmol ,

    go through this

    http://help.sap.com/saphelp_nw73/helpdata/en/fa/9b078eacf24887a8af46c45d09a7fb/content.htm

    here in 7.4 you have to create service groups and and configure a connection ,

    i.e http://host:port/nwa/ --> the choose SOA -->system connections -- choose new then follow the above link which clearly expalins step wise and here you can define the username and password

    Regards

    Govardan

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Govardan,

      I tried maintaining user-id and password there, but it is not working as the header tags are customized one.

      ..Amol

  • author's profile photo Former Member
    Former Member
    Posted on Oct 20, 2014 at 11:02 PM

    hi Amol,

    you are looking into something like this:

    MDL[ModelInstance] modelInstance = getModelInstance(MODEL_INSTANCE_KEY);
    Request_[Operation] request_Operation = new Request_[Operation](modelInstance);
    ....
    request_Operation.wdSetInvocationModifier(
         new IWDWSInvocationModifier() {
    
              public void doModifyInvocation(Object port) {
                   SOAPHeaderInterface soapHeaderInterface = SOAPHeaderIFactory.getInterface(port);
                   soapHeaderInterface.setOutputHeader([QName], [AuthHeader]);
              }
    
              public void doModifyAfterInvocation() {
              }
    
    } );

    I found something in SDN, this may help you too.. http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/900bbf94-a7a8-2910-e298-a651b4706c1e?QuickLink=index&overridelayout=true&12124692685423

    Hope this info helps you somehow,

    D.

    Add comment
    10|10000 characters needed characters exceeded