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;
}
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;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
8 | |
6 | |
5 | |
4 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 |