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: 
shashank31191
Explorer
13,547
Hi Folks, currently I'm working as Cloud Platform Integration Consultant. I have started exploring scripts in Cloud Platform Integration tool and is trying to achieve things which are not possible using palette provided by SAP. This blog will be talking about one of these scenario, where-in requirement is to poll a File from SFTP server in middle in Integration flow.

Problem scenario:

SFTP adapter offered by SAP Cloud platform Integration only allow polling a file from SFTP server using an SFTP sender adapter.

There could be some requirement, wherein business case is to poll a file in middle of an iflow.

 

Possible solution:

We can use a groovy Script in order to create the connection to SFTP server and then poll the file from there.

This would need :

  1. The SFTP credentials which are deployed into our tenant.

  2. Open Source library, using which we can connect to SFTP server.


 

Accessing SFTP credential deployed in our tenant in a groovy script.

 

To access the credential, SecureStoreService API (Public API to access the deployed user credentials) can be used.

Imports:

com.sap.it.api.securestore.SecureStoreService;

com.sap.it.api.securestore.UserCredential;

 

Method Signature:

public UserCredential getUserCredential(String alias)

 

Return Type:

UserCredential

 

Code Snippet:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import com.sap.it.api.securestore.SecureStoreService;
import com.sap.it.api.securestore.UserCredential;
import com.sap.it.api.ITApiFactory;

def Message processData(Message message) {

def secureStorageService = ITApiFactory.getApi(SecureStoreService.class, null);
def cred = secureStorageService.getUserCredential(“<Name of SFTP Credential deployed in tenant>”);

if (cred == null){
// error handling
}

//cerd.getUserName() and cred.getPassword method will give the user name and password as String.

return message;
}

 

 

Accessing SFTP server to poll file.

For fetching the file from SFTP server we can use JSch API (Jsch is widely used to connect to SFTP server).

 

Download JSch library from here

 

JSch class serves as a central configuration point, and as a factory for Session objects configured with these settings.

  • Use getSession() to start a new Session.

  • Use one of the addIdentity() methods for public-key authentication.

  • Use setKnownHosts() to enable checking of host keys.

  • See setConfig() for a list of configuration options.


Download the jar from the above mentioned link and upload it in your Integration project workspace.

In case eclipse is the mode of development, then add the jar file after creating package (src.main.resources.lib) or in Web GUI, add the jar file as an Archive in Resources section of the Integration iflow.

 

Below is the sample code snippet which can be used to poll a file from SFTP server. (You can always modify it as per your need)
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.sap.it.api.securestore.SecureStoreService;
import com.sap.it.api.securestore.UserCredential;
import com.sap.it.api.ITApiFactory;


def Message processData(Message message) {

String userName = “”;
String password = “”;
String hostName = “<Enter the hostname/IP address of SFTP server>”;
String filePath = “<Enter the path of file to be polled>”;
String finalString = "";

def secureStorageService = ITApiFactory.getApi(SecureStoreService.class, null);
def cred = secureStorageService.getUserCredential(“<Name of SFTP Credential deployed in tenant>”);
if (cred == null){
// error handling
}
userName = cerd.getUserName();
password = cred.getPassword();

JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(userName, hostName, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();

Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;

InputStream stream = sftpChannel.get(filePath);
try {
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
String line;
while ((line = br.readLine()) != null) {
finalString = finalString + line; // You can have your own logic over here
}
} catch (IOException io) {
finalString = io.getMessage();
} catch (Exception e) {
finalString = e.getMessage();
}

sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
//Exception Handling
} catch (SftpException ee) {
//Exception Handling
}

message.setBody(finalString);

return message;
}

This piece of code will read the mentioned file from SFTP server and will save it in message body of the exchange.

Below is the brief pictorial representation of the integration flow that depicts implementation of the given approach.

 



 

Hope this helps..!! 🙂

 

Using the above code, you can poll a file from SFTP server without using a SFTP adapter.

I'll be writing more on how we can make use of script in Cloud Platform Integration.
32 Comments
Labels in this area