Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member188961
Participant

In this Document I would like to share how to create a password protected PDF file in SAP PI using Custom Adapter Module.

Requirement: Pick the sample file from source ftp and pass the file as WinZip with password protected PDF file to target FTP through SAP PI. The Password protected PDF file needs to be zipped and sent to customer location.


To achieve this requirement I have used Custom adapter module to create password protected pdf file.

Pre-requisite:

IText library jar –Creating PDF

BouncyCastle – Encrypting the PDF( iText internally uses bouncycastle.org library to encrypt the PDF files).

Jars Used:

  • itext-1.4.8.jar
  • bcprov-jdk15-146.jar

Initially when I started, I was facing the problem with the PDF file being written to NFS with the below statement.

  1. PdfWriter.getInstance(document, new FileOutputStream(

  "/Interface/TEST/AAE/Sucess.pdf"));


Here pdf was getting created but the same file was not getting zipped by payloadZipbean. The trick was to send the output as byte stream and pass it to the next module instead of writing it as a file

Instead of writing the stream to the FileOutputStream I passed the output to ByteArrayOutputStream.

Module code for creating Password protected PDF file.

package com.sap.adaptermodule;

import java.io.ByteArrayOutputStream;
import java.util.Date;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import com.lowagie.text.pdf.*; 
import com.lowagie.text.*;
import com.sap.engine.interfaces.messaging.api.*;
import com.sap.aii.af.lib.mp.module.*;
import com.sap.engine.interfaces.messaging.api.Message;
import com.sap.engine.interfaces.messaging.api.MessageKey;
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;
/**
* @author Srikanth Kakani
*
*/
public class PDFwithPasswordBean implements SessionBean, Module {
      public static final String VERSION_ID = "$Id://tc/aii/30_REL/src/_adapters/_sample/java/user/module/PDFwithPassword_EJBBean.java#1 $";

      static final long serialVersionUID = 7435850550539048633L;

      public void ejbRemove() {
      }

      public void ejbActivate() {
      }

      public void ejbPassivate() {
      }

      public void setSessionContext(SessionContext context) {

      }
      public void ejbCreate() throws javax.ejb.CreateException {
      }

      public ModuleData process(ModuleContext  moduleContext,
                 ModuleData inputModuleData) throws ModuleException {
AuditAccess audit = null;
Object obj = null;
Message msg = null;
MessageKey key = null;
                 Payload pload = null;
String Ownerpassword; // Password required to open PDF
String Userpassword;    // Password for encrypting the PDF file

            try {
obj = inputModuleData.getPrincipalData();
msg = (Message) obj;
                 
key = new MessageKey(msg.getMessageId(), msg.getMessageDirection());
audit = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess();
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS," Before getting main payload");
              
              //To get Message Payload
pload = msg.getMainPayload();
                  // Storing the Payload Content in a Sting
                  String inputMsgPayload = new String(pload.getContent());
                 
//Defining Size of the pdf document
Document document = new Document(PageSize.A4, 36, 72, 108, 180);
                  //For Passing the output as array outputstream
ByteArrayOutputStream output = new ByteArrayOutputStream();
                 
//writing the pdf document data into output
PdfWriter writer = PdfWriter.getInstance(document, output);
                 
//setting the runtime parameters which we can pass from Communication channel as Adapter Module parameters to the Module Context
Ownerpassword = (String) moduleContext.getContextData("pwd_Owner");
Userpassword = (String) moduleContext.getContextData("pwd_User");
                 
//writer.setEncryption() Method to set password to generated PDF
writer.setEncryption(Userpassword.getBytes(), Ownerpassword.getBytes(),
//we can provide multiple permissions by ORing different values.
//For example PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY.
PdfWriter.AllowPrinting, PdfWriter.ENCRYPTION_AES_128);
                 
                 
document.open();
//To print the current Date in the pdf file
document.add(new Paragraph(new Date().toString()));
document.add(new Paragraph("Input payload is:"));   
//Printing input Message payload in the pdf file.
document.add(new Paragraph(inputMsgPayload));
                 
document.close();
                 
//converting the Byte Array Stream into Byte
byte byt[] = output.toByteArray();
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS," -------outputStream is------ "
                                                 +output.toString());
                  //set the payload in bytes
                                                pload.setContent(byt);
                                                msg.setMainPayload(pload);
                                                //Passing the main payload to inputmoduledata
                                                inputModuleData.setPrincipalData(msg);
                                               
            }
//Exception Handling
            catch (Exception e) {
ModuleException me = new ModuleException(e);
            }            return inputModuleData;

      }

}

Receiver communication channel configurations.

Below are the configurations to create zip file in ftp

Below are the Receiver module configurations


Here I am passing the output stream of the pdf file to MessageTransformbean.

The MessageTransformbean will set the file name as Success.pdf and it will send the data to the Standard PayloadZipbean module.This PayloadZipbean module will create a zip file in the target ftp directory.

Note: If we put the Parameter Name as pwd_xxx in the module configuration, automatically it will take the Parameter Value as password text as showed in the screen shot.

Output File in Target FTP:

Zipped Output file containing PDF:

Here is the Output PDF

14 Comments
Labels in this area