on 03-29-2013 7:39 AM
Hi Experts,
We have a requirement for a SFTP to File scenario. In the SFTP we are required to pick data files based on the presence of Trigger File i.e. once the trigger file is present the data files related to it will be picked up first and then the trigger file. For this we developed a Java Module. After deployment we are getting the below error.
Failed to deliver XI message. Reason: Value with name ftp.host not available (Software version: 3.0.26)
Even though there is nothing defined w.r.t. FTP in the module we are getting the error.
Can anyone help us some suggestion ?
Hi Shaibayan,
The error seems to be in the ftp details. As it is evident from the error that is shown by you that the particular ftp host is not available.
please check the details and also let us know the details regarding your developed java module
Regards,
Amarnath
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Amar,
There is no ftp in the sender. It is an SFTP. Also in our program we have used all SFTP parameters. Please find the code below.
Regards,
Shaibayan
/**
*
*/
package com.xi.module;
import javax.ejb.CreateException;
import javax.ejb.Local;
import javax.ejb.LocalHome;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import com.sap.aii.af.lib.mp.module.Module;
import com.sap.aii.af.lib.mp.module.ModuleContext;
import com.sap.aii.af.lib.mp.module.ModuleData;
import com.sap.aii.af.lib.mp.module.ModuleException;
import com.sap.aii.af.service.auditlog.Audit;
import com.sap.engine.interfaces.messaging.api.Message;
import com.sap.engine.interfaces.messaging.api.MessageKey;
import com.sap.engine.interfaces.messaging.api.MessagePropertyKey;
import com.sap.engine.interfaces.messaging.api.Payload;
import com.sap.engine.interfaces.messaging.api.TextPayload;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
import com.sap.engine.interfaces.messaging.api.exception.MessagingException;
import java.io.IOException;
/**
*
* This module which can be configured in sender SFTP channel is used to pick multiple files in sequence.
* The number of files is based on the parameter "fileCount" configured in channel.
* The sequence is based on the parameter "payloadFileName0,1,2...." configured in channel.
* After picking all files,the files will be moved to archive directory.
*
* @author
*
*/
@Stateless(name = "PickFileInOrderBean")
@LocalHome(value = com.sap.aii.af.lib.mp.module.ModuleLocalHome.class)
@Local(com.sap.aii.af.lib.mp.module.ModuleLocal.class)
public class PickFileInOrderBean implements SessionBean,Module {
private SessionContext myContext;
public void ejbRemove() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void setSessionContext(SessionContext context) {
myContext = context;
}
public void ejbCreate() throws CreateException {
}
public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData)
throws ModuleException {
Message msg = null;
Payload mainPayload = null;
String srcDir = null;
String sftpHost = null;
int sftpPort = 22;
String usr = null;
String pwd = null;
String triggerFileName = null;
String suffixFileName = null;
int fileCount = 0;
String payloadFileName[] = null;
String arcDir = null;
SFTPUtil sftp = new SFTPUtil();
try {
msg = (Message)inputModuleData.getPrincipalData();
mainPayload = msg.getMainPayload();
MessageKey amk = new MessageKey(msg.getMessageId(), msg.getMessageDirection());
Audit.flushAuditLogEntries(amk);
Audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS,"PickFileInOrderBean: Module called.");
//Read Main Payload File Name and Modify the mainPayload File Name
MessagePropertyKey msgFileName = new MessagePropertyKey("FileName","http://sap.com/xi/XI/System/File");
triggerFileName = msg.getMessageProperty(msgFileName);
mainPayload.setName(triggerFileName);
msg.setMainPayload(mainPayload);
/**
* to avoid using hard code, all the variable values are given by Customer Parameters in the Sender CC Module tag.
* The following parameters should be given in module configuration of the Sender CC :
* "archiveDirectory" ---- After the payload file is picked, both of the trigger and payload files need to be moved to archive folder.
* "triggerFileName" ---- The trigger File Name.
* "fileCount" ---- The number of Files.
* "payloadFileName0,1,2..." ---- The name of payload File Name.
*/
//Get the module configuration details from channel.
srcDir = moduleContext.getContextData("sourceDir");
arcDir = moduleContext.getContextData("archiveDir");
sftpHost = moduleContext.getContextData("host_sftp");
sftpPort = Integer.parseInt(moduleContext.getContextData("port_sftp"));
usr = moduleContext.getContextData("usr_sftp");
pwd = moduleContext.getContextData("pwd_sftp");
suffixFileName = moduleContext.getContextData("suffixFileName");
fileCount = Integer.parseInt(moduleContext.getContextData("fileCount"));
payloadFileName = new String[fileCount];
for(int i = 0; i < fileCount; i++){
payloadFileName[i] = moduleContext.getContextData("payloadFileName." + i);
}
//Get the number which is the key linking the set of files as a key value.
int index = triggerFileName.indexOf(suffixFileName);
String keyValue = triggerFileName.substring(0, index);
try{
sftp.init(sftpHost, sftpPort, usr, pwd);
Audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS, "Connected to " + sftpHost + " Successful!");
//check if exists the payload files with the same key value.
//If exist, then read the file to message as a attachment.
//If not exist, then give the audit message in channel and move to next file check.
for(int i = 0; i < fileCount; i++){
//the complete payload file name which contains the number which is the key linking the set of files.
payloadFileName[i] = keyValue + payloadFileName[i];
try{
StringBuffer content = sftp.readFile(srcDir + payloadFileName);
Audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS, "File check " + payloadFileName[i] + ": The File is EXIST.");
if(content != null){
// create a new payload
TextPayload attachment = msg.createTextPayload();
// provide attributes and content for the new payload
attachment.setName(payloadFileName[i]);
attachment.setContentType("text/plain");
attachment.setText(content.toString());
// add the new payload as attachment to the message
msg.addAttachment(attachment);
Audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS,"Add the attachment " + payloadFileName[i] + " Successful.");
}else{
sftp.disconnect();
Audit.addAuditLogEntry(amk,AuditLogStatus.ERROR,"Add the attachment " + payloadFileName[i] + " Failed - Aborting...");
throw new ModuleException("Add the attachment " + payloadFileName[i] + " Failed - Aborting...");
}
}catch(NoSuchFileException e){
Audit.addAuditLogEntry(amk,AuditLogStatus.WARNING, "File check " + payloadFileName[i] + ": The File is NOT EXIST!");
}catch(SftpException e){
sftp.disconnect();
Audit.addAuditLogEntry(amk,AuditLogStatus.ERROR, e.toString());
throw new ModuleException(e.getMessage());
}catch(IOException e){
sftp.disconnect();
Audit.addAuditLogEntry(amk,AuditLogStatus.ERROR, e.toString());
throw new ModuleException(e.getMessage());
}
}
//move Payload File to archive folder
for(int i = 0; i < fileCount; i++){
try{
sftp.renameFile(srcDir + payloadFileName[i], arcDir + payloadFileName[i]);
}catch(NoSuchFileException e){
Audit.addAuditLogEntry(amk,AuditLogStatus.WARNING, "File check " + payloadFileName[i] + ": The File is NOT EXIST!");
}catch(SftpException e){
sftp.disconnect();
Audit.addAuditLogEntry(amk,AuditLogStatus.ERROR, e.toString());
throw new ModuleException(e.getMessage());
}
}
sftp.disconnect();
}catch(JSchException e){
sftp.disconnect();
Audit.addAuditLogEntry(amk,AuditLogStatus.ERROR, "Connected to " + sftpHost + " Failed - Aborting...");
Audit.addAuditLogEntry(amk,AuditLogStatus.ERROR, e.getMessage());
throw new ModuleException("Connected to " + sftpHost + " Failed - Aborting...");
}
}catch(Exception e){
Message currentMsg = (Message) inputModuleData.getPrincipalData();
MessageKey mk = new MessageKey(currentMsg.getMessageId(), msg.getMessageDirection());
ModuleException me = new ModuleException(new MessagingException(mk, e.getMessage()));
Audit.addAuditLogEntry(mk,AuditLogStatus.ERROR,"PickFileInOrderBean: Module called failed!");
throw me;
}
inputModuleData.setPrincipalData(msg);
return inputModuleData;
}
}
SFTP UTIL FILE:
package com.xi.module;
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;
public class SFTPUtil {
JSch jsch = new JSch();
Session session = null;
Channel channel = null;
ChannelSftp sftpChannel = null;
public void init(String host,int port,String usr,String pwd) throws JSchException{
session = jsch.getSession(usr,host,port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(pwd);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
sftpChannel = (ChannelSftp) channel;
}
public void disconnect(){
if(sftpChannel!=null){
sftpChannel.exit();
}
//channel.disconnect();
if(session!=null){
session.disconnect();
}
}
public InputStream getFileInputStream(String path) throws SftpException,NoSuchFileException{
InputStream is = null;
try {
is = sftpChannel.get(path);
} catch (SftpException e) {
if(e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE){
throw new NoSuchFileException();
}else{
throw new SftpException(e.id,e.getMessage(),e);
}
}
return is;
}
public StringBuffer readFile(String path) throws SftpException,NoSuchFileException,IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(getFileInputStream(path)));
StringBuffer content = null;
try {
String line = null;
while((line = br.readLine()) != null){
content.append(line);
content.append("\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
public void renameFile(String oldPath, String newPath) throws SftpException,NoSuchFileException{
try {
sftpChannel.rename(oldPath, newPath);
} catch (SftpException e) {
if(e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE){
throw new NoSuchFileException();
}else{
throw new SftpException(e.id,e.getMessage(),e);
}
}
}
}
EXCEPTION:
package com.xi.module;
public class NoSuchFileException extends RuntimeException{
public NoSuchFileException(){
super(); }
}
Hi
Please check if you are using proper SFTP details.Are you able to connect from WINSCP?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Seems that the host is not available. Are you able to reach the host from your PI server? Try pinging it from the PI server.
Regards
Abhishek
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.