Search in sources :

Example 11 with XmlConverter

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);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) TypeConversionException(org.apache.camel.TypeConversionException) Element(org.w3c.dom.Element) JAXBContext(javax.xml.bind.JAXBContext) Document(org.w3c.dom.Document) Properties(java.util.Properties) TransformerException(javax.xml.transform.TransformerException) JAXBException(javax.xml.bind.JAXBException) TypeConversionException(org.apache.camel.TypeConversionException) LinkedHashMap(java.util.LinkedHashMap) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) StringWriter(java.io.StringWriter) TransformerException(javax.xml.transform.TransformerException)

Example 12 with XmlConverter

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);
}
Also used : Document(org.w3c.dom.Document) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 13 with XmlConverter

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);
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) HashMap(java.util.HashMap) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 14 with XmlConverter

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);
                }
            });
        }
    };
}
Also used : Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Element(org.w3c.dom.Element) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) Exchange(org.apache.camel.Exchange) StringReader(java.io.StringReader) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) ArrayList(java.util.ArrayList) List(java.util.List)

Example 15 with XmlConverter

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);
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Element(org.w3c.dom.Element) Source(javax.xml.transform.Source) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Aggregations

XmlConverter (org.apache.camel.converter.jaxp.XmlConverter)29 Element (org.w3c.dom.Element)14 Exchange (org.apache.camel.Exchange)10 Document (org.w3c.dom.Document)10 Source (javax.xml.transform.Source)9 Processor (org.apache.camel.Processor)7 RouteBuilder (org.apache.camel.builder.RouteBuilder)6 NodeList (org.w3c.dom.NodeList)4 StringReader (java.io.StringReader)3 List (java.util.List)3 TransformerException (javax.xml.transform.TransformerException)3 DOMSource (javax.xml.transform.dom.DOMSource)3 SoapHeader (org.apache.cxf.binding.soap.SoapHeader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 JAXBContext (javax.xml.bind.JAXBContext)2