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: 
roger_alluivall
Participant

Adapter Module: ReplaceString


Use

ReplaceString module is used to replace strings within the xml text of the payload of the message. It can be used to modify the message structure before it gets in PI and also before it gets out. It is useful when there are strings like namespaces, types, etc that PI can’t “understand” or that PI is unable to include in outbound messages.

The module obtains a pair of string : <OldString>;<NewString> as parameter. Reads the payload of the message and converts it to a string. Then it searches <OldString> within the message and replace each occurrence with <NewString>. Finally it returns the message modified. If OldString can’t be found in the message nothing is modified and the original message is returned.

The module accepts multiple string pairs to allow multiple replacements in a single execution.

Deployment

Enterprise Java Bean Project: ReplaceString-ejb

Enterprise Java Bean Application: ReplaceString-ear

Integration

The module can be used in any kind of Sender and Receiver adapters.

Activities

This section describes all the activities that have to be carried out in order to configure the module.

Entries in processing sequence

Insert the module in front of the adapter module as shown in the picture below.

Entries in the module configuration

The table below shows the possible parameters and values of the adapter module.

ParameterTypePossible valuesDescriptionExample
separatorOptional

Any alphanumeric character.

Default value: ‘|’

This parameter configures the character that separates a pair of strings.

separator = ‘;’
param(*)

Mandatory

Pair of strings separated by “separator” character.

<OldString><separator>[<NewString>, blankString,emptyString].

Default value: none.

This parameter is used to obtain Old string to be replaced by the New string in the message. The pair is separated by “separator”. As Adapter module tab in PI trims strings by the right side, if it’s needed to replace OldString by a blank string or an empty string, the parameters blankString or emptyString have to be used.

param1=str1;str2

param2=str1;blankString

param3=str1;emptyString

(*) The parameter name can be any string: param1, parameter, aaa, etc.

Example

Payload data before execution:


<MT_ROOT>
     <RECORDSET>
          <DATA>
               <FIELD1>value</FIELD1>
               <FIELD2>value</FIELD2>
               <FIELD3>value</FIELD3>
          </DATA>
          <DATA>
               <FIELD1>value</FIELD1>
               <FIELD2>value</FIELD2>
               <FIELD3>value</FIELD3>
          </DATA>
     </RECORDSET>
</MT_ROOT>




Module parameters:

separator = ‘;’

param1 = ’RECORDSET;ROW’

param2 = <MT_ROOT>;<MESSAGE type=TYPE>

param3 = </MT_ROOT>;</MESSAGE>

param4 = <DATA>;emptyString

param5 = </DATA>;emptyString

Payload data after execution:


< MESSAGE type=TYPE>
     <ROW>
          <FIELD1>value</FIELD1>
          <FIELD2>value</FIELD2>
          <FIELD3>value</FIELD3>
          <FIELD1>value</FIELD1>
          <FIELD2>value</FIELD2>
          <FIELD3>value</FIELD3>
     </ROW>
</MESSAGE>







ReplaceStringBean


/**
*
*/
package com.arms.integrations;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
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.aii.af.service.auditlog.Audit;
import com.sap.engine.interfaces.messaging.api.MessageKey;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
import java.io.*;
import java.util.*;
/**
* @author Roger Allué i Vall
*
*/
public class ReplaceStringBean implements SessionBean, Module {
    private SessionContext myContext;
    private static final String C_SEPARATOR_STRING = "separator";
    private static final String C_AUX_SEPARATOR = "|";
    private static final String C_MODULEKEY_STRING = "module.key";
    private static final String C_BLANK_STRING = "blankString";
    private static final String C_EMPTY_STRING = "emptyString";
    /* (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
        myContext = arg0;
    }
    /* (non-Javadoc)
     * @see javax.ejb.SessionSynchronization#afterBegin()
     */
    public void afterBegin() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
    }
    /* (non-Javadoc)
     * @see javax.ejb.SessionSynchronization#afterCompletion(boolean)
     */
    public void afterCompletion(boolean arg0) throws EJBException,
            RemoteException {
        // TODO Auto-generated method stub
    }
    /* (non-Javadoc)
     * @see javax.ejb.SessionSynchronization#beforeCompletion()
     */
    public void beforeCompletion() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
    }
    public void ejbCreate() throws javax.ejb.CreateException {
    }
    public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData)    throws ModuleException {
        ByteArrayOutputStream payloadOut = null;
        int inputByte = 0;
        String payloadStr = "";
        Message msg;
        Enumeration paramList;
        String sep, paramKey,param, strArray[];
        try {
             msg = (Message) inputModuleData.getPrincipalData();
             MessageKey amk = new MessageKey(msg.getMessageId(), msg.getMessageDirection());
    
             Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,".-Module beginning");
payloadStr = new String(msg.getDocument().getContent(),"UTF-8");
    
            Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Payload before execution:" );
            Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,payloadStr );
             sep = moduleContext.getContextData(C_SEPARATOR_STRING);
             if ( sep == null ) {
                sep = C_AUX_SEPARATOR;
                Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Default separator used: " + sep);
             }
             else {
                Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Separator found: " + sep);
             }
    
   
    
             paramList = moduleContext.getContextDataKeys();
               while( paramList.hasMoreElements()) {
                    paramKey = (String) paramList.nextElement();
                 //Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"ParamKey: " + paramKey);
                 param =  moduleContext.getContextData(paramKey);
                 //Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Param: " + param);
        
                 if ( ! paramKey.equals(C_SEPARATOR_STRING) && ! paramKey.equals(C_MODULEKEY_STRING) ){
                    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"ParamKey: " + paramKey);
                    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Parameter: " + param);
                    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Separator: " + sep);
           
                    strArray = param.split(sep);
                    if (strArray != null){
                       if ((! strArray[0].equals(null)) && (! strArray[1].equals(null))){
                        if ( strArray[1].equals(C_BLANK_STRING)){
                            Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Blank String");
                            strArray[1]=" ";                   
                        }
                        else if (strArray[1].equals(C_EMPTY_STRING)){
                            strArray[1]="";
                            Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Empty String");
                        }
                        Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Substitution strArray[0]: " + strArray[0]);
                        Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Substitution strArray[1]: " + strArray[1]);
                        Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Substitution payloadStrA : " + payloadStr);   
                        payloadStr = payloadStr.replaceAll(strArray[0],strArray[1]);
                        Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Substitution payloadStrB : " + payloadStr);
                       }
                    }
                 }
             }
             Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Payload after replacement:" );
             Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,payloadStr );
   
             payloadOut = new ByteArrayOutputStream();
             payloadOut.write(payloadStr.getBytes());
             msg.getDocument().setContent(payloadOut.toByteArray());
             inputModuleData.setPrincipalData(msg);
        }
        catch(Exception e) {
            ModuleException me = new ModuleException(e);
            throw me;
        }
        return inputModuleData;
    }
}







31 Comments
Labels in this area