Set SOAP Action dynamically using Adapter Module
If you are having a requirement of setting SOAP action in single channel, we could go with custom adapter module. The code which I am using is as below:
package com.pi.module;
import javax.ejb.Stateless;
import java.rmi.RemoteException;
import java.util.Timer;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.xml.parsers.*;
import org.w3c.dom.*;
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.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.XMLPayload;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
/**
* Session Bean implementation class DynamicSOAPAction
*/
@Stateless
public class DynamicSOAPAction implements DynamicSOAPActionRemote, DynamicSOAPActionLocal,SessionBean, Module {
/**
* Default constructor.
*/
public DynamicSOAPAction() {
// TODO Auto-generated constructor stub
}
public ModuleData process(ModuleContext moduleContext,ModuleData inputModuleData)throws ModuleException {
Object obj = null;
Message msg = null;
MessageKey amk = null;
String soapAction = null;
String createSOAPAction = (String) moduleContext.getContextData(“CreateAction”);
String deleteSOAPAction = (String) moduleContext.getContextData(“DeleteAction”);
String modifySOAPAction = (String) moduleContext.getContextData(“ModifyAction”);
String rootNode1 = (String) moduleContext.getContextData(“CreateNode”);
String rootNode2 = (String) moduleContext.getContextData(“DeleteNode”);
String rootNode3 = (String) moduleContext.getContextData(“ModifyNode”);
try{
// Retrieves the current principle data, usually the message , Return type is Object
obj = inputModuleData.getPrincipalData();
// A Message is what an application sends or receives when interacting with the Messaging System.
msg = (Message) obj;
// MessageKey consists of a message Id string and the MessageDirection
amk = new MessageKey(msg.getMessageId(),msg.getMessageDirection());
// Audit log message will appear in MDT of Channel Monitoring
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,“Dynamic SOAPAction Module called”);
// Returns the main document as XMLPayload
XMLPayload xpld = msg.getDocument();
// read XML Root node
DocumentBuilderFactory factory;
factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xpld.getInputStream());
String rootNode = document.getDocumentElement().getNodeName();
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,“Root Node Name is “ + rootNode );
if (rootNode.equals(rootNode1))
soapAction = createSOAPAction;
else if (rootNode.equals(rootNode2))
soapAction = deleteSOAPAction;
else if (rootNode.equals(rootNode3))
soapAction = modifySOAPAction;
//setting Action in the message header
MessagePropertyKey action = new MessagePropertyKey(“THeaderSOAPACTION”,“http://sap.com/xi/XI/System/SOAP“);
msg.setMessageProperty(action, soapAction);
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, “Soap action Set as “ + soapAction );
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, “Soap action successfully set”);
// Sets the principle data that represents usually the message to be processed
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, “Message Successfully updated…“);
inputModuleData.setPrincipalData(msg);
return inputModuleData;
}
catch (Exception e) {
Audit.addAuditLogEntry(amk, AuditLogStatus.ERROR, “Message processing failed..”);
ModuleException me = new ModuleException(e);
throw me;
}
}
/* (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 context) throws EJBException,RemoteException {
}
/* (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 {
}
}
The one which I am trying with SAX parser is:
package com.pi.module;
import javax.ejb.Stateless;
import java.rmi.RemoteException;
import java.util.Timer;
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.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.XMLPayload;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* Session Bean implementation class DynamicSOAPAction
*/
@Stateless
public class DynamicSOAPAction implements DynamicSOAPActionRemote, DynamicSOAPActionLocal,SessionBean, Module {
/**
* Default constructor.
*/
public DynamicSOAPAction() {
// TODO Auto-generated constructor stub
}
private static String rootNode;
private static String rootNode1;
private static String rootNode2;
private static String rootNode3;
private static String rootNode4;
public ModuleData process(ModuleContext moduleContext,ModuleData inputModuleData)throws ModuleException {
Object obj = null;
Message msg = null;
MessageKey amk = null;
String soapAction = null;
String createSOAPAction = (String) moduleContext.getContextData("CreateAction");
String deleteSOAPAction = (String) moduleContext.getContextData("DeleteAction");
String modifySOAPAction = (String) moduleContext.getContextData("ModifyAction");
String blockSOAPAction = (String) moduleContext.getContextData("BlockAction");
rootNode1 = (String) moduleContext.getContextData("CreateNode");
rootNode2 = (String) moduleContext.getContextData("DeleteNode");
rootNode3 = (String) moduleContext.getContextData("ModifyNode");
rootNode4 = (String) moduleContext.getContextData("BlockNode");
try{
// Retrieves the current principle data, usually the message , Return type is Object
obj = inputModuleData.getPrincipalData();
// A Message is what an application sends or receives when interacting with the Messaging System.
msg = (Message) obj;
// MessageKey consists of a message Id string and the MessageDirection
amk = new MessageKey(msg.getMessageId(),msg.getMessageDirection());
// Audit log message will appear in MDT of Channel Monitoring
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Dynamic SOAPAction Module called");
// Returns the main document as XMLPayload
XMLPayload xpld = msg.getDocument();
// read XML Root node
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean flag = false;
public void startElement(String uri, String localName,String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase(rootNode1)) {
rootNode = qName;
flag = true;
}
else if (qName.equalsIgnoreCase(rootNode2)) {
rootNode = qName;
flag = true;
}
else if (qName.equalsIgnoreCase(rootNode3)) {
rootNode = qName;
flag = true;
}
else if (qName.equalsIgnoreCase(rootNode4)) {
rootNode = qName;
flag = true;
}
}
};
saxParser.parse(xpld.getInputStream(),handler);
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Root Element :" + rootNode);
if (rootNode.equals(rootNode1))
soapAction = createSOAPAction;
else if (rootNode.equals(rootNode2))
soapAction = deleteSOAPAction;
else if (rootNode.equals(rootNode3))
soapAction = modifySOAPAction;
else if (rootNode.equals(rootNode4))
soapAction = blockSOAPAction;
//setting Action in the message header
MessagePropertyKey action = new MessagePropertyKey("THeaderSOAPACTION","http://sap.com/xi/XI/System/SOAP");
msg.setMessageProperty(action, soapAction);
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "Soap action Set as" + soapAction );
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "Soap action is Successfully set");
// Sets the principle data that represents usually the message to be processed
Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "Message Successfully updated");
inputModuleData.setPrincipalData(msg);
return inputModuleData;
}
catch (Exception e) {
Audit.addAuditLogEntry(amk, AuditLogStatus.ERROR, "Message processing failed..");
ModuleException me = new ModuleException(e);
throw me;
}
}
/* (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 context) throws EJBException,RemoteException {
}
/* (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 {
}
}