Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package org.sample;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class XMLProcessMediator extends AbstractMediator {
public static final String FILE_LOCATION = "FileLocation";
public static final String FILE_OM_ELEMENT = "FileOMElement";
public boolean mediate(MessageContext context) {
String filePath = (String) context.getProperty(FILE_LOCATION);
File xmlFileToProcess = new File(filePath);
if (xmlFileToProcess.exists()) {
OMElement fileElement;
try {
InputStream xmlInputStream = new FileInputStream(xmlFileToProcess);
fileElement = new StAXOMBuilder(xmlInputStream).getDocumentElement();
} catch (Exception e) {
log.error("Error while parsing XML file : " + xmlFileToProcess.getAbsolutePath());
return false;
}
if (fileElement != null) {
context.setProperty(FILE_OM_ELEMENT, fileElement);
context.getEnvelope().getBody().addChild(fileElement);
}
} else {
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment