public class XmlAlterMediator extends AbstractMediator {
private Log log = LogFactory.getLog(this.getClass());
private String removeEl;
public String getRemoveEl() {
return removeEl;
}
public void setRemoveEl(String removeEl) {
this.removeEl = removeEl;
}
Document doc ;
public boolean mediate(MessageContext context) {
String responsePayload=null;
try {
doc = getDocumentByXml(context.getEnvelope().toString());
String []elements=removeEl.split("_");
for (String element : elements) {
String []elementTmp=element.split("\\.");
String parentElement=elementTmp[0];
String childElement=elementTmp[1];
deleteElements(parentElement,childElement);
log.info(element);
}
responsePayload=xmlDocPrint(doc);
SOAPBody body = context.getEnvelope().getBody();
OMElement firstChild = body.getFirstElement();
OMElement omXML = AXIOMUtil.stringToOM(responsePayload);
if (firstChild == null) {
body.addChild(omXML);
} else {
firstChild.insertSiblingAfter(omXML);
firstChild.detach();
}
} catch (ParserConfigurationException e) {
log.error(e.getMessage());
} catch (SAXException e) {
log.error(e.getMessage());
} catch (IOException e) {
log.error(e.getMessage());
} catch (Exception e) {
log.error(e.getMessage());
}
return true;
}
private Document getDocumentByXml(String xml) throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
Document doc = db.parse(is);
doc.getDocumentElement().normalize();
return doc;
}
private final String xmlDocPrint(Document xml) throws Exception {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
Writer out = new StringWriter();
tf.transform(new DOMSource(xml), new StreamResult(out));
log.info(out.toString());
return out.toString();
}
private Document deleteElements(String parentElement,String childElement) {
NodeList nodeList = doc.getElementsByTagName(childElement);
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if ((node.getNodeType() == Node.ELEMENT_NODE)&&(node.getParentNode().getNodeName().equals(parentElement))) {
node.getParentNode().removeChild(node);
}
}
return doc;
}
}
private Log log = LogFactory.getLog(this.getClass());
private String removeEl;
public String getRemoveEl() {
return removeEl;
}
public void setRemoveEl(String removeEl) {
this.removeEl = removeEl;
}
Document doc ;
public boolean mediate(MessageContext context) {
String responsePayload=null;
try {
doc = getDocumentByXml(context.getEnvelope().toString());
String []elements=removeEl.split("_");
for (String element : elements) {
String []elementTmp=element.split("\\.");
String parentElement=elementTmp[0];
String childElement=elementTmp[1];
deleteElements(parentElement,childElement);
log.info(element);
}
responsePayload=xmlDocPrint(doc);
SOAPBody body = context.getEnvelope().getBody();
OMElement firstChild = body.getFirstElement();
OMElement omXML = AXIOMUtil.stringToOM(responsePayload);
if (firstChild == null) {
body.addChild(omXML);
} else {
firstChild.insertSiblingAfter(omXML);
firstChild.detach();
}
} catch (ParserConfigurationException e) {
log.error(e.getMessage());
} catch (SAXException e) {
log.error(e.getMessage());
} catch (IOException e) {
log.error(e.getMessage());
} catch (Exception e) {
log.error(e.getMessage());
}
return true;
}
private Document getDocumentByXml(String xml) throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
Document doc = db.parse(is);
doc.getDocumentElement().normalize();
return doc;
}
private final String xmlDocPrint(Document xml) throws Exception {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
Writer out = new StringWriter();
tf.transform(new DOMSource(xml), new StreamResult(out));
log.info(out.toString());
return out.toString();
}
private Document deleteElements(String parentElement,String childElement) {
NodeList nodeList = doc.getElementsByTagName(childElement);
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if ((node.getNodeType() == Node.ELEMENT_NODE)&&(node.getParentNode().getNodeName().equals(parentElement))) {
node.getParentNode().removeChild(node);
}
}
return doc;
}
}