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: 
Former Member
0 Kudos

One of most common interface types I worked on is implementation of Message Filter pattern. Let's assume we have simple File->PI->File interface with message having multiple items and only some of them are relevant for further processing. Usually implementation of this interface is pretty straight forward - just use message mapping to filter unwanted items and pass only right ones.  However that if in one particular message ALL the items would be filtered? Right, we will have the message with one root node only. It goes without saying we don't want to write output files with no real data.

There is a setting for File Receiver adapted "Empty-Message handling" that allows to choose how to handle empty messages. We can to choose "ignore" and in this case all empty message will be ignored. The main problem is "empty message" means literally empty, i. e. 0 bytes length, so our message with one root node will be processed as usual. Unfortunately, it's impossible to make "really empty" message using Message Mapping or XSLT transformation. However we still could do it using Java Mapping.

Here is listing for Java Mapping that checks if root node has any children and if not - outputs with empty message. Just use this mapping as last transformation step in Operational mapping pipeline and this will allow you to use "Empty message handling" feature. Please be aware, you can't call other mappings or custom adapter modules after this mapping in sequence. This is because there potentially could be no message payload so most probably next mapping would fail then trying to parse message payload into XML.

public class EmptyMapping extends AbstractTransformation {

public void transform(TransformationInput trInput, TransformationOutput trOutput)

throws StreamTransformationException {

getTrace().addInfo("JAVA Mapping Called");

/** Return input by default **/

String returnString = convertStreamToString(trInput.getInputPayload().getInputStream());

try {

/** Read input Payload  **/

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

InputSource inSrc = new InputSource();

inSrc.setCharacterStream(new StringReader(returnString));

Document doc = db.parse(inSrc);

/** Check if root node has children **/

boolean rootHasChildrenNodes = false;

NodeList rootChildren = doc.getDocumentElement().getChildNodes();

for (int i=0;(!rootHasChildrenNodes) && (i<rootChildren.getLength());i++){

if (rootChildren.item(i).getNodeType() == Node.ELEMENT_NODE){

rootHasChildrenNodes = true;

}

}

if (!rootHasChildrenNodes){

/** return empty string  **/

returnString = "";

}

}catch (Exception e){

getTrace().addWarning("Exception in EmptyMapping.java. Message: "+e.getMessage());

throw new StreamTransformationException("Exception in EmptyMapping.java",e);

}

/** Write output Payload  **/

try {

trOutput.getOutputPayload().getOutputStream().write(returnString.getBytes("UTF-8"));

} catch (Exception e) {

getTrace().addWarning("Error: "+e.getMessage());

throw new StreamTransformationException("Exception in EmptyMapping.java",e);

}

}

public String convertStreamToString(InputStream in) {

StringBuffer sb = new StringBuffer();

try {

InputStreamReader isr = new InputStreamReader(in);

Reader reader = new BufferedReader(isr);

int ch;

while ((ch = in.read()) > -1) {

sb.append((char) ch);

}

reader.close();

} catch (Exception exception) {

}

return sb.toString();

}

}

Labels in this area