import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.DynamicConfigurationKey;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;
public class FileToMailAttachment implements StreamTransformation{
private Map param;
public void setParameter(Map map) {
param = map;
if (param == null) {
param = new HashMap();
}
}
private static AbstractTrace trace = null;
public void execute(InputStream in, OutputStream out) throws StreamTransformationException {
try {
/**
* Required MAP_jar files: aii_map_api.jar
* Purpose of this JavaMap:
* Once File-Sender-Adapter reads file, sending this file as an attachment to Receiver Mail Adapter using MailPackage
* Functionality of This JavaMap:
* 1. Get FileName from ASMA DynamicConfiguration Variable
* 2. To just copy InputStream(FileContent read by FileAdapter) to String
* 3. Prepare "MailPackage_XML" having dynamic attachment name
* 4. Send MailPackage_XML to OutputStream
*/
trace = (AbstractTrace)param.get(StreamTransformationConstants.MAPPING_TRACE);
trace.addInfo("Start of JavaMap class FileToMailAttachment()");
//1. Get FileName from ASMA DynamicConfiguration Variable
String strFileName = "";
DynamicConfiguration conf = (DynamicConfiguration)param.get("DynamicConfiguration");
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
strFileName = conf.get(key); //to get the value from the 'key'
trace.addInfo("FileName: "+ strFileName );
//2. To just copy InputStream(FileContent read by FileAdapter) to String
byte[] bytes = new byte[in.available()];
in.read(bytes);
String fileContent= new String(bytes, "UTF-8");
//3. Prepare "MailPackage_XML" having dynamic attachment name
String subjectMail = "FileToMail_EncSigned_TEST";
String fromMailId = "MiddlewareMail@xyz.com";
String toMailId = "testMail@ghj.com";
String mailPackage = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+ "\r\n" +
"<ns:Mail xmlns:ns=\"http://sap.com/xi/XI/Mail/30\"> "+
"<Subject>"+ subjectMail +"</Subject>" +
"<From>"+ fromMailId +"</From>"+
"<To>"+ toMailId +"</To>"+
"<Reply_To />"+
"<Content_Type>multipart/mixed;boundary = --AaZz</Content_Type>"+
"<Content>"+
"----AaZz"+
"\r\nContent-Type: text/plain; charset=UTF-8" + "\r\n" +
"Content-Disposition: inline" + "\r\n" + "\r\n" +
"The attachment \""+ strFileName + "\" is a encrypted/signed document." + "\r\n" +
"----AaZz" + "\r\n" +
"Content-Type: application/xml; name=" + strFileName + "\r\n" +
"Content-Disposition: attachment; filename=" + strFileName + "\r\n" +
"\r\n" + fileContent + "\r\n"+
"</Content>"+
"</ns:Mail>";
//4. Send MailPackage_XML to OutputStream
out.write(mailPackage.getBytes(Charset.forName("UTF-8")));
trace.addInfo("End of JavaMap class FileToMailAttachment()");
}catch(Exception e){
trace.addInfo("Exception in JavaMap FileToMailAttachment(): " + e.getMessage());
}
}
/*
public static void main(String[] args) {
try {
String filePath = "C:\\Users\\xyz\\Desktop\\TEST\\";
String Fnm = "Input_Sample1.csv";
String inputFile = filePath + Fnm;
String outputFile = filePath + "Output_" + Fnm ;
FileToMailAttachment myClass = new FileToMailAttachment();
FileInputStream in = new FileInputStream(inputFile);
FileOutputStream out = new FileOutputStream(outputFile);
myClass.execute(in, out);
}catch(Exception e){
e.printStackTrace();
}
}*/
}
package com.sap.adaptermodule;
import java.rmi.RemoteException;
import java.util.ArrayList;
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.MessagePropertyKey;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
public class setDynmMailAtchmntBean 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()
*/
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
}
public void ejbCreate() throws javax.ejb.CreateException {
}
private SessionContext myContext;
MessageKey amk = null;
public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData) throws ModuleException{
Object obj = null;
Message msg = null;
try
{
obj = inputModuleData.getPrincipalData();
msg = (Message) obj;
amk = new MessageKey(msg.getMessageId(),msg.getMessageDirection());
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "Start of Custom-JavaAdapterModule JNDI setDynmMailAtchmnt ................");
//1. Read "FileName" MessageProperty
String sourceFileName = "";
for (MessagePropertyKey key : msg.getMessagePropertyKeys()) {
String propertyName = key.getPropertyName(); //"FileName"
String propertyNamespace = key.getPropertyNamespace(); //"http://sap.com/xi/XI/System/File"
String propertyStr = propertyName + "_" + propertyNamespace;
if(propertyStr.indexOf("FileName_http://sap.com/xi/XI/System/File") > -1){
sourceFileName = msg.getMessageProperty(key); //"01.zip"
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, ("MessageProperty " + (propertyName + " " + propertyNamespace + " " + sourceFileName)));
break;
}
}
//3. Set Dynamic mail-AttachmentName in MessageProperty
MessagePropertyKey sKey1 = new MessagePropertyKey("Part[0].Content-Description","http://sap.com/xi/XI/System/Mail");
msg.setMessageProperty(sKey1, sourceFileName);
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, ("Setting MessageProperty as: Namespace=http://sap.com/xi/XI/System/Mail Name=Part[0].Content-Description Value=" + sourceFileName));
MessagePropertyKey sKey2 = new MessagePropertyKey("Part[0].Content-Disposition","http://sap.com/xi/XI/System/Mail");
msg.setMessageProperty(sKey1, ("attachment; filename=\"" + sourceFileName + "\""));
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, ("Setting MessageProperty as: Namespace=http://sap.com/xi/XI/System/Mail Name=Part[0].Content-Disposition Value=" + ("attachment; filename=\"" + sourceFileName + "\"")));
MessagePropertyKey sKey3 = new MessagePropertyKey("Part[0].Content-Type","http://sap.com/xi/XI/System/Mail");
msg.setMessageProperty(sKey1, "text/html; charset=utf-8");
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, ("Setting MessageProperty as: Namespace=http://sap.com/xi/XI/System/Mail Name=Part[0].Content-Type Value=text/html; charset=utf-8"));
inputModuleData.setPrincipalData(msg); //set msg property again as principal data
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "End of Custom-JavaAdapterModule JNDI setDynmMailAtchmnt ..................");
return inputModuleData;
}catch (Exception e) {
ModuleException me = new ModuleException(e);
throw me;
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
37 | |
10 | |
7 | |
5 | |
5 | |
5 | |
4 | |
4 | |
3 | |
3 |