use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.
the class ModelHelper method dumpModelAsXml.
/**
* Dumps the definition as XML
*
* @param context the CamelContext, if <tt>null</tt> then {@link org.apache.camel.spi.ModelJAXBContextFactory} is not in use
* @param definition the definition, such as a {@link org.apache.camel.NamedNode}
* @return the output in XML (is formatted)
* @throws JAXBException is throw if error marshalling to XML
*/
public static String dumpModelAsXml(CamelContext context, NamedNode definition) throws JAXBException {
JAXBContext jaxbContext = getJAXBContext(context);
final Map<String, String> namespaces = new LinkedHashMap<>();
// gather all namespaces from the routes or route which is stored on the expression nodes
if (definition instanceof RoutesDefinition) {
List<RouteDefinition> routes = ((RoutesDefinition) definition).getRoutes();
for (RouteDefinition route : routes) {
extractNamespaces(route, namespaces);
}
} else if (definition instanceof RouteDefinition) {
RouteDefinition route = (RouteDefinition) definition;
extractNamespaces(route, namespaces);
}
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter buffer = new StringWriter();
marshaller.marshal(definition, buffer);
XmlConverter xmlConverter = newXmlConverter(context);
String xml = buffer.toString();
Document dom;
try {
dom = xmlConverter.toDOMDocument(xml, null);
} catch (Exception e) {
throw new TypeConversionException(xml, Document.class, e);
}
// Add additional namespaces to the document root element
Element documentElement = dom.getDocumentElement();
for (String nsPrefix : namespaces.keySet()) {
String prefix = nsPrefix.equals("xmlns") ? nsPrefix : "xmlns:" + nsPrefix;
documentElement.setAttribute(prefix, namespaces.get(nsPrefix));
}
// We invoke the type converter directly because we need to pass some custom XML output options
Properties outputProperties = new Properties();
outputProperties.put(OutputKeys.INDENT, "yes");
outputProperties.put(OutputKeys.STANDALONE, "yes");
try {
return xmlConverter.toStringFromDocument(dom, outputProperties);
} catch (TransformerException e) {
throw new IllegalStateException("Failed converting document object to string", e);
}
}
use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.
the class XsltRouteTest method testSendDomMessage.
public void testSendDomMessage() throws Exception {
XmlConverter converter = new XmlConverter();
Document body = converter.toDOMDocument("<mail><subject>Hey</subject><body>Hello world!</body></mail>");
sendMessageAndHaveItTransformed(body);
}
use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.
the class CxfUtils method elementToString.
public static String elementToString(Element element) throws Exception {
Map<String, String> namespaces = new HashMap<String, String>();
visitNodesForNameSpace(element, namespaces);
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
writeElement(element, writer, namespaces);
XmlConverter converter = new XmlConverter();
return converter.toString(converter.toDOMSource(writer.getDocument()), null);
}
use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.
the class CxfMixedModeRouterTest method createRouteBuilder.
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
errorHandler(noErrorHandler());
from(routerEndpointURI).process(new Processor() {
// convert request message
public void process(Exchange exchange) throws Exception {
CxfPayload<?> message = exchange.getIn().getBody(CxfPayload.class);
List<String> params = new ArrayList<String>();
if (message != null) {
// convert CxfPayload to list of objects any way you like
Element element = new XmlConverter().toDOMElement(message.getBody().get(0));
params.add(element.getFirstChild().getTextContent());
}
// replace the body
exchange.getIn().setBody(params);
// if you need to change the operation name
//exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, GREET_ME_OPERATION);
}
}).to(serviceEndpointURI).process(new Processor() {
// convert response to CxfPayload
public void process(Exchange exchange) throws Exception {
List<?> list = exchange.getIn().getBody(List.class);
CxfPayload<SoapHeader> message = null;
if (list != null) {
// convert the list of objects to CxfPayload any way you like
String s = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<ns1:echoResponse xmlns:ns1=\"http://cxf.component.camel.apache.org/\">" + "<return xmlns=\"http://cxf.component.camel.apache.org/\">" + list.get(0) + "</return></ns1:echoResponse>";
List<Element> body = new ArrayList<Element>();
body.add(StaxUtils.read(new StringReader(s)).getDocumentElement());
message = new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(), body);
}
exchange.getIn().setBody(message);
// we probably should be smarter in detecting data format based on message body
// but for now we need to explicitly reset the mode (see CAMEL-3420)
exchange.setProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.PAYLOAD);
}
});
}
};
}
use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.
the class CxfPayLoadMessageRouterAddressOverrideTest method createRouteBuilder.
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
from(routerEndpointURI).process(new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(Exchange.DESTINATION_OVERRIDE_URL, getServiceAddress());
CxfPayload<?> payload = exchange.getIn().getBody(CxfPayload.class);
List<Source> elements = payload.getBodySources();
assertNotNull("We should get the elements here", elements);
assertEquals("Get the wrong elements size", elements.size(), 1);
Element el = new XmlConverter().toDOMElement(elements.get(0));
assertEquals("Get the wrong namespace URI", el.getNamespaceURI(), "http://cxf.component.camel.apache.org/");
}
}).to(serviceEndpointURI);
}
};
}
Aggregations