Custom Adapter Module in SAP PI - SAP Blogs
Custom Adapter Module in SAP PI - SAP Blogs
Personal Insights
Overview
In a business scenario, a group of users send attachments to one mail-id. They can send multiple attachments
per mail. So, here, we need to nd out a way to extract all attachments with their original names from a mail and
store it in some folder.
For storing attachments to speci c sap application directory, we use Receiver-File Adapter along with a custom
java adapter module.
Note: Receiver-File-Adapter can store only 1st attachment, so, we create a custom adapter module for this
channel which will store multiple attachments to folder.
Pre-requisites:
Pre-requisites to create a custom java adapter module form SAP PI 7.11 is as below:
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 1/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
Include above jar les in the library classpath of SAP Netweaver Developer Studio 7.1 via below steps:
Navigate to Windows → Preference → expand “Java” → select “Build Path” → select “Classpath
Variables” → Click “New” to create a “New Variable Entry” → input “Name” as “PI_AF_LIBS” → select
“Path” (above jar location)
NwceTool
This will help to convert EAR les to SDA les
It uses JRE 1.6
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 2/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
Below java adapter module code is been written to get SAP-Directory path with external module input
variable ‘SapDirPath’ and save the atatchments with original leName into the path:
/**
*
*/
package com.sap.adaptermodule;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.TimedObject;
import javax.ejb.Timer;
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.Payload;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
/**
* @author User
*
*/
public class MAttachToDirBean implements SessionBean, TimedObject {
/* (non-Javadoc)
* @see javax.ejb.SessionBean#ejbActivate()
*/
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
/* (non-Javadoc)
* @see javax.ejb.SessionBean#ejbPassivate()
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 3/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
*/
Follow RSS feed Like
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
/* (non-Javadoc)
* @see javax.ejb.SessionBean#ejbRemove()
*/
public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub
/* (non-Javadoc)
* @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
*/
public void setSessionContext(SessionContext arg0) throws EJBException,
RemoteException {
// TODO Auto-generated method stub
/* (non-Javadoc)
* @see javax.ejb.TimedObject#ejbTimeout(javax.ejb.Timer)
*/
public void ejbTimeout(Timer arg0) {
// TODO Auto-generated method stub
try
{
obj = inputModuleData.getPrincipalData();
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 4/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 5/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 6/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 7/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 8/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
Click “Finish”
Please note, 2 projects will be created, an EJB project (MAttachToDir_EJB) and an EAR project
(MAttachToDir_EAR).
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 9/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 10/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
6. Assign user-module interfaces: Use the following values to assign to the parameters in the screen below:
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 11/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 12/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
When the “ejb-jar.xml” is displayed on the right panel, select the “Enterprise Beans” tab at the bottom.
Expand “Session Beans” and select “MAttachToDir”.
All the local and remote interfaces should be displayed.
The “Service endpoint” should be empty. If not, then go to “Source” and edit the tag “<service-
endpoint>”with blank value
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 13/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 14/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
9. Include external libraries in the EJB project so the java class can be compiled
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 15/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 16/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 17/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
Click on “Extend”.
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 18/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 19/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
11. Delete the package containing the Local and Remote interfaces from the build
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 20/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
Delete all the folders created under build → classes → com → sap -> aii
Follow RSS feed Like
Do not delete ” build → classes → com → sap -> adaptermodule”
12. Con gure the EAR project: The EAR le contains the following:
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 21/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 22/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
Right Click on EAR project -> Export -> SAP EAR File
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 23/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 24/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
14. Custom java adapter module has been created as EAR le “MAttachToDir_EAR.ear “
From above steps, java adapter module EAR le “MAttachToDir_SDA.ear” gets created.
To use this module in SAP-PI, we need to convert it in to SDA le format with “.sda” extension.
Pr-requisites for EAR to SDA le conversion:
Java Jre1.6 Check Java Jre1.6 Directory (here C:\Program Files\Java\jre1.6.0_01)
nwcetool Copy nwcetool folder in to C:\NWDS
Any local Windows system
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 25/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 26/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
4. Post Deployment, SDA le gets extracted in below SAP-PI’s folder path, having folder name as of JNDI-
Name ‘MAttachToDir‘
/usr/sap/<systemID>/<instance>/j2ee/cluster/apps/sap.com/MAttachToDir/
Alert Moderator
Assigned tags
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 27/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
6 Comments
Manoj K
But i believe this module will work only in case NFS isn’t it? Because i can see in your module
implementation you are just building FilePath to the local server. In case you need to drop it to external server
you need to implement FTP/SFTP connection to the server in the module code itself.
Like(0)
Dear Manoj,
Good Morning …
Yes, This module is working for SAP-Ecc-directory which is mapped/accessible to SAP-PI server.
Dilip.
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 28/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
Like(0)
Follow RSS feed Like
Evgeniy Kolmakov
Hi Dilip!
And why not to use SFTP adapter for that at receiver’s side? As I could see from your code, you consider your PI
OS to be the Unix OS (by the way, avoid using hardcoded le path separators in any code, your module will raise
exception on Windows OS, for example. Use “File.separator” instead).
Regards, Evgeniy.
Like(0)
Dear Evgeniy Kolmakov
Thanks for suggestion on lePath check, in module code we can give a condition if directory exists then
proceed or throw module exception with message as “Directory not found”.
We can use SFTP adapter at receiver side as this adapter supports multiple attachments, here no need of
any custom module.
But in my case, I have to work with SAP-ECC-Directory which is accessible from SAP-PI using lePath
reference only, so I have to use File-Receiver Channel and module is to make it compatible to handle multiple
attachments.
Dilip.
Like(0)
Evgeniy Kolmakov
Hi Dilip!
1. I just wanted to point on the way to create more exible, reusable and robust applications.
2. Since you have access to your resource using java IO, this means that it is mounted to PI’s le system. And
most of Unix/Linux systems provide access to its le system using SSH by default. So you could use SFTP
adapter to access PI le system without any additional SFTP server software required.
Regards, Evgeniy.
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 29/30
12/23/2020 Custom Adapter Module in SAP PI | SAP Blogs
Like(0)
Follow RSS feed Like
Dear Evgeniy Kolmakov
Dilip.
Like(0)
Find us on
Newsletter Support
https://blogs.sap.com/2017/10/30/sap-pi-module-to-extract-multiple-attachments-to-directory/?utm_source=dlvr.it&utm_medium=facebook 30/30