import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.DynamicConfigurationKey;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
public class SetMailReceivers extends AbstractTransformation {
String tagValue;
//let's create the dynamic configuration key for the recipients
//we only want to set the email receivers:
private static final DynamicConfigurationKey KEY_RECIPIENTS = DynamicConfigurationKey
.create("http://sap.com/xi/XI/System/Mail", "THeaderTO");
@Override
public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException {
Map mapParameters = (Map) transformationInput.getInputHeader().getAll();
//get the dynamic configuration
DynamicConfiguration conf = (DynamicConfiguration) mapParameters
.get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
try {
InputStream inputstream = transformationInput.getInputPayload().getInputStream();
OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();
//get the message ID, we need it to add a message to the monitoring message
String messageID = transformationInput.getInputHeader().getMessageId();
//create an input stream to read the payload using DOM
InputStream inputstreamDOM = transformationInput.getInputPayload().getInputStream();
//start reading XML with DOM
Document document = createDocument(inputstreamDOM);
//in the operation mapping we set the tag name that we want to read from
//the payload to determine our email receivers:
String elementToReadFromPayload = transformationInput.getInputParameters().getString("elementToReadFromPayload");
AddAuditLogs.execute("elementToReadFromPayload contains: " + elementToReadFromPayload, messageID, "success");
//get specific nodes with tag name contained in "elementToReadFromPayload"
NodeList nl = document.getElementsByTagName(elementToReadFromPayload);
//instead of "i < nl.getLength()" we just do "i < 1" - we only look at the first occurrence
//of our element with tag name "elementToReadFromPayload"
for(int i=0;i<1;i++){
Node messageNode = nl.item(i);
String nodename = messageNode.getNodeName();
if(nodename.equals(elementToReadFromPayload)){
tagValue = messageNode.getFirstChild().getNodeValue();
//add to message monitoring log what tag we read and what it contains
AddAuditLogs.execute("found Element " + nodename + " contains: " + tagValue, messageID, "success");
}
}
//now get the other parameters we set in the Operation mapping which will contain the
//two sets of email receivers:
String receivers1 = transformationInput.getInputParameters().getString("mail1");
String receivers2 = transformationInput.getInputParameters().getString("mail2");
//add to monitoring log what parameters we read and what they contain:
AddAuditLogs.execute("mail1 contains: " + receivers1, messageID, "success");
AddAuditLogs.execute("mail2 contains: " + receivers2, messageID, "success");
//now put the recipients into the dynamic configuration and we are done 🙂
//depending on the value in the XML element we read (elementToReadFromPayload)
//we now set the receivers:
if (tagValue.contains("1710")) {
conf.put(KEY_RECIPIENTS, receivers2);
}else {
conf.put(KEY_RECIPIENTS, receivers1);
}
// b) Just copy Input file content to Output file content
byte[] b = new byte[inputstream.available()];
inputstream.read(b);
outputstream.write(b);
} catch(Exception exception) {
getTrace().addDebugMessage(exception.getMessage());
throw new StreamTransformationException(exception.toString());
}
}
//create the DOM Document
public static Document createDocument(InputStream inputstreamDOM) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document document = db.parse(inputstreamDOM);
document.getDocumentElement().normalize();
return document;
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 | |
3 | |
3 | |
3 |