Skip to Content

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.

To report this post you need to login first.

4 Comments

You must be Logged on to comment or reply to a post.

  1. Gayathri Narayana

    Hi Shashank,
    Currently there is no official way to poll from sftp in the middle of a flow.
    But to add external libraries in the flow may cause trouble when we do an open source update and the integration flows might stop working. So we recommend customers to use standard adapters provided by SAP.

    Regards,
    Gayathri

     

    (1) 
    1. Eng Swee Yeoh

      Hi Gayathri

       

      Can you elaborate further on why external libraries are discouraged?  And if they are discouraged, why provide such a capability in the first place?

       

      Per my understanding, CPI uses the OSGi framework, and each integration flow is deployed as an OSGi bundle. The benefit of using OSGi is that it allows different bundles to work with separate versions of a library. How then would an open source update on the core CPI libraries affect the externally added library?

       

      Regards

      Eng Swee

      (2) 
      1. Mandy Krimmel

         

        Hello,

        I agree that the probability is high that the scenario is not affected, but you can never be sure of the side effects during a open source update on our side.

        The scenario you describe may still run after OS update, but it is not officially supported and not tested in SAP regression tests, so SAP does not support this in case you have problems after OS update.

        Best regards,

        Mandy

        (0) 
        1. Eng Swee Yeoh

          Hi Mandy

           

          Thanks for your response. However “can never be sure” sounds a bit too vague, without any facts to back it up.

           

          The capability to add external libraries has been mentioned since the early days of HCI in the following blog – Blog 9: Scripting in Integration Flows. I’m surprised to hear that now SAP is saying it is discouraged, and not supported.

           

          SAP has been embracing open source/standards in the recent years, and even so many core parts of CPI are based on open source (Camel, OSGi, Groovy). IMHO, to discourage open source usage in custom CPI development seems counter-intuitive.

           

          Regards

          Eng Swee

          (4) 

Leave a Reply