cancel
Showing results for 
Search instead for 
Did you mean: 

Custom file adapter module

Former Member
0 Kudos

Hi All,

My requirement is to get the hostname,port,username details  as a receiver module parameters.Filename and directory details should come from the source payload..Adapter module should place the file in the remote server location which is windows SMB fileshare.I am new to ebj.Please help to achieve this requirement.

Regards,

Karthiga

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member184720
Active Contributor
0 Kudos

Is this in addition to the actual receiver?

if not, you can make use of the standard adapter right?

If yes, refer to below blog.

>>>Filename and directory details should come from the source payload

You can read the dynamic constants in the above module.

Former Member
0 Kudos


Hi Hareesh,

I have developed the ejb code,based on sample blog which you have provided.

Could you please check the below code whether it is correct or not.I am using PI 7.11 server and NWDS 7.2 version.

package com.sap.pi.com;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.util.Vector;

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 jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;

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.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.PublicAPIAccess;
import com.sap.engine.interfaces.messaging.api.PublicAPIAccessFactory;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditAccess;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
import com.sap.engine.interfaces.messaging.api.exception.MessagingException;


public class FileModule 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 javax.ejb.CreateException {
}

public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData)
        throws ModuleException
{
  Message msg = null;
  String hostname = null;
  String port     = null;
  String username = null;
  String password = null;
  String filename = null;
  String directory = null;
// String FileKey = null;
 
 
  try
  {
   msg = (Message)inputModuleData.getPrincipalData(); 
  
   MessageKey amk = new MessageKey(msg.getMessageId(), msg.getMessageDirection());
   PublicAPIAccess pa;
   try
   {
    pa = PublicAPIAccessFactory.getPublicAPIAccess();
   }
   catch (MessagingException me)
   {
     throw new ModuleException("Internal SAP PI 7.11 Error -> Accessing PublicAPIAccessFactory Failed!");    
   }
   AuditAccess audit = pa.getAuditAccess();              
  
   audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS,"ReadFiles: MODULE called");
   //Reading file name from message header
   MessagePropertyKey msgFileName = new MessagePropertyKey("FileName", "http://sap.com/xi/XI/System/File");
   //Reading file directory from message header
   MessagePropertyKey msgDirectory = new MessagePropertyKey("Directory", "http://sap.com/xi/XI/System/File");
  
   //Read Trigger File Name
   filename = msg.getMessageProperty(msgFileName);
   directory = msg.getMessageProperty(msgDirectory);
   audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS,"Trigger File Name -> " + filename);     
  
    
   hostname = moduleContext.getContextData("hostname");
   port = moduleContext.getContextData("port");
   username = moduleContext.getContextData("username");
   password = moduleContext.getContextData("password");
  
   // Get PI Main Payload
   Payload xiMsgPayload = msg.getDocument();
   byte[] inpbyt = xiMsgPayload.getContent();
   audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "Input file read successfully");
  
  
   String path="smb://"+hostname+directory+filename;
   NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(hostname,username, password);
   SmbFile smbFile = new SmbFile(path,auth);
   SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
   smbfos.write(inpbyt);
  
       audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"File placed successfully");
       xiMsgPayload.setContent(inpbyt);
      
       inputModuleData.setPrincipalData(msg);
       audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Principle data is set successfully.");
  
  }
         
 

  catch (Exception e)
  {     
   Message currentMsg = (Message) inputModuleData.getPrincipalData();
   MessageKey mk = new MessageKey(currentMsg.getMessageId(), currentMsg.getMessageDirection());
   ModuleException me = new ModuleException(new MessagingException(mk, e.getMessage()));           
   throw me;
  }
  return inputModuleData;
}
}


Regards,

Karthiga