cancel
Showing results for 
Search instead for 
Did you mean: 

Call a Web Service from within an e-Sourcing script

Faaiez
Advisor
Advisor
0 Kudos

Hi Guys

I would like to know wether anyone has successfully been able to call a Web Service from within an

e-Sourcing script? If you have, can you please share your experience and code?

Thank You

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Faaiez -

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.

You can see this information and other E-Sourcing information at my blog at: http://www.sunshinesys.com/

Rob

Answers (4)

Answers (4)

Former Member
0 Kudos

Hi Rob,

We have a requirement to call Rest WebServices from Sourcing(Version-10). I developed a custom Rest Client in eclipse and created it has jar and basis deployed to sourcing using the wizard. When I try to call the custom jar from scripting it's not recognizing the RestClient.


Below is my Custom Rest Client Class that I created in eclipse and converted into jar RestClientCLM.jar


package com.uprr.ws;

import java.nio.charset.Charset;

import org.apache.commons.codec.binary.Base64;

import org.springframework.http.HttpEntity;

import org.springframework.http.HttpHeaders;

import org.springframework.http.HttpMethod;

import org.springframework.http.ResponseEntity;

import org.springframework.web.client.RestTemplate;

public class RestClientCLM {

  public  void main() {

  RestTemplate restTemplate = new RestTemplate();

  HttpHeaders httpHeaders = new HttpHeaders(){

       {

          String auth = "userid" + ":" + "password";

          byte[] encodedAuth = Base64.encodeBase64(

             auth.getBytes(Charset.forName("US-ASCII")) );

          String authHeader = "Basic " + new String( encodedAuth );

          set( "Authorization", authHeader );

       }

  };

  String url = "Rest Url";

  // ResponseEntity<String> response = restTemplate.getForEntity(url , String.class);

  ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<String>(httpHeaders),  String.class);

  System.out.println(response.getBody());

  }

}

I created my eSourcing script like below.

import com.uprr.ws.*;

RestClientCLM rt = new RestClientCLM();

rt.main();

Error:

Sourced file: inline evaluation of: ``import com.uprr.ws.RestClientCLM Sourced file: inline evaluation of: ``import com.uprr.ws.RestClientCLM RestClientCLM rt = new RestClientCLM(); rt . . . '' : Typed variable declaration : Class: RestClientCLM not found in namespace : at Line: 3 : in file: inline evaluation of: ``import com.uprr.ws.RestClientCLM; RestClientCLM rt = new RestClientCLM(); rt . . . '' : RestClientCLM || at bsh.BSHAmbiguousName.toClass(Unknown Source)| at bsh.BSHType.getType(Unknown Source)| at bsh.BSHTypedVariableDeclaration.eval(Unknown Source)| at bsh.Interpreter.eval(Unknown Source)| at bsh.Interpreter.eval(Unknown Source)| at bsh.Interpreter.eval(Unknown Source)| at com.sap.odp.comp.scripting.ScriptEnvironment.execute(ScriptEnvironment.java:185)| at com.sap.odp.doccommon.scripting.util.ScriptUtil.executeCallableScript(ScriptUtil.java:953)| at com.sap.odp.doccommon.scripting.daemon.ScriptExecutionDaemon.processHook(ScriptExecutionDaemon.java:135)| at com.sap.odp.comp.daemon.AbsEventDaemon.process(AbsEventDaemon.java:160)| at com.sap.odp.comp.daemon.ResponsibleEventDaemon.runHook(ResponsibleEventDaemon.java:140)| at com.sap.odp.comp.daemon.ResponsibleDaemonBase.run(ResponsibleDaemonBase.java:301)| at java.lang.Thread.run(Thread.java:763)|;Sourced file: inline evaluation of: ``import com.uprr.ws.RestClientCLM; RestClientCLM rt = new RestClientCLM(); rt . . . '' : Typed variable declaration : Class: RestClientCLM not found in namespace|Sourced file: inline evaluation of: ``import com.uprr.ws.RestClientCLM;

Please provide your inputs if I miss any thing.

Thanks,

Venu.

Former Member
0 Kudos

Hi Rob,

Thanks so much for your response!

Where can I find the axis jars and wsdl2java tool that is provided with e-sourcing?.. Is it like provided in the installation CD or something..

Thanks again!

Lisha

Former Member
0 Kudos

Hi Lisha -

From what I can tell, you have E-Sourcing installed on-premise, which means that you have the E-Sourcing and related third party JAR files on a server. Wsdl2java is a Java class that is included in the axis JARs, probably installed in a directory such as fci\lib.

To execute wsdl2java, you would run java at the command line, something like:

java -cp <axis jars> org.apache.axis.wsdl.WSDL2Java <parameters> <wsdl file>

Hope this helps.

Rob

Former Member
0 Kudos

Hi Rob, We have a similar requirement where SAP Sourcing 9.0 needs to call a web service from a script.

There is a RFC Function module in ECC that we are planning to create a web service with and activate in the soamanager transaction.

This will generate the WSDL URL for that FM.

I am assuming that we'll still have to use wsdl2java tool (in Apache) and create the custom jar file and deploy it to SAP Sourcing before the script can call this web service/FM?

I wanted to check on the feasibility of this approach.

1.Would WDSL file created from ABAP RFC FMconvert over to java using wsdl2java without any issue

2. How can the authentication be done by the script in case i configure the web service to have a user id & password based authentication?

Your comments on this will be very useful for us.

Regards,

Srivatsan

Former Member
0 Kudos

Hi Rob,

In addition to my previous query above I just had another one :

I had extracted the class files from web service using wsdl2java of axis2-1.5.1.

Will that be an issue since SAP ESO system is on axis 1.3?..

Thanks again!!

Former Member
0 Kudos

Hi Lisha - I suspect that your issue is related to the Axis version. The best thing would be to use the Axis JARs that are delivered with E-Sourcing; they contain the wsdl2java and will ensure that the generated code is compatible. In every case where I have made web service calls, I have used the wsdl2java provided with the E-Sourcing axis jars.

Everything else you have done looks to be correct: generate the Java classes using wsdl2java, compile those classes into a JAR, include that JAR as a custom JAR file in E-Sourcing, import the classes into your script, and make the calls from the script. This should work and has always worked for me.

If, after using the E-Sourcing axis JARs, you still have troubles, I suggest you put some debug code in the script and compiled code to see where the code goes. It will probably take that level of information to debug this issue further.

I hope this helps.

Rob

Former Member
0 Kudos

Hi Rob,

I am trying to do a similar thing, but am stuck up in the ESO scripting. It would be great if you could give a pointer.

I have created a sample java web service that has a method concatenating 2 strings, I extracted the wsdl classes from this using wsdl2java tool for axis2 1.5.

On extraction I got 2 java files, <web service name>CallbackHandler.java and <web service name>Stub.java.

For testing purpose, I imported these into a java project in NWDS(eclipse), created a java class, had a method inside that call the webservice classes to set request and get response. On running the main class I was able to see the desired output on console(concatenated string).

I exported the java project as a jar file, and deployed this jar on the ESO system.

In esourcing script, I added the import statement that points to the main class, instantiated the class and accessed its method that in turn calls the wsdl classes to get and set values.

On trying to execute this script on doc save, I get no errors, but the document just doesnt get saved!

I also tried creating an IAPI scheduler task for this class, same thing happens there, it just doesnt allow me to save the scheduler task!

I checked all the logs but it dint help much.

Please advise!