Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
khusal810
Active Participant
Update 27th Feb 2020 : 

The previous link to the jar was not working or did not have the necessary classes to execute the given function below.

This is why I have shared the jars in one Gdrive link - OKHTTP GDrive

________________________________________________________________________________

Hello SAP PI/PO Consultants,

In this blog post we will try to explore how can you easily trigger a web service already provided by SAP PI or a SOAP sender interface externally using JAVA (OKHTTP).

I am really fascinated by how we can develop external tools that can be used by our customers instead of them login into the system themselves and need to learn each and every functionality of SAP PI.

SAP PI provides various prepackage web services for different reasons. These can be accessed using WSNavigator of SAP PI.

Few of the web services which I have explored myself are:

Adapter Message Monitoring - used to read PI messages
IChannelAdmin - Start/Stop Channel

I will highly recommend you to read the following blogs before understanding the actual JAVA code

Adapter Message Monitoring

Channel Admin Service

Now that you have understood we have these prepackage web services that we can use.

Let me jump you to the actual purpose of the blog.

I was looking to make a JAVA based GUI tool where you can start/stop channel externally which would be useful during cutover activities on your SAP Landscape without a need for an SAP PI consultant to do it for you.

For that, I have to trigger a URL after the channels were selected by the user.

If you have read the previous blog shared, you must be well informed by now that I will have to make a SOAP call to the URL using a POST method.

Now there are many ways in which one can do this in JAVA. But I will be sharing the by far easiest of ways which one can use.

The key is to use an external JAR - OKHTTP

This jar will do the magic with most minimal steps,

Now let us check how?

below is the SOAP Invoke Call function.

The above functions take 4 parameters :

URL - URL of the SOAP web service.
REQUEST STRING - SOAP envelope used as input ( this can be easily taken from external tools like SOAPUI or POSTMAN)
USERNAME - SAP PI user having a role to execute web calls.
PASSWORD - SAP PI user password.

The rest of the code is pretty self-explanatory. I have also put comments for better understanding.

A few other things which I would like to highlight are from which library/package are each of the classes used to avoid confusion.

MediaType - okhttp3
OkHttpClient - okhttp3
Credentials - okhttp3
RequestBody - okhttp3

Request - okhttp3
Response - okhttp3
public String invokeMsgCall(String url,String requestString,String userName ,String password){

// Defining the MediaType we are going to use
MediaType SOAP = MediaType.get("text/xml");

// creating HTTP Client
okhttp3.OkHttpClient httpClient = new
okhttp3.OkHttpClient().newBuilder().connectTimeout(120,TimeUnit.
SECONDS).readTimeout(120, TimeUnit.SECONDS).build();

// creating credentials
String cred = Credentials.basic(userName, password);

// create body with headers as shown below
RequestBody body = RequestBody.create(SOAP, requestString.getBytes());
Request req = new Request.Builder().url(url)
.header("Authorization",cred)
.header("Content-Type","application/xml")
.header("Soap-Action","urn:AdapterMessageMonitoringWsd/getMessages")
.post(body)
.build();
try {
// execute the call
Response res=httpClient.newCall(req).execute();
// return string reponse
ResponseBody resbody = res.body();
String resString = new String(resbody.bytes());
return resString;

} catch (Exception e) {
// TODO Auto-generated catch block
// Handle Expection as per your needs
}
return "NA";


}

Want to explore more on OKHTTP-

Click Here

Additional Tip:

You might be tempted to use these 3rd party jars in UDFs also.

Below blog will help you how to use these jars in UDFs

Add External Jars Files in UDF

This way you can trigger in fact any Webservice using OKHTTP3 Jar without having to worry much about SOAP Header and SOAP Envelope. This is as good as testing your Webservice using SOAPUI or POSTMAN.

Note: This is the best-known method according to my findings. If you find any better method please let me know in the comment section below. I would happily like to explore that too.
7 Comments
Labels in this area