on 01-21-2016 7:41 AM
Hello Experts,
My Scenario is IDoc to Mail. I need to send the file as an attachment with dynamic filename and body. I am able to send the the file with attachment and body but not with the dynamic filename. Also I am not supposed to use Mail Package.So for the reason I have implemented Module code for changing the dynamic filename.Here is the code and error exception occurred in the Message Log.
Module Code:
package com.sap.mailmodule;
import javax.ejb.Stateless;
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.PublicAPIAccessFactory;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditAccess;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
/**
* Session Bean implementation class MailAdapterModule
*/
@Stateless
public class MailAdapterModule implements MailAdapterModuleRemote, MailAdapterModuleLocal
{
public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData)throws ModuleException
{
Message msg = (Message) inputModuleData.getPrincipalData();
MessageKey key = null;
AuditAccess audit = null;
String attName = null;
String targetFileName = null;
try
{
//Custom Adapter module : Module called
//Reading file name from message header
attName = moduleContext.getContextData("AttachmentName");
Payload payload = msg.getAttachment(attName);
MessagePropertyKey fileKey = new MessagePropertyKey("FileName","http://sap.com/xi/XI/System/Mail");
targetFileName = fileKey.getPropertyName();
audit = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess();
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, "Input file read successfully");
//Reading the Directory Names from the Module Context
if(targetFileName == null)
{
targetFileName = "Reservation.txt";
}
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, " Name of Attachment after msg.getattchment method: " + targetFileName);
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, " Name of Attachment after msg.getattchment method: " + targetFileName);
payload.setName(targetFileName);
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS,"Attachment renamed successfully : " +targetFileName);
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, " Entering Mail Adapter");
inputModuleData.setPrincipalData(msg);
}
catch (Exception e)
{
throw new ModuleException(e);
}
return inputModuleData;
}
}
Channel Module Configuration:
Message Exception Error ScreenShot:
Kindly help on the same.!!!
Hi Eng,
Apologies for the delay!!!
Thanks for your Patience and reply.
As per your previous blogs, I have splitted the requirement in to two flows. Here are the below details.
1.IDOC-PI-SOAP
2.SOAP-PI-MAIL
In the first flow I have created one node MAIL with the fields THeaderTo, THeaderCC, THeaderSubject, FileName underneath to it.
In the second flow I have used content conversion in SOAP Channel and then in the Mapping part I have used Java mapping with dynamic configuration parameters with all the above MAIL fields.
Also the first of the flat file I have replaced with blank data instead of MAIL field values and used While loop for multiple Item occurrences.
This is the way we have completed it Eng with out constructing any MIME parameters in JAVA mapping.
Please let me know if you need in detail with screenshots so that i can share the same.
Also will try your case and update you soon.
Thanks in advance!!
Many Thanks,
Best Regards,
Prashanth Bharadwaj Ch.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Eng,
Thanks for the reply!! Will try it and get back to you!!
Many Thanks,
Best Regards,
Prashanth Bharadwaj Ch.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Eng,
Thanks for the reply!!! Is it better idea to include the above two lines of code and test or add the payload.setname and test and if if does not work again adding the payload.contentdisposition one time.
Also I have added the code like below!! Kindly suggest the same whether i can proceed.
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, " Entering Mail Adapter");
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, " Name of Attachment after msg.getattchment method: " + targetFileName);
payload.setContentType("text/plain;charset=\"UTF-8\";name=\"" + targetFileName + "\"");
payload.setName(targetFileName);
payload.setAttribute("content-disposition", "attachment;filename=\"" + targetFileName + "\"");
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS,"Attachment renamed successfully : " +targetFileName);
inputModuleData.setPrincipalData(msg);
Will try the same and deploy it and get back to you!!
Thanks again for the patience!!
Many Thanks,
Best Regards,
Prashanth Bharadwaj Ch.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
HI Eng/
Sorry for the late and refrain!! I have added the MessageLoggerBean in the channel after my custom module. Please find the below screenshots of the same.
Communication Channel Setting:
PIMON Screenshot Step 2:
PIMON Screenshot Step 2:
From the above screenshots i have provided the log details from PIMON and also the data inside the attachment. And from the body attachment it will be converted to mail body.Would request you to kindly help on the same and let me know whether i need to change any module code.
Also Please suggest me if i need to try with attachment api's in the module code.
Kindly help on the same.
Many Thanks,
Best Regards,
Prashanth Bharadwaj Ch.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Prashanth
Unfortunately, different mail providers act differently on the MIME details, so it is always a trial and error.
For your mail server, it looks like the content type is not enough, so let's try a few more things.
1. Set payload name
Add the following to your code at the appropriate position.
payload.setName(targetFileName);
Then retest your case.
If it still does not work, try below
2. Set content disposition
Add following code in addition to above
payload.setAttribute("content-disposition", "attachment;filename=\"" + targetFileName + "\"");
If it still does not work, provide the same screenshot for the Attachment tab of the logged version.
Rgds
Eng Swee
Hi Eng,
Sure will do that and get back to you!!
Thanks alot Eng!!
Many Thanks,
Best Regards,
Prashanth Bharadwaj Ch.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi all/Eng,
I have added the same payload.setContentType("text/plain;charset=\"UTF-8\";name=\"" + targetFileName + "\""); in the module code. Still the same issue persists.The attachment name is not getting changed. Here are the below details.
Module Code:
package com.sap.mailmodule;
import javax.ejb.Stateless;
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.PublicAPIAccessFactory;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditAccess;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
/**
* Session Bean implementation class MailAdapterModule
*/
@Stateless
public class MailAdapterModule implements MailAdapterModuleRemote, MailAdapterModuleLocal
{
public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData)throws ModuleException
{
Message msg = (Message) inputModuleData.getPrincipalData();
MessageKey key = null;
AuditAccess audit = null;
String attName = null;
String targetFileName = null;
try
{
//Custom Adapter module : Module called
//Reading file name from message header
key = new MessageKey(msg.getMessageId(), msg.getMessageDirection());
audit = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess();
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, this.getClass().getName() + ": Module Initialized");
attName = moduleContext.getContextData("AttachmentName");
Payload payload = msg.getAttachment(attName);
MessagePropertyKey fileKey = new MessagePropertyKey("FileName","http://sap.com/xi/XI/System/Mail");
targetFileName = msg.getMessageProperty(fileKey);
if(targetFileName == null)
{
targetFileName = "Reservation.txt";
}
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, " Entering Mail Adapter");
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, " Name of Attachment after msg.getattchment method: " + targetFileName);
payload.setContentType("text/plain;charset=\"UTF-8\";name=\"" + targetFileName + "\"");
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS,"Attachment renamed successfully : " +targetFileName);
inputModuleData.setPrincipalData(msg);
}
catch (Exception e)
{
throw new ModuleException(e);
}
return inputModuleData;
}
}
Message Log in Runtime Workbench:
E-mail Generated with MainDocument name as attachment Name:
Please confirm still i need modify anything in the module code or mapping!!
Kindly help on the same!!
Thanks in advance!!
Many Thanks,
Best Regards,
Prashanth Bharadwaj Ch.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Prashanth
Can you add the module MessageLoggerBean after your custom module so that a version of the payload is available in the logs? Refer to the following on how to do that.
Once you have it logged, can you Open the message in PIMON and provide a screenshot of the Attachments tab for the version that was logged above?
It should look something like below. I would like to see the name and content type for the attachment.
Rgds
Eng Swee
Thank you so much Eng!!!
Will try the same and update the status.
Thanks again for the help and patience!!
Many Thanks,
Best Regards,
Prashanth Bharadwaj Ch.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi everyone/Eng,
I have adjusted the code as per your previous reply and the deployment is successful after the import with the latest change. Now while testing the same the attachment name for the file is still "MainDocument.txt". But the message log is displaying Attachment renamed successfully. Please find the below screen shot of the same!!
Message Log Screen Shot:
Mail ScreenShot:
Kindly help on the same!!!!
Thanks in advance!!!
Many Thanks,
Best Regards,
Prashanth Bharadwaj Ch.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Prashanth
Your logic does not do any renaming of the attachment. It just retrieves the attachment name from dynamic configuration (which was stored by mapping) and does nothing to it, it is just kept in the variable targetFileName.
In order to set the filename of the attachment, you need to set it in the Content Type of the attachment. The content type needs to be set in the following format, assuming you are only dealing with plain text MIME types.
text/plain;charset="UTF-8";name="file.txt"
To do this, you need to use setContentType() method of the Payload interface. For your case, you can do something like below:-
payload.setContentType("text/plain;charset=\"UTF-8\";name=\"" + targetFileName + "\"");
Make sure this is after the variable has been populated.
Rgds
Eng Swee
Thanks alot Eng!!
Will apply the same and let you the know the status!!
Thanks again!!
Many Thanks,
Best Regards,
Prashanth Bharadwaj Ch.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Eng/Hi All,
Yes the correct module is being called.in the As per the earlier screen shot if you could observe the module code and channel setting the same name is given in both. Please find the below screen shots for your reference.
Module code Name screenshot:
Channel Screen shot with same as Module name:
Module code for your reference:
package com.sap.mailmodule;
import javax.ejb.Stateless;
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.PublicAPIAccessFactory;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditAccess;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
/**
* Session Bean implementation class MailAdapterModule
*/
@Stateless
public class MailAdapterModule implements MailAdapterModuleRemote, MailAdapterModuleLocal
{
public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData)throws ModuleException
{
Message msg = (Message) inputModuleData.getPrincipalData();
MessageKey key = null;
AuditAccess audit = null;
String attName = null;
String targetFileName = null;
try
{
//Custom Adapter module : Module called
//Reading file name from message header
attName = moduleContext.getContextData("AttachmentName");
Payload payload = msg.getAttachment(attName);
MessagePropertyKey fileKey = new MessagePropertyKey("FileName","http://sap.com/xi/XI/System/Mail");
targetFileName = fileKey.getPropertyName();
audit = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess();
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, "Input file read successfully");
//Reading the Directory Names from the Module Context
if(targetFileName == null)
{
targetFileName = "Reservation.txt";
}
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, " Name of Attachment after msg.getattchment method: " + targetFileName);
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, " Name of Attachment after msg.getattchment method: " + targetFileName);
payload.setName(targetFileName);
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS,"Attachment renamed successfully : " +targetFileName);
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, " Entering Mail Adapter");
inputModuleData.setPrincipalData(msg);
}
catch (Exception e)
{
throw new ModuleException(e);
}
return inputModuleData;
}
}
Also as per my understanding from the above error, the error is due to the below line of code is missing in the module code.
key = new MessageKey(msg.getMessageId(), msg.getMessageDirection());
So could you please confirm if i add the above the error could be resolved.
Kindly help on the same.
Thanks in advance!!!
Many Thanks,
Best Regards,
Prashanth Bharadwaj Ch.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Prashanth
Yes, your key is not initialized prior to using the AuditLog.
Add these two lines of code as the first statements in the try block
try
{
key = new MessageKey(msg.getMessageId(), msg.getMessageDirection());
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, this.getClass().getName() + ": Module Initialized");
Rgds
Eng Swee
Hi Everyone/ Hi Praveen,
Initially i have provided the setting same as your post. But I am facing the msgkey error. So as a trial and error i just have given the above setting. Here are the below details of my communication channel setting, UDF used in mapping, and module code and error screen shot.
Communication Channel Setting:
Module Code:
try
{
//Custom Adapter module : Module called
//Reading file name from message header
attName = moduleContext.getContextData("AttachmentName");
Payload payload = msg.getAttachment(attName);
MessagePropertyKey fileKey = new MessagePropertyKey("FileName","http://sap.com/xi/XI/System/Mail");
targetFileName = fileKey.getPropertyName();
payload.setName(targetFileName);
}
UDF used in the mapping:
MappingTrace objTrace = container.getTrace();
DynamicConfiguration objDynConfig;
DynamicConfigurationKey objDCKey;
String Fname = FileName;
objTrace.addInfo(Fname);
final String NAMESPACE = "http://sap.com/xi/XI/System/Mail";
final String ATTRIBUTE1 = "THeaderTO"; //Recipients TO List
final String ATTRIBUTE2 = "THeaderSUBJECT"; // Subject of The Email
//final String ATTRIBUTE3 = "XHeaderName3"; //ContentDisposition
//final String ATTRIBUTE4 = "XHeaderName1"; //ContentType
//final String ATTRIBUTE5 = "XHeaderName2"; //ContentDescription
final String ATTRIBUTE6 = "THeaderCC"; //Recipients BCC List
try
{
objDynConfig = (DynamicConfiguration) container.getTransformationParameters().
get(StreamTransformationConstants.DYNAMIC_CONFIGURATION );
container.setParameter(StreamTransformationConstants.DYNAMIC_CONFIGURATION, objDynConfig);
objDCKey = DynamicConfigurationKey.create(NAMESPACE, ATTRIBUTE1);
objDynConfig.put(objDCKey, ZEmail);
objDCKey = DynamicConfigurationKey.create(NAMESPACE, ATTRIBUTE2);
objDynConfig.put(objDCKey, Subject);
objDCKey = DynamicConfigurationKey.create(NAMESPACE, ATTRIBUTE6);
objDynConfig.put(objDCKey, ZEmailCC);
DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/Mail","FileName");
conf.put(key,FileName);
}
catch (Exception objException)
{
objTrace.addWarning(objException.getMessage());
}
return "";
Error In the Message Log:
Also as per your previous reply please confirm whether i need to change the module code to
targetFileName = msg.getMessageProperty(fileKey) instead of targetFileName = fileKey.getPropertyName(); or include both. |
Kindly help on the same!!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Prashanth,
//Reading file name from message header
attName = moduleContext.getContextData("AttachmentName");
>>>>>targetFileName = fileKey.getPropertyName(); (this will return only property name FileName)
targetFileName = msg.getMessageProperty(fileKey)
Regards,
Praveen.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Prashanth,
Just for the understanding what's the reason for the below statement?
>>Also I am not supposed to use Mail Package
Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Prashanth
The error log does not match the adapter module coding. There is no field named "payloadName" in the logic you provided.
Maybe the error is not due to the custom adapter module. Can you provide the whole message log section for further analysis?
Rgds
Eng Swee
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Vila,
I agree with you. In your case the file name is static for the attachment. So you have given
But in my case i need to set the filename dynamically from ASMA parameters. So i have written the dynamic file name UDF in ESR by using the below code
DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/Mail","FileName");
conf.put(key,FileName);
From the above code I am putting the filename dynamically to pick by ASMA.
After that in the module level, I have written the following code to get the attachment name.
Payload payload = msg.getAttachment(attName); |
MessagePropertyKey fileKey = new MessagePropertyKey("FileName","http://sap.com/xi/XI/System/Mail");
targetFileName = fileKey.getPropertyName();
payload.setName(targetFileName);
From the above I am getting the attachment and setting it!!
so after giving the above parameters i am facing this error!!
Kindly help!!
User | Count |
---|---|
94 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.