Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata
/* This adapter module can be used in receiver mail adapter.
   It reads the file name from adapter specific message attribute (ASMA) and stores this in the
   content type of the MIME header.
   Put this module before the standard mail adapter module.
   If the outgoing mail has several parts, then the PayloadSwapBean should be used before this module.

   The module can be used also before the PayloadZipBean in any adapter channel. It sets the
   name of the file inside the zip file.
*/ 
package sample;
import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import com.sap.aii.af.mp.module.*;
import com.sap.aii.af.ra.ms.api.*;
/**
 * @ejbHome <{com.sap.aii.af.mp.module.ModuleHome}>
 * @ejbLocal <{com.sap.aii.af.mp.module.ModuleLocal}>
 * @ejbLocalHome <{com.sap.aii.af.mp.module.ModuleLocalHome}>
 * @ejbRemote <{com.sap.aii.af.mp.module.ModuleRemote}>
 * @stateless
 */
public class SetAttachmentName 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 CreateException {
	}

public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData)
                      throws ModuleException{
     try {
        Message msg = (Message) inputModuleData.getPrincipalData();
        Payload payload = msg.getDocument();
        String fileName = msg.getMessageProperty("http://sap.com/xi/XI/System/File",
                                                 "FileName");

        if(fileName == null) fileName="default.txt";

        payload.setContentType("text/plain;charset = \"UTF-8\";"
                               + "name=\"" + fileName + "\"");

        inputModuleData.setPrincipalData(msg);

    } catch (Exception e) {
        throw new ModuleException(e);
    }

    return inputModuleData;
 }
}
  • No labels

4 Comments

  1. Guest

    Hi all,

    in PI 7.1 you have to change the followingin order to make it working:     

    // OLD
    String fileName = msg.getMessageProperty(http://sap.com/xi/XI/System/File), "FileName");

    ... 

    payload.setContentType("text/plain;charset = \"UTF-ISO-8859-1\";" + "filename=\"" + fileName + "\"");

    //NEW

    String fileName = msg.getMessageProperty(new MessagePropertyKey("FileName", http://sap.com/xi/XI/System/File));

    ... 

    payload.setContentType("text/plain;charset = \"UTF-ISO-8859-1\";" + "name=\"" + fileName + "\"");

    cheers

    matteo

  2. Hi Matteo,
    great useful information! Thanks!

    Best regards,
    Henrique.

  3. Former Member

    Hi,

    The blog seems to be useful.

    Could someone assist me regarding what value should be passed in Parameter in the module configuration corresponding to module key.

    Please suggest.

    Thanks,

    Sushama

  4. Former Member

    Hi Gurus,

    i have a following scenario: XML to Mail as csv attachment. 

    I have set up the scenario and it works if is use transform.content type as a static file name but when i try to rename it using FIleName from asma nothing happens eventhough the message monitoring says it change the name of the attachment.

    Can someone tell me what is wrong here and how can i fix it. I am on a PO 7.3 

    1 single stack 

    Adapter Module Code:

    Adapter Module
    package com.sap.adaptermodule.rapportage;
    import java.util.Iterator;
    import java.util.Set;
    import javax.ejb.CreateException;
    import javax.ejb.SessionBean;
    import javax.ejb.SessionContext;
    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.PublicAPIAccessFactory;
    import com.sap.engine.interfaces.messaging.api.auditlog.AuditAccess;
    import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
    import com.sap.tc.logging.Location;
    public class SetAttachmentNameBean 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 CreateException {
        }
     
    public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData)
                          throws ModuleException{
    	String SIGNATURE = "process(ModuleContext moduleContext, ModuleData inputModuleData)";
    	Location location = null;
    	AuditAccess audit = null;
    	MessageKey key = null;
    	
    	try {
        	 
    		location = Location.getLocation(this.getClass().getName());
    		
            Message msg = (Message) inputModuleData.getPrincipalData();
    		key = new MessageKey(msg.getMessageId(), msg.getMessageDirection());
    		audit = PublicAPIAccessFactory.getPublicAPIAccess()
    				.getAuditAccess();// creating object for audit log
            
            Payload payload = msg.getDocument();
            Set<MessagePropertyKey> mpkSet = msg.getMessagePropertyKeys();
            Iterator<MessagePropertyKey> mpkIterator = mpkSet.iterator();
            MessagePropertyKey mpk = null;
            for (;mpkIterator.hasNext();){
            	mpk = mpkIterator.next();
            	audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS,	"Message Property gevonden met Name: " + mpk.getPropertyName());
            	if ("FileName".equalsIgnoreCase(mpk.getPropertyName())){
            		break;
            	}
            	
            }
            
            audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS,	"Payload gegevens initieel, Name: " + payload.getName()+ 
            		", Description: " + payload.getDescription() + ", ContentType: " + payload.getContentType());
            
            String fileName = msg.getMessageProperty(new MessagePropertyKey("FileName", "http://sap.com/xi/XI/System/File"));
     
            if(fileName == null) fileName="default.txt";
      
            payload.setContentType("text/plain;charset = \"UTF-8\";" + "name=\"" + fileName + "\"");
          
            
            audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS,	"Payload gegevens na setten, Name: " + payload.getName()+ 
            		", Description: " + payload.getDescription() + ", ContentType: " + payload.getContentType());
     
            inputModuleData.setPrincipalData(msg);
     
        } catch (Exception e) {
            throw new ModuleException(e);
        }
     
        return inputModuleData;
     }
    }

    This is my Mail adapter configuration:

    https://www.dropbox.com/s/7gp9n0fdwnxhf84/27-10-2014%209-15-53.jpg?dl=0

    https://www.dropbox.com/s/e2b2wi4uegfdflc/27-10-2014%209-16-29.jpg?dl=0

    https://www.dropbox.com/s/fj46pzkzsnkjc9x/27-10-2014%209-17-19.jpg?dl=0

    This is what message monitoring says:

    https://www.dropbox.com/s/vzz04n3i2fhaivr/27-10-2014%209-14-08.jpg?dl=0

    https://www.dropbox.com/s/9opsgz3dsny3uam/27-10-2014%209-30-25.jpg?dl=0

    https://www.dropbox.com/s/gtws1hgfr7f4l8b/27-10-2014%209-30-55.jpg?dl=0

    And this is my mail result

    https://www.dropbox.com/s/v0gh9my8eulzalm/27-10-2014%2010-08-47.jpg?dl=0

    I want the Attachment.xml to be renamed with ASMA FileName.