use of org.w3c.dom.Document in project camel by apache.
the class BacklogTracerTest method testBacklogTracerEventMessageAsXml.
public void testBacklogTracerEventMessageAsXml() throws Exception {
MBeanServer mbeanServer = getMBeanServer();
ObjectName on = new ObjectName("org.apache.camel:context=camel-1,type=tracer,name=BacklogTracer");
assertNotNull(on);
mbeanServer.isRegistered(on);
Boolean enabled = (Boolean) mbeanServer.getAttribute(on, "Enabled");
assertEquals("Should not be enabled", Boolean.FALSE, enabled);
Integer size = (Integer) mbeanServer.getAttribute(on, "BacklogSize");
assertEquals("Should be 1000", 1000, size.intValue());
// enable it
mbeanServer.setAttribute(on, new Attribute("Enabled", Boolean.TRUE));
getMockEndpoint("mock:foo").expectedMessageCount(2);
getMockEndpoint("mock:bar").expectedMessageCount(2);
template.sendBody("direct:start", "Hello World");
template.sendBody("direct:start", "Bye World");
assertMockEndpointsSatisfied();
String events = (String) mbeanServer.invoke(on, "dumpTracedMessagesAsXml", new Object[] { "foo" }, new String[] { "java.lang.String" });
assertNotNull(events);
log.info(events);
// should be valid XML
Document dom = context.getTypeConverter().convertTo(Document.class, events);
assertNotNull(dom);
NodeList list = dom.getElementsByTagName("backlogTracerEventMessage");
assertEquals(2, list.getLength());
}
use of org.w3c.dom.Document in project camel by apache.
the class BacklogTracerTest method testBacklogTracerEventMessageDumpAllAsXml.
public void testBacklogTracerEventMessageDumpAllAsXml() throws Exception {
MBeanServer mbeanServer = getMBeanServer();
ObjectName on = new ObjectName("org.apache.camel:context=camel-1,type=tracer,name=BacklogTracer");
assertNotNull(on);
mbeanServer.isRegistered(on);
Boolean enabled = (Boolean) mbeanServer.getAttribute(on, "Enabled");
assertEquals("Should not be enabled", Boolean.FALSE, enabled);
// enable it
mbeanServer.setAttribute(on, new Attribute("Enabled", Boolean.TRUE));
getMockEndpoint("mock:foo").expectedMessageCount(2);
getMockEndpoint("mock:bar").expectedMessageCount(2);
template.sendBody("direct:start", "Hello World");
template.sendBody("direct:start", "Bye World");
assertMockEndpointsSatisfied();
String events = (String) mbeanServer.invoke(on, "dumpAllTracedMessagesAsXml", null, null);
assertNotNull(events);
log.info(events);
// should be valid XML
Document dom = context.getTypeConverter().convertTo(Document.class, events);
assertNotNull(dom);
NodeList list = dom.getElementsByTagName("backlogTracerEventMessage");
assertEquals(6, list.getLength());
}
use of org.w3c.dom.Document in project camel by apache.
the class XmlConverter method toDOMSource.
@Converter
public DOMSource toDOMSource(InputStream is, Exchange exchange) throws ParserConfigurationException, IOException, SAXException {
InputSource source = new InputSource(is);
String systemId = source.getSystemId();
DocumentBuilder builder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
Document document = builder.parse(source);
return new DOMSource(document, systemId);
}
use of org.w3c.dom.Document in project camel by apache.
the class XmlConverter method toDOMSourceFromStream.
@Converter
public DOMSource toDOMSourceFromStream(StreamSource source, Exchange exchange) throws ParserConfigurationException, IOException, SAXException {
Document document;
String systemId = source.getSystemId();
DocumentBuilder builder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
Reader reader = source.getReader();
if (reader != null) {
document = builder.parse(new InputSource(reader));
} else {
InputStream inputStream = source.getInputStream();
if (inputStream != null) {
InputSource inputsource = new InputSource(inputStream);
inputsource.setSystemId(systemId);
document = builder.parse(inputsource);
} else {
throw new IOException("No input stream or reader available on StreamSource: " + source);
}
}
return new DOMSource(document, systemId);
}
use of org.w3c.dom.Document in project camel by apache.
the class ManagedCamelContext method dumpRoutesAsXml.
@Override
public String dumpRoutesAsXml(boolean resolvePlaceholders) throws Exception {
List<RouteDefinition> routes = context.getRouteDefinitions();
if (routes.isEmpty()) {
return null;
}
// use a routes definition to dump the routes
RoutesDefinition def = new RoutesDefinition();
def.setRoutes(routes);
String xml = ModelHelper.dumpModelAsXml(context, def);
// if resolving placeholders we parse the xml, and resolve the property placeholders during parsing
if (resolvePlaceholders) {
final AtomicBoolean changed = new AtomicBoolean();
InputStream is = new ByteArrayInputStream(xml.getBytes());
Document dom = XmlLineNumberParser.parseXml(is, new XmlLineNumberParser.XmlTextTransformer() {
@Override
public String transform(String text) {
try {
String after = getContext().resolvePropertyPlaceholders(text);
if (!changed.get()) {
changed.set(!text.equals(after));
}
return after;
} catch (Exception e) {
// ignore
return text;
}
}
});
// okay there were some property placeholder replaced so re-create the model
if (changed.get()) {
xml = context.getTypeConverter().mandatoryConvertTo(String.class, dom);
RoutesDefinition copy = ModelHelper.createModelFromXml(context, xml, RoutesDefinition.class);
xml = ModelHelper.dumpModelAsXml(context, copy);
}
}
return xml;
}
Aggregations