• Home
  • Services
  • SAP® E-Sourcing and CLM
  • Portfolio
  • Company
  • Contact Us
  •  

    Calling a Web Service from a Script

    Hi All - I know it has been quite a while since I have posted…sorry for that…work has been keeping me busy :-)

    Recently, I received a question from an E-Sourcing professional asking about calling a Web Service from an E-Sourcing script. This is absolutely something that can be achieved quite easily; and, in fact, it is something that I have done for customers in the past.

    As with any use of Web Services, however, you should carefully consider the security issues that may come up. How, for example, will the Web Service server validate that the Web Service client (E-Sourcing) is properly authenticated? Will password information be included in the web service call? You will find that it is very easy to make a web service call, but I would encourage you to carefully consider security before implementing a productive solution.

    Web service calls can be made using raw Java web service APIs from the open source Axis library which is included with E-Sourcing; this approach is slightly more difficult to code, but very dynamic. Web service calls can also be made using proxies. In one solution that I worked on, we generated java proxies for the web service, compiled those proxies into a Jar file, and included that jar file as a custom jar in E-Sourcing. Let me provide a few more details on each of these approaches.

    Using raw java web service APIs that are part of the Service and Call classes, I prototyped a web service call to Googles sample spell checker web service. Here is the code:

    import org.apache.axis.client.Call;
    import org.apache.axis.client.Service;
    import org.apache.axis.encoding.XMLType;
    import javax.xml.rpc.ParameterMode;
    import javax.xml.namespace.QName;

    String endpoint = “http://api.google.com/search/beta2“;
    Service  service = new Service();        
    Call call = (Call) service.createCall();        
    call.setTargetEndpointAddress( new java.net.URL(endpoint) );        
    call.setOperationName( “doSpellingSuggestion” );        
    call.setOperationName(new QName(”urn:GoogleSearch”, “doSpellingSuggestion”));        
    call.addParameter(”key”, XMLType.XSD_STRING, ParameterMode.IN);        
    call.addParameter(”phrase”, XMLType.XSD_STRING, ParameterMode.IN);        
    call.setReturnType( XMLType.XSD_STRING );        
    String ret = (String) call.invoke( new Object[] { “googlekey”, doc.getDocumentDescription()} );        
    doc.setDocumentDescription(ret);

    This block of code does a very simple thing…it calls the Google “doSpellingSuggestions” web service with two parameters: a key provided by Google, and a string for which the spelling suggestions should be generated. I used the current document description as my sample string for the web service and I put the results back into the document description - remember, this is just showing how you can call the web service, not doing anything really intelligent or useful from a business perspective :-)

    There is nothing special to E-Sourcing about the above code…this is really just using the Axis java classes to call a web service.

    The second approach that can be used is to generate Java proxies for the web service calls. The open source Axis library includes a tool called “wsdl2java”. Using the WSDL for the web service, you can generate Java proxies. Java classes will be generated by the tool; these Java classes will then need to be compiled and included in E-Sourcing as a custom jar. Once they are part of the E-Sourcing deployment, they can be called like any Java API. If you were to examine the generated code, you would notice that it looks a lot like the raw web service code shown above…the generated classes really just provide a simpler interface to the same functionality.

    Calling web services from a script can offer new possibilities for implementing integrations and other specialized functionality. As I said above, however, managing the security aspects is very important, so it should be done carefully and with thought.

    I hope this article was interesting and helpful.

    Good Luck.

    Comments are closed.